Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
ScreenSpaceOcclusionConfigurable.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
6 using UnityEngine;
7 
8 namespace droid.Runtime.Prototyping.Configurables.Experimental {
12  [AddComponentMenu(ConfigurableComponentMenuPath._ComponentMenuPath
13  + "ScreenSpaceOcclusion"
14  + ConfigurableComponentMenuPath._Postfix)]
15  [RequireComponent(typeof(Renderer))]
20  string _a;
21 
25  string _b;
26 
30  string _g;
31 
35  string _r;
36 
37  [SerializeField] Space4 rot_space = Space4.ZeroOne;
38 
39  [SerializeField] Space2 xy_space2 = Space2.ZeroOne;
40 
41  [SerializeField] Space1 depth_space1 = Space1.ZeroOne;
42  [SerializeField] Space3 size_space = Space3.ZeroOne;
43 
46  [SerializeField]
47  Camera _camera = null;
48 
49  [SerializeField] GameObject[] _prefabs = null;
50  List<GameObject> _spawned = new List<GameObject>();
51  bool _fsafas = true;
52  [SerializeField] int num_obstructions = 10;
53 
57  protected override void PreSetup() {
58  this._r = this.Identifier + "R";
59  this._g = this.Identifier + "G";
60  this._b = this.Identifier + "B";
61  this._a = this.Identifier + "A";
62 
63  if (Application.isPlaying && this._fsafas) {
64  if (this._prefabs != null && this._prefabs.Length > 0 && this._camera) {
65  for (var i = 0; i < this.num_obstructions; i++) {
66  var prefab = this._prefabs[(int)(Space1.ZeroOne.Sample() * this._prefabs.Length)];
67 
68  var xy = this.xy_space2.Sample();
69  var z = this._camera.nearClipPlane + this.depth_space1.Sample() * this._camera.farClipPlane;
70 
71  var a = new Vector3(xy.x, xy.y, z);
72 
73  var c = this._camera.ViewportToWorldPoint(a);
74 
75  var rot = this.rot_space.Sample();
76  var b = new Quaternion(rot.x, rot.y, rot.z, rot.w);
77 
78  var d = Instantiate(prefab, c, b, this.transform);
79  d.transform.localScale = this.size_space.Sample();
80 
81  this._spawned.Add(d);
82  }
83  }
84 
85  this._fsafas = false;
86  }
87  }
88 
92  protected override void RegisterComponent() {
93  this.ParentEnvironment =
94  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._r);
95  this.ParentEnvironment =
96  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._g);
97  this.ParentEnvironment =
98  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._b);
99  this.ParentEnvironment =
100  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._a);
101  }
102 
106  protected override void UnRegisterComponent() {
107  if (this.ParentEnvironment == null) {
108  return;
109  }
110 
111  this.ParentEnvironment.UnRegister(this, this._r);
112  this.ParentEnvironment.UnRegister(this, this._g);
113  this.ParentEnvironment.UnRegister(this, this._b);
114  this.ParentEnvironment.UnRegister(this, this._a);
115  }
116 
117  public override ISpace ConfigurableValueSpace { get; }
118 
122  public override void ApplyConfiguration(IConfigurableConfiguration configuration) {
123  var aa = Random.Range(0, this._spawned.Count);
124  foreach (var bb in this._spawned) {
125  var xy = this.xy_space2.Sample();
126  var z = this._camera.nearClipPlane + this.depth_space1.Sample() * this._camera.farClipPlane;
127 
128  var a = new Vector3(xy.x, xy.y, z);
129 
130  var c = this._camera.ViewportToWorldPoint(a);
131 
132  var rot = this.rot_space.Sample();
133  var b = new Quaternion(rot.x, rot.y, rot.z, rot.w);
134 
135  bb.transform.localScale = this.size_space.Sample();
136 
137  bb.transform.position = c;
138 
139  bb.transform.rotation = b;
140  }
141  #if NEODROID_DEBUG
142  if (this.Debugging) {
143  DebugPrinting.ApplyPrint(this.Debugging, configuration, this.Identifier);
144  }
145  #endif
146  }
147 
152  public override Configuration[] SampleConfigurations() {
153  var s = this.depth_space1.Sample();
154 
155  var sample = Space3.ZeroOne.Sample();
156 
157  return new[] {
158  new Configuration(this._r, sample.x),
159  new Configuration(this._b, sample.y),
160  new Configuration(this._g, sample.z)
161  };
162  }
163  }
164 }