Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
MaterialParametersCreator.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 
5 using SiliconStudio.Core.Diagnostics;
6 using SiliconStudio.Core.IO;
7 using SiliconStudio.Core.Serialization;
8 using SiliconStudio.Paradox.Assets.Materials.Nodes;
9 using SiliconStudio.Paradox.Effects;
10 using SiliconStudio.Paradox.Effects.Data;
11 using SiliconStudio.Paradox.Effects.Modules;
12 using SiliconStudio.Paradox.Graphics;
13 using SiliconStudio.Paradox.Shaders;
14 
15 namespace SiliconStudio.Paradox.Assets.Materials.Processor.Visitors
16 {
18  {
19  #region
20 
21  private string materialUrl;
22 
23  #endregion
24 
25  #region Public properties
26 
27  public ParameterCollectionData Parameters { get; private set; }
28 
29  #endregion
30 
31  #region Public methods
32 
33  public MaterialParametersCreator(MaterialDescription mat, string assetUrl) : base(mat)
34  {
35  Parameters = new ParameterCollectionData();
36  materialUrl = assetUrl;
37  }
38 
39  /// <summary>
40  /// Compute the parameters and store them in the material.
41  /// </summary>
42  /// <param name="log">The logger.</param>
43  /// <returns>A boolean stating that the parameters were incorrectly created.</returns>
44  public bool CreateParameterCollectionData(Logger log = null)
45  {
46  Parameters.Clear();
47 
48  var hasErrors = false;
49 
50  var materialShaderCreator = new MaterialTreeShaderCreator(Material);
51  var shaders = materialShaderCreator.GenerateModelShaders();
52 
53  if (log != null)
54  (materialShaderCreator.Logger).CopyTo(log);
55 
56  foreach (var keyValue in Material.Parameters)
57  {
58  // NOTE: cheap way to activate alpha blending
59  Parameters.Set(keyValue.Key, keyValue.Value);
60  if (keyValue.Key == MaterialParameters.UseTransparent && Material.GetParameter(MaterialParameters.UseTransparent))
61  {
62  // using non premultiply alpha blending
63  var blendStateDescr = new BlendStateDescription(Blend.SourceAlpha, Blend.InverseSourceAlpha);
64  var blendState = new FakeBlendState(blendStateDescr);
65  Parameters.Set(SiliconStudio.Paradox.Graphics.Effect.BlendStateKey, ContentReference.Create((BlendState)blendState));
66 
67  // disable face culling
68  // TODO: make this programmable
69  var rasterizerStateDescr = new RasterizerStateDescription(CullMode.None);
70  var rasterizerState = new FakeRasterizerState(rasterizerStateDescr);
71  Parameters.Set(SiliconStudio.Paradox.Graphics.Effect.RasterizerStateKey, ContentReference.Create((RasterizerState)rasterizerState));
72 
73  // disable depth write
74  var depthStencilStateDescr = new DepthStencilStateDescription(true, false);
75  var depthStencilState = new FakeDepthStencilState(depthStencilStateDescr);
76  Parameters.Set(SiliconStudio.Paradox.Graphics.Effect.DepthStencilStateKey, ContentReference.Create((DepthStencilState)depthStencilState));
77  }
79  {
80  // disable face culling
81  // TODO: make this programmable
82  var rasterizerStateDescr = new RasterizerStateDescription(CullMode.None);
83  var rasterizerState = new FakeRasterizerState(rasterizerStateDescr);
84  Parameters.Set(SiliconStudio.Paradox.Graphics.Effect.RasterizerStateKey, ContentReference.Create((RasterizerState)rasterizerState));
85 
86  // enable depth write
87  var depthStencilStateDescr = new DepthStencilStateDescription(true, true);
88  var depthStencilState = new FakeDepthStencilState(depthStencilStateDescr);
89  Parameters.Set(SiliconStudio.Paradox.Graphics.Effect.DepthStencilStateKey, ContentReference.Create((DepthStencilState)depthStencilState));
90  }
91  }
92 
93  var textureVisitor = new MaterialTextureVisitor(Material);
94  var allTextures = textureVisitor.GetAllModelTextureValuesWithGenerics();
95  foreach (var texture in allTextures)
96  {
97  if (texture.TextureReference == null || (texture.TextureReference.Id == Guid.Empty && String.IsNullOrEmpty(texture.TextureReference.Location)))
98  {
99  if (log != null)
100  log.Error("[Material] Material {0} is missing a texture", materialUrl);
101  hasErrors = true;
102  }
103  else
104  {
105  Parameters.Set(texture.UsedParameterKey, new ContentReference<Texture2D>(texture.TextureReference.Id, texture.TextureReference.Location));
106  AddSampler(texture.Sampler);
107  }
108  }
109 
110  var allSamplers = textureVisitor.GetAllSamplerValues();
111  foreach (var sampler in allSamplers)
112  AddSampler(sampler);
113 
114  var parameterVisitor = new MaterialParametersVisitor(Material);
115  var parameters = parameterVisitor.GetParameters();
116  foreach (var keyValue in parameters)
117  {
118  // The code is separated from the previous code since the key is not generated the same way.
119  if (keyValue.Value is MaterialTextureNode)
120  {
121  var textureNode = (MaterialTextureNode)keyValue.Value;
122  if (textureNode != null)
123  {
124  if (textureNode.TextureReference == null || textureNode.TextureReference.Id == Guid.Empty || String.IsNullOrEmpty(textureNode.TextureReference.Location))
125  {
126  if (log != null)
127  log.Error("[Material] Material {0} is missing a texture", materialUrl);
128  hasErrors = true;
129  }
130  else
131  Parameters.Set(keyValue.Key, new ContentReference<Texture2D>(textureNode.TextureReference.Id, textureNode.TextureReference.Location));
132  }
133  }
134  else if (keyValue.Value is NodeParameterSampler)
135  {
136  var sampler = (NodeParameterSampler)keyValue.Value;
137  if (sampler.SamplerParameterKey == null && keyValue.Key is ParameterKey<SamplerState>)
138  sampler.SamplerParameterKey = (ParameterKey<SamplerState>)keyValue.Key;
139  AddSampler(sampler);
140  }
141  else
142  Parameters.Set(keyValue.Key, keyValue.Value);
143  }
144 
145  // NOTE: this can set the shader uniforms and potentially override what was in Material.Parameters
146  foreach (var keyValue in shaders)
147  {
148  if (log != null && (keyValue.Key == MaterialParameters.BumpMap || keyValue.Key == MaterialParameters.EmissiveMap || keyValue.Key == MaterialParameters.ReflectionMap))
149  log.Warning("[Material] Material {0} contains the key {1} which is not yet handled by the engine.", materialUrl, keyValue.Key);
150 
151  Parameters.Set(keyValue.Key, keyValue.Value);
152  }
153 
154  return hasErrors;
155  }
156 
157  #endregion
158 
159  #region Private methods
160 
161  private void AddSampler(NodeParameterSampler sampler)
162  {
163  if (sampler != null && sampler.SamplerParameterKey != null)
164  {
165  var samplerStateDescr = new SamplerStateDescription(sampler.Filtering, sampler.AddressModeU)
166  {
167  AddressV = sampler.AddressModeV,
168  AddressW = TextureAddressMode.Wrap
169  };
170  var samplerState = new FakeSamplerState(samplerStateDescr);
171  Parameters.Set(sampler.SamplerParameterKey, ContentReference.Create((SamplerState)samplerState));
172  }
173  }
174 
175  #endregion
176  }
177 }
Key of an effect parameter.
Definition: ParameterKey.cs:15
ParameterKey< SamplerState > SamplerParameterKey
The sampler key used in the shader.
TextureAddressMode AddressModeU
The texture address mode.
ParameterCollection Parameters
Definition: Material.cs:24
Contains depth-stencil state for the device.
Data type for SiliconStudio.Paradox.Effects.ParameterCollection.
Definition: ParadoxData.cs:31
bool CreateParameterCollectionData(Logger log=null)
Compute the parameters and store them in the material.
Base implementation for ILogger.
Definition: Logger.cs:10
CullMode
Indicates triangles facing a particular direction are not drawn.
Definition: CullMode.cs:14
static readonly ParameterKey< ShaderMixinSource > BumpMap
Fake sampler state (Description should be valid).
Fake rasterizer state (Description should be valid).
static readonly ParameterKey< ShaderMixinSource > EmissiveMap
static readonly ParameterKey< bool > UseTransparent
TextureFilter Filtering
The texture filtering mode.
Blend
Blend option. A blend option identifies the data source and an optional pre-blend operation...
Definition: Blend.cs:14
Fake blend state (Description should be valid).
static readonly ParameterKey< ShaderMixinSource > ReflectionMap
static readonly ParameterKey< bool > UseTransparentMask