Version: Unity 6.7 Alpha (6000.7)
Language : English
Add shadows from 2D lights in URP
Shadow Caster 2D component reference

Create a custom 2D shadow

To create a custom 2D shadow, use the ShadowShape2DProvider API. For example to create a custom shadow shape.

Follow these steps:

  1. In a C# script, create a class that inherits from ShadowShape2DProvider.

  2. Implement the ProviderName method and return a GUIContent object with your name for the new shadow type. The name appears in the Casting Source property of the Shadow Caster 2D component reference. For example:

    using UnityEngine;
    using UnityEngine.Rendering.Universal;
    
    [System.Serializable]
    public class CustomShadow : ShadowShape2DProvider
    {
        public override GUIContent ProviderName(string componentName)
        {
            return new GUIContent("My Custom Shadow");
        }
    }
    
  3. Implement the IsRequiredComponentData method, which Unity calls for each component on the GameObject. Return true for the component that has the data the shadow needs, for example the Transform component.

    public override bool IsRequiredComponentData(Component currentComponent){
        if (currentComponent is Transform){
            return true;
        } else {
            return false;
        }
    }
    
  4. Implement the OnBeforeRender method to set the shape of the shadow. Unity passes in a ShadowShape2D instance, which you can use to call SetShape with a shadow shape. Use native arrays for the shadow shape data. For more information about native arrays, refer to Introduction to NativeContainer.

    Note: If the sprite is static, set the shadow shape once using the OnInitialized method instead. For more information, refer to the ShadowShape2DProvider API.

  5. To create custom parameters, add a public field to the class. For example:

    public float size = 1f;
    
  6. Add a Shadow Caster 2D component to a GameObject, and set its Casting Source to your new shadow shape. The public field appears in a new Provider section in the Inspector window.

For more information, refer to the ShadowShape2DProvider API.

Example

The following example creates a custom shadow that has an outline triangle shape and a size parameter.

using Unity.Collections;
using UnityEngine;
using UnityEngine.Rendering.Universal;

[System.Serializable]
public class TriangleShadow : ShadowShape2DProvider
{
    public override GUIContent ProviderName(string componentName) => new GUIContent("Triangle shadow");

    public override bool IsRequiredComponentData(Component currentComponent){
        if (currentComponent is Transform){
            return true;
        } else {
            return false;
        }
    }
    [SerializeField] float size = 1f;

    public override void OnBeforeRender(Component sourceComponent, Bounds cullingBounds, ShadowShape2D shadowShape)
    {
        Transform triangle = sourceComponent.transform;

        NativeArray<Vector3> vertices = new NativeArray<Vector3>(3, Allocator.Temp);
        vertices[0] = triangle.TransformPoint(new Vector3(-size, -size, 0f));
        vertices[1] = triangle.TransformPoint(new Vector3(size, -size, 0f));
        vertices[2] = triangle.TransformPoint(new Vector3(0f, size, 0f));

        NativeArray<int> indices = new NativeArray<int>(6, Allocator.Temp);
        indices[0] = 0;
        indices[1] = 1;
        indices[2] = 1;
        indices[3] = 2;
        indices[4] = 2;
        indices[5] = 0;

        shadowShape.SetShape(vertices, indices, ShadowShape2D.OutlineTopology.Lines);

        vertices.Dispose();
        indices.Dispose();
    }
}

Additional resources

Add shadows from 2D lights in URP
Shadow Caster 2D component reference