Documentation

Unity Engine

Unity Engine

Create a custom material for a 2D light

Change how a 2D light looks by assigning a material that uses a Light2D shader graph.
Read time 4 minutesLast updated 6 days ago

By default, a 2D light fills its area with a flat color. Only the falloff and the properties of the Light 2D component shape it. This look works for many scenes, but the light can't show detail of its own. For example, a light can't shine through the leaves of a tree, or show moving patches like sunlight through clouds. A
Sprite

?

light can show a texture, but the texture is fixed to the light and doesn't change over time.
The shadows a 2D light casts have the same limit. They always darken toward black, which can look too harsh in a night scene or an underwater scene.
A custom light material solves these problems. You create the material from a Light2D shader graph, so you control the color of the light at every pixel. For example, you can add noise, a gradient, or color that changes over time. You can also set the color of the shadows the light casts.
To use a custom material, create a Light2D shader graph and assign its material to the light. If you don't assign a material, URP uses its built-in light shader.

Built-in light shader

Custom material with noise

A spot light that uses the built-in light shader.

The same spot light with a custom material that adds cloud-like noise to the light.

You can assign a custom material to every Light Type except Global, including custom 2D light types.

Create a Light2D shader graph

To create a Light2D shader graph and material:
  1. Go to Assets > Create > Shader Graph > URP > Light2D Shader Graph.
  2. Open the new shader graph. The Fragment context contains the Color, Shadow Color, and Volumetric Color blocks.
  3. Connect nodes to the blocks to change the light. For more information about each block, refer to Light2D shader graph reference for URP.
  4. Save the shader graph.
  5. In the Project window, right-click the shader graph and select Create > Material.
URP multiplies the Color block by the Color property of the light. URP also applies the falloff of Spot and Freeform lights, and the sprite of Sprite lights. For a custom light type, the shader graph controls the falloff.

Assign the material to a light

To assign the material to a light:
  1. Select a GameObject that has a Light 2D component.
  2. In the
    Inspector

    ?

    window, set Material to your new material.
The material picker lists only materials that work with 2D lights. If you drag an incompatible material onto the field, Unity doesn't assign it.

Light 2D component with a custom light material

The Light 2D component in the Inspector window, with the Material property highlighted.

Example: Add noise to a light

To add cloud-like noise to a light, connect these nodes to the Color block:
  1. Add a Position node, and set Space to World.
  2. Connect the Position node to the UV input of a Simple Noise node. Set Scale to 1.5.
  3. Add 0.2 to the noise result with an Add node, so dark areas aren't fully black.
  4. Connect the Add node to the Color block.

Shader graph that adds noise to a light

A Light2D shader graph. A Position node in world space connects to the UV input of a Simple Noise node with a Scale of 1.5. The noise result and a Float of 0.2 connect to an Add node, which connects to the Color block.

Change the color of shadows

Use the Shadow Color block to tint the areas where the light is blocked. URP mixes this color into shadowed areas based on the Strength of the shadow. The default is black, which matches the built-in shader.

Shadow Color: black (default)

Shadow Color: blue

Shadows with the default black Shadow Color.

The same shadows with a blue Shadow Color.

To make shadows blue, connect a Color node to the Shadow Color block, and set the color to blue.

Shader graph that makes shadows blue

A Light2D shader graph. A Color node set to blue connects to the Shadow Color block.

Change the color of volumetric light

Use the Volumetric Color block to change the light in empty space. This block has an effect only if you enable Volumetric in the Light 2D component.

Volumetric Color: white (default)

Volumetric Color: red, yellow, and green gradient

A light with the default white volumetric light.

The same light, with volumetric light that changes from red on the left, through yellow, to green on the right.

To make the volumetric light change from red to yellow to green across the light, connect these nodes to the Volumetric Color block:
  1. Add a UV node, and leave Channel set to UV0. For Point, Freeform, and Parametric lights, UV0 goes from 0 to 1 across the area the light covers. For Sprite lights, UV0 is the UV of the sprite.
  2. Connect the UV node to a Split node.
  3. Add a Gradient node, and set its colors to red, yellow, and green.
  4. Connect the Gradient node to the Gradient input of a Sample Gradient node. Connect the R output of the Split node to the Time input.
  5. Connect the Sample Gradient node to the Volumetric Color block.

Shader graph that colors volumetric light with a gradient

A Light2D shader graph. A UV node connects to a Split node. The R output of the Split node and a red, yellow, and green Gradient node connect to a Sample Gradient node, which connects to the Volumetric Color block.

Troubleshooting

If the material doesn't work with 2D lights, the Inspector window shows a warning.

Warning

Solution

Material '<name>' (shader '<name>') is missing the light pass(es)
Use a material that you create from a Light2D shader graph. To use the built-in light shader, clear the Material property.
The assigned Material has no shader
Assign a shader to the material, or clear the Material property.

Additional resources