UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

graphics

[URP] Access Scene Vertex Colors in Full-Screen Shaders

Solution

graphicsshadersrenderingvisual effects

Unity 2021.3.x - Unity 6.3.x

Published 28 days ago

Issue

 In a full-screen shader graph, the Vertex Color node provides data from the full-screen quad rather than scene geometry. This limitation prevents developers from masking effects or excluding specific objects based on baked vertex data during post-processing or full-screen blits.

Explanation

To access scene vertex data, implement a custom render pass that renders the scene with a specific override material into a global texture. The standard Vertex Color node in a full-screen shader graph is bound to the mesh used for the blit operation, which is typically a single quad covering the camera view.

  1. Create a Shader that outputs IN.vertexColor to the fragment color.
  2. Create a ScriptableRenderPass that uses DrawingSettings with a FilteringSettings mask.
  3. Configure the pass to render into a custom RenderTexture.
  4. Set that RenderTexture as a global shader property using cmd.SetGlobalTexture.
  5. In your full-screen shader graph, replace the Vertex Color node with a Sample Texture 2D node referencing the global RenderTexture name.

Additional Tips:

  • Use RenderPassEvent.AfterRenderingOpaques to ensure all static geometry has been processed before sampling.
  • Ensure the RenderTexture format supports the precision required for your vertex data (e.g., GraphicsFormat.R8G8B8A8_SRGB).
  • Utilize ShaderTagId in your DrawingSettings to only include objects with specific tags in the vertex color pass.

TL;DR

The Vertex Color node in a full-screen context samples the procedural quad's data. To access scene-specific colors, you must render them to a RenderTexture via a ScriptableRenderPass using a replacement shader.


Related Posts Haven't quite found a solution to your problem? We think these posts might help you.

Content inspired by a Unity discussion post.