-
Notifications
You must be signed in to change notification settings - Fork 29
/
CP_SSSSS_Main.cs
282 lines (239 loc) · 7.77 KB
/
CP_SSSSS_Main.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using UnityEngine;
using UnityEngine.Rendering;
using System.Collections.Generic;
[RequireComponent(typeof(Camera))]
#if UNITY_5_4_OR_NEWER
[ImageEffectAllowedInSceneView]
#endif
[ImageEffectOpaque]
[ExecuteInEditMode]
public class CP_SSSSS_Main : MonoBehaviour
{
public Shader shader;
public Shader maskShader;
public Shader copyDepthShader;
CommandBuffer buffer;
CameraEvent camEvent = CameraEvent.BeforeImageEffectsOpaque;
private Material m_Material;
Material material
{
get
{
if (m_Material == null && shader != null)
{
m_Material = new Material(shader);
m_Material.hideFlags = HideFlags.HideAndDontSave;
}
return m_Material;
}
}
[Range(1,3)]
public int downscale = 1;
[Range(1, 3)]
public int blurIterations = 1;
[Range(0.01f, 1.6f)]
public float scatterDistance = 0.4f;
[Range(0f,2f)]
public float scatterIntensity = 1f;
[Range(0.001f, 0.3f)]
public float softDepthBias = 0.05f;
[Range(0f, 1f)]
public float affectDirect = 0.5f;
void OnDisable() {
if (m_Material)
{
DestroyImmediate(m_Material);
}
if(m_CopyDepthMaterial) DestroyImmediate(m_CopyDepthMaterial);
foreach(var i in m_MaskMaterials) DestroyImmediate(i);
m_Material = null;
m_CopyDepthMaterial = null;
m_MaskMaterials.Clear();
CleanupBuffer();
}
void OnEnable()
{
if (!SystemInfo.supportsImageEffects)
{
enabled = false;
return;
}
CleanupBuffer();
// Disable the image effect if the shader can't
// run on the users graphics card
if(shader && !shader.isSupported)
{
enabled = false;
return;
}
if(buffer == null) ApplyBuffer();
// cannot call Shader.PropertyToID() at compiler time, so let's do it here
scatteringMapID = Shader.PropertyToID("ScatteringMap");
scatteringColorID = Shader.PropertyToID("ScatteringColor");
}
private void OnPreRender()
{
if(buffer == null) ApplyBuffer();
if(buffer != null) UpdateBuffer();
}
void OnPostRender()
{
TargetMeshes.Clear();
}
public struct SSSParameter
{
public Texture scatteringMap;
public Vector2 scatteringMapOffset;
public Vector2 scatteringMapScale;
public Color scatteringColor;
}
static int scatteringMapID; // = Shader.PropertyToID("ScatteringMap");
static int scatteringColorID; // = Shader.PropertyToID("ScatteringColor");
public static bool HasMaterialSSSParameter(Material material)
{
return material.HasProperty(scatteringColorID) && material.HasProperty(scatteringMapID);
}
public static SSSParameter MakeSSSParameterFromMaterial(Material material)
{
return new SSSParameter {
scatteringMap = material.GetTexture(scatteringMapID),
scatteringMapOffset = material.mainTextureOffset,
scatteringMapScale = material.mainTextureScale,
scatteringColor = material.GetColor(scatteringColorID),
};
}
public static SSSParameter MakeSSSParameterFromTextureAndColor(TextureAndColor src)
{
return new SSSParameter {
scatteringMap = src.texture,
scatteringMapOffset = Vector2.zero,
scatteringMapScale = Vector2.one,
scatteringColor = src.color,
};
}
public struct TargetMesh
{
public TargetMesh(Renderer renderer, int subMeshIndex, SSSParameter sssParameter)
{
this.renderer = renderer;
this.subMeshIndex = subMeshIndex;
this.sssParameter = sssParameter;
}
public Renderer renderer;
public int subMeshIndex;
public SSSParameter sssParameter;
}
List<TargetMesh> m_TargetMeshes = new List<TargetMesh>(256);
List<TargetMesh> TargetMeshes { get { return m_TargetMeshes; } }
public void AddRenderer(Renderer renderer, int subMeshIndex, SSSParameter sssParameter)
{
TargetMeshes.Add(new TargetMesh(renderer, subMeshIndex, sssParameter));
}
List<Material> m_MaskMaterials = new List<Material>();
Material m_CopyDepthMaterial;
Material CopyDepthMaterial {
get {
if(m_CopyDepthMaterial == null && copyDepthShader) {
m_CopyDepthMaterial = new Material(copyDepthShader) {
hideFlags = HideFlags.HideAndDontSave
};
}
return m_CopyDepthMaterial;
}
}
void Reset()
{
shader = Shader.Find("Hidden/CPSSSSSShader");
maskShader = Shader.Find("Hidden/CPSSSSSMask");
copyDepthShader = Shader.Find("Hidden/CPSSSSSBlitDepthTextureToDepth");
}
void ApplyBuffer()
{
buffer = new CommandBuffer();
buffer.name = "Screen Space Subsurface Scattering";
GetComponent<Camera>().AddCommandBuffer(camEvent, buffer);
}
void UpdateBuffer()
{
buffer.Clear();
if(TargetMeshes.Count == 0) return;
AddMakeSSSMaskCommands(buffer);
int blurRT1 = Shader.PropertyToID("_CPSSSSSBlur1");
int blurRT2 = Shader.PropertyToID("_CPSSSSSBlur2");
int src = Shader.PropertyToID("_CPSSSSSSource");
int w = -1;
int h = -1;
Camera cam = Camera.current;
if (cam != null)
{
w = cam.pixelWidth / downscale;
h = cam.pixelHeight / downscale;
}
//buffer.GetTemporaryRT(blurRT1, -1, -1, 16, FilterMode.Bilinear, RenderTextureFormat.ARGBFloat);
buffer.GetTemporaryRT(blurRT1, w, h, 16, FilterMode.Bilinear, RenderTextureFormat.ARGBFloat);
//buffer.GetTemporaryRT(blurRT2, -1, -1, 16, FilterMode.Bilinear, RenderTextureFormat.ARGBFloat);
buffer.GetTemporaryRT(blurRT2, w, h, 16, FilterMode.Bilinear, RenderTextureFormat.ARGBFloat);
buffer.GetTemporaryRT(src, -1, -1, 24, FilterMode.Bilinear, RenderTextureFormat.ARGBFloat);
buffer.SetGlobalFloat("_SoftDepthBias", softDepthBias * 0.05f * 0.2f);
buffer.Blit(BuiltinRenderTextureType.CameraTarget, blurRT2);
//buffer.Blit(BuiltinRenderTextureType.CurrentActive, sourceBuf);
buffer.Blit(BuiltinRenderTextureType.CameraTarget, src);
//multipass pass blur
for (int k = 1; k <= blurIterations; k++)
{
buffer.SetGlobalFloat("_BlurStr", Mathf.Clamp01(scatterDistance * 0.08f - k * 0.022f * scatterDistance));
buffer.SetGlobalVector("_BlurVec", new Vector4(1, 0, 0, 0));
buffer.Blit(blurRT2, blurRT1, material, 0);
buffer.SetGlobalVector("_BlurVec", new Vector4(0, 1, 0, 0));
buffer.Blit(blurRT1, blurRT2, material, 0);
buffer.SetGlobalVector("_BlurVec", new Vector4(1, 1, 0, 0).normalized);
buffer.Blit(blurRT2, blurRT1, material, 0);
buffer.SetGlobalVector("_BlurVec", new Vector4(-1, 1, 0, 0).normalized);
buffer.Blit(blurRT1, blurRT2, material, 0);
}
//buffer.Blit(blurRT2, blurBuf);
buffer.SetGlobalTexture("_BlurTex", blurRT2);
buffer.SetGlobalFloat("_EffectStr", scatterIntensity);
buffer.SetGlobalFloat("_PreserveOriginal", 1 - affectDirect);
buffer.Blit(src, BuiltinRenderTextureType.CameraTarget, material, 1);
buffer.ReleaseTemporaryRT(blurRT1);
buffer.ReleaseTemporaryRT(blurRT2);
buffer.ReleaseTemporaryRT(src);
}
void CleanupBuffer()
{
if (buffer!=null)
{
buffer.Clear();
GetComponent<Camera>().RemoveCommandBuffer(camEvent, buffer);
buffer = null;
}
}
public void AddMakeSSSMaskCommands(CommandBuffer buffer)
{
if(TargetMeshes.Count == 0) return;
for(int i = m_MaskMaterials.Count; i < TargetMeshes.Count; i++) {
if(maskShader != null) {
var material = new Material(maskShader) {
hideFlags = HideFlags.HideAndDontSave
};
m_MaskMaterials.Add(material);
}
}
if(m_MaskMaterials.Count < TargetMeshes.Count) return;
int maskRT = Shader.PropertyToID("_MaskTex");
buffer.GetTemporaryRT(maskRT, -1, -1, 24, FilterMode.Bilinear, RenderTextureFormat.ARGB32);
buffer.Blit(BuiltinRenderTextureType.None, maskRT, CopyDepthMaterial);
buffer.SetRenderTarget(maskRT);
for(int i = 0; i < TargetMeshes.Count; i++) {
var ii = TargetMeshes[i];
var maskMaterial = m_MaskMaterials[i];
maskMaterial.mainTexture = ii.sssParameter.scatteringMap;
maskMaterial.mainTextureOffset = ii.sssParameter.scatteringMapOffset;
maskMaterial.mainTextureScale = ii.sssParameter.scatteringMapScale;
maskMaterial.SetColor(scatteringColorID, ii.sssParameter.scatteringColor);
buffer.DrawRenderer(ii.renderer, maskMaterial, ii.subMeshIndex);
}
buffer.SetGlobalTexture("_MaskTex", maskRT);
}
}