ARBackgroundRendererFeature.cs
8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using UnityEngine;
using UnityEngine.Rendering;
#if MODULE_URP_ENABLED
using UnityEngine.Rendering.Universal;
#elif MODULE_LWRP_ENABLED
using UnityEngine.Rendering.LWRP;
#else
using ScriptableRendererFeature = UnityEngine.ScriptableObject;
#endif
namespace UnityEngine.XR.ARFoundation
{
/// <summary>
/// A render feature for rendering the camera background for AR devies.
/// </summary>
public class ARBackgroundRendererFeature : ScriptableRendererFeature
{
#if MODULE_URP_ENABLED || MODULE_LWRP_ENABLED
/// <summary>
/// The scriptable render pass to be added to the renderer when the camera background is to be rendered.
/// </summary>
CustomRenderPass m_ScriptablePass;
/// <summary>
/// The mesh for rendering the background shader.
/// </summary>
Mesh m_BackgroundMesh;
/// <summary>
/// Create the scriptable render pass.
/// </summary>
public override void Create()
{
#if !UNITY_EDITOR
m_ScriptablePass = new CustomRenderPass(RenderPassEvent.BeforeRenderingOpaques);
m_BackgroundMesh = new Mesh();
m_BackgroundMesh.vertices = new Vector3[]
{
new Vector3(0f, 0f, 0.1f),
new Vector3(0f, 1f, 0.1f),
new Vector3(1f, 1f, 0.1f),
new Vector3(1f, 0f, 0.1f),
};
m_BackgroundMesh.uv = new Vector2[]
{
new Vector2(0f, 0f),
new Vector2(0f, 1f),
new Vector2(1f, 1f),
new Vector2(1f, 0f),
};
m_BackgroundMesh.triangles = new int[] {0, 1, 2, 0, 2, 3};
#endif // !UNITY_EDITOR
}
/// <summary>
/// Add the background rendering pass when rendering a game camera with an enabled AR camera background component.
/// </summary>
/// <param name="renderer">The sriptable renderer in which to enqueue the render pass.</param>
/// <param name="renderingData">Additional rendering data about the current state of rendering.</param>
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
#if !UNITY_EDITOR
Camera currentCamera = renderingData.cameraData.camera;
if ((currentCamera != null) && (currentCamera.cameraType == CameraType.Game))
{
ARCameraBackground cameraBackground = currentCamera.gameObject.GetComponent<ARCameraBackground>();
if ((cameraBackground != null) && cameraBackground.backgroundRenderingEnabled
&& (cameraBackground.material != null))
{
bool invertCulling = cameraBackground.GetComponent<ARCameraManager>()?.subsystem?.invertCulling ?? false;
m_ScriptablePass.Setup(m_BackgroundMesh, cameraBackground.material, invertCulling,
renderer.cameraColorTarget, renderer.cameraDepth);
renderer.EnqueuePass(m_ScriptablePass);
}
}
#endif // !UNITY_EDITOR
}
/// <summary>
/// The custom render pass to render the camera background.
/// </summary>
class CustomRenderPass : ScriptableRenderPass
{
/// <summary>
/// The name for the custom render pass which will be display in graphics debugging tools.
/// </summary>
const string k_CustomRenderPassName = "AR Background Pass (URP)";
/// <summary>
/// The orthogonal projection matrix for the background rendering.
/// </summary>
static readonly Matrix4x4 k_BackgroundOrthoProjection = Matrix4x4.Ortho(0f, 1f, 0f, 1f, -0.1f, 9.9f);
/// <summary>
/// The mesh for rendering the background material.
/// </summary>
Mesh m_BackgroundMesh;
/// <summary>
/// The material used for rendering the device background using the camera video texture and potentially
/// other device-specific properties and textures.
/// </summary>
Material m_BackgroundMaterial;
/// <summary>
/// The destination render target identifier for rendering the background color.
/// </summary>
RenderTargetIdentifier m_ColorTargetIdentifier;
/// <summary>
/// The destination render target identifier for rendering the background depth.
/// </summary>
RenderTargetIdentifier m_DepthTargetIdentifier;
/// <summary>
/// Whether the culling mode should be inverted.
/// ([CommandBuffer.SetInvertCulling](https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.SetInvertCulling.html)).
/// </summary>
bool m_InvertCulling;
/// <summary>
/// Constructs the background render pass.
/// </summary>
/// <param name="renderPassEvent">The render pass event when this pass should be rendered.</param>
public CustomRenderPass(RenderPassEvent renderPassEvent)
{
this.renderPassEvent = renderPassEvent;
}
/// <summary>
/// Setup the background render pass.
/// </summary>
/// <param name="backgroundMesh">The mesh used for rendering the device background.</param>
/// <param name="backgroundMaterial">The material used for rendering the device background.</param>
/// <param name="invertCulling">Whether the culling mode should be inverted.</param>
/// <param name="colorTargetIdentifier">The color target to which to render the background texture.</param>
/// <param name="depthTargetIdentifier">The depth target to which to render the background texture.</param>
public void Setup(Mesh backgroundMesh, Material backgroundMaterial, bool invertCulling,
RenderTargetIdentifier colorTargetIdentifier,
RenderTargetIdentifier depthTargetIdentifier)
{
m_BackgroundMesh = backgroundMesh;
m_BackgroundMaterial = backgroundMaterial;
m_InvertCulling = invertCulling;
m_ColorTargetIdentifier = colorTargetIdentifier;
m_DepthTargetIdentifier = depthTargetIdentifier;
}
/// <summary>
/// Configure the render pass by configuring the render target and clear values.
/// </summary>
/// <param name="commandBuffer">The command buffer for configuration.</param>
/// <param name="renderTextureDescriptor">The descriptor of the target render texture.</param>
public override void Configure(CommandBuffer commandBuffer, RenderTextureDescriptor renderTextureDescriptor)
{
ConfigureTarget(m_ColorTargetIdentifier, m_DepthTargetIdentifier);
ConfigureClear(ClearFlag.Depth, Color.clear);
}
/// <summary>
/// Execute the commands to render the camera background.
/// </summary>
/// <param name="context">The render context for executing the render commands.</param>
/// <param name="renderingData">Additional rendering data about the current state of rendering.</param>
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
var cmd = CommandBufferPool.Get(k_CustomRenderPassName);
cmd.BeginSample(k_CustomRenderPassName);
ARCameraBackground.AddBeforeBackgroundRenderHandler(cmd);
cmd.SetInvertCulling(m_InvertCulling);
cmd.SetViewProjectionMatrices(Matrix4x4.identity, k_BackgroundOrthoProjection);
cmd.DrawMesh(m_BackgroundMesh, Matrix4x4.identity, m_BackgroundMaterial);
cmd.SetViewProjectionMatrices(renderingData.cameraData.camera.worldToCameraMatrix,
renderingData.cameraData.camera.projectionMatrix);
cmd.EndSample(k_CustomRenderPassName);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
/// <summary>
/// Clean up any resources for the render pass.
/// </summary>
/// <param name="commandBuffer">The command buffer for frame cleanup.</param>
public override void FrameCleanup(CommandBuffer commandBuffer)
{
}
}
#endif // MODULE_URP_ENABLED || MODULE_LWRP_ENABLED
}
}