|
| 1 | +# Use lighting in a custom URP shader |
| 2 | + |
| 3 | +To use lighting in a custom Universal Render Pipeline (URP) shader, follow these steps: |
| 4 | + |
| 5 | +1. Add `#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"` inside the `HLSLPROGRAM` in your shader file. |
| 6 | +2. Use any of the methods from the following sections. |
| 7 | + |
| 8 | +## Get light data |
| 9 | + |
| 10 | +The `Lighting.hlsl` file imports the `RealtimeLights.hlsl` file, which contains the following methods. |
| 11 | + |
| 12 | +| **Method** | **Syntax** | **Description** | |
| 13 | +|-|-|-| |
| 14 | +| `GetMainLight` | `Light GetMainLight()` | Returns the main light in the scene. | |
| 15 | +| `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. | |
| 16 | +| `GetAdditionalLightsCount` | `int GetAdditionalLightsCount()` | Returns the number of additional lights. | |
| 17 | + |
| 18 | +Refer to [Use shadows in a custom URP shader](use-built-in-shader-methods-shadows.md) for information on versions of these methods you can use to calculate shadows. |
| 19 | + |
| 20 | +### Calculate lighting for a surface normal |
| 21 | + |
| 22 | +| **Method** | **Syntax** | **Description** | |
| 23 | +|-|-|-| |
| 24 | +| `LightingLambert` | `half3 LightingLambert(half3 lightColor, half3 lightDirection, half3 surfaceNormal)` | Returns the diffuse lighting for the surface normal, calculated using the Lambert model. | |
| 25 | +| `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](shading-model.md#simple-shading). | |
| 26 | + |
| 27 | +## Calculate ambient occlusion |
| 28 | + |
| 29 | +The `Lighting.hlsl` file imports the `AmbientOcclusion.hlsl` file, which contains the following methods. |
| 30 | + |
| 31 | +| **Method** | **Syntax** | **Description** | |
| 32 | +|-|-|-| |
| 33 | +| `SampleAmbientOcclusion` | `half SampleAmbientOcclusion(float2 normalizedScreenSpaceUV)` | Returns the ambient occlusion value at the position in screen space, where 0 means occluded and 1 means unoccluded. | |
| 34 | +| `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. | |
| 35 | + |
| 36 | +Refer to [Ambient occlusion](post-processing-ssao.md) for more information. |
| 37 | + |
| 38 | +## Structs |
| 39 | + |
| 40 | +### AmbientOcclusionFactor |
| 41 | + |
| 42 | +Use the `GetScreenSpaceAmbientOcclusion` method to return this struct. |
| 43 | + |
| 44 | +| **Field** | **Description** | |
| 45 | +|-|-| |
| 46 | +| `half indirectAmbientOcclusion` | The amount the object is in shadow from ambient occlusion caused by objects blocking indirect light. | |
| 47 | +| `half directAmbientOcclusion` | The amount the object is in shadow from ambient occlusion caused by objects blocking direct light. | |
| 48 | + |
| 49 | +### Light |
| 50 | + |
| 51 | +Use the `GetMainLight` and `GetAdditionalLight` methods to return this struct. |
| 52 | + |
| 53 | +| **Field** | **Description** | |
| 54 | +|-|-| |
| 55 | +| `half3 direction` | The direction of the light. | |
| 56 | +| `half3 color` | The color of the light. | |
| 57 | +| `float distanceAttenuation` | The strength of the light, based on its distance from the object. | |
| 58 | +| `half shadowAttenuation` | The strength of the light, based on whether the object is in shadow. | |
| 59 | +| `uint layerMask` | The layer mask of the light. | |
| 60 | + |
| 61 | +## Example |
| 62 | + |
| 63 | +The following URP shader draws object surfaces with the amount of light they receive from the main directional light. |
| 64 | + |
| 65 | +```hlsl |
| 66 | +Shader "Custom/LambertLighting" |
| 67 | +{ |
| 68 | + SubShader |
| 69 | + { |
| 70 | + Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" } |
| 71 | +
|
| 72 | + Pass |
| 73 | + { |
| 74 | + HLSLPROGRAM |
| 75 | +
|
| 76 | + #pragma vertex vert |
| 77 | + #pragma fragment frag |
| 78 | +
|
| 79 | + #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" |
| 80 | +
|
| 81 | + struct Attributes |
| 82 | + { |
| 83 | + float4 positionOS : POSITION; |
| 84 | + float2 uv: TEXCOORD0; |
| 85 | + }; |
| 86 | +
|
| 87 | + struct Varyings |
| 88 | + { |
| 89 | + float4 positionCS : SV_POSITION; |
| 90 | + float2 uv: TEXCOORD0; |
| 91 | + half3 lightAmount : TEXCOORD2; |
| 92 | + }; |
| 93 | +
|
| 94 | + Varyings vert(Attributes IN) |
| 95 | + { |
| 96 | + Varyings OUT; |
| 97 | +
|
| 98 | + OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz); |
| 99 | +
|
| 100 | + // Get the VertexNormalInputs of the vertex, which contains the normal in world space |
| 101 | + VertexNormalInputs positions = GetVertexNormalInputs(IN.positionOS); |
| 102 | +
|
| 103 | + // Get the properties of the main light |
| 104 | + Light light = GetMainLight(); |
| 105 | +
|
| 106 | + // Calculate the amount of light the vertex receives |
| 107 | + OUT.lightAmount = LightingLambert(light.color, light.direction, positions.normalWS.xyz); |
| 108 | +
|
| 109 | + return OUT; |
| 110 | + } |
| 111 | +
|
| 112 | + half4 frag(Varyings IN) : SV_Target |
| 113 | + { |
| 114 | + // Set the fragment color to the interpolated amount of light |
| 115 | + return float4(IN.lightAmount, 1); |
| 116 | + } |
| 117 | + ENDHLSL |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +## Additional resources |
| 124 | + |
| 125 | +- [Writing custom shaders](writing-custom-shaders-urp.md) |
| 126 | +- [Upgrade custom shaders for URP compatibility](urp-shaders/birp-urp-custom-shader-upgrade-guide.md) |
| 127 | +- [HLSL in Unity](https://docs.unity3d.com/Manual/SL-ShaderPrograms.html) |
| 128 | +- [Diffuse](https://docs.unity3d.com/Manual/shader-NormalDiffuse.html) |
| 129 | +- [Specular](https://docs.unity3d.com/Manual/shader-NormalSpecular.html) |
| 130 | + |
| 131 | + |
| 132 | + |
0 commit comments