BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Scripts / Composition / OVRCameraComposition.cs

OVRCameraComposition.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRCameraComposition.cs 12.6 KiB Download Raw
1/************************************************************************************
2Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
3
4Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
5the Utilities SDK except in compliance with the License, which is provided at the time of installation
6or download, or which otherwise accompanies this software in either electronic or hard copy form.
7
8You may obtain a copy of the License at
9https://developer.oculus.com/licenses/utilities-1.31
10
11Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
12under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
13ANY KIND, either express or implied. See the License for the specific language governing
14permissions and limitations under the License.
15************************************************************************************/
16
17using UnityEngine;
18using System.Collections;
19
20#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
21
22public abstract class OVRCameraComposition : OVRComposition {
23 protected GameObject cameraFramePlaneObject;
24 protected float cameraFramePlaneDistance;
25
26 protected readonly bool hasCameraDeviceOpened = false;
27 protected readonly bool useDynamicLighting = false;
28
29 internal readonly OVRPlugin.CameraDevice cameraDevice = OVRPlugin.CameraDevice.WebCamera0;
30
31 private Mesh boundaryMesh = null;
32 private float boundaryMeshTopY = 0.0f;
33 private float boundaryMeshBottomY = 0.0f;
34 private OVRManager.VirtualGreenScreenType boundaryMeshType = OVRManager.VirtualGreenScreenType.Off;
35
36 protected OVRCameraComposition(GameObject parentObject, Camera mainCamera, OVRManager.CameraDevice inCameraDevice, bool inUseDynamicLighting, OVRManager.DepthQuality depthQuality)
37 : base(parentObject, mainCamera)
38 {
39 cameraDevice = OVRCompositionUtil.ConvertCameraDevice(inCameraDevice);
40
41 Debug.Assert(!hasCameraDeviceOpened);
42 Debug.Assert(!OVRPlugin.IsCameraDeviceAvailable(cameraDevice) || !OVRPlugin.HasCameraDeviceOpened(cameraDevice));
43 hasCameraDeviceOpened = false;
44 useDynamicLighting = inUseDynamicLighting;
45
46 bool cameraSupportsDepth = OVRPlugin.DoesCameraDeviceSupportDepth(cameraDevice);
47 if (useDynamicLighting && !cameraSupportsDepth)
48 {
49 Debug.LogWarning("The camera device doesn't support depth. The result of dynamic lighting might not be correct");
50 }
51
52 if (OVRPlugin.IsCameraDeviceAvailable(cameraDevice))
53 {
54 OVRPlugin.CameraExtrinsics extrinsics;
55 OVRPlugin.CameraIntrinsics intrinsics;
56 OVRPlugin.Posef calibrationRawPose;
57 if (OVRPlugin.GetExternalCameraCount() > 0 && OVRPlugin.GetMixedRealityCameraInfo(0, out extrinsics, out intrinsics, out calibrationRawPose))
58 {
59 OVRPlugin.SetCameraDevicePreferredColorFrameSize(cameraDevice, intrinsics.ImageSensorPixelResolution.w, intrinsics.ImageSensorPixelResolution.h);
60 }
61
62 if (useDynamicLighting)
63 {
64 OVRPlugin.SetCameraDeviceDepthSensingMode(cameraDevice, OVRPlugin.CameraDeviceDepthSensingMode.Fill);
65 OVRPlugin.CameraDeviceDepthQuality quality = OVRPlugin.CameraDeviceDepthQuality.Medium;
66 if (depthQuality == OVRManager.DepthQuality.Low)
67 {
68 quality = OVRPlugin.CameraDeviceDepthQuality.Low;
69 }
70 else if (depthQuality == OVRManager.DepthQuality.Medium)
71 {
72 quality = OVRPlugin.CameraDeviceDepthQuality.Medium;
73 }
74 else if (depthQuality == OVRManager.DepthQuality.High)
75 {
76 quality = OVRPlugin.CameraDeviceDepthQuality.High;
77 }
78 else
79 {
80 Debug.LogWarning("Unknown depth quality");
81 }
82 OVRPlugin.SetCameraDevicePreferredDepthQuality(cameraDevice, quality);
83 }
84
85 Debug.LogFormat("Opening camera device {0}", cameraDevice);
86 OVRPlugin.OpenCameraDevice(cameraDevice);
87 if (OVRPlugin.HasCameraDeviceOpened(cameraDevice))
88 {
89 Debug.LogFormat("Opened camera device {0}", cameraDevice);
90 hasCameraDeviceOpened = true;
91 }
92 }
93 }
94
95 public override void Cleanup()
96 {
97 OVRCompositionUtil.SafeDestroy(ref cameraFramePlaneObject);
98 if (hasCameraDeviceOpened)
99 {
100 Debug.LogFormat("Close camera device {0}", cameraDevice);
101 OVRPlugin.CloseCameraDevice(cameraDevice);
102 }
103 }
104
105 public override void RecenterPose()
106 {
107 boundaryMesh = null;
108 }
109
110 protected void CreateCameraFramePlaneObject(GameObject parentObject, Camera mixedRealityCamera, bool useDynamicLighting)
111 {
112 Debug.Assert(cameraFramePlaneObject == null);
113 cameraFramePlaneObject = GameObject.CreatePrimitive(PrimitiveType.Quad);
114 cameraFramePlaneObject.name = "MRCameraFrame";
115 cameraFramePlaneObject.transform.parent = cameraInTrackingSpace ? cameraRig.trackingSpace : parentObject.transform;
116 cameraFramePlaneObject.GetComponent<Collider>().enabled = false;
117 cameraFramePlaneObject.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
118 Material cameraFrameMaterial = new Material(Shader.Find(useDynamicLighting ? "Oculus/OVRMRCameraFrameLit" : "Oculus/OVRMRCameraFrame"));
119 cameraFramePlaneObject.GetComponent<MeshRenderer>().material = cameraFrameMaterial;
120 cameraFrameMaterial.SetColor("_Color", Color.white);
121 cameraFrameMaterial.SetFloat("_Visible", 0.0f);
122 cameraFramePlaneObject.transform.localScale = new Vector3(4, 4, 4);
123 cameraFramePlaneObject.SetActive(true);
124 OVRCameraFrameCompositionManager cameraFrameCompositionManager = mixedRealityCamera.gameObject.AddComponent<OVRCameraFrameCompositionManager>();
125 cameraFrameCompositionManager.cameraFrameGameObj = cameraFramePlaneObject;
126 cameraFrameCompositionManager.composition = this;
127 }
128
129 private bool nullcameraRigWarningDisplayed = false;
130 protected void UpdateCameraFramePlaneObject(Camera mainCamera, Camera mixedRealityCamera, RenderTexture boundaryMeshMaskTexture)
131 {
132 bool hasError = false;
133 Material cameraFrameMaterial = cameraFramePlaneObject.GetComponent<MeshRenderer>().material;
134 Texture2D colorTexture = Texture2D.blackTexture;
135 Texture2D depthTexture = Texture2D.whiteTexture;
136 if (OVRPlugin.IsCameraDeviceColorFrameAvailable(cameraDevice))
137 {
138 colorTexture = OVRPlugin.GetCameraDeviceColorFrameTexture(cameraDevice);
139 }
140 else
141 {
142 Debug.LogWarning("Camera: color frame not ready");
143 hasError = true;
144 }
145 bool cameraSupportsDepth = OVRPlugin.DoesCameraDeviceSupportDepth(cameraDevice);
146 if (useDynamicLighting && cameraSupportsDepth)
147 {
148 if (OVRPlugin.IsCameraDeviceDepthFrameAvailable(cameraDevice))
149 {
150 depthTexture = OVRPlugin.GetCameraDeviceDepthFrameTexture(cameraDevice);
151 }
152 else
153 {
154 Debug.LogWarning("Camera: depth frame not ready");
155 hasError = true;
156 }
157 }
158 if (!hasError)
159 {
160 Vector3 offset = mainCamera.transform.position - mixedRealityCamera.transform.position;
161 float distance = Vector3.Dot(mixedRealityCamera.transform.forward, offset);
162 cameraFramePlaneDistance = distance;
163
164 cameraFramePlaneObject.transform.position = mixedRealityCamera.transform.position + mixedRealityCamera.transform.forward * distance;
165 cameraFramePlaneObject.transform.rotation = mixedRealityCamera.transform.rotation;
166
167 float tanFov = Mathf.Tan(mixedRealityCamera.fieldOfView * Mathf.Deg2Rad * 0.5f);
168 cameraFramePlaneObject.transform.localScale = new Vector3(distance * mixedRealityCamera.aspect * tanFov * 2.0f, distance * tanFov * 2.0f, 1.0f);
169
170 float worldHeight = distance * tanFov * 2.0f;
171 float worldWidth = worldHeight * mixedRealityCamera.aspect;
172
173 float cullingDistance = float.MaxValue;
174
175 if (OVRManager.instance.virtualGreenScreenType != OVRManager.VirtualGreenScreenType.Off)
176 {
177 RefreshBoundaryMesh(mixedRealityCamera, out cullingDistance);
178 }
179
180 cameraFrameMaterial.mainTexture = colorTexture;
181 cameraFrameMaterial.SetTexture("_DepthTex", depthTexture);
182 cameraFrameMaterial.SetVector("_FlipParams", new Vector4((OVRManager.instance.flipCameraFrameHorizontally ? 1.0f : 0.0f), (OVRManager.instance.flipCameraFrameVertically ? 1.0f : 0.0f), 0.0f, 0.0f));
183 cameraFrameMaterial.SetColor("_ChromaKeyColor", OVRManager.instance.chromaKeyColor);
184 cameraFrameMaterial.SetFloat("_ChromaKeySimilarity", OVRManager.instance.chromaKeySimilarity);
185 cameraFrameMaterial.SetFloat("_ChromaKeySmoothRange", OVRManager.instance.chromaKeySmoothRange);
186 cameraFrameMaterial.SetFloat("_ChromaKeySpillRange", OVRManager.instance.chromaKeySpillRange);
187 cameraFrameMaterial.SetVector("_TextureDimension", new Vector4(colorTexture.width, colorTexture.height, 1.0f / colorTexture.width, 1.0f / colorTexture.height));
188 cameraFrameMaterial.SetVector("_TextureWorldSize", new Vector4(worldWidth, worldHeight, 0, 0));
189 cameraFrameMaterial.SetFloat("_SmoothFactor", OVRManager.instance.dynamicLightingSmoothFactor);
190 cameraFrameMaterial.SetFloat("_DepthVariationClamp", OVRManager.instance.dynamicLightingDepthVariationClampingValue);
191 cameraFrameMaterial.SetFloat("_CullingDistance", cullingDistance);
192 if (OVRManager.instance.virtualGreenScreenType == OVRManager.VirtualGreenScreenType.Off || boundaryMesh == null || boundaryMeshMaskTexture == null)
193 {
194 cameraFrameMaterial.SetTexture("_MaskTex", Texture2D.whiteTexture);
195 }
196 else
197 {
198 if (cameraRig == null)
199 {
200 if (!nullcameraRigWarningDisplayed)
201 {
202 Debug.LogWarning("Could not find the OVRCameraRig/CenterEyeAnchor object. Please check if the OVRCameraRig has been setup properly. The virtual green screen has been temporarily disabled");
203 nullcameraRigWarningDisplayed = true;
204 }
205
206 cameraFrameMaterial.SetTexture("_MaskTex", Texture2D.whiteTexture);
207 }
208 else
209 {
210 if (nullcameraRigWarningDisplayed)
211 {
212 Debug.Log("OVRCameraRig/CenterEyeAnchor object found. Virtual green screen is activated");
213 nullcameraRigWarningDisplayed = false;
214 }
215
216 cameraFrameMaterial.SetTexture("_MaskTex", boundaryMeshMaskTexture);
217 }
218 }
219 }
220 }
221
222 protected void RefreshBoundaryMesh(Camera camera, out float cullingDistance)
223 {
224 float depthTolerance = OVRManager.instance.virtualGreenScreenApplyDepthCulling ? OVRManager.instance.virtualGreenScreenDepthTolerance : float.PositiveInfinity;
225 cullingDistance = OVRCompositionUtil.GetMaximumBoundaryDistance(camera, OVRCompositionUtil.ToBoundaryType(OVRManager.instance.virtualGreenScreenType)) + depthTolerance;
226 if (boundaryMesh == null || boundaryMeshType != OVRManager.instance.virtualGreenScreenType || boundaryMeshTopY != OVRManager.instance.virtualGreenScreenTopY || boundaryMeshBottomY != OVRManager.instance.virtualGreenScreenBottomY)
227 {
228 boundaryMeshTopY = OVRManager.instance.virtualGreenScreenTopY;
229 boundaryMeshBottomY = OVRManager.instance.virtualGreenScreenBottomY;
230 boundaryMesh = OVRCompositionUtil.BuildBoundaryMesh(OVRCompositionUtil.ToBoundaryType(OVRManager.instance.virtualGreenScreenType), boundaryMeshTopY, boundaryMeshBottomY);
231 boundaryMeshType = OVRManager.instance.virtualGreenScreenType;
232
233 // Creating GameObject for testing purpose only
234 //GameObject boundaryMeshObject = new GameObject("BoundaryMeshObject");
235 //boundaryMeshObject.AddComponent<MeshFilter>().mesh = boundaryMesh;
236 //boundaryMeshObject.AddComponent<MeshRenderer>();
237 }
238 }
239
240 public class OVRCameraFrameCompositionManager : MonoBehaviour
241 {
242 public GameObject cameraFrameGameObj;
243 public OVRCameraComposition composition;
244 public RenderTexture boundaryMeshMaskTexture;
245 private Material cameraFrameMaterial;
246 private Material whiteMaterial;
247
248 void Start()
249 {
250 Shader shader = Shader.Find("Oculus/Unlit");
251 if (!shader)
252 {
253 Debug.LogError("Oculus/Unlit shader does not exist");
254 return;
255 }
256 whiteMaterial = new Material(shader);
257 whiteMaterial.color = Color.white;
258 }
259
260 void OnPreRender()
261 {
262 if (OVRManager.instance.virtualGreenScreenType != OVRManager.VirtualGreenScreenType.Off && boundaryMeshMaskTexture != null && composition.boundaryMesh != null)
263 {
264 RenderTexture oldRT = RenderTexture.active;
265 RenderTexture.active = boundaryMeshMaskTexture;
266
267 // The camera matrices haven't been setup when OnPreRender() is executed. Load the projection manually
268 GL.PushMatrix();
269 GL.LoadProjectionMatrix(GetComponent<Camera>().projectionMatrix);
270
271 GL.Clear(false, true, Color.black);
272
273 for (int i = 0; i < whiteMaterial.passCount; ++i)
274 {
275 if (whiteMaterial.SetPass(i))
276 {
277 Graphics.DrawMeshNow(composition.boundaryMesh, composition.cameraRig.ComputeTrackReferenceMatrix());
278 }
279 }
280
281 GL.PopMatrix();
282 RenderTexture.active = oldRT;
283 }
284
285 if (cameraFrameGameObj)
286 {
287 if (cameraFrameMaterial == null)
288 cameraFrameMaterial = cameraFrameGameObj.GetComponent<MeshRenderer>().material;
289 cameraFrameMaterial.SetFloat("_Visible", 1.0f);
290 }
291 }
292 void OnPostRender()
293 {
294 if (cameraFrameGameObj)
295 {
296 Debug.Assert(cameraFrameMaterial);
297 cameraFrameMaterial.SetFloat("_Visible", 0.0f);
298 }
299 }
300 }
301
302}
303
304#endif