-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomPass.cs
89 lines (72 loc) · 2.78 KB
/
CustomPass.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
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class CustomPass<T> : ScriptableRenderPass where T : VolumeComponent, IPostProcessComponent
{
protected T volumeComponent;
CustomRenderFeature<T>.Settings settings;
public virtual string GetShaderPath() { return null; }
public virtual string renderTag => "Render Custom Effects";
public FilterMode filterMode { get; set; }
protected Material material;
protected RenderTargetIdentifier source;
protected RenderTargetIdentifier destination;
public CustomPass(RenderPassEvent renderPassEvent)
{
this.renderPassEvent = renderPassEvent;
}
public void Setup(CustomRenderFeature<T>.Settings featureSettings)
{
settings = featureSettings;
renderPassEvent = featureSettings.renderPassEvent;
}
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
RenderTextureDescriptor blitTargetDescriptor = renderingData.cameraData.cameraTargetDescriptor;
blitTargetDescriptor.depthBufferBits = 0;
var isSourceAndDestinationSameTarget = settings.sourceType == settings.destinationType &&
(settings.sourceType == CustomRenderFeature<T>.BufferType.CameraColor || settings.sourceTexture == settings.destinationTexture);
var renderer = renderingData.cameraData.renderer;
if (settings.sourceType == CustomRenderFeature<T>.BufferType.CameraColor) {
source = renderer.cameraColorTargetHandle;
}
else {
var sourceId = Shader.PropertyToID(settings.sourceTexture);
cmd.GetTemporaryRT(sourceId, blitTargetDescriptor, filterMode);
source = new RenderTargetIdentifier(sourceId);
}
if (isSourceAndDestinationSameTarget) {
destination = source;
}
else {
var destinationId = Shader.PropertyToID(settings.destinationTexture);
cmd.GetTemporaryRT(destinationId, blitTargetDescriptor, filterMode);
destination = new RenderTargetIdentifier(destinationId);
}
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
if (!renderingData.cameraData.postProcessEnabled || !IsValid())
return;
var stack = VolumeManager.instance.stack;
volumeComponent = stack.GetComponent<T>();
if (volumeComponent == null || !volumeComponent.IsActive()) return;
var cmd = CommandBufferPool.Get(renderTag);
Render(cmd, ref renderingData);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
public virtual bool IsValid()
{
return material != null;
}
public virtual void Init(string shaderPath)
{
var shader = Shader.Find(shaderPath);
if (shader == null)
return;
material = CoreUtils.CreateEngineMaterial(shader);
}
public virtual void Release() { if (material != null) CoreUtils.Destroy(material); }
protected virtual void Render(CommandBuffer cmd, ref RenderingData renderingData) { }
}