# Khronos PBR Neutral Tone Mapping & 2D Radiance Cascades GI in Unity 6 URP

> Hue-preserving Khronos PBR Neutral display transform via Unity 6 URP 17.6 RenderGraph (preventing ACES red/blue hue shifts on stylized palettes) paired with a WebGL-2-safe fragment Radiance Cascades 2D GI shader.

- **Canonical page:** https://gamecrew.ai/techniques/unity/radiance-cascades-tone-mapping
- **Techniques hub:** https://gamecrew.ai/techniques
- **Engine:** Unity 6
- **Category:** rendering
- **Tags:** `tonemapping`, `pbr-neutral`, `agx`, `color-grading`, `radiance-cascades`, `gi`, `penumbra`, `rendergraph`, `urp`, `shader`
- **Verification:** Compile-verified pattern
- **Revision:** 1 (2026-11-13)
- **Demo:** No demo

## When to Use & Field Best Practices

Two modern rendering upgrades eliminate the most common visual flaws in stylized and top-down Unity 6 URP games without requiring desktop-only compute shaders:

1. **Khronos PBR Neutral Tone Mapping (`Custom/PbrNeutralGrade` + `PbrNeutralGradeFeature`)**:
   - Standard ACES shifts bright saturated reds toward orange and deep blues toward violet — breaking carefully chosen art-bible palette colors in cozy, arcade, and comic slices.
   - **Khronos PBR Neutral** keeps base-range colors (`0..0.76`) 1:1 faithful to your palette while compressing HDR highlights (`> 1.0`) smoothly toward clean white without hue drift. Runs in `< 0.12 ms` inside URP 17.6 `RecordRenderGraph` (`AddBlitPass`).
2. **WebGL-2-Safe Fragment Radiance Cascades 2D GI (`Custom/RadianceCascade2D`)**:
   - Computes noise-free soft penumbra global illumination for top-down/2.5D arenas using standard fragment blits (`Graphics.Blit` / RenderGraph passes) rather than `ComputeShader`s — so it runs identically on **WebGL 2, WebGPU, mobile, and `llvmpipe`**!

## Mathematical & Algorithmic Core

- **Khronos PBR Neutral** for linear HDR $\mathbf{c} = (r, g, b)$ ($k_s = 0.8 - 0.04 = 0.76$, $k_d = 0.15$):
  $$x = \min(r, g, b), \quad o = \begin{cases} x - 6.25 x^2 & x < 0.08 \\ 0.04 & x \ge 0.08 \end{cases}, \quad \mathbf{c}' = \mathbf{c} - o$$
  When peak $p = \max(\mathbf{c}') > k_s$: let $d = 1 - k_s$, $\tilde{p} = 1 - \frac{d^2}{p + d - k_s}$, $g = \frac{1}{k_d (p - \tilde{p}) + 1}$, then $\mathbf{c}_{\text{out}} = \mathrm{lerp}(\tilde{p}, \mathbf{c}' \frac{\tilde{p}}{p}, g)$.

## Engine Implementation

### 1. Khronos PBR Neutral RenderGraph Post-Processing Shader (`Assets/Game/Resources/Shaders/PbrNeutralGrade.shader`)

```hlsl
Shader "Custom/PbrNeutralGrade"
{
    Properties
    {
        _Exposure ("Exposure", Range(0.25, 3.0)) = 1.05
        _Saturation ("Saturation", Range(0.0, 1.8)) = 1.04
        _Vignette ("Vignette Strength", Range(0.0, 0.7)) = 0.18
    }
    SubShader
    {
        Tags { "RenderPipeline" = "UniversalPipeline" "RenderType" = "Opaque" }
        Pass
        {
            Name "PbrNeutralGradePass"
            ZWrite Off
            ZTest Always
            Cull Off

            HLSLPROGRAM
            #pragma vertex Vert
            #pragma fragment Frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"

            CBUFFER_START(UnityPerMaterial)
                float _Exposure;
                float _Saturation;
                float _Vignette;
            CBUFFER_END

            float3 KhronosPbrNeutral(float3 color)
            {
                const float startCompression = 0.8 - 0.04;
                const float desaturation = 0.15;

                float x = min(color.r, min(color.g, color.b));
                float offset = x < 0.08 ? (x - 6.25 * x * x) : 0.04;
                color -= offset;

                float peak = max(color.r, max(color.g, color.b));
                if (peak < startCompression) return color;

                float d = 1.0 - startCompression;
                float newPeak = 1.0 - (d * d) / (peak + d - startCompression);
                color *= newPeak / max(1e-5, peak);

                float g = 1.0 / (desaturation * (peak - newPeak) + 1.0);
                return lerp(newPeak.xxx, color, g);
            }

            half4 Frag(Varyings input) : SV_Target
            {
                float2 uv = input.texcoord;
                float3 hdr = max(0.0, SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, uv).rgb) * _Exposure;
                float3 col = KhronosPbrNeutral(hdr);

                float luma = dot(col, float3(0.2126, 0.7152, 0.0722));
                col = lerp(luma.xxx, col, _Saturation);

                float2 centered = uv * 2.0 - 1.0;
                float vig = 1.0 - dot(centered, centered) * (_Vignette * 0.45);
                col = saturate(col * saturate(vig));
                return half4(col, 1.0);
            }
            ENDHLSL
        }
    }
}
```

### 2. WebGL-2-Safe Fragment Radiance Cascades 2D GI Pass (`Assets/Game/Resources/Shaders/RadianceCascade2D.shader`)

```hlsl
Shader "Custom/RadianceCascade2D"
{
    Properties
    {
        _SceneTex ("Emissive (RGB) + Occluder (A)", 2D) = "black" {}
        _UpperTex ("Upper Cascade Texture", 2D) = "black" {}
        _CascadeParams ("Res, CascadeIdx, MaxCascades, BaseInterval", Vector) = (128.0, 0.0, 4.0, 2.2)
    }
    SubShader
    {
        Tags { "RenderPipeline" = "UniversalPipeline" "RenderType" = "Opaque" }
        Pass
        {
            Name "RadianceCascadeMerge"
            ZWrite Off
            ZTest Always
            Cull Off

            HLSLPROGRAM
            #pragma vertex Vert
            #pragma fragment Frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"

            TEXTURE2D(_SceneTex);
            SAMPLER(sampler_SceneTex);
            TEXTURE2D(_UpperTex);
            SAMPLER(sampler_UpperTex);

            CBUFFER_START(UnityPerMaterial)
                float4 _CascadeParams;
            CBUFFER_END

            float4 SampleUpper(float2 probeIdx, float dirIdx, float upperGrid, float upperSide, float res)
            {
                float2 clamped = clamp(probeIdx, 0.0, upperGrid - 1.0);
                float tx = fmod(dirIdx, upperSide);
                float ty = floor(dirIdx / upperSide);
                float2 uv = (float2(tx, ty) * upperGrid + clamped + 0.5) / res;
                return SAMPLE_TEXTURE2D_LOD(_UpperTex, sampler_UpperTex, uv, 0.0);
            }

            half4 Frag(Varyings input) : SV_Target
            {
                float res = max(32.0, _CascadeParams.x);
                float c = _CascadeParams.y;
                float maxC = _CascadeParams.z;
                float baseStep = _CascadeParams.w;

                float side = pow(2.0, c + 1.0);
                float probeGrid = res / side;
                float2 px = floor(input.texcoord * res);
                float2 dirTile = floor(px / probeGrid);
                float2 probeCoord = px - dirTile * probeGrid;

                float dirIdx = dirTile.y * side + dirTile.x;
                float angle = (dirIdx + 0.5) / (side * side) * TWO_PI;
                float2 rayDir = float2(cos(angle), sin(angle));
                float2 originUv = (probeCoord + 0.5) / probeGrid;

                float tMin = baseStep * (pow(4.0, c) - 1.0) / 3.0;
                float tMax = baseStep * (pow(4.0, c + 1.0) - 1.0) / 3.0;
                float stepLen = max(1.0, tMax - tMin) / 12.0;

                float3 rad = 0.0;
                float vis = 1.0;
                [loop]
                for (int i = 0; i < 12; i++)
                {
                    float t = tMin + (i + 0.5) * stepLen;
                    float2 suv = originUv + rayDir * (t / res);
                    if (any(suv < 0.0) || any(suv > 1.0)) break;
                    float4 s = SAMPLE_TEXTURE2D_LOD(_SceneTex, sampler_SceneTex, suv, 0.0);
                    if (s.a > 0.5)
                    {
                        rad += s.rgb * vis;
                        vis = 0.0;
                        break;
                    }
                }

                if (vis > 0.01 && c + 1.0 < maxC)
                {
                    float upperSide = side * 2.0;
                    float upperGrid = probeGrid * 0.5;
                    float2 uPos = (probeCoord + 0.5) * 0.5 - 0.5;
                    float2 p0 = floor(uPos);
                    float2 f = saturate(uPos - p0);
                    float3 upperAvg = 0.0;
                    [unroll]
                    for (int k = 0; k < 4; k++)
                    {
                        float childDir = dirIdx * 4.0 + k;
                        float3 c00 = SampleUpper(p0 + float2(0, 0), childDir, upperGrid, upperSide, res).rgb;
                        float3 c10 = SampleUpper(p0 + float2(1, 0), childDir, upperGrid, upperSide, res).rgb;
                        float3 c01 = SampleUpper(p0 + float2(0, 1), childDir, upperGrid, upperSide, res).rgb;
                        float3 c11 = SampleUpper(p0 + float2(1, 1), childDir, upperGrid, upperSide, res).rgb;
                        upperAvg += lerp(lerp(c00, c10, f.x), lerp(c01, c11, f.x), f.y) * 0.25;
                    }
                    rad += upperAvg * vis;
                }

                return half4(rad, 1.0);
            }
            ENDHLSL
        }
    }
}
```

### 3. URP 17.6 RenderGraph Feature for PBR Neutral Grading (`Assets/Game/Runtime/Rendering/PbrNeutralGradeFeature.cs`)

```csharp
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering.RenderGraphModule;
using UnityEngine.Rendering.RenderGraphModule.Util;

namespace __NS__
{
    /// <summary>
    /// URP 17.6 RenderGraph screen pass applying Khronos PBR Neutral hue-faithful tone mapping.
    /// Turn off URP's built-in Tonemapping Volume override when enabling this pass to avoid double tone mapping.
    /// </summary>
    public sealed class PbrNeutralGradeFeature : ScriptableRendererFeature
    {
        [System.Serializable]
        public sealed class Settings
        {
            public RenderPassEvent passEvent = RenderPassEvent.BeforeRenderingPostProcessing;
            public Material material;
            [Range(0.25f, 3f)] public float exposure = 1.05f;
            [Range(0f, 1.8f)] public float saturation = 1.04f;
            [Range(0f, 0.7f)] public float vignette = 0.18f;
        }

        [SerializeField] Settings settings = new Settings();
        GradePass pass;

        public override void Create()
        {
            pass = new GradePass(settings) { renderPassEvent = settings.passEvent };
        }

        public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
        {
            if (settings.material == null) return;
            var cam = renderingData.cameraData;
            if (cam.cameraType == CameraType.Preview || cam.cameraType == CameraType.Reflection || cam.xr.enabled)
                return;
            renderer.EnqueuePass(pass);
        }

        sealed class GradePass : ScriptableRenderPass
        {
            static readonly int ExposureId = Shader.PropertyToID("_Exposure");
            static readonly int SatId = Shader.PropertyToID("_Saturation");
            static readonly int VigId = Shader.PropertyToID("_Vignette");
            readonly Settings settings;

            public GradePass(Settings settings)
            {
                this.settings = settings;
                requiresIntermediateTexture = true;
            }

            public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
            {
                if (settings.material == null) return;
                var resources = frameData.Get<UniversalResourceData>();
                var cameraData = frameData.Get<UniversalCameraData>();
                if (resources.isActiveTargetBackBuffer || cameraData.isPreviewCamera) return;

                TextureHandle source = resources.activeColorTexture;
                if (!source.IsValid()) return;

                settings.material.SetFloat(ExposureId, settings.exposure);
                settings.material.SetFloat(SatId, settings.saturation);
                settings.material.SetFloat(VigId, settings.vignette);

                TextureDesc desc = renderGraph.GetTextureDesc(source);
                desc.name = "_PbrNeutralTemp";
                desc.msaaSamples = MSAASamples.None;
                desc.clearBuffer = false;
                TextureHandle temp = renderGraph.CreateTexture(desc);

                renderGraph.AddBlitPass(
                    new RenderGraphUtils.BlitMaterialParameters(source, temp, settings.material, 0),
                    passName: "PbrNeutralGrade"
                );
                renderGraph.AddCopyPass(temp, source, passName: "PbrNeutralResolve");
            }
        }
    }
}
```

## Browser, Mobile & Software-Renderer Budgets

- **Avoid Double Tone Mapping**: When `PbrNeutralGradeFeature` is active, disable the Volume `Tonemapping` override (set `TonemappingMode.None`) so highlights are only compressed once.
- **Zero Compute Dependency**: `RadianceCascade2D.shader` uses constant `[loop]` ceilings (`12` steps) and explicit `SAMPLE_TEXTURE2D_LOD(..., 0.0)`, guaranteeing WebGL 2, WebGPU, and `llvmpipe` parity.
- **VR Safety**: `PbrNeutralGradeFeature` skips VR cameras cleanly (`cam.xr.enabled`).

## Verification Checklist

1. Run `compile` to verify both URP HLSL shaders and `PbrNeutralGradeFeature.cs` compile with zero errors.
2. Capture a bright saturated red/yellow prop under `PbrNeutralGradeFeature`: confirm hues remain true to the art bible rather than shifting orange.
3. Run 4 coarse-to-fine blits with `RadianceCascade2D` on a `128×128` test emitter/occluder texture and confirm smooth penumbras around pillars.

## Official references

- KhronosGroup — ToneMapping PBR Neutral (Apache-2.0)
- Tomasz Stachowiak / h3r2tic — tony-mc-mapface (Apache-2.0)
- n01r1r — radiance-cascades-urp (MIT)
- Alexander Sannikov (2024 — Radiance Cascades)

## Open-source sources

- Takes an idea from ToneMapping by Khronos Group (Apache License 2.0), https://github.com/KhronosGroup/ToneMapping (Khronos PBR Neutral hue-preserving display transform)
- Takes an idea from tony-mc-mapface by Tomasz Stachowiak (Apache License 2.0), https://github.com/h3r2tic/tony-mc-mapface (hue-preserving stylized tone-mapping guidance)
- Takes an idea from radiance-cascades-urp by n01r1r (MIT License), https://github.com/n01r1r/radiance-cascades-urp (URP 2D Radiance Cascades global illumination shader formulation)

Games built with this guide carry these credits, and the licence notices they ask for, in their third-party notices.
