Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
ColorConfigurable.cs
Go to the documentation of this file.
5 using UnityEngine;
6 
7 namespace droid.Runtime.Prototyping.Configurables {
11  [AddComponentMenu(ConfigurableComponentMenuPath._ComponentMenuPath
12  + "Color"
13  + ConfigurableComponentMenuPath._Postfix)]
14  [RequireComponent(typeof(Renderer))]
19  const char _a = 'A';
20 
24  const char _b = 'B';
25 
29  const char _g = 'G';
30 
34  const char _r = 'R';
35 
36  string _r_id;
37  string _b_id;
38  string _g_id;
39  string _a_id;
40 
43  Renderer _renderer;
44 
45  [SerializeField] Space4 _space = Space4.TwentyEighty;
46 
47  [SerializeField] bool use_shared = false;
48 
52  protected override void PreSetup() {
53  this._r_id = this.Identifier + _r;
54  this._b_id = this.Identifier + _b;
55  this._g_id = this.Identifier + _g;
56  this._a_id = this.Identifier + _a;
57 
58  this._renderer = this.GetComponent<Renderer>();
59  }
60 
64  protected override void RegisterComponent() {
65  this.ParentEnvironment =
66  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._r_id);
67  this.ParentEnvironment =
68  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._g_id);
69  this.ParentEnvironment =
70  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._b_id);
71  this.ParentEnvironment =
72  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._a_id);
73  }
74 
78  protected override void UnRegisterComponent() {
79  if (this.ParentEnvironment == null) {
80  return;
81  }
82 
83  this.ParentEnvironment.UnRegister(this, this._r_id);
84  this.ParentEnvironment.UnRegister(this, this._b_id);
85  this.ParentEnvironment.UnRegister(this, this._g_id);
86  this.ParentEnvironment.UnRegister(this, this._a_id);
87  }
88 
89  public override ISpace ConfigurableValueSpace { get { return this._space; } }
90 
94  public override void ApplyConfiguration(IConfigurableConfiguration configuration) {
95  #if NEODROID_DEBUG
96  if (this.Debugging) {
97  DebugPrinting.ApplyPrint(this.Debugging, configuration, this.Identifier);
98  }
99  #endif
100 
101  if (this.use_shared) {
102  foreach (var mat in this._renderer.sharedMaterials) {
103  var c = mat.color;
104 
105  switch (configuration.ConfigurableName[configuration.ConfigurableName.Length - 1]) {
106  case _r:
107  c.r = configuration.ConfigurableValue;
108  break;
109  case _g:
110  c.g = configuration.ConfigurableValue;
111  break;
112  case _b:
113  c.b = configuration.ConfigurableValue;
114  break;
115  case _a:
116  c.a = configuration.ConfigurableValue;
117  break;
118  }
119 
120  mat.color = c;
121  }
122  } else {
123  foreach (var mat in this._renderer.materials) {
124  var c = mat.color;
125 
126  switch (configuration.ConfigurableName[configuration.ConfigurableName.Length - 1]) {
127  case _r:
128  c.r = configuration.ConfigurableValue;
129  break;
130  case _g:
131  c.g = configuration.ConfigurableValue;
132  break;
133  case _b:
134  c.b = configuration.ConfigurableValue;
135  break;
136  case _a:
137  c.a = configuration.ConfigurableValue;
138  break;
139  }
140 
141  mat.color = c;
142  }
143  }
144  }
145 
146  protected override void Randomise() {
147  if (this.use_shared) {
148  foreach (var mat in this._renderer.sharedMaterials) {
149  mat.color = this._space.Sample();
150  }
151  } else {
152  foreach (var mat in this._renderer.materials) {
153  mat.color = this._space.Sample();
154  }
155  }
156  }
157 
162  public override Configuration[] SampleConfigurations() {
163  var v = this._space.Sample();
164 
165  return new[] {
166  new Configuration(this._r_id, v.x),
167  new Configuration(this._g_id, v.y),
168  new Configuration(this._b_id, v.z),
169  new Configuration(this._a_id, v.w)
170  };
171  }
172  }
173 }
override void ApplyConfiguration(IConfigurableConfiguration configuration)