Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
MaterialDescription.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 using SiliconStudio.Assets;
7 using SiliconStudio.Core;
8 using SiliconStudio.Core.Serialization.Contents;
9 using SiliconStudio.Paradox.Assets.Materials.Nodes;
10 using SiliconStudio.Paradox.Effects;
11 using SiliconStudio.Paradox.Effects.Data;
12 using SiliconStudio.Paradox.Shaders;
13 
14 namespace SiliconStudio.Paradox.Assets.Materials
15 {
16  /// <summary>
17  /// Description of a material.
18  /// </summary>
19  [ContentSerializer(typeof(DataContentSerializer<MaterialDescription>))]
20  [DataContract("Material")]
21  public sealed class MaterialDescription
22  {
23  /// <summary>
24  /// The tree describing the flow of instructions leading to this material.
25  /// </summary>
26  /// <userdoc>
27  /// All the color mapping nodes of the materials. They are map descriptions (texture or values) and operations on them.
28  /// </userdoc>
29  [DataMember(10)]
30  public Dictionary<string, IMaterialNode> Nodes { get; set; }
31 
32  /// <summary>
33  /// The tree used in this model.
34  /// </summary>
35  /// <userdoc>
36  /// The final output of the material. Each item references a node and put it behind the chosen ParameterKey.
37  /// </userdoc>
38  [DataMember(30)]
39  public Dictionary<ParameterKey<ShaderMixinSource>, string> ColorNodes { get; set; }
40 
41  /// <summary>
42  /// The parameters of this model.
43  /// </summary>
44  /// <userdoc>
45  /// The parameters of the material. Any parameter can be set here. This is the lowest priority collection so it can be overridden by the model and mesh parameters.
46  /// </userdoc>
47  [DataMember(50)]
48  public ParameterCollectionData Parameters { get; set; }
49 
50  /// <summary>
51  /// Initializes a new instance of the <see cref="MaterialDescription"/> class.
52  /// </summary>
54  {
55  //MaterialTrees = new Dictionary<string, MaterialTree>();
56  Nodes = new Dictionary<string, IMaterialNode>();
57  ColorNodes = new Dictionary<ParameterKey<ShaderMixinSource>, string>();
58  Parameters = new ParameterCollectionData();
59  }
60 
61  /// <summary>
62  /// Inserts a new tree in the material
63  /// </summary>
64  /// <param name="referenceName">The name of the reference.</param>
65  /// <param name="node">The material onde.</param>
66  public void AddNode(string referenceName, IMaterialNode node)
67  {
68  if (Nodes.ContainsKey(referenceName))
69  throw new Exception("A reference with name " + referenceName + " already exists");
70  Nodes.Add(referenceName, node);
71  }
72 
73  /// <summary>
74  /// Adds a tree in the model.
75  /// </summary>
76  /// <param name="key">The key of the slot in the model.</param>
77  /// <param name="referenceName">The name of the tree.</param>
78  /// <param name="node">The node to add.</param>
79  public void AddColorNode(ParameterKey<ShaderMixinSource> key, string referenceName, IMaterialNode node)
80  {
81  if (ColorNodes.ContainsKey(key))
82  ColorNodes[key] = referenceName;
83  else
84  ColorNodes.Add(key, referenceName);
85 
86  AddNode(referenceName, node);
87  }
88 
89  /// <summary>
90  /// Returns the material node corresponding to the brdf slot.
91  /// </summary>
92  /// <param name="key">The key of the brdf slot.</param>
93  /// <returns>The material node.</returns>
95  {
96  string treeName;
97  if (ColorNodes.TryGetValue(key, out treeName))
98  {
99  IMaterialNode materialTree;
100  if (Nodes.TryGetValue(treeName, out materialTree))
101  return materialTree;
102  }
103  return null;
104  }
105 
106  /// <summary>
107  /// Returns the material node corresponding to the reference
108  /// </summary>
109  /// <param name="referenceName">The name of the reference.</param>
110  /// <returns>The material node.</returns>
111  public IMaterialNode FindNode(string referenceName)
112  {
113  IMaterialNode materialTree;
114  if (referenceName != null && Nodes.TryGetValue(referenceName, out materialTree))
115  return materialTree;
116  return null;
117  }
118 
119  /// <summary>
120  /// Create a reference to a node.
121  /// </summary>
122  /// <param name="node">The material node.</param>
123  /// <param name="referenceName">The name of the reference.</param>
124  public void MakeReference(IMaterialNode node, string referenceName)
125  {
126  IMaterialNode prevNode;
127  if (Nodes.TryGetValue(referenceName, out prevNode))
128  {
129  if (!ReferenceEquals(node, prevNode))
130  throw new Exception("Unable to create a reference with the name " + referenceName + " because there is already a reference with that name.");
131  }
132  else
133  {
134  var matReference = new MaterialReferenceNode(referenceName);
135  foreach (var tree in Nodes.Select(x => x.Value))
136  {
137  //var replacer = new MaterialNodeReplacer.
138  }
139  Nodes.Add(referenceName, node);
140  }
141  }
142 
143  /// <summary>
144  /// Set the value of a compilation parameter. Creates a new entry if necessary.
145  /// </summary>
146  /// <param name="parameterKey">The ParameterKey.</param>
147  /// <param name="value">The value.</param>
148  public void SetParameter(ParameterKey parameterKey, object value)
149  {
150  Parameters.Set(parameterKey, value);
151  }
152 
153  /// <summary>
154  /// Get the value of a compilation parameter. Creates a new entry if necessary.
155  /// </summary>
156  /// <param name="parameterKey">The parameter key.</param>
157  /// <returns>The value of the parameter.</returns>
158  public T GetParameter<T>(ParameterKey<T> parameterKey)
159  {
160  return (T)Parameters[parameterKey];
161  }
162 
163  /// <summary>
164  /// Get all the Compilation parameters for this model.
165  /// </summary>
166  /// <returns>A collection of all the parameters.</returns>
168  {
169  return Parameters;
170  }
171 
172  /// <inheritdoc/>
174  {
175  // TODO: use more efficient cloning method using binary serialization
176  return (MaterialDescription)AssetCloner.Clone(this);
177  }
178  }
179 }
Key of an effect parameter.
Definition: ParameterKey.cs:15
void SetParameter(ParameterKey parameterKey, object value)
Set the value of a compilation parameter. Creates a new entry if necessary.
IMaterialNode GetMaterialNode(ParameterKey< ShaderMixinSource > key)
Returns the material node corresponding to the brdf slot.
void AddColorNode(ParameterKey< ShaderMixinSource > key, string referenceName, IMaterialNode node)
Adds a tree in the model.
MaterialDescription()
Initializes a new instance of the MaterialDescription class.
void MakeReference(IMaterialNode node, string referenceName)
Create a reference to a node.
IMaterialNode FindNode(string referenceName)
Returns the material node corresponding to the reference
Key of an gereric effect parameter.
Data type for SiliconStudio.Paradox.Effects.ParameterCollection.
Definition: ParadoxData.cs:31
void AddNode(string referenceName, IMaterialNode node)
Inserts a new tree in the material
ParameterCollectionData GetParameters()
Get all the Compilation parameters for this model.
Base interface for all nodes in the material tree