Esempio n. 1
0
        //
        protected virtual void OnPreRender()
        {
            UpdateHighlightingBuffer();

            int aa = GetAA();

            bool depthAvailable = (aa == 1);

            // In case MSAA is enabled in forward/vertex lit rendeirng paths - depth buffer is not available
            if (aa > 1 && (cam.actualRenderingPath == RenderingPath.Forward || cam.actualRenderingPath == RenderingPath.VertexLit))
            {
                depthAvailable = false;
            }

            // Check if framebuffer depth data availability has changed
            if (isDepthAvailable != depthAvailable)
            {
                isDepthAvailable = depthAvailable;
                // Update ZWrite value for all highlighting shaders correspondingly (isDepthAvailable ? ZWrite Off : ZWrite On)
                Highlighter.SetZWrite(isDepthAvailable ? 0f : 1f);
                if (isDepthAvailable)
                {
                    Debug.LogWarning("HighlightingSystem : Framebuffer depth data is available back again and will be used to occlude highlighting. Highlighting occluders disabled.");
                }
                else
                {
                    Debug.LogWarning("HighlightingSystem : Framebuffer depth data is not available and can't be used to occlude highlighting. Highlighting occluders enabled.");
                }
                isDirty = true;
            }

            // Set global depth offset properties for highlighting shaders to the values which has this HighlightingBase component
            Highlighter.SetOffsetFactor(offsetFactor);
            Highlighter.SetOffsetUnits(offsetUnits);

            isDirty |= HighlighterManager.isDirty;
            isDirty |= HighlightersChanged();

            if (isDirty)
            {
                RebuildCommandBuffer();
                isDirty = false;
            }
        }
Esempio n. 2
0
        protected virtual void OnPreRender()
        {
            int aa = GetAA();

            bool depthAvailable = (aa == 1);

            if (aa > 1 && (cam.actualRenderingPath == RenderingPath.Forward || cam.actualRenderingPath == RenderingPath.VertexLit))
            {
                depthAvailable = false;
            }

            if (isDepthAvailable != depthAvailable)
            {
                isDepthAvailable = depthAvailable;
                Highlighter.SetZWrite(isDepthAvailable ? 0f : 1f);
                if (isDepthAvailable)
                {
//					Debug.LogWarning("HighlightingSystem : Framebuffer depth data is available back again and will be used to occlude highlighting. Highlighting occluders disabled.");
                }
                else
                {
//					Debug.LogWarning("HighlightingSystem : Framebuffer depth data is not available and can't be used to occlude highlighting. Highlighting occluders enabled.");
                }
                isDirty = true;
            }

            Highlighter.SetOffsetFactor(offsetFactor);
            Highlighter.SetOffsetUnits(offsetUnits);

            isDirty |= HighlighterManager.isDirty;
            isDirty |= HighlightersChanged();

            if (isDirty)
            {
                RebuildCommandBuffer();
                isDirty = false;
            }
        }
Esempio n. 3
0
        //
        protected virtual void OnPreRender()
        {
            bool updateHighlightingBuffer = false;
            int  aa = GetAA();

            bool depthAvailable = (aa == 1);

            if (cam.actualRenderingPath == RenderingPath.Forward || cam.actualRenderingPath == RenderingPath.VertexLit)
            {
                // In case MSAA is enabled in forward/vertex lit rendeirng paths - depth buffer is not available
                if (aa > 1)
                {
                    depthAvailable = false;
                }

                // In case camera clearFlags is set to 'Depth only' or 'Don't clear' in forward/vertex lit rendeirng paths - depth buffer is not available
                if (cam.clearFlags == CameraClearFlags.Depth || cam.clearFlags == CameraClearFlags.Nothing)
                {
                    depthAvailable = false;
                }
            }

            // Check if framebuffer depth data availability has changed
            if (isDepthAvailable != depthAvailable)
            {
                updateHighlightingBuffer = true;
                isDepthAvailable         = depthAvailable;
                // Update ZWrite value for all highlighting shaders correspondingly (isDepthAvailable ? ZWrite Off : ZWrite On)
                Highlighter.SetZWrite(isDepthAvailable ? 0 : 1);
                if (isDepthAvailable)
                {
                    Debug.LogWarning("HighlightingSystem : Framebuffer depth data is available back again. Depth occlusion enabled, highlighting occluders disabled. (This message is harmless)");
                }
                else
                {
                    Debug.LogWarning("HighlightingSystem : Framebuffer depth data is not available. Depth occlusion disabled, highlighting occluders enabled. (This message is harmless)");
                }
            }

            updateHighlightingBuffer |= (highlightingBuffer == null || cam.pixelWidth != cachedWidth || cam.pixelHeight != cachedHeight || aa != cachedAA);

            if (updateHighlightingBuffer)
            {
                if (highlightingBuffer != null && highlightingBuffer.IsCreated())
                {
                    highlightingBuffer.Release();
                }

                cachedWidth  = cam.pixelWidth;
                cachedHeight = cam.pixelHeight;
                cachedAA     = aa;

                highlightingBuffer = new RenderTexture(cachedWidth, cachedHeight, isDepthAvailable ? 0 : 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
                highlightingBuffer.antiAliasing = cachedAA;
                highlightingBuffer.filterMode   = FilterMode.Point;
                highlightingBuffer.useMipMap    = false;
                highlightingBuffer.wrapMode     = TextureWrapMode.Clamp;
                if (!highlightingBuffer.Create())
                {
                    Debug.LogError("HighlightingSystem : UpdateHighlightingBuffer() : Failed to create highlightingBuffer RenderTexture!");
                }

                highlightingBufferID = new RenderTargetIdentifier(highlightingBuffer);
                cutMaterial.SetTexture(ShaderPropertyID._HighlightingBuffer, highlightingBuffer);
                compMaterial.SetTexture(ShaderPropertyID._HighlightingBuffer, highlightingBuffer);

                Vector4 v = new Vector4(1f / (float)highlightingBuffer.width, 1f / (float)highlightingBuffer.height, 0f, 0f);
                cutMaterial.SetVector(ShaderPropertyID._HighlightingBufferTexelSize, v);
            }

            // Set global depth offset properties for highlighting shaders to the values which has this HighlightingBase component
            Highlighter.SetOffsetFactor(offsetFactor);
            Highlighter.SetOffsetUnits(offsetUnits);

            RebuildCommandBuffer();
        }
Esempio n. 4
0
        /// <summary>
        /// Render highlighting to the highlightingBuffer using frameBuffer.depthBuffer.
        /// </summary>
        /// <param name="frameBuffer">Frame buffer RenderTexture, depthBuffer of which will be used to occlude highlighting.</param>
        public void RenderHighlighting(RenderTexture frameBuffer)
        {
            // Release highlightingBuffer if it wasn't released already
            if (highlightingBuffer != null)
            {
                RenderTexture.ReleaseTemporary(highlightingBuffer);
                highlightingBuffer = null;
            }

            if (!isSupported || !enabled || !go.activeInHierarchy)
            {
                return;
            }

            int aa = QualitySettings.antiAliasing;

            if (aa == 0)
            {
                aa = 1;
            }

            bool depthAvailable = true;

            // Check if frameBuffer.depthBuffer is not available, contains garbage (when MSAA is enabled) or doesn't have stencil bits (when depth is 16 or 0)
            if (frameBuffer == null || frameBuffer.depth < 24)
            {
                depthAvailable = false;
            }

            // Reset aa value to 1 in case mainCam is in DeferredLighting Rendering Path
            if (refCam.actualRenderingPath == RenderingPath.DeferredLighting)
            {
                aa = 1;
            }
            // In case MSAA is enabled in forward/vertex lit rendeirng paths - depth buffer contains garbage
            else if (aa > 1)
            {
                depthAvailable = false;
            }

            // Check if framebuffer depth data availability has changed
            if (isDepthAvailable != depthAvailable)
            {
                isDepthAvailable = depthAvailable;
                // Update ZWrite value for all highlighting shaders correspondingly (isDepthAvailable ? ZWrite Off : ZWrite On)
                Highlighter.SetZWrite(isDepthAvailable ? 0f : 1f);
                if (isDepthAvailable)
                {
                    Debug.LogWarning("HighlightingSystem : Framebuffer depth data is available back again and will be used to occlude highlighting. Highlighting occluders disabled.");
                }
                else
                {
                    Debug.LogWarning("HighlightingSystem : Framebuffer depth data is not available and can't be used to occlude highlighting. Highlighting occluders enabled.");
                }
            }

            // Set global depth offset properties for highlighting shaders to the values which has this HighlightingBase component
            Highlighter.SetOffsetFactor(offsetFactor);
            Highlighter.SetOffsetUnits(offsetUnits);

            // Set this component as currently active HighlightingBase before enabling Highlighters
            current = this;

            // Turn on highlighting shaders on all highlighter components
            int count = 0;

            for (int i = 0; i < highlighters.Count; i++)
            {
                if (highlighters[i].Highlight())
                {
                    count++;
                }
            }

            // Do nothing in case no Highlighters is currently visible
            if (count == 0)
            {
                current = null;
                return;
            }

            // If frameBuffer.depthBuffer is not available
            int w     = Screen.width;
            int h     = Screen.height;
            int depth = 24;                             // because stencil will be rendered to the highlightingBuffer.depthBuffer

            // If frameBuffer.depthBuffer is available
            if (isDepthAvailable)
            {
                w     = frameBuffer.width;
                h     = frameBuffer.height;
                depth = 0;                                      // because stencil will be rendered to frameBuffer.depthBuffer
            }

            // Setup highlightingBuffer RenderTexture
            highlightingBuffer = RenderTexture.GetTemporary(w, h, depth, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default, aa);
            if (!highlightingBuffer.IsCreated())
            {
                highlightingBuffer.filterMode = FilterMode.Point;
                highlightingBuffer.useMipMap  = false;
                highlightingBuffer.wrapMode   = TextureWrapMode.Clamp;
            }

            // Clear highlightingBuffer colorBuffer and clear depthBuffer only in case frameBuffer depth data is not available
            RenderTexture.active = highlightingBuffer;
            GL.Clear((isDepthAvailable ? false : true), true, Color.clear);

            // Use depth data from frameBuffer in case it is available. Use highlightingBuffer.depthBuffer otherwise
            RenderBuffer depthBuffer = isDepthAvailable ? frameBuffer.depthBuffer : highlightingBuffer.depthBuffer;

            if (!shaderCameraGO)
            {
                shaderCameraGO           = new GameObject("HighlightingCamera");
                shaderCameraGO.hideFlags = HideFlags.HideAndDontSave;
                shaderCamera             = shaderCameraGO.AddComponent <Camera>();
                shaderCamera.enabled     = false;
            }

            shaderCamera.CopyFrom(refCam);
            //shaderCamera.projectionMatrix = mainCam.projectionMatrix;		// Uncomment this line if you have problems using Highlighting System with custom projection matrix on your camera
            shaderCamera.cullingMask         = layerMask;
            shaderCamera.rect                = new Rect(0f, 0f, 1f, 1f);
            shaderCamera.renderingPath       = RenderingPath.Forward;
            shaderCamera.depthTextureMode    = DepthTextureMode.None;
            shaderCamera.allowHDR            = false;
            shaderCamera.useOcclusionCulling = false;
            shaderCamera.backgroundColor     = new Color(0, 0, 0, 0);
            shaderCamera.clearFlags          = CameraClearFlags.Nothing;
            shaderCamera.SetTargetBuffers(highlightingBuffer.colorBuffer, depthBuffer);

            // Get rid of "Tiled GPU Perf warning" if we're not in debug mode
                        #if !DEBUG_ENABLED
            frameBuffer.MarkRestoreExpected();
                        #endif
            shaderCamera.Render();

            // Extinguish all highlighters
            for (int i = 0; i < highlighters.Count; i++)
            {
                highlighters[i].Extinguish();
            }

            // Highlighting buffer rendering finished. Reset currently active HighlightingBase
            current = null;

            // Create two buffers for blurring the image
            int           width   = highlightingBuffer.width / _downsampleFactor;
            int           height  = highlightingBuffer.height / _downsampleFactor;
            RenderTexture buffer  = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default, 1);
            RenderTexture buffer2 = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default, 1);
            if (!buffer.IsCreated())
            {
                buffer.useMipMap = false;
                buffer.wrapMode  = TextureWrapMode.Clamp;
            }
            if (!buffer2.IsCreated())
            {
                buffer2.useMipMap = false;
                buffer2.wrapMode  = TextureWrapMode.Clamp;
            }

            // Copy highlighting buffer to the smaller texture
            Graphics.Blit(highlightingBuffer, buffer, blitMaterial);

            // Blur the small texture
            bool oddEven = true;
            for (int i = 0; i < iterations; i++)
            {
                if (oddEven)
                {
                    FourTapCone(buffer, buffer2, i);
                }
                else
                {
                    FourTapCone(buffer2, buffer, i);
                }
                oddEven = !oddEven;
            }

            // Upscale blurred texture and cut stencil from it
            Graphics.SetRenderTarget(highlightingBuffer.colorBuffer, depthBuffer);
            cutMaterial.SetTexture(ShaderPropertyID._MainTex, oddEven ? buffer : buffer2);
            DoubleBlit(cutMaterial, 0, cutMaterial, 1);

            // Cleanup
            RenderTexture.ReleaseTemporary(buffer);
            RenderTexture.ReleaseTemporary(buffer2);
        }