Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
CreateStereoCubemaps.cs
Go to the documentation of this file.
1 using System;
3 using UnityEngine;
4 
5 namespace droid.Runtime.Utilities.GameObjects.NeodroidCamera.Experimental.Camera360 {
6  public class CreateStereoCubemaps : MonoBehaviour {
7  public RenderTexture cubemapLeftEye;
8  public RenderTexture cubemapRightEye;
9  public RenderTexture cubemapEquirect;
10  public Texture2D _texture;
11  public bool renderStereo = true;
12  public float stereoSeparation = 0.064f;
13 
14  Camera _cam;
15 
16  void Start() {
17  this._cam = this.GetComponent<Camera>();
18 
19  if (this._cam == null) {
20  this._cam = this.GetComponentInParent<Camera>();
21  }
22 
23  if (this._cam) {
24  var target_texture = this._cam.targetTexture;
25  if (!target_texture) {
26  Debug.LogWarning($"No targetTexture defaulting to a texture of size ({NeodroidConstants._Default_Width}, {NeodroidConstants._Default_Height})");
27 
28  this._texture = new Texture2D(NeodroidConstants._Default_Width, NeodroidConstants._Default_Height);
29  } else {
30  var texture_format_str = target_texture.format.ToString();
31  if (Enum.TryParse(texture_format_str, out TextureFormat texture_format)) {
32  this._texture = new Texture2D(target_texture.width,
33  target_texture.height,
34  texture_format,
35  target_texture.useMipMap,
36  !target_texture.sRGB);
37  }
38  }
39  }
40  }
41 
42  void LateUpdate() {
43  if (this._cam == null) {
44  Debug.Log("stereo 360 capture node has no camera or parent camera");
45  }
46 
47  if (this.renderStereo) {
48  this._cam.stereoSeparation = this.stereoSeparation;
49  this._cam.RenderToCubemap(this.cubemapLeftEye, 63, Camera.MonoOrStereoscopicEye.Left);
50  this._cam.RenderToCubemap(this.cubemapRightEye, 63, Camera.MonoOrStereoscopicEye.Right);
51  } else {
52  this._cam.RenderToCubemap(this.cubemapLeftEye, 63, Camera.MonoOrStereoscopicEye.Mono);
53  }
54 
55  //optional: convert cubemaps to equirect
56  if (this.cubemapEquirect != null) {
57  if (this.renderStereo) {
58  this.cubemapLeftEye.ConvertToEquirect(this.cubemapEquirect, Camera.MonoOrStereoscopicEye.Left);
59  this.cubemapRightEye.ConvertToEquirect(this.cubemapEquirect, Camera.MonoOrStereoscopicEye.Right);
60  } else {
61  this.cubemapLeftEye.ConvertToEquirect(this.cubemapEquirect);
62  }
63  }
64  }
65  }
66 }