4 using System.Collections.Generic;
6 using SiliconStudio.Paradox.Assets.Materials.Nodes;
7 using SiliconStudio.Paradox.Effects;
8 using SiliconStudio.Paradox.Effects.Modules;
9 using SiliconStudio.Paradox.Graphics;
11 namespace SiliconStudio.
Paradox.Assets.Materials.Processor.Visitors
15 #region Private static members
22 TexturingKeys.Texture0,
23 TexturingKeys.Texture1,
24 TexturingKeys.Texture2,
25 TexturingKeys.Texture3,
26 TexturingKeys.Texture4,
27 TexturingKeys.Texture5,
28 TexturingKeys.Texture6,
29 TexturingKeys.Texture7,
30 TexturingKeys.Texture8,
31 TexturingKeys.Texture9
39 MaterialTexturingKeys.DiffuseTexture0,
40 MaterialTexturingKeys.DiffuseTexture1,
41 MaterialTexturingKeys.DiffuseTexture2,
42 MaterialTexturingKeys.DiffuseTexture3
50 MaterialTexturingKeys.SpecularTexture0,
51 MaterialTexturingKeys.SpecularTexture1,
52 MaterialTexturingKeys.SpecularTexture2
60 MaterialTexturingKeys.NormalMapTexture0,
61 MaterialTexturingKeys.NormalMapTexture1
69 MaterialTexturingKeys.DisplacementTexture0
74 #region Private members
84 private int nextDefaultIndex = 0;
89 private int nextDiffuseIndex = 0;
94 private int nextSpecularIndex = 0;
99 private int nextNormalMapIndex = 0;
104 private int nextDisplacementIndex = 0;
113 #region Public methods
124 nextDefaultIndex = 0;
125 nextDiffuseIndex = 0;
126 nextSpecularIndex = 0;
127 nextNormalMapIndex = 0;
128 nextDisplacementIndex = 0;
137 AssignParameterKeys(nodes, samplers, GetNextDefaultTextureKey);
146 AssignParameterKeys(node, GetNextDefaultTextureKey);
155 AssignParameterKeys(node, GetNextDiffuseTextureKey);
164 AssignParameterKeys(node, GetNextSpecularTextureKey);
173 AssignParameterKeys(node, GetNextNormalMapTextureKey);
182 AssignParameterKeys(node, GetNextDisplacementTextureKey);
193 var allTextures = GatherTextureValues(node).Distinct().ToList();
194 texcoord = TextureCoordinate.TexcoordNone;
196 foreach (var texSlot
in allTextures)
199 texcoord = texSlot.TexcoordIndex;
201 if (texcoord != texSlot.TexcoordIndex)
214 return GatherTextureValues(node).Distinct().ToList();
224 return GatherTextureValuesWithGenerics(node).Distinct().ToList();
234 return GatherSamplerValues(node).Distinct().ToList();
243 var returnList =
new List<MaterialTextureNode>();
244 foreach (var reference
in Material.ColorNodes)
246 returnList.AddRange(GatherTextureValues(
Material.FindNode(reference.Value)));
248 return returnList.Distinct().ToList();
257 var returnList =
new List<MaterialTextureNode>();
258 foreach (var reference
in Material.ColorNodes)
260 returnList.AddRange(GatherTextureValuesWithGenerics(
Material.FindNode(reference.Value)));
262 return returnList.Distinct().ToList();
271 var returnList =
new List<NodeParameterSampler>();
272 foreach (var reference
in Material.ColorNodes)
274 returnList.AddRange(GatherSamplerValues(
Material.FindNode(reference.Value)));
276 return returnList.Distinct().ToList();
285 var returnList =
new List<MaterialTextureNode>();
286 foreach (var tree
in Material.Nodes)
287 returnList.AddRange(GatherTextureValues(tree.Value));
288 return returnList.Distinct().ToList();
293 #region Private methods
300 private void AssignParameterKeys(
IMaterialNode node, TextureKeyGetter getTextureKey)
302 var allTextures = GatherTextureValues(node).Distinct().ToList();
305 foreach (var texSlot
in allTextures)
309 var textureName = texSlot.TextureName;
310 if (textureName == null || !textureKeys.TryGetValue(textureName, out pk))
312 pk = getTextureKey(
this);
313 if (textureName != null)
314 textureKeys.Add(textureName, pk);
317 texSlot.UsedParameterKey = pk;
318 texSlot.Sampler.SamplerParameterKey = TexturingKeys.Sampler;
331 var samplerKeys =
new Dictionary<SamplerDescription, ParameterKey<SamplerState>>();
332 int samplerIndex = 0;
335 foreach (var texSlot
in nodes.Distinct())
337 if (!texSlot.AutoAssignKey && texSlot.Key != null)
340 textureKeys.Add(texSlot, texSlot.UsedParameterKey);
341 usedTextureKeys.Add(texSlot.UsedParameterKey);
346 foreach (var texSlot
in nodes.Distinct())
349 if (!textureKeys.TryGetValue(texSlot, out textureParameterKey))
351 textureParameterKey = getTextureKey(
this);
352 textureKeys.Add(texSlot, textureParameterKey);
354 texSlot.UsedParameterKey = textureParameterKey;
356 SetSamplerKey(texSlot.Sampler, samplerKeys, ref samplerIndex);
359 if (samplers != null)
361 foreach (var gen
in samplers)
362 SetSamplerKey(gen, samplerKeys, ref samplerIndex);
373 private void SetSamplerKey(NodeParameterSampler sampler, Dictionary<SamplerDescription,
ParameterKey<SamplerState>> samplerKeys, ref
int samplerIndex)
375 var state =
new SamplerDescription { Filtering = sampler.Filtering, AddressModeU = sampler.AddressModeU, AddressModeV = sampler.AddressModeV, AddressModeW = TextureAddressMode.Wrap };
377 if (!samplerKeys.TryGetValue(state, out samplerParameterKey))
379 samplerParameterKey = GetDefaultSamplerKey(samplerIndex);
381 samplerKeys.Add(state, samplerParameterKey);
383 sampler.SamplerParameterKey = samplerParameterKey;
394 while (usedTextureKeys.Contains(textureKeysList[nextIndex]) && nextIndex < textureKeysList.Count)
399 if (nextIndex == textureKeysList.Count)
400 throw new IndexOutOfRangeException(
"There is no more available texture key.");
402 return textureKeysList[nextIndex++];
412 var materialContext =
new MaterialContext {
Material =
Material, ExploreGenerics =
false };
413 return GatherTextures(node, materialContext);
423 var materialContext =
new MaterialContext {
Material =
Material, ExploreGenerics =
true };
424 return GatherTextures(node, materialContext);
435 var textureValues =
new List<MaterialTextureNode>();
436 node.VisitNodes((context, nodeEntry) =>
439 if (textureValue != null)
441 textureValues.Add(textureValue);
444 return textureValues;
454 var samplerValues =
new List<NodeParameterSampler>();
455 node.VisitNodes((context, nodeEntry) =>
458 if (shaderClassNode != null)
460 foreach (var gen
in shaderClassNode.Generics)
462 var genSampler = gen.Value as NodeParameterSampler;
463 if (genSampler != null)
465 samplerValues.Add(genSampler);
470 return samplerValues;
475 #region Private static methods
481 private static ParameterKey<Graphics.Texture> GetNextDefaultTextureKey(MaterialTextureVisitor textureVisitor)
483 return textureVisitor.GetNextTextureKey(DefaultTextureKeys, ref textureVisitor.nextDefaultIndex);
490 private static ParameterKey<Graphics.Texture> GetNextDiffuseTextureKey(MaterialTextureVisitor textureVisitor)
492 return textureVisitor.GetNextTextureKey(DiffuseTextureKeys, ref textureVisitor.nextDiffuseIndex);
499 private static ParameterKey<Graphics.Texture> GetNextSpecularTextureKey(MaterialTextureVisitor textureVisitor)
501 return textureVisitor.GetNextTextureKey(SpecularTextureKeys, ref textureVisitor.nextSpecularIndex);
508 private static ParameterKey<Graphics.Texture> GetNextNormalMapTextureKey(MaterialTextureVisitor textureVisitor)
510 return textureVisitor.GetNextTextureKey(NormalMapTextureKeys, ref textureVisitor.nextNormalMapIndex);
517 private static ParameterKey<Graphics.Texture> GetNextDisplacementTextureKey(MaterialTextureVisitor textureVisitor)
519 return textureVisitor.GetNextTextureKey(DisplacementTextureKeys, ref textureVisitor.nextDisplacementIndex);
532 return TexturingKeys.Sampler0;
534 return TexturingKeys.Sampler1;
536 return TexturingKeys.Sampler2;
538 return TexturingKeys.Sampler3;
540 return TexturingKeys.Sampler4;
542 return TexturingKeys.Sampler5;
544 return TexturingKeys.Sampler6;
546 return TexturingKeys.Sampler7;
548 return TexturingKeys.Sampler8;
550 return TexturingKeys.Sampler9;
552 throw new ArgumentOutOfRangeException(
"Asked for " + i +
" but no more than 10 default textures are currently supported");
558 private struct SamplerDescription
List< NodeParameterSampler > GetAllSamplerValues(IMaterialNode node)
Get all the textures needed for this node.
Key of an effect parameter.
bool HasUniqueTexcoord(IMaterialNode node, out TextureCoordinate texcoord)
Test if the tree can be reduced to a single texture or color.
Description of a material.
void AssignDiffuseTextureKeys(IMaterialNode node)
Assign the diffuse texture keys to the texture slots.
List< MaterialTextureNode > GetAllModelTextureValuesWithGenerics()
Get all the textures for this model.
TextureAddressMode
Identify a technique for resolving texture coordinates that are outside of the boundaries of a textur...
MaterialTextureVisitor(MaterialDescription mat)
void AssignDisplacementTextureKeys(IMaterialNode node)
Assign the displacement texture keys to the texture slots.
TextureCoordinate
The texture coordinate.
TextureFilter
Filtering options during texture sampling.
List< MaterialTextureNode > GetAllTextureValues()
Get all the texture from this material.
List< MaterialTextureNode > GetAllTextureValues(IMaterialNode node)
Get all the textures needed for this node.
void AssignSpecularTextureKeys(IMaterialNode node)
Assign the specular texture keys to the texture slots.
void AssignDefaultTextureKeys(IMaterialNode node)
Assign the default texture keys to the texture slots.
void AssignDefaultTextureKeys(IEnumerable< MaterialTextureNode > nodes, IEnumerable< NodeParameterSampler > samplers)
Assign the default texture keys to the texture slots.
List< MaterialTextureNode > GetAllTextureValuesWithGenerics(IMaterialNode node)
Get all the textures needed for this node with the potential generic parameters.
void AssignNormalMapTextureKeys(IMaterialNode node)
Assign the normal map texture keys to the texture slots.
List< NodeParameterSampler > GetAllSamplerValues()
Get all the Sampler from the generics for this model.
void ResetTextureIndices()
Reset the indices.
Base interface for all nodes in the material tree
List< MaterialTextureNode > GetAllModelTextureValues()
Get all the textures for this model.
Base class for texture resources.