我们游戏是回合制卡牌,场景并不会像MMO那样人物在屏幕上可以自由移动,这里产生的一个优化的可能就是将场景先渲染到一张图上,然后在Blit到屏幕上,然后在绘制后面的人物。前提是相机不能移动和旋转,这样在低端机上可以从设计上锁了镜头的旋转移动。如果对效果要求不那么高其实RT用ARGB32就够了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public Camera RtCamera; private void OnGUI() { if (GUILayout.Button("截图")) { var rt = RenderTexture.GetTemporary(Screen.width, Screen.height, 0, RenderTextureFormat.RGB111110Float); RtCamera.targetTexture = rt; RtCamera.Render(); ColorBlitRendererFeature.Enable = true; ColorBlitRendererFeature.RenderIdentifier = rt; } } |
接着就是Blit的操作了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering; using UnityEngine.Rendering.Universal; public class ColorBlitRendererFeature : ScriptableRendererFeature { public static bool Enable = false; public static RenderTargetIdentifier RenderIdentifier; public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) { if (Enable) { if (renderingData.cameraData.cameraType == CameraType.Game) { m_RenderPass.SetTarget(renderer.cameraColorTarget); renderer.EnqueuePass(m_RenderPass); } } } ColorBlitPass m_RenderPass = null; public override void Create() { m_RenderPass = new ColorBlitPass(); } class ColorBlitPass : ScriptableRenderPass { ProfilingSampler m_ProfilingSampler = new ProfilingSampler("ColorBlit"); RenderTargetIdentifier m_CameraColorTarget; public ColorBlitPass() { renderPassEvent = RenderPassEvent.BeforeRenderingOpaques; } public void SetTarget(RenderTargetIdentifier colorHandle) { m_CameraColorTarget = colorHandle; } public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) { var camera = renderingData.cameraData.camera; if (camera.cameraType != CameraType.Game || !ColorBlitRendererFeature.Enable) return; CommandBuffer cmd = CommandBufferPool.Get(); using (new ProfilingScope(cmd, m_ProfilingSampler)) { Blit(cmd, RenderIdentifier, m_CameraColorTarget); } context.ExecuteCommandBuffer(cmd); cmd.Clear(); CommandBufferPool.Release(cmd); } } } |
URP的skybox是晚于不透明物体的,如果使用了内置的skybox还需要调整渲染层级。
这个例子中我使用了一个RT摄像机,其实完全可以不使用额外相机,但是需要在Feature中先画场景层在保存到一张临时RT中,因为实际过程中场景可能由多个层组成也包括半透明和不透明两种,所以多使用一个RT相机负责截图更灵活方便。
- 本文固定链接: https://www.xuanyusong.com/archives/4997
- 转载请注明: 雨松MOMO 于 雨松MOMO程序研究院 发表
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!