using UnityEngine.Rendering.Universal; using UnityEngine.Rendering; using UnityEngine; public class RenderToTextureFeature : ScriptableRendererFeature { class RenderToTexturePass : ScriptableRenderPass { public RenderTexture renderTexture; private Material blitMaterial; public RenderToTexturePass(RenderTexture renderTexture) { this.renderTexture = renderTexture; blitMaterial = new Material(Shader.Find("Hidden/BlitCopy")); } public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) { CommandBuffer cmd = CommandBufferPool.Get("RenderToTexture"); // 获取当前渲染目标 RenderTargetIdentifier currentTarget = renderingData.cameraData.renderer.cameraColorTarget; // 设置渲染目标为 RenderTexture cmd.SetRenderTarget(renderTexture); cmd.ClearRenderTarget(true, true, Color.clear); // 执行渲染操作(例如渲染相机的内容到目标纹理) cmd.Blit(currentTarget, renderTexture, blitMaterial); context.ExecuteCommandBuffer(cmd); CommandBufferPool.Release(cmd); } } private RenderToTexturePass renderPass; public RenderToTextureFeature(RenderTexture renderTexture) { renderPass = new RenderToTexturePass(renderTexture); } public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) { renderer.EnqueuePass(renderPass); } public override void Create() { throw new System.NotImplementedException(); } }