lwrp渲染频率优化

如果没有东西刷新我们可以尽量复用以前的渲染效果会比较好。因为如果还要继续走一遍管线流程,会做裁剪以及渲染。

类似的比如在主界面,然后也没特效,那么这个勾朑可以降低摄像机计算的频率,从而减少不必要的渲染时机。

所以我改了lwrp的LightweightRenderPipeline的render,让他没点击事件,滑动事件以及特效等我们自定义需要刷新的事件的时候再做更新。

具体代码如下(主要注意mOrthographicCmd 的设置,然后在需要刷新的地方调用UnityEngine.Experimental.Rendering.LightweightPipeline.LightweightRenderPipeline.UIRefresh();就好了,一般是__touchBegin,__touchMove,TweenUpdate,必要的update的地方,这种方式的缺点主要是要充分自定义,而且静态界面比较多时,因为没事件刷新频率很低):

public static void UIRefresh()
        {
            if (mOrthographicCmd != null)
            {
                IsFresh = true;
                currentTime = 0;
                mOrthographicCmd.Clear();
                CommandBufferPool.Release(mOrthographicCmd);
                mOrthographicCmd = null;
            }
        }
        private static bool IsFresh = true;
        private static float currentTime = 0.0f;
        private static float refreshTime = 0.6f;
        private static CommandBuffer mOrthographicCmd = null;
        public override void Render(ScriptableRenderContext renderContext, Camera[] cameras)
        {
            base.Render(renderContext, cameras);
            BeginFrameRendering(cameras);

            GraphicsSettings.lightsUseLinearIntensity = true;
            SetupPerFrameShaderConstants();

            if (mOrthographicCmd != null)
            {
                currentTime += Time.deltaTime;
                if (currentTime >= refreshTime)
                {
                    UIRefresh();
                }
            }

            SortCameras(cameras);
            foreach (Camera camera in cameras)
            {
                if (camera.orthographic && mOrthographicCmd != null && !IsFresh)
                {
                    renderContext.ExecuteCommandBuffer(mOrthographicCmd);
                    renderContext.Submit();
                    return;
                }
                BeginCameraRendering(camera);

                foreach (var beforeCamera in camera.GetComponents())
                    beforeCamera.ExecuteBeforeCameraRender(this, renderContext, camera);

                RenderSingleCamera(this, renderContext, camera, ref m_CullResults, camera.GetComponent());
            }
        }
        
        public static void RenderSingleCamera(LightweightRenderPipeline pipelineInstance, ScriptableRenderContext context, Camera camera, ref CullResults cullResults, IRendererSetup setup = null)
        {

            if (pipelineInstance == null)
            {
                Debug.LogError("Trying to render a camera with an invalid render pipeline instance.");
                return;
            }

            ScriptableCullingParameters cullingParameters;    
            if (!CullResults.GetCullingParameters(camera, IsStereoEnabled(camera), out cullingParameters))
                return;

            CommandBuffer cmd = CommandBufferPool.Get(k_RenderCameraTag);
            using (new ProfilingSample(cmd, k_RenderCameraTag))
            {
                CameraData cameraData;
                PipelineSettings settings = pipelineInstance.settings;
                ScriptableRenderer renderer = pipelineInstance.renderer;
                InitializeCameraData(settings, camera, out cameraData);
                SetupPerCameraShaderConstants(cameraData);

                cullingParameters.shadowDistance = Mathf.Min(cameraData.maxShadowDistance, camera.farClipPlane);

                context.ExecuteCommandBuffer(cmd);
                if (!camera.orthographic)
                    cmd.Clear();

#if UNITY_EDITOR

                // Emit scene view UI
                if (cameraData.isSceneViewCamera)
                    ScriptableRenderContext.EmitWorldGeometryForSceneView(camera);
#endif
                CullResults.Cull(ref cullingParameters, context, ref cullResults);

                RenderingData renderingData;
                InitializeRenderingData(settings, ref cameraData, ref cullResults,
                    renderer.maxVisibleAdditionalLights, renderer.maxPerObjectAdditionalLights, out renderingData);

                var setupToUse = setup;
                if (setupToUse == null)
                    setupToUse = defaultRendererSetup;

                renderer.Clear();
                setupToUse.Setup(renderer, ref renderingData);
                renderer.Execute(context, ref renderingData);
                if (camera.orthographic)
                {
                    mOrthographicCmd = cmd;
                    currentTime = 0;
                }
            }

            context.ExecuteCommandBuffer(cmd);
            if (!camera.orthographic)
                CommandBufferPool.Release(cmd);
            context.Submit();
#if UNITY_EDITOR
            Handles.DrawGizmos(camera);
#endif
        }

如有问题请与我联系!

你可能感兴趣的:(lwrp渲染频率优化)