Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Custom Material Integration

It is possible to integrate 2D Radiance Cascades lighting system with your custom materials and shaders. Both ShaderGraph and HLSL integrations are available.

Shader Graph Integration

To use 2D Radiance Cascades lighting in Shader Graph:

  1. Open or create a new shader graph.
  2. In Unity Editor, go to Packages/2D Radiance Cascades/Shaders/ShaderGraph.
  3. See the three pre-configured subgraphs:
    • GetRCLightMapColor - provides the main lightmap color,
    • GetRCDirectionalColor - provides the directional lightmap color for a specified direction
    • GetRCPhongDiffuse - Provides Phong diffuse shading term using the directional lightmaps, and the surface normal
  4. Drag and drop the subgraph you need into your shader graph
  5. Connect inputs and outputs to your needs
img-shader-subgraphsPre-configured subgraphs location
img-shader-lightmap-mainGetRCLightMapColor
Inputs:
- UV: Lightmap UV location. Most likely you'd want to pass Screen Position here
Outputs:
- Color: The sampled lightmap color
img-shader-lightmap-dirGetRCDirectionalColor
Inputs:
- UV: Lightmap UV location. Most likely you'd want to pass Screen Position here
- Direction Index: integer index of the direction to sample the color from. Should be an integer in the range [0; 3]
Outputs:
- Direction: a vector representing the direction the sampled light is coming from
- Color: The sampled lightmap color
img-shader-phong-diffuseGetRCPhongDiffuse
Inputs:
- UV: Lightmap UV location. Most likely you'd want to pass Screen Position here
- Normal: surface normal
- Distance: Distance from the light to the surface
Outputs:
- Color: Computed diffuse color value

Advanced Manual Integration

For a completely manual integration, you can access the lightmaps directly as global textures and use them however you like.

The Main Lightmap:

  1. In the shader graph add a Texture 2D parameter
  2. Set it's Scope to Global
  3. Set the Reference to _RCLightmap

The Directional Lightmaps:

  1. In the shader graph add a Texture 2D Array parameter
  2. Set it's Scope to Global
  3. Set the Reference to _RCLightmapDirectional
  4. Directional lightmaps are stored in four slices of the Texture 2D Array.

HLSL Shader Integration

To access the lightmaps in a custom HLSL shader:

// Include the required headers
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.dim0v.radiance-cascades-2d/Shaders/Common.hlsl"

struct appdata
{
    float3 pos : POSITION;
    float2 uv : TEXCOORD0;
};

struct v2f
{
    float4 pos : SV_POSITION;
    float2 uv : TEXCOORD0;
};

// Vertex shader
v2f vert(appdata i) {
    v2f o;
    
    o.uv = i.uv;
    o.pos = GetNormalizedScreenSpaceUV(i.pos);
    
    return o;
}

// Fragment shader
float4 frag(v2f i) : SV_Target
{
    // ...

    // Sample main lightmap
    float3 main_light = GetRCLightmapColor(i.pos);
    
    // Sample directional lightmaps
    float3 directional_lights[RC_DIRECTION_COUNT];
    for (int i = 0; i < RC_DIRECTION_COUNT; ++i) {
      float2 dir;
      directional_lights[i] = GetRCDirectionalColor(i.pos, i, dir);
    }
    
    // Compute Phong diffuse term
    float3 lighting = GetRCPhongDiffuse(i.pos, my_normal, my_distance);

    // ...
}

Key points:

  • Include Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl for convenience.
  • There are tree functions for accessing the lightmap data:
    • GetRCLightmapColor - Accessing the main lightmap
    • GetRCDirectionalColor - Accessing the directional lightmaps
    • GetRCPhongDiffuse - Computing the Phong Diffuse shading using the directional lightmaps
  • You need to sample using screen-space coordinates (normalized)
  • The lightmap contains RGB values representing the accumulated lighting
  • RC_DIRECTION_COUNT contains the number of directional lightmaps available
  • GetRCDirectionalColor has an out parameter for the direction the light is coming from for convenience
  • For shortcut functions used in the example to exist, you need to include (directly or indirectly) Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl before the Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl. Otherwise, use more verbose explicitly typed alternatives (e.g., GetRCLightmapColor_float or GetRCLightmapColor_half instead of GetRCLightmapColor)

Advanced Manual Integration

For a completely manual integration, you can access the lightmaps directly as global textures and use them however you like. You can either still include the Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl, or manually declare the following 2 globals:

  1. Texture2D<half4> _RCLightmap - the main lightmap.
  2. Texture2DArray<half4> _RCLightmapDirectional - directional lightmaps. Individual lightmaps for different directions are stored in four slices of the Texture2DArray.