using UnityEngine; using UnityEngine.Rendering; using UnityEngine.Rendering.Universal; public class VertexColorsFeature : ScriptableRendererFeature { class VertexColorsPass : ScriptableRenderPass { private const int kDepthBufferBits = 32; private RTHandle colorAttachmentHandle; private RTHandle depthAttachmentHandle; internal RenderTextureDescriptor colorDescriptor { get; private set; } internal RenderTextureDescriptor depthDescriptor { get; private set; } private Material vertexColorsMaterial = null; private FilteringSettings m_FilteringSettings; string m_ProfilerTag = "Vertex Color Prepass"; ShaderTagId m_ShaderTagId = new ShaderTagId("DepthOnly"); public VertexColorsPass(RenderQueueRange renderQueueRange, LayerMask layerMask, Material material) { m_FilteringSettings = new FilteringSettings(renderQueueRange, layerMask); vertexColorsMaterial = material; } public void Setup(RenderTextureDescriptor baseDescriptor, RTHandle colorAttachmentHandle, RTHandle depthAttachmentHandle) { this.colorAttachmentHandle = colorAttachmentHandle; this.depthAttachmentHandle = depthAttachmentHandle; // Set up color descriptor (only graphics format, no depth) baseDescriptor.depthBufferBits = 0; baseDescriptor.graphicsFormat = UnityEngine.Experimental.Rendering.GraphicsFormat.R8G8B8A8_UNorm; colorDescriptor = baseDescriptor; // Set up depth descriptor (only depthStencilFormat, no color) baseDescriptor.graphicsFormat = UnityEngine.Experimental.Rendering.GraphicsFormat.None; baseDescriptor.depthStencilFormat = UnityEngine.Experimental.Rendering.GraphicsFormat.D32_SFloat_S8_UInt; baseDescriptor.colorFormat = RenderTextureFormat.Depth; depthDescriptor = baseDescriptor; } public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor) { // Allocate color texture RenderingUtils.ReAllocateIfNeeded(ref colorAttachmentHandle, colorDescriptor, name: "_CameraVertexColorsColorTexture"); // Allocate depth texture RenderingUtils.ReAllocateIfNeeded(ref depthAttachmentHandle, depthDescriptor, name: "_CameraVertexColorsDepthTexture"); ConfigureTarget(colorAttachmentHandle, depthAttachmentHandle); ConfigureClear(ClearFlag.All, Color.black); } public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) { CommandBuffer cmd = CommandBufferPool.Get(m_ProfilerTag); using (new ProfilingSample(cmd, m_ProfilerTag)) { context.ExecuteCommandBuffer(cmd); cmd.Clear(); var sortFlags = renderingData.cameraData.defaultOpaqueSortFlags; var drawSettings = CreateDrawingSettings(m_ShaderTagId, ref renderingData, sortFlags); drawSettings.perObjectData = PerObjectData.None; ref CameraData cameraData = ref renderingData.cameraData; Camera camera = cameraData.camera; if (cameraData.xr.enabled) context.StartMultiEye(camera); drawSettings.overrideMaterial = vertexColorsMaterial; context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref m_FilteringSettings); cmd.SetGlobalTexture("_CameraVertexColorsColorTexture", colorAttachmentHandle.nameID); cmd.SetGlobalTexture("_CameraVertexColorsDepthTexture", depthAttachmentHandle.nameID); } context.ExecuteCommandBuffer(cmd); CommandBufferPool.Release(cmd); } public override void FrameCleanup(CommandBuffer cmd) { if (colorAttachmentHandle != null) { colorAttachmentHandle.Release(); colorAttachmentHandle = null; } if (depthAttachmentHandle != null) { depthAttachmentHandle.Release(); depthAttachmentHandle = null; } } } VertexColorsPass vertexColorsPass; RTHandle vertexColorsColorTexture; RTHandle vertexColorsDepthTexture; [SerializeField] Shader vertexColorsShader; Material vertexColorsMaterial; public override void Create() { vertexColorsMaterial = CoreUtils.CreateEngineMaterial(vertexColorsShader); vertexColorsPass = new VertexColorsPass(RenderQueueRange.opaque, -1, vertexColorsMaterial); vertexColorsPass.renderPassEvent = RenderPassEvent.AfterRenderingPrePasses; // Allocate RTHandles for color and depth textures vertexColorsColorTexture = RTHandles.Alloc("_CameraVertexColorsColorTexture", name: "_CameraVertexColorsColorTexture"); vertexColorsDepthTexture = RTHandles.Alloc("_CameraVertexColorsDepthTexture", name: "_CameraVertexColorsDepthTexture"); } public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) { vertexColorsPass.Setup(renderingData.cameraData.cameraTargetDescriptor, vertexColorsColorTexture, vertexColorsDepthTexture); renderer.EnqueuePass(vertexColorsPass); } }