using UnityEngine; using UnityEngine.Rendering; using UnityEngine.Rendering.Universal; #if UNITY_2023_3_OR_NEWER using UnityEngine.Rendering.RenderGraphModule; #endif public class VertexColorsFeature : ScriptableRendererFeature { class VertexColorsPass : ScriptableRenderPass { 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 colorHandle, RTHandle depthHandle) { colorAttachmentHandle = colorHandle; depthAttachmentHandle = depthHandle; baseDescriptor.depthBufferBits = 0; baseDescriptor.graphicsFormat = UnityEngine.Experimental.Rendering.GraphicsFormat.R8G8B8A8_UNorm; colorDescriptor = baseDescriptor; baseDescriptor.graphicsFormat = UnityEngine.Experimental.Rendering.GraphicsFormat.None; baseDescriptor.depthStencilFormat = UnityEngine.Experimental.Rendering.GraphicsFormat.D32_SFloat_S8_UInt; baseDescriptor.colorFormat = RenderTextureFormat.Depth; depthDescriptor = baseDescriptor; } #if !UNITY_6000_0_OR_NEWER #if UNITY_2023_3_OR_NEWER [System.Obsolete] #endif public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor) { RenderingUtils.ReAllocateIfNeeded(ref colorAttachmentHandle, colorDescriptor, name: "_CameraVertexColorsColorTexture"); RenderingUtils.ReAllocateIfNeeded(ref depthAttachmentHandle, depthDescriptor, name: "_CameraVertexColorsDepthTexture"); ConfigureTarget(colorAttachmentHandle, depthAttachmentHandle); ConfigureClear(ClearFlag.All, Color.black); } #if UNITY_2023_3_OR_NEWER [System.Obsolete] #endif 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); } #endif // !UNITY_6000_0_OR_NEWER #if UNITY_6000_0_OR_NEWER // Classe de données pour RenderGraph private class PassData { public Material material; public FilteringSettings filteringSettings; public ShaderTagId shaderTagId; public TextureHandle colorTarget; public TextureHandle depthTarget; } public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData) { UniversalResourceData resourceData = frameData.Get(); UniversalCameraData cameraData = frameData.Get(); // Crée les textures dans le render graph var colorDesc = colorDescriptor; var depthDesc = depthDescriptor; TextureHandle colorHandle = UniversalRenderer.CreateRenderGraphTexture( renderGraph, colorDesc, "_CameraVertexColorsColorTexture", false); TextureHandle depthHandle = UniversalRenderer.CreateRenderGraphTexture( renderGraph, depthDesc, "_CameraVertexColorsDepthTexture", false); using (var builder = renderGraph.AddUnsafePass(m_ProfilerTag, out var passData)) { passData.material = vertexColorsMaterial; passData.filteringSettings = m_FilteringSettings; passData.shaderTagId = m_ShaderTagId; passData.colorTarget = colorHandle; passData.depthTarget = depthHandle; builder.UseTexture(colorHandle, AccessFlags.Write); builder.UseTexture(depthHandle, AccessFlags.Write); builder.AllowPassCulling(false); builder.AllowGlobalStateModification(true); builder.SetRenderFunc((PassData data, UnsafeGraphContext rgContext) => { var cmd = CommandBufferHelpers.GetNativeCommandBuffer(rgContext.cmd); cmd.SetRenderTarget( data.colorTarget, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, data.depthTarget, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store); cmd.ClearRenderTarget(true, true, Color.black); // Les DrawRenderers ne sont pas disponibles via NativeCommandBuffer — // on expose les textures globalement pour usage dans les shaders. cmd.SetGlobalTexture("_CameraVertexColorsColorTexture", data.colorTarget); cmd.SetGlobalTexture("_CameraVertexColorsDepthTexture", data.depthTarget); }); } } #endif // UNITY_6000_0_OR_NEWER public override void FrameCleanup(CommandBuffer cmd) { #if !UNITY_6000_0_OR_NEWER if (colorAttachmentHandle != null) { colorAttachmentHandle.Release(); colorAttachmentHandle = null; } if (depthAttachmentHandle != null) { depthAttachmentHandle.Release(); depthAttachmentHandle = null; } #endif } } 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; #if !UNITY_6000_0_OR_NEWER vertexColorsColorTexture = RTHandles.Alloc("_CameraVertexColorsColorTexture", name: "_CameraVertexColorsColorTexture"); vertexColorsDepthTexture = RTHandles.Alloc("_CameraVertexColorsDepthTexture", name: "_CameraVertexColorsDepthTexture"); #endif } public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) { #if !UNITY_6000_0_OR_NEWER vertexColorsPass.Setup(renderingData.cameraData.cameraTargetDescriptor, vertexColorsColorTexture, vertexColorsDepthTexture); #else vertexColorsPass.Setup(renderingData.cameraData.cameraTargetDescriptor, null, null); #endif renderer.EnqueuePass(vertexColorsPass); } protected override void Dispose(bool disposing) { #if !UNITY_6000_0_OR_NEWER vertexColorsColorTexture?.Release(); vertexColorsDepthTexture?.Release(); #endif CoreUtils.Destroy(vertexColorsMaterial); } }