docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Use lighting in a custom URP shader

    To use lighting in a custom Universal Render Pipeline (URP) shader, follow these steps:

    1. Add #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" inside the HLSLPROGRAM in your shader file.
    2. Use any of the methods from the following sections.

    Get light data

    The Lighting.hlsl file imports the RealtimeLights.hlsl file, which contains the following methods.

    Method Syntax Description
    GetMainLight Light GetMainLight() Returns the main light in the scene.
    GetAdditionalLight Light GetAdditionalLight(uint lightIndex, float3 positionInWorldSpace) Returns the lightIndex additional light that affects positionWS. For example, if lightIndex is 0, this method returns the first additional light.
    GetAdditionalLightsCount int GetAdditionalLightsCount() Returns the number of additional lights.

    Refer to Use shadows in a custom URP shader for information on versions of these methods you can use to calculate shadows.

    Calculate lighting for a surface normal

    Method Syntax Description
    LightingLambert half3 LightingLambert(half3 lightColor, half3 lightDirection, half3 surfaceNormal) Returns the diffuse lighting for the surface normal, calculated using the Lambert model.
    LightingSpecular half3 LightingSpecular(half3 lightColor, half3 lightDirection, half3 surfaceNormal, half3 viewDirection, half4 specularAmount, half smoothnessAmount) Returns the specular lighting for the surface normal, using simple shading.

    Calculate ambient occlusion

    The Lighting.hlsl file imports the AmbientOcclusion.hlsl file, which contains the following methods.

    Method Syntax Description
    SampleAmbientOcclusion half SampleAmbientOcclusion(float2 normalizedScreenSpaceUV) Returns the ambient occlusion value at the position in screen space, where 0 means occluded and 1 means unoccluded.
    GetScreenSpaceAmbientOcclusion AmbientOcclusionFactor GetScreenSpaceAmbientOcclusion(float2 normalizedScreenSpaceUV) Returns the indirect and direct ambient occlusion values at the position in screen space, where 0 means occluded and 1 means unoccluded.

    Refer to Ambient occlusion for more information.

    Structs

    AmbientOcclusionFactor

    Use the GetScreenSpaceAmbientOcclusion method to return this struct.

    Field Description
    half indirectAmbientOcclusion The amount the object is in shadow from ambient occlusion caused by objects blocking indirect light.
    half directAmbientOcclusion The amount the object is in shadow from ambient occlusion caused by objects blocking direct light.

    Light

    Use the GetMainLight and GetAdditionalLight methods to return this struct.

    Field Description
    half3 direction The direction of the light.
    half3 color The color of the light.
    float distanceAttenuation The strength of the light, based on its distance from the object.
    half shadowAttenuation The strength of the light, based on whether the object is in shadow.
    uint layerMask The layer mask of the light.

    Example

    The following URP shader draws object surfaces with the amount of light they receive from the main directional light.

    Shader "Custom/LambertLighting"
    {
        SubShader
        {
            Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
    
            Pass
            {
                HLSLPROGRAM
    
                #pragma vertex vert
                #pragma fragment frag
    
                #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    
                struct Attributes
                {
                    float4 positionOS : POSITION;
                    float2 uv: TEXCOORD0;
                };
    
                struct Varyings
                {
                    float4 positionCS  : SV_POSITION;
                    float2 uv: TEXCOORD0;
                    half3 lightAmount : TEXCOORD2;
                };
    
                Varyings vert(Attributes IN)
                {
                    Varyings OUT;
    
                    OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
    
                    // Get the VertexNormalInputs of the vertex, which contains the normal in world space
                    VertexNormalInputs positions = GetVertexNormalInputs(IN.positionOS);
    
                    // Get the properties of the main light
                    Light light = GetMainLight();
    
                    // Calculate the amount of light the vertex receives
                    OUT.lightAmount = LightingLambert(light.color, light.direction, positions.normalWS.xyz);
    
                    return OUT;
                }
    
                half4 frag(Varyings IN) : SV_Target
                {
                    // Set the fragment color to the interpolated amount of light
                    return float4(IN.lightAmount, 1);
                }
                ENDHLSL
            }
        }
    }
    

    Additional resources

    • Writing custom shaders
    • Upgrade custom shaders for URP compatibility
    • HLSL in Unity
    • Diffuse
    • Specular

    Did you find this page useful? Please give it a rating:

    Thanks for rating this page!

    Report a problem on this page

    What kind of problem would you like to report?

    • This page needs code samples
    • Code samples do not work
    • Information is missing
    • Information is incorrect
    • Information is unclear or confusing
    • There is a spelling/grammar error on this page
    • Something else

    Thanks for letting us know! This page has been marked for review based on your feedback.

    If you have time, you can provide more information to help us fix the problem faster.

    Provide more information

    You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:

    You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:

    You've told us there is information missing from this page. Please tell us more about what's missing:

    You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:

    You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:

    You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:

    You've told us this page has a problem. Please tell us more about what's wrong:

    Thank you for helping to make the Unity documentation better!

    Your feedback has been submitted as a ticket for our documentation team to review.

    We are not able to reply to every ticket submitted.

    In This Article
    • Get light data
      • Calculate lighting for a surface normal
    • Calculate ambient occlusion
    • Structs
      • AmbientOcclusionFactor
      • Light
    • Example
    • Additional resources
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)