using UnityEngine; using UnityEngine.Rendering; using UnityEngine.XR; public class CameraEyeRotator : MonoBehaviour { [SerializeField] [VectorRange(-90, 90, -90, 90, true)] Vector2 leftEyeRotation = Vector2.zero; [SerializeField] [VectorRange(-90, 90, -90, 90, true)] Vector2 rightEyeRotation = Vector2.zero; [SerializeField] Camera referenceCamera; public Vector4 CamData => new Vector4(leftEyeRotation.x, leftEyeRotation.y, rightEyeRotation.x, rightEyeRotation.y); private void Awake() { XRSettings.gameViewRenderMode = GameViewRenderMode.BothEyes; } void OnEnable() { RenderPipelineManager.beginCameraRendering += RenderPipelineManager_beginCameraRendering; } void OnDisable() { RenderPipelineManager.beginCameraRendering -= RenderPipelineManager_beginCameraRendering; } private void RenderPipelineManager_beginCameraRendering(ScriptableRenderContext context, Camera camera) { if (camera.gameObject == gameObject) { // Disable frustrum culling Camera.main.cullingMatrix = Matrix4x4.Ortho(-99999, 99999, -99999, 99999, 0.001f, 99999) * Matrix4x4.Translate(Vector3.forward * -99999 / 2f) * referenceCamera.worldToCameraMatrix; // Get view matrices from reference camera Matrix4x4 viewL = referenceCamera.GetStereoViewMatrix(Camera.StereoscopicEye.Left); Matrix4x4 viewR = referenceCamera.GetStereoViewMatrix(Camera.StereoscopicEye.Right); // Calculate rotation matrices Matrix4x4 rotationLV = Matrix4x4.Rotate(Quaternion.AngleAxis(leftEyeRotation.y, transform.up)); Matrix4x4 rotationLH = Matrix4x4.Rotate(Quaternion.AngleAxis(leftEyeRotation.x, transform.right)); Matrix4x4 rotationRV = Matrix4x4.Rotate(Quaternion.AngleAxis(rightEyeRotation.y, transform.up)); Matrix4x4 rotationRH = Matrix4x4.Rotate(Quaternion.AngleAxis(rightEyeRotation.x, transform.right)); // Set view matrices to MainCamera Camera.main.SetStereoViewMatrix(Camera.StereoscopicEye.Left, rotationLH * rotationLV * viewL); Camera.main.SetStereoViewMatrix(Camera.StereoscopicEye.Right, rotationRH * rotationRV * viewR); } } public void SetCameraEyesRotation(Vector2 lRot, Vector2 rRot) { leftEyeRotation = lRot; rightEyeRotation = rRot; } }