Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
ByteArrayCameraSensor.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
7 using UnityEngine;
8 using UnityEngine.Experimental.Rendering;
9 
10 namespace droid.Runtime.Prototyping.Sensors.Camera {
11  [AddComponentMenu(SensorComponentMenuPath._ComponentMenuPath
12  + "ByteArrayCamera"
13  + SensorComponentMenuPath._Postfix)]
14  public class ByteArrayCameraSensor : Sensor,
16  [Header("Specific", order = 102)]
17  [SerializeField]
18  UnityEngine.Camera _camera = null;
19 
20  bool _grab = true;
21 
22  //[SerializeField] bool linear_space;
23  IManager _manager = null;
24 
25  [SerializeField] Texture2D _texture = null;
26 
27  const TextureCreationFlags _flags = TextureCreationFlags.None;
28 
32  protected override void PreSetup() {
33  if (this._manager == null) {
34  this._manager = FindObjectOfType<AbstractNeodroidManager>();
35  }
36 
37  if (this._camera == null) {
38  this._camera = this.GetComponent<UnityEngine.Camera>();
39  }
40 
41  var target_texture = this._camera.targetTexture;
42  if (!target_texture) {
43  #if NEODROID_DEBUG
44  Debug.LogWarning("Texture not available!");
45  #endif
46  this._texture = new Texture2D(NeodroidConstants._Default_Width,
47  NeodroidConstants._Default_Height,
48  NeodroidConstants._Default_TextureFormat,
49  false
50  //,this.linear_space
51  );
52  } else {
53  this._texture = new Texture2D(target_texture.width,
54  target_texture.height,
55  target_texture.graphicsFormat,
56  _flags);
57  }
58 
59  /*this._post_material = new Material( Shader.Find("Neodroid/Gamma") );
60  this._post_material.SetFloat("_gamma", this.gamma);
61  Graphics.Blit(source, destination, this._post_material);*/
62  }
63 
67  protected virtual void OnPostRender() {
68  #if NEODROID_DEBUG
69  if (this.Debugging) {
70  this._grab = true;
71  }
72  #endif
73  if (this._camera.targetTexture) {
74  this.UpdateArray();
75  }
76  #if NEODROID_DEBUG
77  if (this.Debugging) {
78  Graphics.DrawTexture(new Rect(new Vector2(0, 0), new Vector2(0, 0)), this._texture);
79  }
80  #endif
81  }
82 
86  protected virtual void UpdateArray() {
87  if (!this._grab) {
88  return;
89  }
90 
91  this._grab = false;
92 
93  if (this._camera) {
94  var current_render_texture = RenderTexture.active;
95  var texture = this._camera.targetTexture;
96  //texture.GetNativeTexturePtr()
97  RenderTexture.active = texture;
98 
99  this._texture.ReadPixels(new Rect(0, 0, this._texture.width, this._texture.height),
100  0,
101  0,
102  recalculateMipMaps : false);
103 
104  //this._texture.Apply();
105 
106  this.Bytes = this._texture.GetRawTextureData();
107  //Debug.Log($"{this.Identifier}:{this.Bytes.Length}");
108 
109  RenderTexture.active = current_render_texture;
110  } else {
111  Debug.LogWarning($"No camera found on {this}");
112  }
113  }
114 
115  public override String PrototypingTypeName { get { return ""; } }
116 
117  public override IEnumerable<float> FloatEnumerable {
118  get { return null; } //this.ObservationArray; }
119  }
120 
121  public override void UpdateObservation() {
122  this._grab = true;
123  if (this._manager?.SimulatorConfiguration?.SimulationType != SimulationType.Frame_dependent_) {
124  #if NEODROID_DEBUG
125  Debug.Log($"{this._manager?.SimulatorConfiguration?.SimulationType}");
126  #endif
127  if (Application.isPlaying) {
128  this._camera.Render();
129  }
130 
131  this.UpdateArray();
132  }
133  }
134 
139  public override string ToString() {
140  var rep = $"Byte Array (Length: {this.Bytes.Length}), "
141  + $"Sample [{this.Bytes[0]}.."
142  + $"{this.Bytes[this.Bytes.Length - 1]}]";
143 
144  return rep;
145  }
146 
150  public Byte[] Bytes { get; private set; }
151 
155  public Int32[] Shape {
156  get {
157  int channels;
158  switch (this._texture.graphicsFormat) {
159  case GraphicsFormat.R8_UNorm:
160  case GraphicsFormat.R16_SFloat:
161  case GraphicsFormat.R32_SFloat:
162  channels = 1;
163  break;
164  //case GraphicsFormat.R32G32B32A32_SFloat:
165  //case GraphicsFormat.B8G8R8A8_UNorm:
166  //case GraphicsFormat.R16G16B16A16_SFloat:
167  default:
168  channels = 4;
169  break;
170  }
171 
172  return new[] {this._texture.width, this._texture.height, channels};
173  }
174  }
175 
179  public String ArrayEncoding {
180  get {
181  string s;
182  switch (this._texture.graphicsFormat) {
183  case GraphicsFormat.R8G8B8A8_UNorm:
184  case GraphicsFormat.R8_UInt:
185  s = "UINT8";
186  break;
187  case GraphicsFormat.R16_SFloat:
188  case GraphicsFormat.R16G16B16A16_SFloat:
189  s = "FLOAT16";
190  break;
191  case GraphicsFormat.R32_SFloat:
192  case GraphicsFormat.R32G32B32A32_SFloat:
193  s = "FLOAT32";
194  break;
195  default:
196  s = "Unknown";
197  break;
198  }
199 
200  return s;
201  }
202  }
203  }
204 }
SimulationType
Determines the discrete timesteps of the simulation environment.