//	We have to use OnPreCull or OnPreRender as otherwise particlesystems will make unity crash
        //	void OnPreRender() {
        //	This is what is active in 1.08
        void OnPreCull()
        {
                        #if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                if (defaultBump == null)
                {
                    defaultBump = new Texture2D(1, 1, TextureFormat.RGBA32, false);
                    defaultBump.SetPixel(0, 0, new Color(0.5f, 0.5f, 1.0f, 0.5f).gamma);
                    defaultBump.Apply(false);
                }
                Shader.SetGlobalTexture(_LuxWater_NormalOverlayPID, defaultBump);
                Shader.SetGlobalTexture(_LuxWater_FoamOverlayPID, Texture2D.blackTexture);
                return;
            }
                        #endif

            cam = GetComponent <Camera>();

                        #if UNITY_EDITOR
            if (UnityEditor.SceneView.currentDrawingSceneView != null && UnityEditor.SceneView.currentDrawingSceneView.camera == cam)
            {
                //		Shader.DisableKeyword("USINGWATERPROJECTORS");
                return;
            }
                        #endif

            //	Check if we have to do anything
            var numFoamProjectors   = LuxWater_Projector.FoamProjectors.Count;
            var numNormalProjectors = LuxWater_Projector.NormalProjectors.Count;

            //	No registered projectors
            if (numFoamProjectors + numNormalProjectors == 0)
            {
                if (cb_Foam != null)
                {
                    cb_Foam.Clear();
                }
                if (cb_Normals != null)
                {
                    cb_Normals.Clear();
                }
                Shader.DisableKeyword("USINGWATERPROJECTORS");
                return;
            }
            else
            {
                Shader.EnableKeyword("USINGWATERPROJECTORS");
            }

            var projectionMatrix        = cam.projectionMatrix;
            var worldToCameraMatrix     = cam.worldToCameraMatrix;
            var worldToProjectionMatrix = projectionMatrix * worldToCameraMatrix;

            var camPixelWidth  = cam.pixelWidth;
            var camPixelHeight = cam.pixelHeight;

            //UnityEngine.Profiling.Profiler.BeginSample("Get Planes");
            GeomUtil.CalculateFrustumPlanes(frustumPlanes, worldToProjectionMatrix);
            //UnityEngine.Profiling.Profiler.EndSample();

            //	Foam Buffer
            var rtWidth  = Mathf.FloorToInt(camPixelWidth / (int)FoamBufferResolution);
            var rtHeight = Mathf.FloorToInt(camPixelHeight / (int)FoamBufferResolution);

            //	Check if buffer's rt has to be created/updated
            if (!ProjectedFoam)
            {
                ProjectedFoam = new RenderTexture(rtWidth, rtHeight, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
                //Shader.SetGlobalTexture(_LuxWater_FoamOverlayPID, ProjectedFoam);
            }
            else if (ProjectedFoam.width != rtWidth)
            {
                DestroyImmediate(ProjectedFoam);
                ProjectedFoam = new RenderTexture(rtWidth, rtHeight, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
                //	We have to reassign the texture (prevented projectors from being updated after pause)
                //Shader.SetGlobalTexture(_LuxWater_FoamOverlayPID, ProjectedFoam);
            }

            GL.PushMatrix();
            GL.modelview = worldToCameraMatrix;
            GL.LoadProjectionMatrix(projectionMatrix);

            cb_Foam.Clear();
            cb_Foam.SetRenderTarget(ProjectedFoam);
            cb_Foam.ClearRenderTarget(true, true, new Color(0, 0, 0, 0), 1.0f);
            //Shader.SetGlobalTexture(_CameraDepthTexturePID, Texture2D.whiteTexture);

            drawnFoamProjectors = 0;

            for (int i = 0; i < numFoamProjectors; i++)
            {
                //	Check renderer's bounds against frustum before calling DrawRenderer
                var currentProjector = LuxWater_Projector.FoamProjectors[i];
                tempBounds = currentProjector.m_Rend.bounds;
                if (GeometryUtility.TestPlanesAABB(frustumPlanes, tempBounds))
                {
                    cb_Foam.DrawRenderer(currentProjector.m_Rend, currentProjector.m_Mat);

                    drawnFoamProjectors++;
                }
            }
            Graphics.ExecuteCommandBuffer(cb_Foam);
            //	We might have multiple Cameras (split screen) - so we have to assign the Rendertexture each time.
            Shader.SetGlobalTexture(_LuxWater_FoamOverlayPID, ProjectedFoam);

            //	Normal Buffer
            rtWidth  = Mathf.FloorToInt(camPixelWidth / (int)NormalBufferResolution);
            rtHeight = Mathf.FloorToInt(camPixelHeight / (int)NormalBufferResolution);

            //	Check if buffer's rt has to be created/updated
            if (!ProjectedNormals)
            {
                ProjectedNormals = new RenderTexture(rtWidth, rtHeight, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
                //Shader.SetGlobalTexture(_LuxWater_NormalOverlayPID, ProjectedNormals);
            }
            else if (ProjectedNormals.width != rtWidth)
            {
                DestroyImmediate(ProjectedNormals);
                ProjectedNormals = new RenderTexture(rtWidth, rtHeight, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
                //	We have to reassign the texture (prevented projectors from being updated after pause)
                //Shader.SetGlobalTexture(_LuxWater_NormalOverlayPID, ProjectedNormals);
            }

            cb_Normals.Clear();
            cb_Normals.SetRenderTarget(ProjectedNormals);
            // Regular ARGB buffer
            // cb_Normals.ClearRenderTarget(true, true, new Color(0.5f,0.5f,1.0f,0.5f), 1.0f);
            // ARGBHalf buffer
            // cb_Normals.ClearRenderTarget(true, true, new Color(0.0f, 0.0f, 1.0f, 0.0f), 1.0f); // blue was 1.0 corrupting height!
            cb_Normals.ClearRenderTarget(true, true, new Color(0.0f, 0.0f, 0.0f, 0.0f), 1.0f);

            drawnNormalProjectors = 0;

            for (int i = 0; i < numNormalProjectors; i++)
            {
                //	Check renderer's bounds against frustum before calling DrawRenderer
                var currentProjector = LuxWater_Projector.NormalProjectors[i];
                tempBounds = currentProjector.m_Rend.bounds;
                if (GeometryUtility.TestPlanesAABB(frustumPlanes, tempBounds))
                {
                    cb_Normals.DrawRenderer(currentProjector.m_Rend, currentProjector.m_Mat);

                    drawnNormalProjectors++;
                }
            }
            Graphics.ExecuteCommandBuffer(cb_Normals);
            //	We might have multiple Cameras (split screen) - so we have to assign the Rendertexture each time.
            Shader.SetGlobalTexture(_LuxWater_NormalOverlayPID, ProjectedNormals);
            GL.PopMatrix();
        }
Esempio n. 2
0
        private void OnPreCull()
        {
            this.cam = (Camera)((Component)this).GetComponent <Camera>();
            int count1 = LuxWater_Projector.FoamProjectors.Count;
            int count2 = LuxWater_Projector.NormalProjectors.Count;

            if (count1 + count2 == 0)
            {
                if (LuxWater_ProjectorRenderer.cb_Foam != null)
                {
                    LuxWater_ProjectorRenderer.cb_Foam.Clear();
                }
                if (LuxWater_ProjectorRenderer.cb_Normals != null)
                {
                    LuxWater_ProjectorRenderer.cb_Normals.Clear();
                }
                Shader.DisableKeyword("USINGWATERPROJECTORS");
            }
            else
            {
                Shader.EnableKeyword("USINGWATERPROJECTORS");
                Matrix4x4 projectionMatrix     = this.cam.get_projectionMatrix();
                Matrix4x4 worldToCameraMatrix  = this.cam.get_worldToCameraMatrix();
                Matrix4x4 worldToProjectMatrix = Matrix4x4.op_Multiply(projectionMatrix, worldToCameraMatrix);
                int       pixelWidth           = this.cam.get_pixelWidth();
                int       pixelHeight          = this.cam.get_pixelHeight();
                GeomUtil.CalculateFrustumPlanes(this.frustumPlanes, worldToProjectMatrix);
                int num1 = Mathf.FloorToInt((float)(pixelWidth / (int)this.FoamBufferResolution));
                int num2 = Mathf.FloorToInt((float)(pixelHeight / (int)this.FoamBufferResolution));
                if (!Object.op_Implicit((Object)this.ProjectedFoam))
                {
                    this.ProjectedFoam = new RenderTexture(num1, num2, 0, (RenderTextureFormat)0, (RenderTextureReadWrite)1);
                }
                else if (((Texture)this.ProjectedFoam).get_width() != num1)
                {
                    Object.DestroyImmediate((Object)this.ProjectedFoam);
                    this.ProjectedFoam = new RenderTexture(num1, num2, 0, (RenderTextureFormat)0, (RenderTextureReadWrite)1);
                }
                GL.PushMatrix();
                GL.set_modelview(worldToCameraMatrix);
                GL.LoadProjectionMatrix(projectionMatrix);
                LuxWater_ProjectorRenderer.cb_Foam.Clear();
                LuxWater_ProjectorRenderer.cb_Foam.SetRenderTarget(RenderTargetIdentifier.op_Implicit((Texture)this.ProjectedFoam));
                LuxWater_ProjectorRenderer.cb_Foam.ClearRenderTarget(true, true, new Color(0.0f, 0.0f, 0.0f, 0.0f), 1f);
                this.drawnFoamProjectors = 0;
                for (int index = 0; index < count1; ++index)
                {
                    LuxWater_Projector foamProjector = LuxWater_Projector.FoamProjectors[index];
                    this.tempBounds = foamProjector.m_Rend.get_bounds();
                    if (GeometryUtility.TestPlanesAABB(this.frustumPlanes, this.tempBounds))
                    {
                        LuxWater_ProjectorRenderer.cb_Foam.DrawRenderer(foamProjector.m_Rend, foamProjector.m_Mat);
                        ++this.drawnFoamProjectors;
                    }
                }
                Graphics.ExecuteCommandBuffer(LuxWater_ProjectorRenderer.cb_Foam);
                Shader.SetGlobalTexture(this._LuxWater_FoamOverlayPID, (Texture)this.ProjectedFoam);
                int num3 = Mathf.FloorToInt((float)(pixelWidth / (int)this.NormalBufferResolution));
                int num4 = Mathf.FloorToInt((float)(pixelHeight / (int)this.NormalBufferResolution));
                if (!Object.op_Implicit((Object)this.ProjectedNormals))
                {
                    this.ProjectedNormals = new RenderTexture(num3, num4, 0, (RenderTextureFormat)2, (RenderTextureReadWrite)1);
                }
                else if (((Texture)this.ProjectedNormals).get_width() != num3)
                {
                    Object.DestroyImmediate((Object)this.ProjectedNormals);
                    this.ProjectedNormals = new RenderTexture(num3, num4, 0, (RenderTextureFormat)2, (RenderTextureReadWrite)1);
                }
                LuxWater_ProjectorRenderer.cb_Normals.Clear();
                LuxWater_ProjectorRenderer.cb_Normals.SetRenderTarget(RenderTargetIdentifier.op_Implicit((Texture)this.ProjectedNormals));
                LuxWater_ProjectorRenderer.cb_Normals.ClearRenderTarget(true, true, new Color(0.0f, 0.0f, 0.0f, 0.0f), 1f);
                this.drawnNormalProjectors = 0;
                for (int index = 0; index < count2; ++index)
                {
                    LuxWater_Projector normalProjector = LuxWater_Projector.NormalProjectors[index];
                    this.tempBounds = normalProjector.m_Rend.get_bounds();
                    if (GeometryUtility.TestPlanesAABB(this.frustumPlanes, this.tempBounds))
                    {
                        LuxWater_ProjectorRenderer.cb_Normals.DrawRenderer(normalProjector.m_Rend, normalProjector.m_Mat);
                        ++this.drawnNormalProjectors;
                    }
                }
                Graphics.ExecuteCommandBuffer(LuxWater_ProjectorRenderer.cb_Normals);
                Shader.SetGlobalTexture(this._LuxWater_NormalOverlayPID, (Texture)this.ProjectedNormals);
                GL.PopMatrix();
            }
        }
        private void OnPreRender()
        {
            this.cam = base.GetComponent <Camera>();
            int count  = LuxWater_Projector.FoamProjectors.Count;
            int count2 = LuxWater_Projector.NormalProjectors.Count;

            if (count + count2 == 0)
            {
                if (LuxWater_ProjectorRenderer.cb_Foam != null)
                {
                    LuxWater_ProjectorRenderer.cb_Foam.Clear();
                }
                if (LuxWater_ProjectorRenderer.cb_Normals != null)
                {
                    LuxWater_ProjectorRenderer.cb_Normals.Clear();
                }
                Shader.DisableKeyword("USINGWATERPROJECTORS");
                return;
            }
            Shader.EnableKeyword("USINGWATERPROJECTORS");
            Matrix4x4 projectionMatrix     = this.cam.projectionMatrix;
            Matrix4x4 worldToCameraMatrix  = this.cam.worldToCameraMatrix;
            Matrix4x4 worldToProjectMatrix = projectionMatrix * worldToCameraMatrix;
            int       pixelWidth           = this.cam.pixelWidth;
            int       pixelHeight          = this.cam.pixelHeight;

            GeomUtil.CalculateFrustumPlanes(this.frustumPlanes, worldToProjectMatrix);
            int num    = Mathf.FloorToInt((float)(pixelWidth / (int)this.FoamBufferResolution));
            int height = Mathf.FloorToInt((float)(pixelHeight / (int)this.FoamBufferResolution));

            if (!this.ProjectedFoam)
            {
                this.ProjectedFoam = new RenderTexture(num, height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
                Shader.SetGlobalTexture(this._LuxWater_FoamOverlayPID, this.ProjectedFoam);
            }
            else if (this.ProjectedFoam.width != num)
            {
                this.ProjectedFoam = new RenderTexture(num, height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
            }
            GL.PushMatrix();
            GL.modelview = worldToCameraMatrix;
            GL.LoadProjectionMatrix(projectionMatrix);
            LuxWater_ProjectorRenderer.cb_Foam.Clear();
            LuxWater_ProjectorRenderer.cb_Foam.SetRenderTarget(this.ProjectedFoam);
            LuxWater_ProjectorRenderer.cb_Foam.ClearRenderTarget(true, true, new Color(0f, 0f, 0f, 0f), 1f);
            this.drawnFoamProjectors = 0;
            for (int i = 0; i < count; i++)
            {
                LuxWater_Projector luxWater_Projector = LuxWater_Projector.FoamProjectors[i];
                this.tempBounds = luxWater_Projector.m_Rend.bounds;
                if (GeometryUtility.TestPlanesAABB(this.frustumPlanes, this.tempBounds))
                {
                    LuxWater_ProjectorRenderer.cb_Foam.DrawRenderer(luxWater_Projector.m_Rend, luxWater_Projector.m_Mat);
                    this.drawnFoamProjectors++;
                }
            }
            Graphics.ExecuteCommandBuffer(LuxWater_ProjectorRenderer.cb_Foam);
            num    = Mathf.FloorToInt((float)(pixelWidth / (int)this.NormalBufferResolution));
            height = Mathf.FloorToInt((float)(pixelHeight / (int)this.NormalBufferResolution));
            if (!this.ProjectedNormals)
            {
                this.ProjectedNormals = new RenderTexture(num, height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
                Shader.SetGlobalTexture(this._LuxWater_NormalOverlayPID, this.ProjectedNormals);
            }
            else if (this.ProjectedNormals.width != num)
            {
                this.ProjectedNormals = new RenderTexture(num, height, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
            }
            LuxWater_ProjectorRenderer.cb_Normals.Clear();
            LuxWater_ProjectorRenderer.cb_Normals.SetRenderTarget(this.ProjectedNormals);
            LuxWater_ProjectorRenderer.cb_Normals.ClearRenderTarget(true, true, new Color(0f, 0f, 1f, 0f), 1f);
            this.drawnNormalProjectors = 0;
            for (int j = 0; j < count2; j++)
            {
                LuxWater_Projector luxWater_Projector2 = LuxWater_Projector.NormalProjectors[j];
                this.tempBounds = luxWater_Projector2.m_Rend.bounds;
                if (GeometryUtility.TestPlanesAABB(this.frustumPlanes, this.tempBounds))
                {
                    LuxWater_ProjectorRenderer.cb_Normals.DrawRenderer(luxWater_Projector2.m_Rend, luxWater_Projector2.m_Mat);
                    this.drawnNormalProjectors++;
                }
            }
            Graphics.ExecuteCommandBuffer(LuxWater_ProjectorRenderer.cb_Normals);
            GL.PopMatrix();
        }