Esempio n. 1
0
    private void CreatePortalCamera(Camera currentCamera, Camera.StereoscopicEye eye, out Camera portalCamera, ref RenderTexture portalTexture)
    {
        portalCamera = null;

        // Create the render texture (if needed)
        if (!portalTexture || m_OldReflectionTextureSize != m_TextureSize)                        // if it doesn't exist or the size has changed
        {
            if (portalTexture)                                                                    // if it does exist
            {
                DestroyImmediate(portalTexture);                                                  // destroy it first
            }
            portalTexture              = new RenderTexture(m_TextureSize, m_TextureSize, 24);     // <<<< make buffer size 24??
            portalTexture.name         = "__MirrorReflection" + eye.ToString() + GetInstanceID(); // create the name of the object
            portalTexture.isPowerOfTwo = true;                                                    // https://docs.unity3d.com/Manual/Textures.html: Non power of two texture assets can be scaled up at import time using the Non Power of 2 option in the advanced texture type in the import settings. Unity will scale texture contents as requested, and in the game they will behave just like any other texture, so they can still be compressed and very fast to load.
            portalTexture.hideFlags    = HideFlags.DontSave;                                      // The object will not be saved to the Scene. It will not be destroyed when a new Scene is loaded.

            portalTexture.antiAliasing = 4;                                                       // < <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<ResourceIntensive but pretty

            m_OldReflectionTextureSize = m_TextureSize;                                           // save the old texture size
        }

        // Create camera with the render texture
        if (!m_PortalCameras.TryGetValue(currentCamera, out portalCamera))                                                                                             // if it does not yet exist in the dictionary, create it. If it does, assign it. (catch both not-in-dictionary and in-dictionary-but-deleted-GO)
        {
            GameObject go = new GameObject("Mirror Reflection Camera id" + GetInstanceID() + " for " + currentCamera.GetInstanceID(), typeof(Camera), typeof(Skybox)); // create the new game object
            portalCamera                    = go.GetComponent <Camera>();
            portalCamera.enabled            = false;
            portalCamera.transform.position = transform.position;
            portalCamera.transform.rotation = transform.rotation;
            portalCamera.tag                = "PortalCam";    // Tag it as a portal camera so it doesn't participate in the additional CameraRender function
            //portalCamera.gameObject.AddComponent<FlareLayer>(); // Adds a flare layer to make Lens Flares appear in the image?? disabled for now
            go.hideFlags = HideFlags.DontSave;                // The object will not be saved to the Scene. It will not be destroyed when a new Scene is loaded.
            m_PortalCameras.Add(currentCamera, portalCamera); // add the newly created camera to the dictionary
        }
    }
    // Use this function for procedural rendering
    // Gets called twice per frame, once for each eye.
    // On each frame, left eye renders before right eye so
    // we keep track of a boolean that toggles back and forth
    // between each eye.
    void OnRenderObject()
    {
        if (keyboardProvider == null || !IsReady)
        {
            return;
        }
#if UNITY_HAS_GOOGLEVR
        Camera camera = Camera.current;
        if (camera && camera == Camera.main)
        {
            // Get current eye.
            Camera.StereoscopicEye camEye = isRight ? Camera.StereoscopicEye.Right : Camera.StereoscopicEye.Left;

            // Camera matrices.
            Matrix4x4 proj      = camera.GetStereoProjectionMatrix(camEye);
            Matrix4x4 modelView = camera.GetStereoViewMatrix(camEye);

            // Camera viewport.
            Rect viewport = camera.pixelRect;

            // Render keyboard.
            keyboardProvider.Render((int)camEye, modelView, proj, viewport);

            // Swap.
            isRight = !isRight;
        }
#else
        Debug.LogWarning("Keyboard is not supported in versions of Unity without the native integration");
#endif  // !UNITY_HAS_GOOGLEVR
    }
Esempio n. 3
0
 public static Vector4 GetProjectionExtents(this Camera camera, Camera.StereoscopicEye eye)
 {
     return(GetProjectionExtents(camera,
                                 eye,
                                 0.0f,
                                 0.0f));
 }
Esempio n. 4
0
        //protected override void RenderVirtualImage(RenderTexture targetTexture, Camera.StereoscopicEye eye, float stereoSeparation = 0)
        //{
        //    ReflectCamera(stereoSeparation);

        //    Matrix4x4 eLeftViewMatrix = _currentCamera.GetStereoViewMatrix(Camera.StereoscopicEye.Left);
        //    Matrix4x4 eRightViewMatrix = _currentCamera.GetStereoViewMatrix(Camera.StereoscopicEye.Right);
        //    Matrix4x4 eLeftProjMatrix = _currentCamera.GetStereoProjectionMatrix(Camera.StereoscopicEye.Left);
        //    Matrix4x4 eRightProjMatrix = _currentCamera.GetStereoProjectionMatrix(Camera.StereoscopicEye.Right);

        //    _reflectionCamera.SetStereoProjectionMatrix(Camera.StereoscopicEye.Left, eLeftProjMatrix);
        //    _reflectionCamera.SetStereoProjectionMatrix(Camera.StereoscopicEye.Right, eRightProjMatrix);
        //    _reflectionCamera.SetStereoViewMatrix(Camera.StereoscopicEye.Left, eRightViewMatrix);
        //    _reflectionCamera.SetStereoViewMatrix(Camera.StereoscopicEye.Right, eLeftViewMatrix);
        //    Debug.Log("MATRICES");
        //    Debug.Log(_reflectionCamera.GetStereoProjectionMatrix(Camera.StereoscopicEye.Left).ToString("f3"));
        //    Debug.Log(_reflectionCamera.GetStereoProjectionMatrix(Camera.StereoscopicEye.Right).ToString("f3"));
        //    Debug.Log(_reflectionCamera.GetStereoViewMatrix(Camera.StereoscopicEye.Left).ToString("f3"));
        //    Debug.Log(_reflectionCamera.GetStereoViewMatrix(Camera.StereoscopicEye.Right).ToString("f3"));

        //    //2) Compute ProjectionMatrix
        //    // Setup oblique projection matrix so that near plane is our reflection
        //    // plane. This way we clip everything below/above it for free.
        //    _reflectionCamera.ResetProjectionMatrix();
        //    //_reflectionCamera.projectionMatrix = (_currentCamera.stereoEnabled) ? _currentCamera.GetStereoProjectionMatrix(eye) : _currentCamera.projectionMatrix;
        //    //_reflectionCamera.projectionMatrix = _currentCamera.projectionMatrix;
        //    _reflectionCamera.targetTexture = targetTexture;

        //    if (_useRenderWithShader)
        //    {
        //        Shader.SetGlobalVector("MIRROR_PLANE_POS", transform.position);
        //        Shader.SetGlobalVector("MIRROR_PLANE_NORMAL", GetNormal());
        //        _reflectionCamera.RenderWithShader(_replacementShader, "RenderType");
        //    }
        //    else
        //    {
        //        Vector4 clipPlane = CameraSpacePlane(_reflectionCamera.worldToCameraMatrix, transform.position, GetNormal(), 1.0f);
        //        _reflectionCamera.projectionMatrix = _reflectionCamera.CalculateObliqueMatrix(clipPlane);
        //        _reflectionCamera.Render();
        //    }
        //}

        protected override void RenderVirtualImage(Camera.StereoscopicEye eye, float stereoSeparation = 0)
        {
            GL.invertCulling = true;

            //1) Compute worldToCamera Matrix
            _reflectionCamera.worldToCameraMatrix  = _currentCamera.stereoEnabled ? _currentCamera.GetStereoViewMatrix(eye) : _currentCamera.worldToCameraMatrix;
            _reflectionCamera.worldToCameraMatrix *= CalculateReflectionMatrix();

            //2) Compute the projection matrix
            _reflectionCamera.projectionMatrix = (_currentCamera.stereoEnabled) ? _currentCamera.GetStereoProjectionMatrix(eye) : _currentCamera.projectionMatrix;

            //3) Do the render
#if UNITY_ANDROID && UNITY_EDITOR
            Shader.SetGlobalInt(QuickMirrorReflectionManager.REFLECTION_INVERT_Y, Application.isPlaying ? 0 : 1);
#else
            Shader.SetGlobalInt(QuickMirrorReflectionManager.REFLECTION_INVERT_Y, Application.isMobilePlatform ? 0 : 1);
#endif

            if (_useRenderWithShader)
            {
                Shader.SetGlobalVector("MIRROR_PLANE_POS", transform.position);
                Shader.SetGlobalVector("MIRROR_PLANE_NORMAL", GetNormal());
                _reflectionCamera.RenderWithShader(_replacementShader, "RenderType");
            }
            else
            {
                Vector4 clipPlane = CameraSpacePlane(_reflectionCamera.worldToCameraMatrix, transform.position, GetNormal(), 1.0f);
                _reflectionCamera.projectionMatrix = _reflectionCamera.CalculateObliqueMatrix(clipPlane);
                _reflectionCamera.Render();
            }

            GL.invertCulling = false;
        }
Esempio n. 5
0
    // Use this function for procedural rendering
    // Gets called twice per frame, once for each eye.
    // On each frame, left eye renders before right eye so
    // we keep track of a boolean that toggles back and forth
    // between each eye.
    private void OnRenderObject()
    {
        if (keyboardProvider == null || !IsReady)
        {
            return;
        }

#if UNITY_ANDROID
        Camera camera = Camera.current;
        if (camera && camera == Camera.main)
        {
            // Get current eye.
            Camera.StereoscopicEye camEye = isRight ?
                                            Camera.StereoscopicEye.Right :
                                            Camera.StereoscopicEye.Left;

            // Camera matrices.
            Matrix4x4 proj      = camera.GetStereoProjectionMatrix(camEye);
            Matrix4x4 modelView = camera.GetStereoViewMatrix(camEye);

            // Camera viewport.
            Rect viewport = camera.pixelRect;

            // Render keyboard.
            keyboardProvider.Render((int)camEye, modelView, proj, viewport);

            // Swap.
            isRight = !isRight;
        }
#endif  // !UNITY_ANDROID
    }
Esempio n. 6
0
        public Matrix4x4 GetStereoProjectionMatrix(Camera.StereoscopicEye eye)
        {
            Matrix4x4 result;

            Camera.INTERNAL_CALL_GetStereoProjectionMatrix(this, eye, out result);
            return(result);
        }
Esempio n. 7
0
        public Matrix4x4 GetStereoProjectionMatrix(Camera.StereoscopicEye eye)
        {
            Matrix4x4 result;

            this.GetStereoProjectionMatrix_Injected(eye, out result);
            return(result);
        }
Esempio n. 8
0
        private void adjustViewMatrix(Camera.StereoscopicEye eye, float baselineAdjust)
        {
            float   eyeOffset              = eye == Camera.StereoscopicEye.Left ? 1 : -1;
            Vector3 ipdOffset              = eyeOffset * Vector3.right * baselineAdjust * 0.5f;
            Vector3 providerForwardOffset  = Vector3.zero,
                    providerVerticalOffset = Vector3.zero;
            Quaternion providerRotation    = Quaternion.Euler(0f, 180f, 0f);

            if (_provider is LeapXRServiceProvider || _provider.GetType().BaseType == typeof(LeapXRServiceProvider))
            {
                LeapXRServiceProvider _xrProvider = _provider as LeapXRServiceProvider;
                providerForwardOffset  = Vector3.forward * _xrProvider.deviceOffsetZAxis;
                providerVerticalOffset = -Vector3.up * _xrProvider.deviceOffsetYAxis;
                providerRotation       = Quaternion.AngleAxis(_xrProvider.deviceTiltXAxis, Vector3.right);
            }
            else
            {
                Matrix4x4 imageMatWarp = _camera.projectionMatrix
                                         * Matrix4x4.TRS(Vector3.zero, providerRotation, Vector3.one)
                                         * _camera.projectionMatrix.inverse;
                Shader.SetGlobalMatrix("_LeapGlobalWarpedOffset", imageMatWarp);
            }

            var existingMatrix = _camera.GetStereoViewMatrix(eye);

            _camera.SetStereoViewMatrix(eye, Matrix4x4.TRS(Vector3.zero, providerRotation, Vector3.one) *
                                        Matrix4x4.Translate(providerForwardOffset + ipdOffset) *
                                        Matrix4x4.Translate(providerVerticalOffset) *
                                        existingMatrix);
        }
    // On-demand create any objects we need
    private void CreateMirrorObjects(Camera currentCamera, Camera.StereoscopicEye eye, out Camera reflectionCamera, ref RenderTexture reflectionTexture)
    {
        reflectionCamera = null;


        // Reflection render texture
        if (!reflectionTexture || m_OldReflectionTextureSize != m_TextureSize)
        {
            if (reflectionTexture)
            {
                DestroyImmediate(reflectionTexture);
            }
            reflectionTexture              = new RenderTexture(m_TextureSize, m_TextureSize, 16);
            reflectionTexture.name         = "__MirrorReflection" + eye.ToString() + GetInstanceID();
            reflectionTexture.isPowerOfTwo = true;
            reflectionTexture.hideFlags    = HideFlags.DontSave;
            m_OldReflectionTextureSize     = m_TextureSize;
        }

        // Camera for reflection
        if (!m_ReflectionCameras.TryGetValue(currentCamera, out reflectionCamera)) // catch both not-in-dictionary and in-dictionary-but-deleted-GO
        {
            GameObject go = new GameObject("Mirror Reflection Camera id" + GetInstanceID() + " for " + currentCamera.GetInstanceID(), typeof(Camera), typeof(Skybox));
            reflectionCamera                    = go.GetComponent <Camera>();
            reflectionCamera.enabled            = false;
            reflectionCamera.transform.position = transform.position;
            reflectionCamera.transform.rotation = transform.rotation;
            reflectionCamera.gameObject.AddComponent <FlareLayer>();
            go.hideFlags = HideFlags.DontSave;
            m_ReflectionCameras.Add(currentCamera, reflectionCamera);
        }
    }
Esempio n. 10
0
 internal XRView(Matrix4x4 proj, Matrix4x4 view, Rect vp)
 {
     projMatrix      = proj;
     viewMatrix      = view;
     viewport        = vp;
     occlusionMesh   = null;
     legacyStereoEye = (Camera.StereoscopicEye)(-1);
 }
 internal XRView(XRDisplaySubsystem.XRRenderParameter renderParameter)
 {
     projMatrix      = renderParameter.projection;
     viewMatrix      = renderParameter.view;
     viewport        = renderParameter.viewport;
     occlusionMesh   = renderParameter.occlusionMesh;
     legacyStereoEye = (Camera.StereoscopicEye)(-1);
 }
Esempio n. 12
0
 internal XRView(Camera camera, Camera.StereoscopicEye eye)
 {
     projMatrix      = camera.GetStereoProjectionMatrix(eye);
     viewMatrix      = camera.GetStereoViewMatrix(eye);
     viewport        = camera.pixelRect;
     occlusionMesh   = null;
     legacyStereoEye = eye;
 }
Esempio n. 13
0
 internal XRView(Matrix4x4 proj, Matrix4x4 view, Rect vp, int dstSlice)
 {
     projMatrix        = proj;
     viewMatrix        = view;
     viewport          = vp;
     occlusionMesh     = null;
     textureArraySlice = dstSlice;
     legacyStereoEye   = (Camera.StereoscopicEye)(-1);
 }
Esempio n. 14
0
    private void onPreCullCallback(Camera camera)
    {
        if (camera != _targetCamera)
        {
            return;
        }

        _currentEye = Camera.StereoscopicEye.Left;
    }
Esempio n. 15
0
    Vector4 GetViewportSpaceOffset(Vector3 vignetteCenterWorldSpace, Camera.StereoscopicEye eye)
    {
        Matrix4x4 worldToClipSpace = GetWorldToClipMatrix(eye);

        // get world-space position as clip-space position
        Vector4 viewportSpaceOffset = worldToClipSpace.MultiplyPoint(vignetteCenterWorldSpace);

        // convert clip-space [-1,1] to viewport space [0,1]. also negate the offset.
        viewportSpaceOffset = -NormalizeScreenSpaceCords(viewportSpaceOffset);
        return(viewportSpaceOffset);
    }
Esempio n. 16
0
    //----------------------------------------------------------------------------

#if UNITY_STEREOSCOPIC_EYE
    void UpdateViewMatrix(bool isVR = false, Camera.StereoscopicEye eye = Camera.StereoscopicEye.Left)
    {
        if (!isVR)
        {
            m_CameraDescription.ViewMatrix = this.m_Camera.worldToCameraMatrix;
        }
        else
        {
            m_CameraDescription.ViewMatrix = this.m_Camera.GetStereoViewMatrix(eye);
        }
    }
        public static void Postfix(Camera.StereoscopicEye eye, ref Matrix4x4 __result)
        {
            //if (Input.GetKey(KeyCode.RightArrow))
            //{
            //    matrixAdditave[selectedMatrixIndex] += 0.01f;
            //    Console.WriteLine($"matrix {selectedMatrixIndex} offset is {matrixAdditave[selectedMatrixIndex] }");
            //}
            //else if (Input.GetKey(KeyCode.LeftArrow))
            //{
            //    matrixAdditave[selectedMatrixIndex] -= 0.01f;
            //    Console.WriteLine($"matrix {selectedMatrixIndex} offset is {matrixAdditave[selectedMatrixIndex] }");
            //}

            //if (Input.anyKey)
            //{
            //    if (!keyDown)
            //    {
            //        if (Input.GetKey(KeyCode.UpArrow))
            //        {
            //            selectedMatrixIndex++;

            //            if (selectedMatrixIndex > 15)
            //                selectedMatrixIndex = 15;

            //            keyDown = true;
            //            Console.WriteLine($"Selected matrix {selectedMatrixIndex}");
            //        }
            //        else if (Input.GetKey(KeyCode.DownArrow))
            //        {
            //            selectedMatrixIndex--;

            //            if (selectedMatrixIndex < 0)
            //                selectedMatrixIndex = 0;

            //            keyDown = true;
            //            Console.WriteLine($"Selected matrix {selectedMatrixIndex}");
            //        }
            //        else if (Input.GetKey(KeyCode.R))
            //        {
            //            matrixAdditave[selectedMatrixIndex] = 0;
            //            keyDown = true;
            //            Console.WriteLine($"reset matrix {selectedMatrixIndex}");
            //        }
            //    }
            //}
            //else if (keyDown)
            //    keyDown = false;
            //for (int i = 0; i < 15; i++)
            //    __result[i] += eye == Camera.StereoscopicEye.Left ? -matrixAdditave[i] : matrixAdditave[i];

            __result[8] += eye == Camera.StereoscopicEye.Left ? -0.1f : 0.1f;
        }
        /// <summary>
        /// Renders the camera to the specified target texture.
        /// </summary>
        ///
        /// <remarks>
        /// The specified target eye will determine which projection matrix
        /// to use when rendering. For example, if the eye is set to
        /// Camera.StereoscopicEye.Left, the camera will use its left eye
        /// stereo projection matrix.
        /// </remarks>
        ///
        /// <param name="targetTexture">
        /// The target texture to render to.
        /// </param>
        /// <param name="eye">
        /// The target eye to render the perspective from.
        /// </param>
        public static void Render(
            this Camera c,
            RenderTexture targetTexture,
            Camera.StereoscopicEye eye)
        {
            Matrix4x4 originalProjectionMatrix = c.projectionMatrix;

            {
                c.projectionMatrix = c.GetStereoProjectionMatrix(eye);
                c.Render(targetTexture);
            }
            c.projectionMatrix = originalProjectionMatrix;
        }
Esempio n. 19
0
    Matrix4x4 GetCameraMatrix(Camera.StereoscopicEye eye)
    {
        var cam = Camera.main;

        if (cam.stereoEnabled)
        {
            return(cam.GetStereoViewMatrix(eye).inverse);
        }
        else
        {
            return(cam.cameraToWorldMatrix);
        }
    }
        /// <summary>
        /// Renders the camera to the specified target texture.
        /// </summary>
        ///
        /// <remarks>
        /// The specified target eye will determine which projection matrix
        /// to use when rendering. For example, if the eye is set to
        /// Camera.StereoscopicEye.Left, the camera will use its left eye
        /// stereo projection matrix.
        ///
        /// Additionally, the specified pose corresponds to the desired world
        /// pose to render the camera perspective from.
        /// </remarks>
        ///
        /// <param name="targetTexture">
        /// The target texture to render to.
        /// </param>
        /// <param name="eye">
        /// The target eye to render the perspective from.
        /// </param>
        /// <param name="pose">
        /// The world pose to render the perspective from.
        /// </param>
        public static void Render(
            this Camera c,
            RenderTexture targetTexture,
            Camera.StereoscopicEye eye,
            Pose pose)
        {
            Pose originalPose = c.transform.ToPose();

            {
                c.transform.SetPose(pose);
                c.Render(targetTexture, eye);
            }
            c.transform.SetPose(originalPose);
        }
Esempio n. 21
0
        internal XRView(XRDisplaySubsystem.XRRenderPass renderPass, XRDisplaySubsystem.XRRenderParameter renderParameter)
        {
            projMatrix      = renderParameter.projection;
            viewMatrix      = renderParameter.view;
            viewport        = renderParameter.viewport;
            occlusionMesh   = renderParameter.occlusionMesh;
            legacyStereoEye = (Camera.StereoscopicEye)(-1);

            // Convert viewport from normalized to screen space
            viewport.x      *= renderPass.renderTargetDesc.width;
            viewport.width  *= renderPass.renderTargetDesc.width;
            viewport.y      *= renderPass.renderTargetDesc.height;
            viewport.height *= renderPass.renderTargetDesc.height;
        }
Esempio n. 22
0
    void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        descriptor                 = src.descriptor;
        descriptor.width           = Screen.width / FastFilter;
        descriptor.height          = Screen.height / FastFilter;
        descriptor.depthBufferBits = 0;
        switch ((int)SampleCount)
        {
        case 6:
            motionBlurMaterial.DisableKeyword(eightFeature);
            motionBlurMaterial.DisableKeyword(tenFeature);
            break;

        case 8:
            motionBlurMaterial.EnableKeyword(eightFeature);
            motionBlurMaterial.DisableKeyword(tenFeature);
            break;

        case 10:
            motionBlurMaterial.EnableKeyword(eightFeature);
            motionBlurMaterial.EnableKeyword(tenFeature);
            break;
        }

        eyeIndex = cam.stereoActiveEye == Camera.MonoOrStereoscopicEye.Left ? Camera.StereoscopicEye.Left : Camera.StereoscopicEye.Right;
        viewProj = cam.GetStereoProjectionMatrix(eyeIndex) * cam.worldToCameraMatrix;
        if (eyeIndex == Camera.StereoscopicEye.Left && viewProj == previousViewProjectionLeft || eyeIndex == Camera.StereoscopicEye.Right && viewProj == previousViewProjectionRight)
        {
            Graphics.Blit(src, dest);
            return;
        }
        currentToPreviousViewProjectionMatrix = (cam.stereoActiveEye == Camera.MonoOrStereoscopicEye.Left ? previousViewProjectionLeft : previousViewProjectionRight) * viewProj.inverse;
        motionBlurMaterial.SetMatrix(currentPrevString, currentToPreviousViewProjectionMatrix);
        motionBlurMaterial.SetFloat(distanceString, 1 - Distance);
        if (eyeIndex == Camera.StereoscopicEye.Left)
        {
            previousViewProjectionLeft = viewProj;
        }
        else
        {
            previousViewProjectionRight = viewProj;
        }
        var rt = RenderTexture.GetTemporary(descriptor);

        Graphics.Blit(src, rt, motionBlurMaterial, 0);
        motionBlurMaterial.SetTexture(blurTexString, rt);
        Graphics.Blit(src, dest, motionBlurMaterial, 1);
        RenderTexture.ReleaseTemporary(rt);
    }
Esempio n. 23
0
    //----------------------------------------------------------------------------

    void UpdateProjectionMatrix(bool isVR = false, Camera.StereoscopicEye eye = Camera.StereoscopicEye.Left)
    {
        Matrix4x4 projectionMatrix;

        if (!isVR)
        {
            projectionMatrix = m_Camera.projectionMatrix;
        }
        else
        {
            projectionMatrix = m_Camera.GetStereoProjectionMatrix(eye);
        }

        m_CameraDescription.m_ProjectionMatrix = GL.GetGPUProjectionMatrix(projectionMatrix, false);
    }
Esempio n. 24
0
    private void setCameraMatrix(Camera.StereoscopicEye eye)
    {
        Matrix4x4 P = _targetCamera.GetStereoProjectionMatrix(eye);
        Matrix4x4 V = _targetCamera.GetStereoViewMatrix(eye);

        V *= calculateReflectionMatrix(calculatePlane());

        _reflectionCamera.projectionMatrix    = P;
        _reflectionCamera.worldToCameraMatrix = V;

        Vector4 plane = calculateCameraSpacePlane(V, _reflectionAxis.position, _reflectionAxis.forward, 1, 0);

        P = _reflectionCamera.CalculateObliqueMatrix(plane);
        _reflectionCamera.projectionMatrix = P;
    }
Esempio n. 25
0
    /// <summary>
    /// 対象の目におけるOblique near-plane projection matrix を計算して返す
    /// </summary>
    /// <returns>対象の目のOblique near-plane projection matrix</returns>
    private static Matrix4x4 CalculateObliqueMatrix(Camera camera, Camera.StereoscopicEye eye, Transform clipPlaneTransform)
    {
        // Require the projection matrix of the specified eye to be reset previously
        // but it is impossible because of no method to reset only either,
        // so this method should be private for now.

        Matrix4x4 baseViewMatrix = camera.GetStereoViewMatrix(eye);
        Matrix4x4 baseProjMatrix = camera.GetStereoProjectionMatrix(eye);

        Vector3 clipPlaneNormal   = baseViewMatrix.MultiplyVector(clipPlaneTransform.forward);
        Vector3 clipPlanePosition = baseViewMatrix.MultiplyPoint(clipPlaneTransform.position);

        Vector4 clipPlane = CalculateClipPlane(clipPlaneNormal, clipPlanePosition);

        return(CalculateObliqueMatrix(baseProjMatrix, clipPlane));
    }
        /// <summary>
        /// Makes this camera's settings match the other camera.
        /// </summary>
        ///
        /// <remarks>
        /// This will copy all camera variables (field of view, clear flags,
        /// culling mask, etc.) from the other camera.
        ///
        /// Additionally it will copy the other camera's stereo projection
        /// matrix to this camera's mono/stereo projection matrices and set
        /// the stereo target eye based on the specified eye.
        /// </remarks>
        ///
        /// <param name="other">
        /// The camera to copy settings from.
        /// </param>
        /// <param name="eye">
        /// The stereo target eye to copy the projection matrix from.
        /// </param>
        public static void CopyFrom(
            this Camera c, Camera other, Camera.StereoscopicEye eye)
        {
            c.CopyFrom(other);
            c.projectionMatrix = c.GetStereoProjectionMatrix(eye);

            switch (eye)
            {
            case Camera.StereoscopicEye.Left:
                c.stereoTargetEye = StereoTargetEyeMask.Left;
                break;

            case Camera.StereoscopicEye.Right:
                c.stereoTargetEye = StereoTargetEyeMask.Right;
                break;
            }
        }
Esempio n. 27
0
    private Vector4 GetProjectionExtentsCTAA(Camera camera, Camera.StereoscopicEye eye, float texelOffsetX, float texelOffsetY)
    {
        Matrix4x4 inv   = Matrix4x4.Inverse(camera.GetStereoProjectionMatrix(eye));
        Vector3   ray00 = inv.MultiplyPoint3x4(new Vector3(-1.0f, -1.0f, 0.95f));
        Vector3   ray11 = inv.MultiplyPoint3x4(new Vector3(1.0f, 1.0f, 0.95f));

        ray00 /= -ray00.z;
        ray11 /= -ray11.z;

        float oneExtentX = 0.5f * (ray11.x - ray00.x);
        float oneExtentY = 0.5f * (ray11.y - ray00.y);
        float texelSizeX = oneExtentX / (0.5f * camera.pixelWidth);
        float texelSizeY = oneExtentY / (0.5f * camera.pixelHeight);
        float oneJitterX = 0.5f * (ray11.x + ray00.x) + texelSizeX * texelOffsetX;
        float oneJitterY = 0.5f * (ray11.y + ray00.y) + texelSizeY * texelOffsetY;

        return(new Vector4(oneExtentX, oneExtentY, oneJitterX, oneJitterY));
    }
Esempio n. 28
0
    //----------------------------------------------------------------------------

#if UNITY_STEREOSCOPIC_EYE
    void UpdateProjectionMatrix(bool isVR = false, Camera.StereoscopicEye eye = Camera.StereoscopicEye.Left)
    {
        bool isRenderingToTexture = m_HasPostFx ||
                                    this.m_Camera.actualRenderingPath == RenderingPath.DeferredLighting ||
                                    this.m_Camera.actualRenderingPath == RenderingPath.DeferredShading;
        Matrix4x4 projectionMatrix;

        if (!isVR)
        {
            projectionMatrix = this.m_Camera.projectionMatrix;
        }
        else
        {
            projectionMatrix = this.m_Camera.GetStereoProjectionMatrix(eye);
        }

        m_CameraDescription.ProjectionMatrix = GL.GetGPUProjectionMatrix(projectionMatrix, isRenderingToTexture);
    }
Esempio n. 29
0
        public void ConfigureStereoJitteredProjectionMatrices(PostProcessRenderContext context)
        {
            Camera camera = context.camera;

            this.jitter  = GenerateRandomOffset();
            this.jitter *= jitterSpread;
            for (Camera.StereoscopicEye stereoscopicEye = Camera.StereoscopicEye.Left; stereoscopicEye <= Camera.StereoscopicEye.Right; stereoscopicEye++)
            {
                context.camera.CopyStereoDeviceProjectionMatrixToNonJittered(stereoscopicEye);
                Matrix4x4 stereoNonJitteredProjectionMatrix = context.camera.GetStereoNonJitteredProjectionMatrix(stereoscopicEye);
                Matrix4x4 matrix = RuntimeUtilities.GenerateJitteredProjectionMatrixFromOriginal(context, stereoNonJitteredProjectionMatrix, this.jitter);
                context.camera.SetStereoProjectionMatrix(stereoscopicEye, matrix);
            }
            Vector2 jitter  = this.jitter;
            float   x       = jitter.x / (float)context.screenWidth;
            Vector2 jitter2 = this.jitter;

            this.jitter = new Vector2(x, jitter2.y / (float)context.screenHeight);
            camera.useJitteredProjectionMatrixForTransparentRendering = false;
        }
Esempio n. 30
0
	private void GetTanFovAndOffsetForStereoEye(Camera.StereoscopicEye eye, out float tanFovX, out float tanFovY, out float offsetX, out float offsetY)
	{
		var pt = _Camera.GetStereoProjectionMatrix(eye).transpose;

		var right = pt * new Vector4(-1, 0, 0, 1);
		var left = pt * new Vector4(1, 0, 0, 1);
		var up = pt * new Vector4(0, -1, 0, 1);
		var down = pt * new Vector4(0, 1, 0, 1);

		float rightTanFovX = right.z / right.x;
		float leftTanFovX = left.z / left.x;
		float upTanFovY = up.z / up.y;
		float downTanFovY = down.z / down.y;

		offsetX = -(rightTanFovX + leftTanFovX) / 2;
		offsetY = -(upTanFovY + downTanFovY) / 2;

		tanFovX = (rightTanFovX - leftTanFovX) / 2;
		tanFovY = (upTanFovY - downTanFovY) / 2;
	}