Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
StandardShaderMaterialConfigurable.cs
Go to the documentation of this file.
1 using System;
6 using UnityEngine;
7 
8 namespace droid.Runtime.Prototyping.Configurables {
12  [AddComponentMenu(ConfigurableComponentMenuPath._ComponentMenuPath
13  + "StandardShaderMaterial"
14  + ConfigurableComponentMenuPath._Postfix)]
15  [RequireComponent(typeof(Renderer))]
17  IHasTArray {
18  string _reflection;
19  string _smoothness;
20  string _tiling_x;
21  string _tiling_y;
22  string _offset_x;
23  string _offset_y;
24  string _r;
25  string _g;
26  string _b;
27  string _a;
28 
29  [SerializeField] Space2 _tiling_space = Space2.TwentyEighty;
30  [SerializeField] Space2 _offset_space = Space2.TwentyEighty;
31 
32  [SerializeField] Space4 _color_space = Space4.TwentyEighty;
33  [SerializeField] Space1 _smoothness_space = Space1.TwentyEighty;
34  [SerializeField] Space1 _reflection_space = Space1.TwentyEighty;
35 
38  Renderer _renderer;
39 
40  [SerializeField] bool _use_shared = false;
41  [SerializeField] Space4 _configurable_value_space;
42 
43  static readonly int _glossiness = Shader.PropertyToID("_Glossiness");
44  static readonly int _glossy_reflections = Shader.PropertyToID("_GlossyReflections");
45  static readonly int _main_tex = Shader.PropertyToID("_MainTex");
46 
50  protected override void PreSetup() {
51  this._r = this.Identifier + "R";
52  this._g = this.Identifier + "G";
53  this._b = this.Identifier + "B";
54  this._a = this.Identifier + "A";
55  this._reflection = this.Identifier + "Reflection";
56  this._smoothness = this.Identifier + "Smoothness";
57  this._tiling_x = this.Identifier + "TilingX";
58  this._tiling_y = this.Identifier + "TilingY";
59  this._offset_x = this.Identifier + "OffsetX";
60  this._offset_y = this.Identifier + "OffsetY";
61 
62  this._renderer = this.GetComponent<Renderer>();
63  }
64 
68  protected override void RegisterComponent() {
69  this.ParentEnvironment =
70  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._r);
71  this.ParentEnvironment =
72  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._g);
73  this.ParentEnvironment =
74  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._b);
75  this.ParentEnvironment =
76  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._a);
77  this.ParentEnvironment =
78  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._reflection);
79  this.ParentEnvironment =
80  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._smoothness);
81  this.ParentEnvironment =
82  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._offset_x);
83  this.ParentEnvironment =
84  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._offset_y);
85  this.ParentEnvironment =
86  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._tiling_x);
87  this.ParentEnvironment =
88  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._tiling_y);
89  }
90 
94  protected override void UnRegisterComponent() {
95  if (this.ParentEnvironment == null) {
96  return;
97  }
98 
99  this.ParentEnvironment.UnRegister(this, this._r);
100  this.ParentEnvironment.UnRegister(this, this._g);
101  this.ParentEnvironment.UnRegister(this, this._b);
102  this.ParentEnvironment.UnRegister(this, this._a);
103  this.ParentEnvironment.UnRegister(this, this._reflection);
104  this.ParentEnvironment.UnRegister(this, this._smoothness);
105  this.ParentEnvironment.UnRegister(this, this._offset_x);
106  this.ParentEnvironment.UnRegister(this, this._offset_y);
107  this.ParentEnvironment.UnRegister(this, this._tiling_x);
108  this.ParentEnvironment.UnRegister(this, this._tiling_y);
109  }
110 
114  public override ISpace ConfigurableValueSpace { get { return this._configurable_value_space; } }
115 
119  public override void ApplyConfiguration(IConfigurableConfiguration configuration) {
120  #if NEODROID_DEBUG
121  if (this.Debugging) {
122  DebugPrinting.ApplyPrint(this.Debugging, configuration, this.Identifier);
123  }
124  #endif
125 
126  if (!this._use_shared) {
127  foreach (var mat in this._renderer.materials) {
128  var c = mat.color;
129 
130  if (configuration.ConfigurableName.Equals(this._r, StringComparison.Ordinal)) {
131  c.r = configuration.ConfigurableValue;
132  } else if (string.Equals(configuration.ConfigurableName, this._g, StringComparison.Ordinal)) {
133  c.g = configuration.ConfigurableValue;
134  } else if (string.Equals(configuration.ConfigurableName, this._b, StringComparison.Ordinal)) {
135  c.b = configuration.ConfigurableValue;
136  } else if (string.Equals(configuration.ConfigurableName, this._a, StringComparison.Ordinal)) {
137  c.a = configuration.ConfigurableValue;
138  } else if (string.Equals(configuration.ConfigurableName,
139  this._smoothness,
140  StringComparison.Ordinal)) {
141  mat.SetFloat(_glossiness, configuration.ConfigurableValue);
142  } else if (string.Equals(configuration.ConfigurableName,
143  this._reflection,
144  StringComparison.Ordinal)) {
145  mat.SetFloat(_glossy_reflections, configuration.ConfigurableValue);
146  } else if (string.Equals(configuration.ConfigurableName,
147  this._offset_x,
148  StringComparison.Ordinal)) {
149  var a = mat.GetTextureOffset(_main_tex);
150  a.x = configuration.ConfigurableValue;
151  mat.SetTextureOffset(_main_tex, a);
152  } else if (string.Equals(configuration.ConfigurableName,
153  this._offset_y,
154  StringComparison.Ordinal)) {
155  var a = mat.GetTextureOffset(_main_tex);
156  a.y = configuration.ConfigurableValue;
157  mat.SetTextureOffset(_main_tex, a);
158  } else if (string.Equals(configuration.ConfigurableName,
159  this._tiling_x,
160  StringComparison.Ordinal)) {
161  var a = mat.GetTextureScale(_main_tex);
162  a.x = configuration.ConfigurableValue;
163  mat.SetTextureScale(_main_tex, a);
164  } else if (string.Equals(configuration.ConfigurableName,
165  this._tiling_y,
166  StringComparison.Ordinal)) {
167  var a = mat.GetTextureScale(_main_tex);
168  a.y = configuration.ConfigurableValue;
169  mat.SetTextureScale(_main_tex, a);
170  }
171 
172  mat.color = c;
173  }
174  } else {
175  foreach (var mat in this._renderer.sharedMaterials) {
176  var c = mat.color;
177 
178  if (string.Equals(configuration.ConfigurableName, this._r, StringComparison.Ordinal)) {
179  c.r = configuration.ConfigurableValue;
180  } else if (string.Equals(configuration.ConfigurableName, this._g, StringComparison.Ordinal)) {
181  c.g = configuration.ConfigurableValue;
182  } else if (string.Equals(configuration.ConfigurableName, this._b, StringComparison.Ordinal)) {
183  c.b = configuration.ConfigurableValue;
184  } else if (string.Equals(configuration.ConfigurableName, this._a, StringComparison.Ordinal)) {
185  c.a = configuration.ConfigurableValue;
186  } else if (string.Equals(configuration.ConfigurableName,
187  this._smoothness,
188  StringComparison.Ordinal)) {
189  mat.SetFloat(_glossiness, configuration.ConfigurableValue);
190  } else if (string.Equals(configuration.ConfigurableName,
191  this._reflection,
192  StringComparison.Ordinal)) {
193  mat.SetFloat(_glossy_reflections, configuration.ConfigurableValue);
194  } else if (string.Equals(configuration.ConfigurableName,
195  this._offset_x,
196  StringComparison.Ordinal)) {
197  var a = mat.GetTextureOffset(_main_tex);
198  a.x = configuration.ConfigurableValue;
199  mat.SetTextureOffset(_main_tex, a);
200  } else if (string.Equals(configuration.ConfigurableName,
201  this._offset_y,
202  StringComparison.Ordinal)) {
203  var a = mat.GetTextureOffset(_main_tex);
204  a.y = configuration.ConfigurableValue;
205  mat.SetTextureOffset(_main_tex, a);
206  } else if (string.Equals(configuration.ConfigurableName,
207  this._tiling_x,
208  StringComparison.Ordinal)) {
209  var a = mat.GetTextureScale(_main_tex);
210  a.x = configuration.ConfigurableValue;
211  mat.SetTextureScale(_main_tex, a);
212  } else if (string.Equals(configuration.ConfigurableName,
213  this._tiling_y,
214  StringComparison.Ordinal)) {
215  var a = mat.GetTextureScale(_main_tex);
216  a.y = configuration.ConfigurableValue;
217  mat.SetTextureScale(_main_tex, a);
218  }
219 
220  mat.color = c;
221  }
222  }
223  }
224 
229  public override Configuration[] SampleConfigurations() {
230  var cs1 = this._color_space.Sample();
231  var tl1 = this._tiling_space.Sample();
232  var os1 = this._offset_space.Sample();
233  return new[] {
234  new Configuration(this._r, cs1.x),
235  new Configuration(this._g, cs1.y),
236  new Configuration(this._b, cs1.z),
237  new Configuration(this._a, cs1.w),
238  new Configuration(this._reflection, this._reflection_space.Sample()),
239  new Configuration(this._smoothness, this._smoothness_space.Sample()),
240  new Configuration(this._tiling_x, tl1.x),
241  new Configuration(this._tiling_y, tl1.y),
242  new Configuration(this._offset_x, os1.x),
243  new Configuration(this._offset_y, os1.y)
244  };
245  }
246 
250  protected override void Randomise() {
251  Material[] materials;
252  if (this._use_shared) {
253  materials = this._renderer.sharedMaterials;
254  } else {
255  materials = this._renderer.materials;
256  }
257 
258  foreach (var mat in materials) {
259  mat.color = this._color_space.Sample();
260  mat.SetTextureScale(_main_tex, this._tiling_space.Sample());
261  mat.SetTextureOffset(_main_tex, this._offset_space.Sample());
262  mat.SetFloat(_glossiness, this._smoothness_space.Sample());
263  mat.SetFloat(_glossy_reflections, this._reflection_space.Sample());
264  }
265  }
266 
270  public dynamic[] ObservationArray { get { return new dynamic[] { }; } }
271 
275  public ISpace[] ObservationSpace { get { return new ISpace[] {this._configurable_value_space}; } }
276  }
277 }