Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
ScreenSpacePositionConfigurable.cs
Go to the documentation of this file.
5 using UnityEngine;
6 
7 namespace droid.Runtime.Prototyping.Configurables {
11  [AddComponentMenu(ConfigurableComponentMenuPath._ComponentMenuPath
12  + "ScreenSpacePosition"
13  + ConfigurableComponentMenuPath._Postfix)]
14  [RequireComponent(typeof(Renderer))]
16  string _x;
17  string _y;
18  string _z;
19  string _rx;
20  string _ry;
21  string _rw;
22  string _rz;
23 
26  [SerializeField]
27  Camera _camera;
28 
32  protected override void PreSetup() {
33  this._x = this.Identifier + "X";
34  this._y = this.Identifier + "Y";
35  this._z = this.Identifier + "Z";
36  this._rx = this.Identifier + "RX";
37  this._ry = this.Identifier + "RY";
38  this._rz = this.Identifier + "RZ";
39  this._rw = this.Identifier + "RW";
40 
41  if (!this._camera) {
42  this._camera = FindObjectOfType<Camera>();
43  }
44  }
45 
49  protected override void RegisterComponent() {
50  this.ParentEnvironment =
51  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._x);
52  this.ParentEnvironment =
53  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._y);
54  this.ParentEnvironment =
55  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._z);
56  this.ParentEnvironment =
57  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._rx);
58  this.ParentEnvironment =
59  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._ry);
60  this.ParentEnvironment =
61  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._rz);
62  this.ParentEnvironment =
63  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._rw);
64  }
65 
69  protected override void UnRegisterComponent() {
70  if (this.ParentEnvironment == null) {
71  return;
72  }
73 
74  this.ParentEnvironment.UnRegister(this, this._x);
75  this.ParentEnvironment.UnRegister(this, this._y);
76  this.ParentEnvironment.UnRegister(this, this._z);
77  this.ParentEnvironment.UnRegister(this, this._rx);
78  this.ParentEnvironment.UnRegister(this, this._ry);
79  this.ParentEnvironment.UnRegister(this, this._rz);
80  this.ParentEnvironment.UnRegister(this, this._rw);
81  }
82 
83  public override ISpace ConfigurableValueSpace { get; }
84 
88  public override void ApplyConfiguration(IConfigurableConfiguration configuration) {
89  #if NEODROID_DEBUG
90  if (this.Debugging) {
91  DebugPrinting.ApplyPrint(this.Debugging, configuration, this.Identifier);
92  }
93  #endif
94 
95  var pos = this.transform.position;
96  var rot = this.transform.rotation;
97 
98  if (configuration.ConfigurableName == this._x) {
99  pos.x = configuration.ConfigurableValue;
100  } else if (configuration.ConfigurableName == this._y) {
101  pos.y = configuration.ConfigurableValue;
102  } else if (configuration.ConfigurableName == this._z) {
103  pos.z = configuration.ConfigurableValue;
104  } else if (configuration.ConfigurableName == this._rx) {
105  rot.x = configuration.ConfigurableValue;
106  } else if (configuration.ConfigurableName == this._ry) {
107  rot.y = configuration.ConfigurableValue;
108  } else if (configuration.ConfigurableName == this._rz) {
109  rot.z = configuration.ConfigurableValue;
110  } else if (configuration.ConfigurableName == this._rw) {
111  rot.w = configuration.ConfigurableValue;
112  }
113 
114  this.transform.position = pos;
115  this.transform.rotation = rot;
116  }
117 
122  public override Configuration[] SampleConfigurations() {
123  var x = Space1.ZeroOne.Sample();
124  var y = Space1.ZeroOne.Sample();
125 
126  var a = new Vector2(x, y);
127  var bounded = Vector2.Min(Vector2.Max(a, new Vector2(0.2f, 0.2f)), new Vector2(0.8f, 0.8f));
128 
129  //var z = Space1.ZeroOne.Sample() * this._camera.farClipPlane;
130  var z = this._camera.nearClipPlane + 2;
131  var bounded3 = new Vector3(bounded.x, bounded.y, z);
132 
133  var c = this._camera.ViewportToWorldPoint(bounded3);
134 
135  var b = new Quaternion(Space1.ZeroOne.Sample(),
138  Space1.ZeroOne.Sample());
139  var sample1 = Space1.ZeroOne.Sample();
140  var sample = Space1.ZeroOne.Sample();
141 
142  if (sample1 > 0.5f) {
143  if (sample < .33f) {
144  return new[] {new Configuration(this._x, c.x)};
145  }
146 
147  if (sample > .66f) {
148  return new[] {new Configuration(this._y, c.y)};
149  }
150 
151  return new[] {new Configuration(this._z, c.z)};
152  }
153 
154  if (sample < .33f) {
155  return new[] {new Configuration(this._rx, b.x)};
156  }
157 
158  if (sample > .66f) {
159  return new[] {new Configuration(this._ry, b.y)};
160  }
161 
162  return new[] {new Configuration(this._rz, b.z)};
163  }
164  }
165 }
override void ApplyConfiguration(IConfigurableConfiguration configuration)