Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
NativeColorFloatArrayCameraSensor.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
8 using UnityEngine;
9 
10 namespace droid.Runtime.Prototyping.Sensors.Camera.Deprecated {
11  [AddComponentMenu(SensorComponentMenuPath._ComponentMenuPath
12  + "NativeColorArrayCamera"
13  + SensorComponentMenuPath._Postfix)]
16  [Header("Specific", order = 102)]
17  [SerializeField]
18  UnityEngine.Camera _camera = null;
19 
20  bool _grab = true;
21 
22  IManager _manager = null;
23 
24  [SerializeField] Texture2D _texture = null;
25 
29  [field : Header("Observation", order = 103)]
30  public float[] ObservationArray { get; private set; }
31 
35  public Space1[] ObservationSpace { get { return new[] {Space1.ZeroOne}; } }
36 
37  protected override void PreSetup() {
38  if (this._manager == null) {
39  this._manager = FindObjectOfType<AbstractNeodroidManager>();
40  }
41 
42  if (this._camera == null) {
43  this._camera = this.GetComponent<UnityEngine.Camera>();
44  }
45 
46  var target_texture = this._camera.targetTexture;
47  if (target_texture) {
48  this.ObservationArray = new float[target_texture.width * target_texture.height * 4];
49  } else {
50  #if NEODROID_DEBUG
51  Debug.LogWarning("Texture not available!");
52  #endif
53  this._texture = new Texture2D(NeodroidConstants._Default_Width,
54  NeodroidConstants._Default_Height,
55  NeodroidConstants._Default_TextureFormat,
56  false);
57  this.ObservationArray = new float[this._texture.width * this._texture.height * 4];
58  }
59  }
60 
64  protected virtual void OnPostRender() {
65  #if NEODROID_DEBUG
66  if(this.Debugging){
67  this._grab = true;
68  }
69  #endif
70  if (this._camera.targetTexture) {
71  this.UpdateArray();
72  }
73  #if NEODROID_DEBUG
74  if(this.Debugging){
75  Graphics.DrawTexture(new Rect(new Vector2(0,0),new Vector2(128,128)), this._texture);
76  }
77  #endif
78  }
79 
83  protected virtual void UpdateArray() {
84  if (!this._grab) {
85  return;
86  }
87 
88  this._grab = false;
89 
90  if (this._camera) {
91  var current_render_texture = RenderTexture.active;
92  RenderTexture.active = this._camera.targetTexture;
93 
94  if (this._texture
95  && this._camera.targetTexture.width == this._texture.width
96  && this._camera.targetTexture.height == this._texture.height) {
97  this._texture.ReadPixels(new Rect(0, 0, this._texture.width, this._texture.height), 0, 0);
98  //this._texture.Apply();
99  } else {
100  #if NEODROID_DEBUG
101  Debug.LogWarning("Texture not available!");
102  #endif
103  this._texture = new Texture2D(this._camera.targetTexture.width,
104  this._camera.targetTexture.height,
105  NeodroidConstants._Default_TextureFormat,
106  false);
107  this.ObservationArray = new float[this._texture.width * this._texture.height * 4];
108  }
109 
110  //Texture2D texCopy = new Texture2D(_texture.width, _texture.height, _texture.format, _texture.mipmapCount > 1);
111  //texCopy.LoadRawTextureData(_texture.GetRawTextureData());
112  //texCopy.Apply();
113  //var a = texCopy.GetRawTextureData<Color>();
114  var a = this._texture.GetRawTextureData<Color>();
115 
116  #if NEODROID_DEBUG
117  var min = a[0];
118  var max = a[0];
119 
120  #endif
121 
122  var i = 0;
123 /*
124  foreach (var b in a) {
125  this.flat_float_array[i] = b.r;
126  this.flat_float_array[i + 1] = b.g;
127  this.flat_float_array[i + 2] = b.b;
128  this.flat_float_array[i + 3] = b.a;
129  i += 4;
130  }
131 */
132  for (var index = 0; index < a.Length; index++) {
133  var b = a[index];
134  //i = index*4;
135  this.ObservationArray[i] = b.r;
136  this.ObservationArray[i + 1] = b.g;
137  this.ObservationArray[i + 2] = b.b;
138  this.ObservationArray[i + 3] = b.a;
139  i += 4;
140 
141  #if NEODROID_DEBUG
142  if (this.Debugging)
143  {
144  max[0] = Mathf.Max(b[0], max[0]);
145  min[0] = Mathf.Min(b[0], min[0]);
146  max[1] = Mathf.Max(b[1], max[1]);
147  min[1] = Mathf.Min(b[1], min[1]);
148  max[2] = Mathf.Max(b[2], max[2]);
149  min[2] = Mathf.Min(b[2], min[2]);
150  max[3] = Mathf.Max(b[3], max[3]);
151  min[3] = Mathf.Min(b[3], min[3]);
152  }
153  #endif
154  }
155 
156  #if NEODROID_DEBUG
157  if (this.Debugging)
158  Debug.Log($"len(a):{a.Length}, min:{min}, max:{max}");
159  #endif
160 
161  RenderTexture.active = current_render_texture;
162  } else {
163  Debug.LogWarning($"No camera found on {this}");
164  }
165  }
166 
167  public override String PrototypingTypeName { get { return ""; } }
168 
169  public override IEnumerable<float> FloatEnumerable {
170  get { return null; } //this.ObservationArray; }
171  }
172 
173  public override void UpdateObservation() {
174  this._grab = true;
175  if (this._manager?.SimulatorConfiguration?.SimulationType != SimulationType.Frame_dependent_) {
176  #if NEODROID_DEBUG
177  #endif
178  Debug.Log($"{this._manager?.SimulatorConfiguration?.SimulationType}");
179  if (Application.isPlaying) {
180  this._camera.Render();
181  }
182 
183  this.UpdateArray();
184  }
185  }
186 
191  public override string ToString() {
192  var rep = $"Float Array (Length: {this.ObservationArray.Length}), "
193  + $"Sample [{Mathf.Clamp01(this.ObservationArray[0])}.."
194  + $"{Mathf.Clamp01(this.ObservationArray[this.ObservationArray.Length - 1])}]";
195 
196  return rep;
197  }
198  }
199 }
Contains everything relevant to configuring simulation environments engine specific settings ...
SimulationType
Determines the discrete timesteps of the simulation environment.