private void OnWillRenderObject()
        {
            if (!enabled || targetCamera == null || reflectionCamera == null)
            {
                return;
            }

            if (isRendering)
            {
                return;
            }
            isRendering          = true;
            GL.invertCulling     = true;
            RenderTexture.active = renderTexture;

            SetCamera(targetCamera, reflectionCamera);
            reflectionCamera.cullingMask   = reflectionLayers;
            reflectionCamera.targetTexture = renderTexture;

            Vector3 position        = transform.position;
            Vector3 normal          = transform.TransformDirection(reflectionNormal);
            float   offset          = -Vector3.Dot(normal, position) - clipPlaneOffset;
            Vector4 reflectionPlane = new Vector4(normal.x, normal.y, normal.z, offset);

            Matrix4x4 reflectionMatrix = Matrix4x4.zero;

            EZUtility.GetReflectionMatrix(reflectionPlane, ref reflectionMatrix);

            reflectionCamera.worldToCameraMatrix = targetCamera.worldToCameraMatrix * reflectionMatrix;
            Vector4 clipPlane = GetCameraSpacePlane(reflectionCamera, position, normal);

            reflectionCamera.projectionMatrix = targetCamera.CalculateObliqueMatrix(clipPlane);

            reflectionCamera.transform.position = reflectionMatrix.MultiplyPoint(targetCamera.transform.position);
            Vector3 forward = reflectionMatrix.MultiplyVector(targetCamera.transform.forward);
            Vector3 up      = reflectionMatrix.MultiplyVector(targetCamera.transform.up);

            reflectionCamera.transform.rotation = Quaternion.LookRotation(forward, up);
            reflectionCamera.Render();

            RenderTexture.active = null;
            GL.invertCulling     = false;
            isRendering          = false;
        }
        private void OnDrawGizmosSelected()
        {
            if (!enabled || targetCamera == null || reflectionCamera == null)
            {
                return;
            }

            if (targetCamera != null)
            {
                Gizmos.color = Color.grey;
                EZUtility.DrawGizmosCameraFrustum(targetCamera);
            }

            if (reflectionCamera != null)
            {
                Gizmos.color = Color.green;
                EZUtility.DrawGizmosCameraFrustum(reflectionCamera);
            }
        }
Beispiel #3
0
        private void OnWillRenderObject()
        {
            if (!enabled)
            {
                return;
            }

            Camera targetCamera = Camera.current;

            if (targetCamera == null)
            {
                return;
            }
            if (isRendering)
            {
                return;
            }
            isRendering = true;

            Vector3 position = transform.position;
            Vector3 normal   = transform.TransformDirection(normalDirection);

            if (refractionOn && refractionTexture != null)
            {
                material.SetTexture("_ReflectionTex", m_ReflectionTexture);
                Camera refractionCamera = GetRenderCamera(m_RefractionCameras, targetCamera, TAG_REFRACTION);
                SetCamera(targetCamera, refractionCamera);
                refractionCamera.cullingMask   = ~(1 << 4) & refractionLayers;
                refractionCamera.targetTexture = refractionTexture;

                refractionCamera.worldToCameraMatrix = targetCamera.worldToCameraMatrix;
                Vector4 clipPlane = GetCameraSpacePlane(refractionCamera, position, normal, -1);
                refractionCamera.projectionMatrix = targetCamera.CalculateObliqueMatrix(clipPlane);
                refractionCamera.cullingMatrix    = targetCamera.projectionMatrix * targetCamera.worldToCameraMatrix;

                refractionCamera.transform.position = targetCamera.transform.position;
                refractionCamera.transform.rotation = targetCamera.transform.rotation;
                refractionCamera.Render();
            }
            if (reflectionOn && reflectionTexture != null)
            {
                material.SetTexture("_RefractionTex", m_RefractionTexture);
                Camera reflectionCamera = GetRenderCamera(m_ReflectionCameras, targetCamera, TAG_REFLECTION);
                SetCamera(targetCamera, reflectionCamera);
                reflectionCamera.cullingMask   = ~(1 << 4) & reflectionLayers;
                reflectionCamera.targetTexture = reflectionTexture;

                float   offset          = -Vector3.Dot(normal, position) - clipPlaneOffset;
                Vector4 reflectionPlane = new Vector4(normal.x, normal.y, normal.z, offset);

                Matrix4x4 reflectionMatrix = Matrix4x4.zero;
                EZUtility.GetReflectionMatrix(reflectionPlane, ref reflectionMatrix);

                reflectionCamera.worldToCameraMatrix = targetCamera.worldToCameraMatrix * reflectionMatrix;
                Vector4 clipPlane = GetCameraSpacePlane(reflectionCamera, position, normal);
                reflectionCamera.projectionMatrix = targetCamera.CalculateObliqueMatrix(clipPlane);
                reflectionCamera.cullingMatrix    = targetCamera.projectionMatrix * targetCamera.worldToCameraMatrix;

                GL.invertCulling = true;
                reflectionCamera.transform.position = reflectionMatrix.MultiplyPoint(targetCamera.transform.position);
                Vector3 forward = reflectionMatrix.MultiplyVector(targetCamera.transform.forward);
                Vector3 up      = reflectionMatrix.MultiplyVector(targetCamera.transform.up);
                reflectionCamera.transform.rotation = Quaternion.LookRotation(forward, up);
                reflectionCamera.Render();
                reflectionCamera.transform.position = targetCamera.transform.position;
                GL.invertCulling = false;
            }

            isRendering = false;
        }