Beispiel #1
0
        /// <summary>
        /// Create the reflection camera for this camera.
        /// </summary>
        void CreateReflectionCameraFor(Camera cam, ReflectionData data)
        {
            if (data.cam == null)
            {
                GameObject go = new GameObject("Ceto Reflection Camera: " + cam.name);
                //go.AddComponent<IgnoreOceanEvents>();
                go.hideFlags = HideFlags.HideAndDontSave;

                DisableFog disableFog = go.AddComponent <DisableFog>();
                disableFog.enabled = !fogInReflection;

                data.cam = go.AddComponent <Camera>();

                data.cam.depthTextureMode    = DepthTextureMode.None;
                data.cam.renderingPath       = RenderingPath.Forward;
                data.cam.enabled             = false;
                data.cam.allowHDR            = cam.allowHDR;
                data.cam.targetTexture       = null;
                data.cam.useOcclusionCulling = false;

                //Copy the cull distances used by the camera.
                //Since the reflection camera uses a oblique projection matrix
                //the layer culling must be spherical or the cull wont match
                //that used by the camera. There will still be some mismatch
                //between the reflection culling and camera culling if the
                //camera does not use spherical culling.
                if (copyCullDistances)
                {
                    data.cam.layerCullDistances = cam.layerCullDistances;
                    data.cam.layerCullSpherical = true;
                }
            }

            data.cam.fieldOfView      = cam.fieldOfView;
            data.cam.nearClipPlane    = cam.nearClipPlane;
            data.cam.farClipPlane     = cam.farClipPlane;
            data.cam.orthographic     = cam.orthographic;
            data.cam.aspect           = cam.aspect;
            data.cam.orthographicSize = cam.orthographicSize;
            data.cam.rect             = new Rect(0, 0, 1, 1);
            data.cam.backgroundColor  = m_ocean.defaultSkyColor;
            data.cam.clearFlags       = (skyboxInReflection) ? CameraClearFlags.Skybox : CameraClearFlags.SolidColor;
        }
Beispiel #2
0
        /// <summary>
        /// Render the reflections for this objects position
        /// and the current camera.
        /// </summary>
        public void RenderReflection(GameObject go)
        {
            try
            {
                if (!enabled)
                {
                    return;
                }

                Camera cam = Camera.current;
                if (cam == null)
                {
                    return;
                }

                CameraData data = m_ocean.FindCameraData(cam);

                //Create the data needed if not already created.
                if (data.reflection == null)
                {
                    data.reflection = new ReflectionData();
                }

                if (data.reflection.IsViewUpdated(cam))
                {
                    return;
                }

                //If this camera has disable the reflection turn it off in the shader and return.
                if (GetDisableReflections(data.settings))
                {
                    Shader.DisableKeyword("CETO_REFLECTION_ON");
                    data.reflection.SetViewAsUpdated(cam);
                    return;
                }
                else
                {
                    Shader.EnableKeyword("CETO_REFLECTION_ON");
                }

                RenderTexture reflections0 = null;
                RenderTexture reflections1 = null;

                if (data.reflection.cam != null)
                {
                    DisableFog disableFog = data.reflection.cam.GetComponent <DisableFog>();
                    if (disableFog != null)
                    {
                        disableFog.enabled = !fogInReflection;
                    }
                }

                if (RenderReflectionCustom != null)
                {
                    //If using a custom method
                    //Destroy the camera if already created as its no longer needed.
                    if (data.reflection.cam != null)
                    {
                        data.reflection.DestroyTargets();
                    }

                    CreateRenderTarget(data.reflection, cam.pixelWidth, cam.pixelHeight, cam.allowHDR, false, data.settings);

                    //Create the dummy object if null
                    if (m_dummy == null)
                    {
                        m_dummy           = new GameObject("Ceto Reflection Dummy Gameobject");
                        m_dummy.hideFlags = HideFlags.HideAndDontSave;
                    }

                    //Set the position of the reflection plane.
                    m_dummy.transform.position = new Vector3(0.0f, m_ocean.level, 0.0f);
                    //Copy returned texture in target.
                    Graphics.Blit(RenderReflectionCustom(m_dummy), data.reflection.target0);
                    reflections0 = data.reflection.target0;
                    reflections1 = null; //Custom stero not supported.
                }
                else
                {
                    //Else use normal method.
                    CreateReflectionCameraFor(cam, data.reflection);
                    CreateRenderTarget(data.reflection, cam.pixelWidth, cam.pixelHeight, cam.allowHDR, cam.stereoEnabled, data.settings);

                    if (cam.stereoEnabled)
                    {
                        RenderSteroReflection(data.reflection, cam, data.settings);
                        reflections0 = data.reflection.target0;
                        reflections1 = data.reflection.target1;
                    }
                    else
                    {
                        Shader.DisableKeyword("CETO_STERO_CAMERA");
                        RenderReflection(data.reflection.cam, data.reflection.target0, cam.transform.position, cam.transform.rotation, cam.projectionMatrix, data.settings);
                        reflections0 = data.reflection.target0;
                        reflections1 = null;
                    }
                }

                //The reflections texture should now contain the rendered
                //reflections for the current cameras view.
                if (reflections0 != null)
                {
                    m_imageBlur.BlurIterations = blurIterations;
                    m_imageBlur.BlurMode       = blurMode;
                    m_imageBlur.BlurSpread     = blurSpread;
                    m_imageBlur.Blur(reflections0);

                    Shader.SetGlobalTexture(Ocean.REFLECTION_TEXTURE_NAME0, reflections0);
                }

                if (reflections1 != null)
                {
                    m_imageBlur.BlurIterations = blurIterations;
                    m_imageBlur.BlurMode       = blurMode;
                    m_imageBlur.BlurSpread     = blurSpread;
                    m_imageBlur.Blur(reflections1);

                    Shader.SetGlobalTexture(Ocean.REFLECTION_TEXTURE_NAME1, reflections1);
                }

                data.reflection.SetViewAsUpdated(cam);
            }
            catch (Exception e)
            {
                Ocean.LogError(e.ToString());
                WasError = true;
                enabled  = false;
            }
        }
Beispiel #3
0
 public override void RenderReflection(GameObject go)
 {
     try
     {
         if (base.enabled)
         {
             Camera current = Camera.current;
             if (!(current == null))
             {
                 CameraData cameraData = this.m_ocean.FindCameraData(current);
                 if (cameraData.reflection == null)
                 {
                     cameraData.reflection = new ReflectionData();
                 }
                 if (!cameraData.reflection.updated)
                 {
                     if (this.GetDisableReflections(cameraData.settings))
                     {
                         Shader.DisableKeyword("CETO_REFLECTION_ON");
                         cameraData.reflection.updated = true;
                     }
                     else
                     {
                         Shader.EnableKeyword("CETO_REFLECTION_ON");
                         if (cameraData.reflection.cam != null)
                         {
                             DisableFog component = cameraData.reflection.cam.GetComponent <DisableFog>();
                             if (component != null)
                             {
                                 component.enabled = !this.fogInReflection;
                             }
                         }
                         RenderTexture renderTexture;
                         if (this.RenderReflectionCustom != null)
                         {
                             if (cameraData.reflection.cam != null)
                             {
                                 RTUtility.ReleaseAndDestroy(cameraData.reflection.cam.targetTexture);
                                 cameraData.reflection.cam.targetTexture = null;
                                 UnityEngine.Object.Destroy(cameraData.reflection.cam.gameObject);
                                 UnityEngine.Object.Destroy(cameraData.reflection.cam);
                                 cameraData.reflection.cam = null;
                             }
                             this.CreateRenderTarget(cameraData.reflection, current.name, current.pixelWidth, current.pixelHeight, current.hdr, cameraData.settings);
                             if (this.m_dummy == null)
                             {
                                 this.m_dummy           = new GameObject("Ceto Reflection Dummy Gameobject");
                                 this.m_dummy.hideFlags = HideFlags.HideAndDontSave;
                             }
                             this.m_dummy.transform.position = new Vector3(0f, this.m_ocean.level, 0f);
                             renderTexture = this.RenderReflectionCustom(this.m_dummy);
                         }
                         else
                         {
                             this.CreateReflectionCameraFor(current, cameraData.reflection);
                             this.CreateRenderTarget(cameraData.reflection, current.name, current.pixelWidth, current.pixelHeight, current.hdr, cameraData.settings);
                             if (current.stereoEnabled)
                             {
                             }
                             NotifyOnEvent.Disable = true;
                             this.RenderReflectionFor(current, cameraData.reflection.cam, cameraData.settings);
                             NotifyOnEvent.Disable = false;
                             renderTexture         = cameraData.reflection.cam.targetTexture;
                         }
                         if (renderTexture != null)
                         {
                             if (this.blitmaterial == null)
                             {
                                 this.blitmaterial = new Material(Shader.Find("Hidden/TheForestBlitCopyFullscreen"));
                             }
                             Graphics.Blit(renderTexture, cameraData.reflection.tex, this.blitmaterial);
                             this.m_imageBlur.BlurIterations = this.blurIterations;
                             this.m_imageBlur.BlurMode       = this.blurMode;
                             this.m_imageBlur.BlurSpread     = this.blurSpread;
                             this.m_imageBlur.Blur(cameraData.reflection.tex);
                             Shader.SetGlobalTexture(Ocean.REFLECTION_TEXTURE_NAME, cameraData.reflection.tex);
                         }
                         cameraData.reflection.updated = true;
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Ocean.LogError(ex.ToString());
         base.WasError = true;
         base.enabled  = false;
     }
 }
        /// <summary>
        /// Render the reflections for this objects position
        /// and the current camera.
        /// </summary>
        public override void RenderReflection(GameObject go)
        {
            try
            {
                if (!enabled)
                {
                    return;
                }

                Camera cam = Camera.current;
                if (cam == null)
                {
                    return;
                }

                CameraData data = m_ocean.FindCameraData(cam);

                //Create the data needed if not already created.
                if (data.reflection == null)
                {
                    data.reflection = new ReflectionData();
                }

                if (data.reflection.updated)
                {
                    return;
                }

                //If this camera has disable the reflection turn it off in the shader and return.
                if (GetDisableReflections(data.settings))
                {
                    Shader.DisableKeyword("CETO_REFLECTION_ON");
                    data.reflection.updated = true;
                    return;
                }
                else
                {
                    Shader.EnableKeyword("CETO_REFLECTION_ON");
                }

                RenderTexture reflections = null;

                if (data.reflection.cam != null)
                {
                    DisableFog disableFog = data.reflection.cam.GetComponent <DisableFog>();
                    if (disableFog != null)
                    {
                        disableFog.enabled = !fogInReflection;
                    }
                }

                if (RenderReflectionCustom != null)
                {
                    //If using a custom method

                    //Destroy the camera if already created as its no longer needed.
                    if (data.reflection.cam != null)
                    {
                        RTUtility.ReleaseAndDestroy(data.reflection.cam.targetTexture);
                        data.reflection.cam.targetTexture = null;

                        Destroy(data.reflection.cam.gameObject);
                        Destroy(data.reflection.cam);
                        data.reflection.cam = null;
                    }

                    CreateRenderTarget(data.reflection, cam.name, cam.pixelWidth, cam.pixelHeight, cam.allowHDR, data.settings);

                    //Create the dummy object if null
                    if (m_dummy == null)
                    {
                        m_dummy           = new GameObject("Ceto Reflection Dummy Gameobject");
                        m_dummy.hideFlags = HideFlags.HideAndDontSave;
                    }

                    //Set the position of the reflection plane.
                    m_dummy.transform.position = new Vector3(0.0f, m_ocean.level, 0.0f);
                    reflections = RenderReflectionCustom(m_dummy);
                }
                else
                {
                    //Else use normal method.

                    CreateReflectionCameraFor(cam, data.reflection);
                    CreateRenderTarget(data.reflection, cam.name, cam.pixelWidth, cam.pixelHeight, cam.allowHDR, data.settings);

                    NotifyOnEvent.Disable = true;
                    RenderReflectionFor(cam, data.reflection.cam, data.settings);
                    NotifyOnEvent.Disable = false;

                    reflections = data.reflection.cam.targetTexture;
                }

                //The reflections texture should now contain the rendered
                //reflections for the current cameras view.
                if (reflections != null)
                {
                    //Blit into another texture to take a copy.
                    Graphics.Blit(reflections, data.reflection.tex);

                    m_imageBlur.BlurIterations = blurIterations;
                    m_imageBlur.BlurMode       = blurMode;
                    m_imageBlur.BlurSpread     = blurSpread;
                    m_imageBlur.Blur(data.reflection.tex);

                    Shader.SetGlobalTexture(Ocean.REFLECTION_TEXTURE_NAME, data.reflection.tex);
                }

                data.reflection.updated = true;
            }
            catch (Exception e)
            {
                Ocean.LogError(e.ToString());
                WasError = true;
                enabled  = false;
            }
        }