Beispiel #1
0
        private void RenderObstracles(ref ScriptableRenderContext context)
        {
            var obstracleTime = TimeSample.Sample("Obstracles Sample");
            int width, height;

            GetLightingResolution(out width, out height);

            var cmdBuf = CmdBufferPool.Get("Obstracles");

            cmdBuf.GetTemporaryRT(obstracle, width, height, asset.GetDepthBits());
            SetRT(cmdBuf, obstracleRT, cWite.linear, true, true);
            context.ExecuteCommandBuffer(cmdBuf);
            CmdBufferPool.Release(cmdBuf);

            var opaqueDrawSettings = new DrawRendererSettings(m_CurrCamera, m_NormalsLitPassName);

            opaqueDrawSettings.SetShaderPassName(1, m_NormalsLitPassName);
            opaqueDrawSettings.sorting.flags = SortFlags.CommonOpaque;

            var opaqueFilterSettings = new FilterRenderersSettings(true)
            {
                renderQueueRange = RenderQueueRange.opaque,
            };

            context.DrawRenderers(m_CullResults.visibleRenderers, ref opaqueDrawSettings, opaqueFilterSettings);
            asset.ObstraclesRenderTime = obstracleTime.Result();
        }
Beispiel #2
0
        private void ClearResources(ref ScriptableRenderContext context)
        {
            var cmdBuf = CmdBufferPool.Get("release resources");

            cmdBuf.ReleaseTemporaryRT(obstracle);
            cmdBuf.ReleaseTemporaryRT(lights);
            cmdBuf.ReleaseTemporaryRT(ambient);
            cmdBuf.ReleaseTemporaryRT(blurLights);
            cmdBuf.ReleaseTemporaryRT(screenColor);
            context.ExecuteCommandBuffer(cmdBuf);
            CmdBufferPool.Release(cmdBuf);
        }
Beispiel #3
0
        private void InternalRender(ref ScriptableRenderContext context)
        {
            // TODO : temporary for mip map camera renderers
            //if((m_CurrCamera.cullingMask & (1 << 17)) == 1<<17 && m_CurrCamera.cameraType != CameraType.SceneView)
            //{
            //    RenderUnlit(ref context, false);
            //    return;
            //}


            if (asset.Mode == FlatLightRPAsset.DrawMode.Unlit)
            {
                RenderUnlit(ref context, true);
            }
            else
            {
                RenderOpaquesAndTransparent(ref context);
                if (asset.Mode == FlatLightRPAsset.DrawMode.Opaque)
                {
                    CommandBuffer cmd = CmdBufferPool.Get("Display Opaque");
                    cmd.Blit(screenColorRT, BuiltinRenderTextureType.CameraTarget);
                    context.ExecuteCommandBuffer(cmd);
                    CmdBufferPool.Release(cmd);
                }
                else
                {
                    RenderObstracles(ref context);
                    if (asset.Mode == FlatLightRPAsset.DrawMode.Obstracles)
                    {
                        CommandBuffer cmd = CmdBufferPool.Get("Display Obstracles");
                        cmd.Blit(obstracleRT, BuiltinRenderTextureType.CameraTarget);
                        context.ExecuteCommandBuffer(cmd);
                        CmdBufferPool.Release(cmd);
                    }
                    else
                    {
                        RenderLights(ref context);
                        RenderUnlit(ref context, false);
                    }
                }
                ClearResources(ref context);
            }

            if (CmdBufferPool.ReferencesCount > 0)
            {
                Debug.LogError("pool references count is large then 0 !");
            }
        }
Beispiel #4
0
        private void RenderUnlit(ref ScriptableRenderContext context, bool clear)
        {
            var time = TimeSample.Sample("Unlit Sample");

            if (clear)
            {
                CommandBuffer cmd = CmdBufferPool.Get("Unlit");
                SetRT(cmd, BuiltinRenderTextureType.CameraTarget, m_CurrCamera.backgroundColor.linear, true, true);
                context.ExecuteCommandBuffer(cmd);
                CmdBufferPool.Release(cmd);
            }

            var opaqueDrawSettings = new DrawRendererSettings(m_CurrCamera, m_DrawUnlt);

            opaqueDrawSettings.sorting.flags = SortFlags.CommonOpaque;

            var opaqueFilterSettings = new FilterRenderersSettings(true)
            {
                renderQueueRange = RenderQueueRange.opaque,
            };

            context.DrawRenderers(m_CullResults.visibleRenderers, ref opaqueDrawSettings, opaqueFilterSettings);
            asset.UnlitRenderTime = time.Result();
        }
Beispiel #5
0
        private void RenderLights(ref ScriptableRenderContext context)
        {
            var lightsTime = TimeSample.Sample("Lights Sample");
            int width, height;

            GetLightingResolution(out width, out height);

            var ColorRT = BuiltinRenderTextureType.CameraTarget;

            Vector2 downSampleTexelSize = new Vector2(1F / width, 1F / height);
            Vector2 texel                    = new Vector2(1F / m_CurrCamera.pixelWidth, 1F / m_CurrCamera.pixelHeight);
            float   LightPixelSize           = m_CurrCamera.orthographicSize * 2f / m_CurrCamera.pixelHeight;
            float   lightPixelsPerUnityMeter = 1F / LightPixelSize;
            Vector3 mainPos                  = m_CurrCamera.transform.position;
            Vector3 gridPos                  = new Vector3(
                Mathf.Round(mainPos.x * lightPixelsPerUnityMeter) / lightPixelsPerUnityMeter,
                Mathf.Round(mainPos.y * lightPixelsPerUnityMeter) / lightPixelsPerUnityMeter,
                Mathf.Round(mainPos.z * lightPixelsPerUnityMeter) / lightPixelsPerUnityMeter);
            Vector3 posDiff = gridPos - mainPos;
            Vector3 pos     = posDiff + mainPos;
            Vector2 offset  = Vector2.Scale(texel, -posDiff * (lightPixelsPerUnityMeter));

            Matrix4x4 view = Matrix4x4.Inverse(Matrix4x4.TRS(pos, m_CurrCamera.transform.rotation, new Vector3(1, 1, -1)));
            Matrix4x4 proj = GL.GetGPUProjectionMatrix(m_CurrCamera.projectionMatrix, true);
            Matrix4x4 gPos = proj * view;

            var cmdBuf = CmdBufferPool.Get("Lights");

            cmdBuf.SetGlobalMatrix(GridPosMatrix, gPos);
            cmdBuf.GetTemporaryRT(ambient, width, height, 0);
            cmdBuf.GetTemporaryRT(lights, width, height, 0);
            SetRT(cmdBuf, lightsRT, Color.clear, true, false);;
            cmdBuf.SetGlobalTexture(envObstracleTex, obstracleRT);
            context.ExecuteCommandBuffer(cmdBuf);

            var opaqueDrawSettings = new DrawRendererSettings(m_CurrCamera, m_DrawLights);

            opaqueDrawSettings.SetShaderPassName(1, m_DrawLights);
            opaqueDrawSettings.sorting.flags = SortFlags.CommonOpaque;

            var opaqueFilterSettings = new FilterRenderersSettings(true)
            {
                renderQueueRange = RenderQueueRange.opaque,
            };

            context.DrawRenderers(m_CullResults.visibleRenderers, ref opaqueDrawSettings, opaqueFilterSettings);

            if (asset.Mode == FlatLightRPAsset.DrawMode.Lights)
            {
                cmdBuf.Clear();
                cmdBuf.Blit(lights, ColorRT);
                context.ExecuteCommandBuffer(cmdBuf);
                CmdBufferPool.Release(cmdBuf);
                asset.LightsRenderTime = lightsTime.Result();
                return;
            }

            asset.LightsRenderTime = lightsTime.Result();
            var compositeTime = TimeSample.Sample("Composite Sample");

            // composite
            LightsComposite.SetVector(TexelSize, downSampleTexelSize);
            LightsComposite.SetVector(ENVLightOffset, offset);
            LightsComposite.SetColor(Ambient, asset.GetAmbientColor());
            LightsComposite.SetFloat(EmissionBlend, asset.GetEmissionBlend());
            LightsComposite.SetFloat(SampleDistBlur, asset.GetSampleDistBlur());

            cmdBuf.Clear();

            if (asset.BlurLights && asset.BlurLightsIterations > 0)
            {
                cmdBuf.GetTemporaryRT(blurLights, width, height, 0);
                RenderTargetIdentifier dst   = blurLightsRT;
                RenderTargetIdentifier src   = lightsRT;
                RenderTargetIdentifier final = src;
                for (int i = 0; i < asset.BlurLightsIterations; ++i)
                {
                    cmdBuf.Blit(src, dst, LightsComposite, LIGHT_BLUR_PASS);
                    RenderTargetIdentifier tmp = dst;
                    final = dst;
                    dst   = src;
                    src   = tmp;
                }
                if (asset.AmbientPass)
                {
                    cmdBuf.Blit(final, ambientRT, LightsComposite, AMBIENT_PASS);
                    cmdBuf.SetGlobalTexture(envLightsTex, ambientRT);
                }
                else
                {
                    cmdBuf.SetGlobalTexture(envLightsTex, final);
                }
            }
            else if (asset.AmbientPass)
            {
                cmdBuf.Blit(lightsRT, ambientRT, LightsComposite, AMBIENT_PASS);
                cmdBuf.SetGlobalTexture(envLightsTex, ambientRT);
            }
            else
            {
                cmdBuf.SetGlobalTexture(envLightsTex, lightsRT);
            }

            if (asset.AmbientPass)
            {
                if (asset.Mode == FlatLightRPAsset.DrawMode.Ambient)
                {
                    cmdBuf.Blit(ambientRT, ColorRT);
                    context.ExecuteCommandBuffer(cmdBuf);
                    CmdBufferPool.Release(cmdBuf);
                    asset.CompositeRenderTime = compositeTime.Result();
                    return;
                }

                cmdBuf.Blit(screenColorRT, BuiltinRenderTextureType.CameraTarget, LightsComposite, COMPOSITE_PASS);
            }
            else
            {
                cmdBuf.Blit(screenColorRT, BuiltinRenderTextureType.CameraTarget, LightsComposite, COMPOSITE_AMBIENT_PASS);
            }

            context.ExecuteCommandBuffer(cmdBuf);
            CmdBufferPool.Release(cmdBuf);
            asset.CompositeRenderTime = compositeTime.Result();
        }
Beispiel #6
0
        private void RenderOpaquesAndTransparent(ref ScriptableRenderContext context)
        {
            int opaque_width  = m_CurrCamera.pixelWidth / asset.GetDownsample(FlatLightRPAsset.FLTechnique.Opaques);
            int opaque_height = m_CurrCamera.pixelHeight / asset.GetDownsample(FlatLightRPAsset.FLTechnique.Opaques);

            if (asset.GroundIsRenderedSeparately)
            {
                var           groundTime    = TimeSample.Sample("Ground Sample");
                int           ground_width  = m_CurrCamera.pixelWidth / asset.GetDownsample(FlatLightRPAsset.FLTechnique.Ground);
                int           ground_height = m_CurrCamera.pixelHeight / asset.GetDownsample(FlatLightRPAsset.FLTechnique.Ground);
                CommandBuffer cmd           = CmdBufferPool.Get("SetCameraRenderTarget(Ground)");
                cmd.GetTemporaryRT(ground, ground_width, ground_height, asset.GetDepthBits());
                ClearAndSetRT(cmd, groundRT);
                context.ExecuteCommandBuffer(cmd);

                var groundDrawSettings = new DrawRendererSettings(m_CurrCamera, m_DrawGround);
                groundDrawSettings.SetShaderPassName(1, m_DrawGround);
                groundDrawSettings.sorting.flags = SortFlags.CommonOpaque;

                var groundFilterSettings = new FilterRenderersSettings(true)
                {
                    renderQueueRange = RenderQueueRange.opaque,
                };

                if (m_CurrCamera.clearFlags == CameraClearFlags.Skybox)
                {
                    context.DrawSkybox(m_CurrCamera);
                }

                context.DrawRenderers(m_CullResults.visibleRenderers, ref groundDrawSettings, groundFilterSettings);

                cmd.Clear();
                cmd.GetTemporaryRT(screenColor, opaque_width, opaque_height, asset.GetDepthBits());
                SetRT(cmd, screenColorRT, m_CurrCamera.backgroundColor.linear, false, true);
                cmd.Blit(groundRT, screenColorRT);
                cmd.ReleaseTemporaryRT(ground);
                context.ExecuteCommandBuffer(cmd);
                CmdBufferPool.Release(cmd);
                asset.GroundRenderTime = groundTime.Result();
            }
            else
            {
                CommandBuffer cmd = CmdBufferPool.Get("SetCameraRenderTarget");
                cmd.GetTemporaryRT(screenColor, opaque_width, opaque_height, asset.GetDepthBits());
                ClearAndSetRT(cmd, screenColorRT);
                context.ExecuteCommandBuffer(cmd);
                CmdBufferPool.Release(cmd);

                if (m_CurrCamera.clearFlags == CameraClearFlags.Skybox)
                {
                    context.DrawSkybox(m_CurrCamera);
                }
            }

            var opTime             = TimeSample.Sample("Opaques And Transparent Sample");
            var opaqueDrawSettings = new DrawRendererSettings(m_CurrCamera, m_LitPassName);

            opaqueDrawSettings.SetShaderPassName(1, m_LitPassName);
            opaqueDrawSettings.sorting.flags = SortFlags.CommonOpaque;

            var opaqueFilterSettings = new FilterRenderersSettings(true)
            {
                renderQueueRange = RenderQueueRange.opaque,
            };

            context.DrawRenderers(m_CullResults.visibleRenderers, ref opaqueDrawSettings, opaqueFilterSettings);

            var transparentSettings = new DrawRendererSettings(m_CurrCamera, m_LitPassName);

            transparentSettings.SetShaderPassName(1, m_LitPassName);
            transparentSettings.sorting.flags = SortFlags.CommonTransparent;

            var transparentFilterSettings = new FilterRenderersSettings(true)
            {
                renderQueueRange = RenderQueueRange.transparent
            };

            context.DrawRenderers(m_CullResults.visibleRenderers, ref transparentSettings, transparentFilterSettings);
            asset.OpaquesAndTransparentRenderTime = opTime.Result();
        }