|
| 1 | +# Restrict a render pass to a scene area in URP |
| 2 | + |
| 3 | +To restrict a render pass to a specific area of a scene, add a [volume](../Volumes.md) to the scene, then add code to your render pass and your shader to check if the camera is inside the volume. |
| 4 | + |
| 5 | +Follow these steps: |
| 6 | + |
| 7 | +1. Update your shader code to enable or disable your custom rendering effect based on a Boolean value. |
| 8 | + |
| 9 | + For example, add the following code to your shader: |
| 10 | + |
| 11 | + ```hlsl |
| 12 | + Pass |
| 13 | + { |
| 14 | + ... |
| 15 | +
|
| 16 | + // Add a variable to enable or disable your custom rendering effect |
| 17 | + float _Enabled; |
| 18 | +
|
| 19 | + ... |
| 20 | +
|
| 21 | + float4 Frag(Varyings input) : SV_Target0 |
| 22 | + { |
| 23 | + ... |
| 24 | +
|
| 25 | + // Return the color with the effect if the variable is 1, or the original color if the variable is 0 |
| 26 | + if (_Enabled == 1){ |
| 27 | + return colorWithEffect; |
| 28 | + } else { |
| 29 | + return originalColor; |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + ``` |
| 34 | +
|
| 35 | +2. Create a script that implements the `VolumeComponent` class. This creates a volume override component that you can add to a volume. |
| 36 | +
|
| 37 | + ```c# |
| 38 | + using UnityEngine; |
| 39 | + using UnityEngine.Rendering; |
| 40 | +
|
| 41 | + public class MyVolumeOverride : VolumeComponent |
| 42 | + { |
| 43 | + } |
| 44 | + ``` |
| 45 | +
|
| 46 | +3. In the **Hierarchy** window, select the **Add** (**+**) button, then select **GameObject** > **Volume** > **Box Volume**. |
| 47 | +
|
| 48 | +4. In the **Inspector** window for the new box volume, under **Volume**, select **New** to create a new volume profile. |
| 49 | +
|
| 50 | +5. Select **Add override**, then select your volume override component, for example **My Volume Override**. |
| 51 | +
|
| 52 | +6. Add a property to the volume override script. Unity adds the property in the **Inspector** window of the volume override. |
| 53 | +
|
| 54 | + For example: |
| 55 | +
|
| 56 | + ```c# |
| 57 | + public class MyVolumeOverride : VolumeComponent |
| 58 | + { |
| 59 | + // Add an 'Effect Enabled' checkbox to the Volume Override, with a default value of true. |
| 60 | + public BoolParameter effectEnabled = new BoolParameter(true); |
| 61 | + } |
| 62 | + ``` |
| 63 | +
|
| 64 | +5. In your custom pass, use the `GetComponent` API to get the volume override component and check the value of the property. |
| 65 | +
|
| 66 | + For example: |
| 67 | +
|
| 68 | + ```c# |
| 69 | + class myCustomPass : ScriptableRenderPass |
| 70 | + { |
| 71 | +
|
| 72 | + ... |
| 73 | +
|
| 74 | + public void Setup(Material material) |
| 75 | + { |
| 76 | + // Get the volume override component |
| 77 | + MyVolumeOverride myOverride = VolumeManager.instance.stack.GetComponent<MyVolumeOverride>(); |
| 78 | +
|
| 79 | + // Get the value of the 'Effect Enabled' property |
| 80 | + bool effectStatus = myOverride.effectEnabled.overrideState ? myOverride.effectEnabled.value : false; |
| 81 | + } |
| 82 | + } |
| 83 | + ``` |
| 84 | +
|
| 85 | +6. Pass the value of the property to the variable you added to the shader code. |
| 86 | +
|
| 87 | + For example: |
| 88 | +
|
| 89 | + ```c# |
| 90 | + class myCustomPass : ScriptableRenderPass |
| 91 | + { |
| 92 | +
|
| 93 | + ... |
| 94 | +
|
| 95 | + public void Setup(Material material) |
| 96 | + { |
| 97 | + MyVolumeOverride myOverride = VolumeManager.instance.stack.GetComponent<MyVolumeOverride>(); |
| 98 | + bool effectStatus = myOverride.effectEnabled.overrideState ? myOverride.effectEnabled.value : false; |
| 99 | +
|
| 100 | + // Pass the value to the shader |
| 101 | + material.SetFloat("_Enabled", effectStatus ? 1 : 0); |
| 102 | + } |
| 103 | + } |
| 104 | + ``` |
| 105 | +
|
| 106 | +Your custom rendering effect is now enabled when the camera is inside the volume, and disabled when the camera is outside the volume. |
| 107 | +
|
| 108 | +## Additional resources |
| 109 | +
|
| 110 | +- [Write a Scriptable Render Pass](../renderer-features/write-a-scriptable-render-pass.md) |
| 111 | +- [Volumes in URP](../volumes.md) |
| 112 | +- [Writing custom shaders in URP](../writing-custom-shaders-urp.md) |
0 commit comments