Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
SynchroniseCameraProperties.cs
Go to the documentation of this file.
1 using System;
3 using UnityEngine;
4 
5 namespace droid.Runtime.Utilities.GameObjects.NeodroidCamera {
9  [RequireComponent(typeof(Camera))]
10  [ExecuteInEditMode]
11  [Serializable]
12  public class SynchroniseCameraProperties : MonoBehaviour {
15  [SerializeField]
16  SynchroniseCameraProperties[] _cameras = null;
17 
20  [SerializeField]
21  bool _run_only_in_edit_mode = true;
22 
25  [SerializeField]
26  bool _only_run_on_awake = false;
27 
30  [SerializeField]
31  bool _sync_culling_mask = true;
32 
35  [SerializeField]
36  bool _sync_far_clip_plane = true;
37 
40  [SerializeField]
41  bool _sync_fov = true;
42 
45  [SerializeField]
46  bool _sync_near_clip_plane = true;
47 
50  [SerializeField]
51  bool _sync_orthographic_projection = true;
52 
55  [SerializeField]
56  bool _sync_orthographic_size = true;
57 
60  [SerializeField]
61  bool _sync_physicality = true;
62 
65  [SerializeField]
66  bool _sync_sensor_size = true;
67 
70  [SerializeField]
71  bool _sync_lens_shift = true;
72 
75  [SerializeField]
76  bool _sync_gate_fit = true;
77 
80  [SerializeField]
81  bool _sync_focal_length = true;
82 
83 /*
86  [SerializeField]
87  bool _sync_sensor_type = true;
88 
91  [SerializeField]
92  bool _sync_fov_axis = true;
93 */
94  [SerializeField] Camera _camera;
95 
96  #region old
97 
98  int _old_culling_mask = 0;
99 
100  float _old_far_clip_plane = 0;
101 
102  float _old_fov = 0;
103 
104  float _old_near_clip_plane = 0;
105 
106  bool _old_orthographic_projection = false;
107 
108  float _old_orthographic_size = 0;
109 
110  float _old_foc;
111  bool _old_physicality = false;
112  Camera.GateFitMode _old_gate_fit = Camera.GateFitMode.Fill;
113  Vector2 _old_sensor_size = Vector2.one;
114  Vector2 _old_lens_shift = Vector2.zero;
115 
116  #endregion
117 
121  public float Foc {
122  get { return this._camera.focalLength; }
123  set {
124  if (this._sync_focal_length) {
125  this._camera.focalLength = value;
126  this._old_foc = value;
127  }
128  }
129  }
130 
134  public float OrtSize {
135  get { return this._camera.orthographicSize; }
136  set {
137  if (this._sync_orthographic_size) {
138  this._camera.orthographicSize = value;
139  this._old_orthographic_size = value;
140  }
141  }
142  }
143 
147  public float Near {
148  get { return this._camera.nearClipPlane; }
149  set {
150  if (this._sync_far_clip_plane) {
151  this._camera.nearClipPlane = value;
152  this._old_near_clip_plane = value;
153  }
154  }
155  }
156 
157  public float Fov {
158  get { return this._camera.fieldOfView; }
159  set {
160  if (this._sync_fov) {
161  this._camera.fieldOfView = value;
162  this._old_fov = value;
163  }
164  }
165  }
166 
167  public bool Ort {
168  get { return this._camera.orthographic; }
169  set {
170  if (this._sync_orthographic_projection) {
171  this._camera.orthographic = value;
172  this._old_orthographic_projection = value;
173  }
174  }
175  }
176 
177  public int Mask {
178  get { return this._camera.cullingMask; }
179  set {
180  if (this._sync_culling_mask) {
181  this._camera.cullingMask = value;
182  this._old_culling_mask = value;
183  }
184  }
185  }
186 
187  public float Far {
188  get { return this._camera.farClipPlane; }
189  set {
190  if (this._sync_far_clip_plane) {
191  this._camera.farClipPlane = value;
192  this._old_far_clip_plane = value;
193  }
194  }
195  }
196 
197  public bool Physicality {
198  get { return this._camera.usePhysicalProperties; }
199  set {
200  if (this._sync_physicality) {
201  this._camera.usePhysicalProperties = value;
202  this._old_physicality = value;
203  }
204  }
205  }
206 
207  public Camera.GateFitMode Gate {
208  get { return this._camera.gateFit; }
209  set {
210  if (this._sync_gate_fit) {
211  this._camera.gateFit = value;
212  this._old_gate_fit = value;
213  }
214  }
215  }
216 
217  public Vector2 SensSize {
218  get { return this._camera.sensorSize; }
219  set {
220  if (this._sync_sensor_size) {
221  this._camera.sensorSize = value;
222  this._old_sensor_size = value;
223  }
224  }
225  }
226 
227  public Vector2 Shift {
228  get { return this._camera.lensShift; }
229  set {
230  if (this._sync_lens_shift) {
231  this._camera.lensShift = value;
232  this._old_lens_shift = value;
233  }
234  }
235  }
236 
239  public void Awake() {
240  this._camera = this.GetComponent<Camera>();
241  if (this._camera) {
242  this.OrtSize = this._camera.orthographicSize;
243  this.Near = this._camera.nearClipPlane;
244  this.Far = this._camera.farClipPlane;
245  this.Mask = this._camera.cullingMask;
246  this.Ort = this._camera.orthographic;
247  this.Fov = this._camera.fieldOfView;
248  this.Foc = this._camera.focalLength;
249  this.Physicality = this._camera.usePhysicalProperties;
250  this.Gate = this._camera.gateFit;
251  this.SensSize = this._camera.sensorSize;
252  this.Shift = this._camera.lensShift;
253 
254  this._cameras = FindObjectsOfType<SynchroniseCameraProperties>();
255  } else {
256  Debug.Log("No camera component found on GameObject");
257  }
258 
259  this.Sync_Cameras();
260  }
261 
262  public void Sync_Cameras() {
263  if (this.enabled) {
264  if (this._camera) {
265  if (this._sync_orthographic_size) {
266  var orthographic_size = this.OrtSize;
267  if (Math.Abs(this._old_orthographic_size - orthographic_size)
268  > NeodroidConstants._Double_Tolerance) {
269  this.OrtSize = orthographic_size;
270  foreach (var cam in this._cameras) {
271  if (cam != this && cam != null) {
272  cam.OrtSize = orthographic_size;
273  }
274  }
275  }
276  }
277 
278  if (this._sync_near_clip_plane) {
279  var near_clip_plane = this._camera.nearClipPlane;
280  if (Math.Abs(this._old_near_clip_plane - near_clip_plane) > NeodroidConstants._Double_Tolerance) {
281  this._old_near_clip_plane = near_clip_plane;
282  foreach (var cam in this._cameras) {
283  if (cam != this && cam != null) {
284  cam.Near = near_clip_plane;
285  }
286  }
287  }
288  }
289 
290  if (this._sync_far_clip_plane) {
291  var far_clip_plane = this._camera.farClipPlane;
292  if (Math.Abs(this._old_far_clip_plane - far_clip_plane) > NeodroidConstants._Double_Tolerance) {
293  this._old_far_clip_plane = far_clip_plane;
294  foreach (var cam in this._cameras) {
295  if (cam != this && cam != null) {
296  cam.Far = far_clip_plane;
297  }
298  }
299  }
300  }
301 
302  if (this._sync_culling_mask) {
303  var culling_mask = this._camera.cullingMask;
304  if (this._old_culling_mask != culling_mask) {
305  this._old_culling_mask = culling_mask;
306  foreach (var cam in this._cameras) {
307  if (cam != this && cam != null) {
308  cam.Mask = culling_mask;
309  }
310  }
311  }
312 
313  if (this._sync_orthographic_projection) {
314  var orthographic = this._camera.orthographic;
315  if (this._old_orthographic_projection != orthographic) {
316  this._old_orthographic_projection = orthographic;
317  foreach (var cam in this._cameras) {
318  if (cam != this && cam != null) {
319  cam.Ort = orthographic;
320  }
321  }
322  }
323  }
324  }
325 
326  if (this._sync_fov) {
327  var fov = this._camera.fieldOfView;
328  if (Math.Abs(this._old_fov - fov) > NeodroidConstants._Double_Tolerance) {
329  this._old_fov = fov;
330  foreach (var cam in this._cameras) {
331  if (cam != this && cam != null) {
332  cam.Fov = fov;
333  }
334  }
335  }
336  }
337 
338  if (this._sync_focal_length) {
339  var foc = this._camera.focalLength;
340  if (Math.Abs(this._old_foc - foc) > NeodroidConstants._Double_Tolerance) {
341  this._old_foc = foc;
342  foreach (var cam in this._cameras) {
343  if (cam != this && cam != null) {
344  cam.Foc = foc;
345  }
346  }
347  }
348  }
349 
350  if (this._sync_physicality) {
351  var physicality = this._camera.usePhysicalProperties;
352  if (this._old_physicality != physicality) {
353  this._old_physicality = physicality;
354  foreach (var cam in this._cameras) {
355  if (cam != this && cam != null) {
356  cam.Physicality = physicality;
357  }
358  }
359  }
360  }
361 
362  if (this._sync_sensor_size) {
363  var a = this._camera.sensorSize;
364  if (this._old_sensor_size != a) {
365  this._old_sensor_size = a;
366  foreach (var cam in this._cameras) {
367  if (cam != this && cam != null) {
368  cam.SensSize = a;
369  }
370  }
371  }
372  }
373 
374  if (this._sync_lens_shift) {
375  var a = this._camera.lensShift;
376  if (this._old_lens_shift != a) {
377  this._old_lens_shift = a;
378  foreach (var cam in this._cameras) {
379  if (cam != this && cam != null) {
380  cam.Shift = a;
381  }
382  }
383  }
384  }
385 
386  if (this._sync_gate_fit) {
387  var a = this._camera.gateFit;
388  if (this._old_gate_fit != a) {
389  this._old_gate_fit = a;
390  foreach (var cam in this._cameras) {
391  if (cam != this && cam != null) {
392  cam.Gate = a;
393  }
394  }
395  }
396  }
397  } else {
398  #if NEODROID_DEBUG
399  Debug.Log($"No Camera component found on {this.name} GameObject");
400  #endif
401  }
402  } else {
403  #if NEODROID_DEBUG
404 
405  //Debug.Log($"No SyncCameraProperties component found on {this.name} GameObject");
406 
407  #endif
408  }
409  }
410 
413  public void Update() {
414  if (!this._only_run_on_awake) {
415  if (this._run_only_in_edit_mode) {
416  #if UNITY_EDITOR
417  if (!Application.isPlaying) {
418  this.Sync_Cameras();
419  }
420  #endif
421  } else {
422  this.Sync_Cameras();
423  }
424  }
425  }
426  }
427 }