Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
FloatArrayCameraSensor.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
6 using UnityEngine;
7 
8 namespace droid.Runtime.Prototyping.Sensors.Camera.Deprecated {
12  [AddComponentMenu(SensorComponentMenuPath._ComponentMenuPath
13  + "FloatArrayCamera"
14  + SensorComponentMenuPath._Postfix)]
17  [SerializeField] bool _black_white = false;
18 
19  [Header("Specific", order = 102)]
20  [SerializeField]
21  UnityEngine.Camera _camera = null;
22 
23  bool _grab = true;
24 
25  IManager _manager = null;
26 
27  [SerializeField] Texture2D _texture = null;
28 
32  [field : Header("Observation", order = 103)]
33  public float[] ObservationArray { get; private set; } = null;
34 
38  public Space1[] ObservationSpace { get { return new[] {Space1.ZeroOne}; } }
39 
43  protected override void PreSetup() {
44  if (this._manager == null) {
45  this._manager = FindObjectOfType<AbstractNeodroidManager>();
46  }
47 
48  if (this._camera == null) {
49  this._camera = this.GetComponent<UnityEngine.Camera>();
50  }
51 
52  var target_texture = this._camera.targetTexture;
53  if (target_texture) {
54  this._texture = new Texture2D(target_texture.width, target_texture.height);
55  if (this._black_white) {
56  this.ObservationArray = new float[this._texture.width * this._texture.height * 1]; // *1 for clarity
57  } else {
58  this.ObservationArray = new float[this._texture.width * this._texture.height * 3];
59  }
60  } else {
61  this.ObservationArray = new float[0];
62  }
63  }
64 
68  protected virtual void OnPostRender() {
69  if (this._camera.targetTexture) {
70  this.UpdateArray();
71  }
72  }
73 
77  protected virtual void UpdateArray() {
78  if (!this._grab) {
79  return;
80  }
81 
82  this._grab = false;
83 
84  var current_render_texture = RenderTexture.active;
85  var target_texture = this._camera.targetTexture;
86  RenderTexture.active = target_texture;
87 
88  this._texture.ReadPixels(new Rect(0, 0, target_texture.width, target_texture.height), 0, 0);
89  this._texture.Apply();
90 
91  if (!this._black_white) {
92  for (var w = 0; w < this._texture.width; w++) {
93  for (var h = 0; h < this._texture.height; h++) {
94  var c = this._texture.GetPixel(w, h);
95  this.ObservationArray[this._texture.width * w + h * 3] = c.r;
96  this.ObservationArray[this._texture.width * w + h * 3 + 1] = c.g;
97  this.ObservationArray[this._texture.width * w + h * 3 + 2] = c.b;
98  }
99  }
100  } else {
101  for (var w = 0; w < this._texture.width; w++) {
102  for (var h = 0; h < this._texture.height; h++) {
103  var c = this._texture.GetPixel(w, h);
104  this.ObservationArray[this._texture.width * w + h] = (c.r + c.g + c.b) / 3;
105  }
106  }
107  }
108 
109  RenderTexture.active = current_render_texture;
110  }
111 
115  public override IEnumerable<float> FloatEnumerable { get { return this.ObservationArray; } }
116 
120  public override void UpdateObservation() {
121  if (this._manager?.SimulatorConfiguration != null) {
122  if (this._manager.SimulatorConfiguration.SimulationType != SimulationType.Frame_dependent_) {
123  Debug.LogWarning("WARNING! Camera Observations may be out of sync other data");
124  }
125  }
126 
127  this._grab = true;
128  var manager = this._manager;
129  if (manager != null
130  && manager.SimulatorConfiguration.SimulationType != SimulationType.Frame_dependent_) {
131  this.UpdateArray();
132  }
133  }
134  }
135 }
ISimulatorConfiguration SimulatorConfiguration
Definition: IManager.cs:8
Contains everything relevant to configuring simulation environments engine specific settings ...
SimulationType
Determines the discrete timesteps of the simulation environment.