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<IBeforeCameraRender>())
                    beforeCamera.ExecuteBeforeCameraRender(this, renderContext, camera);

                RenderSingleCamera(this, renderContext, camera, ref m_CullResults, camera.GetComponent<IRendererSetup>());
            }
        }
        
        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
        }

如有问题请与我联系!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值