-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomPostProcessFeature.cs
90 lines (68 loc) · 3.36 KB
/
CustomPostProcessFeature.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
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace SRXDPostProcessing;
internal class CustomPostProcessFeature : ScriptableRendererFeature {
private static readonly int ASPECT = Shader.PropertyToID("_Aspect");
private CustomPostProcessPass pass;
public override void Create() {
pass = new CustomPostProcessPass();
pass.renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) => renderer.EnqueuePass(pass);
private class CustomPostProcessPass : ScriptableRenderPass {
private RenderTargetIdentifier cameraColorTarget;
private RenderTargetHandle tempTexture1;
private RenderTargetHandle tempTexture2;
private float aspect;
private bool shouldProcess;
private PostProcessingLayer targetLayer;
public CustomPostProcessPass() {
tempTexture1.Init("_tempTexture1");
tempTexture2.Init("_tempTexture2");
}
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData) {
var mainCamera = MainCamera.Instance;
var cameraData = renderingData.cameraData;
var camera = cameraData.camera;
shouldProcess = mainCamera != null && (camera == mainCamera.CurrentCamera || camera == mainCamera.backgroundCamera);
if (!shouldProcess)
return;
cameraColorTarget = cameraData.renderer.cameraColorTarget;
aspect = camera.aspect;
if (camera == mainCamera.CurrentCamera)
targetLayer = PostProcessingLayer.Foreground;
else
targetLayer = PostProcessingLayer.Background;
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) {
if (!shouldProcess)
return;
var cmd = CommandBufferPool.Get("CustomRenderFeature");
var descriptor = renderingData.cameraData.cameraTargetDescriptor;
descriptor.depthBufferBits = 0;
cmd.GetTemporaryRT(tempTexture1.id, descriptor, FilterMode.Bilinear);
cmd.GetTemporaryRT(tempTexture2.id, descriptor, FilterMode.Bilinear);
Blit(cmd, cameraColorTarget, tempTexture1.Identifier());
var source = tempTexture1.Identifier();
var destination = tempTexture2.Identifier();
foreach (var instance in PostProcessingManager.PostProcessingInstances) {
if (!instance.Enabled || instance.Layer != targetLayer)
continue;
var material = instance.Material;
material.SetFloat(ASPECT, aspect);
Blit(cmd, source, destination, material);
(source, destination) = (destination, source);
}
Blit(cmd, source, cameraColorTarget);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
public override void FrameCleanup(CommandBuffer cmd) {
if (!shouldProcess)
return;
cmd.ReleaseTemporaryRT(tempTexture1.id);
cmd.ReleaseTemporaryRT(tempTexture2.id);
}
}
}