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
        }

如有問題請與我聯繫!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章