void UpdateCamera(Camera camera) { EyeCamera eyeCamera; if (!eyeCameras.TryGetValue(camera, out eyeCamera)) { return; } if (connected) { if (GetHeadPose(out headPose, out timestamp)) { SetEditorEmulatorsEnabled(false); camera.transform.localRotation = Quaternion.LookRotation(headPose.GetColumn(2), headPose.GetColumn(1)); camera.transform.localPosition = camera.transform.localRotation * headPose.GetRow(3) * -1; } else { SetEditorEmulatorsEnabled(true); } var eyeViews = new UnityEyeViews(); if (GetEyeViews(out eyeViews)) { SetTransformFromMatrix(eyeCamera.leftEyeCamera.gameObject.transform, eyeViews.leftEyePose); SetTransformFromMatrix(eyeCamera.rightEyeCamera.gameObject.transform, eyeViews.rightEyePose); var near = Camera.main.nearClipPlane; var far = Camera.main.farClipPlane; eyeCamera.leftEyeCamera.projectionMatrix = PerspectiveMatrixFromUnityRect(eyeViews.leftEyeViewSize, near, far); eyeCamera.rightEyeCamera.projectionMatrix = PerspectiveMatrixFromUnityRect(eyeViews.rightEyeViewSize, near, far); bool multisampleChanged = multisampleCounts[(int)MultisampleCount] != renderTexture.antiAliasing; // Adjusts render texture size. if (OutputResolution != Resolutions.WindowSized) { var selectedResolutionSize = resolutionSizes[(int)OutputResolution]; if (selectedResolutionSize.width != renderTexture.width || selectedResolutionSize.height != renderTexture.height || multisampleChanged) { ResizeRenderTexture(selectedResolutionSize.width, selectedResolutionSize.height); } } else // OutputResolution == Resolutions.WindowSized { var screenAspectRatio = (float)Screen.width / Screen.height; var eyeViewsWidth = -eyeViews.leftEyeViewSize.left + eyeViews.leftEyeViewSize.right + -eyeViews.rightEyeViewSize.left + eyeViews.rightEyeViewSize.right; var eyeViewsHeight = eyeViews.leftEyeViewSize.top + -eyeViews.leftEyeViewSize.bottom; if (eyeViewsHeight > 0f) { int renderTextureHeight; int renderTextureWidth; var eyeViewsAspectRatio = eyeViewsWidth / eyeViewsHeight; if (screenAspectRatio > eyeViewsAspectRatio) { renderTextureHeight = Screen.height; renderTextureWidth = (int)(Screen.height * eyeViewsAspectRatio); } else { renderTextureWidth = Screen.width; renderTextureHeight = (int)(Screen.width / eyeViewsAspectRatio); } renderTextureWidth = renderTextureWidth & ~0x3; renderTextureHeight = renderTextureHeight & ~0x3; if (multisampleChanged || renderTexture.width != renderTextureWidth || renderTexture.height != renderTextureHeight) { ResizeRenderTexture(renderTextureWidth, renderTextureHeight); } } } } } else // !connected { SetEditorEmulatorsEnabled(true); if (renderTexture.width != Screen.width || renderTexture.height != Screen.height) { ResizeRenderTexture(Screen.width, Screen.height); } } }
void Update() { if (!EnsureCameras()) { return; } if (GvrViewer.Instance != null) { GvrViewer.Instance.VRModeEnabled = false; } var mainCamera = Camera.main; var mainCameraObject = mainCamera.gameObject; var mainCameraTransform = mainCameraObject.transform; var newConnectionState = IsConnected(); if (connected && !newConnectionState) { Debug.Log("Disconnected from Instant Preview."); } else if (!connected && newConnectionState) { Debug.Log("Connected to Instant Preview."); } connected = newConnectionState; // Removes extra cameras created by GvrViewer. foreach (var camera in mainCamera.GetComponentsInChildren <Camera>()) { if (camera.GetComponent <GvrEye>() != null) { camera.gameObject.SetActive(false); } } // Disables gvr pre and post render objects. if (GvrViewer.Instance != null) { foreach (Transform transform in GvrViewer.Instance.transform) { transform.gameObject.SetActive(false); } } if (connected) { var gvrHead = mainCameraObject.GetComponent <GvrHead>(); if (GetHeadPose(out headPose, out timestamp)) { // Disables gvr head tracking if using Instant Preview head tracking. if (gvrHead != null) { gvrHead.trackPosition = false; gvrHead.trackRotation = false; } mainCameraTransform.localRotation = Quaternion.LookRotation(headPose.GetColumn(2), headPose.GetColumn(1)); } else { // Re-enables gvr head tracking if using controller only mode. if (gvrHead != null) { gvrHead.trackRotation = true; } } var eyeViews = new UnityEyeViews(); if (GetEyeViews(out eyeViews)) { SetTransformFromMatrix(leftEyeCamera.gameObject.transform, eyeViews.leftEyePose); SetTransformFromMatrix(rightEyeCamera.gameObject.transform, eyeViews.rightEyePose); var near = Camera.main.nearClipPlane; var far = Camera.main.farClipPlane; leftEyeCamera.projectionMatrix = PerspectiveMatrixFromUnityRect(eyeViews.leftEyeViewSize, near, far); rightEyeCamera.projectionMatrix = PerspectiveMatrixFromUnityRect(eyeViews.rightEyeViewSize, near, far); bool multisampleChanged = multisampleCounts[(int)MultisampleCount] != renderTexture.antiAliasing; // Adjusts render texture size. if (OutputResolution != Resolutions.WindowSized) { var selectedResolutionSize = resolutionSizes[(int)OutputResolution]; if (selectedResolutionSize.width != renderTexture.width || selectedResolutionSize.height != renderTexture.height || multisampleChanged) { ResizeRenderTexture(selectedResolutionSize.width, selectedResolutionSize.height); } } else // OutputResolution == Resolutions.WindowSized { var screenAspectRatio = (float)Screen.width / Screen.height; var eyeViewsWidth = -eyeViews.leftEyeViewSize.left + eyeViews.leftEyeViewSize.right + -eyeViews.rightEyeViewSize.left + eyeViews.rightEyeViewSize.right; var eyeViewsHeight = eyeViews.leftEyeViewSize.top + -eyeViews.leftEyeViewSize.bottom; if (eyeViewsHeight > 0f) { int renderTextureHeight; int renderTextureWidth; var eyeViewsAspectRatio = eyeViewsWidth / eyeViewsHeight; if (screenAspectRatio > eyeViewsAspectRatio) { renderTextureHeight = Screen.height; renderTextureWidth = (int)(Screen.height * eyeViewsAspectRatio); } else { renderTextureWidth = Screen.width; renderTextureHeight = (int)(Screen.width / eyeViewsAspectRatio); } renderTextureWidth = renderTextureWidth & ~0x3; renderTextureHeight = renderTextureHeight & ~0x3; if (multisampleChanged || renderTexture.width != renderTextureWidth || renderTexture.height != renderTextureHeight) { ResizeRenderTexture(renderTextureWidth, renderTextureHeight); } } } } } else // !connected { if (renderTexture.width != Screen.width || renderTexture.height != Screen.height) { ResizeRenderTexture(Screen.width, Screen.height); } } }
private static extern bool GetEyeViews(out UnityEyeViews outputEyeViews);
void Update() { if (!EnsureCameras()) { return; } var mainCamera = Camera.main; var mainCameraObject = mainCamera.gameObject; var mainCameraTransform = mainCameraObject.transform; var newConnectionState = IsConnected(); if (connected && !newConnectionState) { Debug.Log("Disconnected from Instant Preview."); } else if (!connected && newConnectionState) { Debug.Log("Connected to Instant Preview."); } connected = newConnectionState; if (connected) { if (GetHeadPose(out headPose, out timestamp)) { SetEditorEmulatorsEnabled(false); mainCameraTransform.localRotation = Quaternion.LookRotation(headPose.GetColumn(2), headPose.GetColumn(1)); } else { SetEditorEmulatorsEnabled(true); } var eyeViews = new UnityEyeViews(); if (GetEyeViews(out eyeViews)) { SetTransformFromMatrix(leftEyeCamera.gameObject.transform, eyeViews.leftEyePose); SetTransformFromMatrix(rightEyeCamera.gameObject.transform, eyeViews.rightEyePose); var near = Camera.main.nearClipPlane; var far = Camera.main.farClipPlane; leftEyeCamera.projectionMatrix = PerspectiveMatrixFromUnityRect(eyeViews.leftEyeViewSize, near, far); rightEyeCamera.projectionMatrix = PerspectiveMatrixFromUnityRect(eyeViews.rightEyeViewSize, near, far); bool multisampleChanged = multisampleCounts[(int)MultisampleCount] != renderTexture.antiAliasing; // Adjusts render texture size. if (OutputResolution != Resolutions.WindowSized) { var selectedResolutionSize = resolutionSizes[(int)OutputResolution]; if (selectedResolutionSize.width != renderTexture.width || selectedResolutionSize.height != renderTexture.height || multisampleChanged) { ResizeRenderTexture(selectedResolutionSize.width, selectedResolutionSize.height); } } else // OutputResolution == Resolutions.WindowSized { var screenAspectRatio = (float)Screen.width / Screen.height; var eyeViewsWidth = -eyeViews.leftEyeViewSize.left + eyeViews.leftEyeViewSize.right + -eyeViews.rightEyeViewSize.left + eyeViews.rightEyeViewSize.right; var eyeViewsHeight = eyeViews.leftEyeViewSize.top + -eyeViews.leftEyeViewSize.bottom; if (eyeViewsHeight > 0f) { int renderTextureHeight; int renderTextureWidth; var eyeViewsAspectRatio = eyeViewsWidth / eyeViewsHeight; if (screenAspectRatio > eyeViewsAspectRatio) { renderTextureHeight = Screen.height; renderTextureWidth = (int)(Screen.height * eyeViewsAspectRatio); } else { renderTextureWidth = Screen.width; renderTextureHeight = (int)(Screen.width / eyeViewsAspectRatio); } renderTextureWidth = renderTextureWidth & ~0x3; renderTextureHeight = renderTextureHeight & ~0x3; if (multisampleChanged || renderTexture.width != renderTextureWidth || renderTexture.height != renderTextureHeight) { ResizeRenderTexture(renderTextureWidth, renderTextureHeight); } } } } } else // !connected { SetEditorEmulatorsEnabled(true); if (renderTexture.width != Screen.width || renderTexture.height != Screen.height) { ResizeRenderTexture(Screen.width, Screen.height); } } }