-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathSEGIVoxelizeScene.shader
355 lines (270 loc) · 8.74 KB
/
SEGIVoxelizeScene.shader
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
Shader "Hidden/SEGIVoxelizeScene" {
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_EmissionColor("Color", Color) = (0,0,0)
_EmissionMap("Emission", 2D) = "white" {}
_Cutoff ("Alpha Cutoff", Range(0,1)) = 0.333
_BlockerValue ("Blocker Value", Range(0, 10)) = 0
}
SubShader
{
Cull Off
ZTest Always
Pass
{
CGPROGRAM
#pragma target 5.0
#pragma vertex vert
#pragma fragment frag
#pragma geometry geom
#include "UnityCG.cginc"
RWTexture3D<uint> RG0;
int LayerToVisualize;
float4x4 SEGIVoxelViewFront;
float4x4 SEGIVoxelViewLeft;
float4x4 SEGIVoxelViewTop;
sampler2D _MainTex;
sampler2D _EmissionMap;
float _Cutoff;
float4 _MainTex_ST;
half4 _EmissionColor;
float SEGISecondaryBounceGain;
float _BlockerValue;
struct v2g
{
float4 pos : SV_POSITION;
half4 uv : TEXCOORD0;
float3 normal : TEXCOORD1;
float angle : TEXCOORD2;
};
struct g2f
{
float4 pos : SV_POSITION;
half4 uv : TEXCOORD0;
float3 normal : TEXCOORD1;
float angle : TEXCOORD2;
};
half4 _Color;
v2g vert(appdata_full v)
{
v2g o;
UNITY_INITIALIZE_OUTPUT(v2g, o);
float4 vertex = v.vertex;
o.normal = UnityObjectToWorldNormal(v.normal);
float3 absNormal = abs(o.normal);
o.pos = vertex;
o.uv = float4(TRANSFORM_TEX(v.texcoord.xy, _MainTex), 1.0, 1.0);
return o;
}
int SEGIVoxelResolution;
[maxvertexcount(3)]
void geom(triangle v2g input[3], inout TriangleStream<g2f> triStream)
{
v2g p[3];
int i = 0;
for (i = 0; i < 3; i++)
{
p[i] = input[i];
p[i].pos = mul(unity_ObjectToWorld, p[i].pos);
}
float3 realNormal = float3(0.0, 0.0, 0.0);
float3 V = p[1].pos.xyz - p[0].pos.xyz;
float3 W = p[2].pos.xyz - p[0].pos.xyz;
realNormal.x = (V.y * W.z) - (V.z * W.y);
realNormal.y = (V.z * W.x) - (V.x * W.z);
realNormal.z = (V.x * W.y) - (V.y * W.x);
float3 absNormal = abs(realNormal);
int angle = 0;
if (absNormal.z > absNormal.y && absNormal.z > absNormal.x)
{
angle = 0;
}
else if (absNormal.x > absNormal.y && absNormal.x > absNormal.z)
{
angle = 1;
}
else if (absNormal.y > absNormal.x && absNormal.y > absNormal.z)
{
angle = 2;
}
else
{
angle = 0;
}
for (i = 0; i < 3; i ++)
{
///*
if (angle == 0)
{
p[i].pos = mul(SEGIVoxelViewFront, p[i].pos);
}
else if (angle == 1)
{
p[i].pos = mul(SEGIVoxelViewLeft, p[i].pos);
}
else
{
p[i].pos = mul(SEGIVoxelViewTop, p[i].pos);
}
p[i].pos = mul(UNITY_MATRIX_P, p[i].pos);
#if defined(UNITY_REVERSED_Z)
p[i].pos.z = 1.0 - p[i].pos.z;
#else
p[i].pos.z *= -1.0;
#endif
p[i].angle = (float)angle;
}
triStream.Append(p[0]);
triStream.Append(p[1]);
triStream.Append(p[2]);
}
float3 rgb2hsv(float3 c)
{
float4 k = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
float4 p = lerp(float4(c.bg, k.wz), float4(c.gb, k.xy), step(c.b, c.g));
float4 q = lerp(float4(p.xyw, c.r), float4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
float3 hsv2rgb(float3 c)
{
float4 k = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
float3 p = abs(frac(c.xxx + k.xyz) * 6.0 - k.www);
return c.z * lerp(k.xxx, saturate(p - k.xxx), c.y);
}
float4 DecodeRGBAuint(uint value)
{
uint ai = value & 0x0000007F;
uint vi = (value / 0x00000080) & 0x000007FF;
uint si = (value / 0x00040000) & 0x0000007F;
uint hi = value / 0x02000000;
float h = float(hi) / 127.0;
float s = float(si) / 127.0;
float v = (float(vi) / 2047.0) * 10.0;
float a = ai * 2.0;
v = pow(v, 3.0);
float3 color = hsv2rgb(float3(h, s, v));
return float4(color.rgb, a);
}
uint EncodeRGBAuint(float4 color)
{
//7[HHHHHHH] 7[SSSSSSS] 11[VVVVVVVVVVV] 7[AAAAAAAA]
float3 hsv = rgb2hsv(color.rgb);
hsv.z = pow(hsv.z, 1.0 / 3.0);
uint result = 0;
uint a = min(127, uint(color.a / 2.0));
uint v = min(2047, uint((hsv.z / 10.0) * 2047));
uint s = uint(hsv.y * 127);
uint h = uint(hsv.x * 127);
result += a;
result += v * 0x00000080; // << 7
result += s * 0x00040000; // << 18
result += h * 0x02000000; // << 25
return result;
}
void interlockedAddFloat4(RWTexture3D<uint> destination, int3 coord, float4 value)
{
uint writeValue = EncodeRGBAuint(value);
uint compareValue = 0;
uint originalValue;
[allow_uav_condition]
while (true)
{
InterlockedCompareExchange(destination[coord], compareValue, writeValue, originalValue);
if (compareValue == originalValue)
break;
compareValue = originalValue;
float4 originalValueFloats = DecodeRGBAuint(originalValue);
writeValue = EncodeRGBAuint(originalValueFloats + value);
}
}
void interlockedAddFloat4b(RWTexture3D<uint> destination, int3 coord, float4 value)
{
uint writeValue = EncodeRGBAuint(value);
uint compareValue = 0;
uint originalValue;
[allow_uav_condition]
while (true)
{
InterlockedCompareExchange(destination[coord], compareValue, writeValue, originalValue);
if (compareValue == originalValue)
break;
compareValue = originalValue;
float4 originalValueFloats = DecodeRGBAuint(originalValue);
writeValue = EncodeRGBAuint(originalValueFloats + value);
}
}
float4x4 SEGIVoxelToGIProjection;
float4x4 SEGIVoxelProjectionInverse;
sampler2D SEGISunDepth;
float4 SEGISunlightVector;
float4 GISunColor;
float4 SEGIVoxelSpaceOriginDelta;
sampler3D SEGIVolumeTexture1;
int SEGIInnerOcclusionLayers;
#define VoxelResolution (SEGIVoxelResolution * (1 + SEGIVoxelAA))
int SEGIVoxelAA;
float4 frag (g2f input) : SV_TARGET
{
int3 coord = int3((int)(input.pos.x), (int)(input.pos.y), (int)(input.pos.z * VoxelResolution));
float3 absNormal = abs(input.normal);
int angle = 0;
angle = (int)input.angle;
if (angle == 1)
{
coord.xyz = coord.zyx;
coord.z = VoxelResolution - coord.z - 1;
}
else if (angle == 2)
{
coord.xyz = coord.xzy;
coord.y = VoxelResolution - coord.y - 1;
}
float3 fcoord = (float3)(coord.xyz) / VoxelResolution;
float4 shadowPos = mul(SEGIVoxelProjectionInverse, float4(fcoord * 2.0 - 1.0, 0.0));
shadowPos = mul(SEGIVoxelToGIProjection, shadowPos);
shadowPos.xyz = shadowPos.xyz * 0.5 + 0.5;
float sunDepth = tex2Dlod(SEGISunDepth, float4(shadowPos.xy, 0, 0)).x;
#if defined(UNITY_REVERSED_Z)
sunDepth = 1.0 - sunDepth;
#endif
float sunVisibility = saturate((sunDepth - shadowPos.z + 0.2525) * 1000.0);
float sunNdotL = saturate(dot(input.normal, -SEGISunlightVector.xyz));
float4 tex = tex2D(_MainTex, input.uv.xy);
float4 emissionTex = tex2D(_EmissionMap, input.uv.xy);
float4 color = _Color;
if (length(_Color.rgb) < 0.0001)
{
color.rgb = float3(1, 1, 1);
}
float3 col = sunVisibility.xxx * sunNdotL * color.rgb * tex.rgb * GISunColor.rgb * GISunColor.a + _EmissionColor.rgb * 0.9 * emissionTex.rgb;
float4 prevBounce = tex3D(SEGIVolumeTexture1, fcoord + SEGIVoxelSpaceOriginDelta.xyz);
col.rgb += prevBounce.rgb * 1.6 * SEGISecondaryBounceGain * tex.rgb * color.rgb;
float4 result = float4(col.rgb, 2.0);
const float sqrt2 = sqrt(2.0) * 1.0;
coord /= (uint)SEGIVoxelAA + 1u;
if (_BlockerValue > 0.01)
{
result.a += 20.0;
result.a += _BlockerValue;
result.rgb = float3(0.0, 0.0, 0.0);
}
interlockedAddFloat4(RG0, coord, result);
if (SEGIInnerOcclusionLayers > 0)
{
interlockedAddFloat4b(RG0, coord - int3((int)(input.normal.x * sqrt2 * 1.0), (int)(input.normal.y * sqrt2 * 1.0), (int)(input.normal.z * sqrt2 * 1.0)), float4(0.0, 0.0, 0.0, 8.0));
}
if (SEGIInnerOcclusionLayers > 1)
{
interlockedAddFloat4b(RG0, coord - int3((int)(input.normal.x * sqrt2 * 2.0), (int)(input.normal.y * sqrt2 * 2.0), (int)(input.normal.z * sqrt2 * 2.0)), float4(0.0, 0.0, 0.0, 22.0));
}
return float4(0.0, 0.0, 0.0, 0.0);
}
ENDCG
}
}
FallBack Off
}