-
Notifications
You must be signed in to change notification settings - Fork 136
/
CopyPass.cs
101 lines (83 loc) · 2.98 KB
/
CopyPass.cs
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
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Rendering.HighDefinition;
[CustomPassDrawer(typeof(CopyPass))]
public class CopyPassDrawer : CustomPassDrawer
{
protected override PassUIFlag commonPassUIFlags => PassUIFlag.Name;
}
#endif
public class CopyPass : CustomPass
{
public enum BufferType
{
Color,
Normal,
Roughness,
Depth,
MotionVectors,
}
public RenderTexture outputRenderTexture;
[SerializeField, HideInInspector]
Shader customCopyShader;
Material customCopyMaterial;
public BufferType bufferType;
protected override bool executeInSceneView => false;
int normalPass;
int roughnessPass;
int depthPass;
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
{
if (customCopyShader == null)
customCopyShader = Shader.Find("Hidden/FullScreen/CustomCopy");
customCopyMaterial = CoreUtils.CreateEngineMaterial(customCopyShader);
normalPass = customCopyMaterial.FindPass("Normal");
roughnessPass = customCopyMaterial.FindPass("Roughness");
depthPass = customCopyMaterial.FindPass("Depth");
}
protected override void Execute(CustomPassContext ctx)
{
if (outputRenderTexture == null || customCopyMaterial == null)
return;
SyncRenderTextureAspect(outputRenderTexture, ctx.hdCamera.camera);
var scale = RTHandles.rtHandleProperties.rtHandleScale;
customCopyMaterial.SetVector("_Scale", scale);
switch (bufferType)
{
default:
case BufferType.Color:
ctx.cmd.Blit(ctx.cameraColorBuffer, outputRenderTexture, new Vector2(scale.x, scale.y), Vector2.zero, 0, 0);
break;
case BufferType.Normal:
ctx.cmd.Blit(ctx.cameraNormalBuffer, outputRenderTexture, customCopyMaterial, normalPass);
break;
case BufferType.Roughness:
ctx.cmd.Blit(ctx.cameraNormalBuffer, outputRenderTexture, customCopyMaterial, roughnessPass);
break;
case BufferType.Depth:
ctx.cmd.Blit(ctx.cameraNormalBuffer, outputRenderTexture, customCopyMaterial, depthPass);
break;
case BufferType.MotionVectors:
ctx.cmd.Blit(ctx.cameraMotionVectorsBuffer, outputRenderTexture, new Vector2(scale.x, scale.y), Vector2.zero, 0, 0);
break;
}
}
void SyncRenderTextureAspect(RenderTexture rt, Camera camera)
{
float aspect = rt.width / (float)rt.height;
if (!Mathf.Approximately(aspect, camera.aspect))
{
rt.Release();
rt.width = camera.pixelWidth;
rt.height = camera.pixelHeight;
rt.Create();
}
}
protected override void Cleanup()
{
CoreUtils.Destroy(customCopyMaterial);
}
}