Skip to content

Commit c983a9f

Browse files
markg-unityEvergreen
authored and
Evergreen
committed
[Port] [2022.3] DOCG-6491 Custom render passes and volumes documentation
1 parent 1cf4122 commit c983a9f

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

Packages/com.unity.render-pipelines.universal/Documentation~/TableOfContents.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@
143143
* [Scriptable Render Passes](renderer-features/scriptable-render-passes.md)
144144
* [Scriptable Render Passes](renderer-features/intro-to-scriptable-render-passes.md)
145145
* [Write a Scriptable Render Pass](renderer-features/write-a-scriptable-render-pass.md)
146-
* [Inject a pass via scripting](customize/inject-render-pass-via-script.md)
146+
* [Inject a pass via scripting](customize/inject-render-pass-via-script.md)
147+
* [Restrict a render pass to a scene area](customize/restrict-render-pass-scene-area.md)
147148
* [Scriptable Renderer Features](renderer-features/scriptable-renderer-features/scriptable-renderer-features-landing.md)
148149
* [Introduction to Scriptable Renderer Features](renderer-features/scriptable-renderer-features/intro-to-scriptable-renderer-features.md)
149150
* [Inject a custom render pass using a Scriptable Renderer Feature](renderer-features/scriptable-renderer-features/inject-a-pass-using-a-scriptable-renderer-feature.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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)

Packages/com.unity.render-pipelines.universal/Documentation~/renderer-features/scriptable-render-passes.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Use the `ScriptableRenderPass` API to write a custom render pass. You can then i
77
|[Introduction to Scriptable Render Passes](intro-to-scriptable-render-passes.md)|What a Scriptable Render Pass is, and how you can inject it into a scene.|
88
|[Write a Scriptable Render Pass](write-a-scriptable-render-pass.md)|An example of a `ScriptableRenderPass` instance that uses `Blit` to create a red tint effect.|
99
|[Inject a pass via scripting](../customize/inject-render-pass-via-script.md)|Use the `RenderPipelineManager` API to inject a render pass, without using a Scriptable Renderer Feature.|
10+
|[Restrict a render pass to a scene area](../customize/restrict-render-pass-scene-area.md) | Enable a custom rendering effect only if the camera is inside a volume in a scene. |
1011

1112
## Additional resources
1213

0 commit comments

Comments
 (0)