Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
MaterialCompilerParametersGenerator.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 
7 using SiliconStudio.Assets.Compiler;
8 using SiliconStudio.Core;
9 using SiliconStudio.Core.Diagnostics;
10 using SiliconStudio.Core.Serialization.Assets;
11 using SiliconStudio.Core.Storage;
12 using SiliconStudio.Paradox.Assets.Effect;
13 using SiliconStudio.Paradox.Effects;
14 using SiliconStudio.Paradox.Effects.Data;
15 using SiliconStudio.Paradox.Shaders.Compiler;
16 
17 namespace SiliconStudio.Paradox.Assets.Materials.Generators
18 {
20  {
21  public override int GeneratorPriority
22  {
23  get
24  {
25  return 10;
26  }
27  }
28 
30  {
31  var allMaterialParameters = new List<ParameterCollection>();
32  if (baseParameters.Get(MaterialAssetKeys.GenerateShader))
33  {
34  var assetManager = new AssetManager();
35  var settings = new AssetManagerLoaderSettings()
36  {
37  ContentFilter = AssetManagerLoaderSettings.NewContentFilterByType(typeof(MaterialData)),
38  };
39 
40  var hashParameters = new HashSet<ObjectId>();
41 
42  foreach (var materialAssetItem in context.Package.Assets.Where(item => item.Asset is MaterialAsset))
43  {
44  var assetPath = materialAssetItem.Location.GetDirectoryAndFileName();
45  try
46  {
47  var materialData = assetManager.Load<MaterialData>(assetPath, settings);
48  if (materialData != null && materialData.Parameters != null && materialData.Parameters.Count > 0)
49  {
50  var materialParameters = new ParameterCollection();
51  AddToParameters(materialData.Parameters, materialParameters);
52  if (materialParameters.Count > 0)
53  {
54  byte[] buffer1;
55  var id = ObjectId.FromObject(materialParameters, out buffer1);
56  if (!hashParameters.Contains(id))
57  {
58  hashParameters.Add(id);
59  allMaterialParameters.Add(materialParameters);
60  }
61  }
62  }
63  }
64  catch (Exception ex)
65  {
66  log.Error("Error while loading material [{0}]", ex, assetPath);
67  }
68  }
69  }
70 
71  if (allMaterialParameters.Count != 0)
72  {
73  foreach (var materialParams in allMaterialParameters)
74  {
75  var compilerParameters = baseParameters.Clone();
76  materialParams.CopyTo(compilerParameters);
77  yield return compilerParameters;
78  }
79  }
80  else
81  {
82  yield return baseParameters.Clone();
83  }
84  }
85 
86  [ModuleInitializer]
87  internal static void Register()
88  {
89  // Register an instance of this generator to the effect asset compiler.
90  EffectLibraryAssetCompiler.RegisterCompilerParametersGenerator(new MaterialCompilerParametersGenerator());
91  }
92  }
93 }
static readonly ParameterKey< bool > GenerateShader
Allow material compilation without mesh.
Data type for SiliconStudio.Paradox.Effects.Material.
Definition: EngineData.cs:153
PackageAssetCollection Assets
Gets the assets stored in this package.
Definition: Package.cs:144
Specifies settings for AssetManager.Load{T} operations.
The context used when compiling an asset in a Package.
Defines keys associated with mesh used for compiling assets.
override IEnumerable< CompilerParameters > Generate(AssetCompilerContext context, CompilerParameters baseParameters, ILogger log)
Generates derived CompilerParameters from a base parameters.
Interface for logging.
Definition: ILogger.cs:8
A container to handle a hierarchical collection of effect variables.