-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathSEGI.shader
712 lines (497 loc) · 19.5 KB
/
SEGI.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
Shader "Hidden/SEGI" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
CGINCLUDE
#include "UnityCG.cginc"
#include "SEGI.cginc"
#pragma target 5.0
struct v2f
{
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
#if UNITY_UV_STARTS_AT_TOP
half4 uv2 : TEXCOORD1;
#endif
};
v2f vert(appdata_img v)
{
v2f o;
o.pos = UnityObjectToClipPos (v.vertex);
o.uv = float4(v.texcoord.xy, 1, 1);
#if UNITY_UV_STARTS_AT_TOP
o.uv2 = float4(v.texcoord.xy, 1, 1);
if (_MainTex_TexelSize.y < 0.0)
o.uv.y = 1.0 - o.uv.y;
#endif
return o;
}
#define PI 3.147159265
ENDCG
SubShader
{
ZTest Off
Cull Off
ZWrite Off
Fog { Mode off }
Pass //0
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4x4 CameraToWorld;
sampler2D _CameraGBufferTexture2;
int FrameSwitch;
int HalfResolution;
sampler3D SEGIVolumeTexture1;
sampler2D NoiseTexture;
float4 frag(v2f input) : SV_Target
{
#if UNITY_UV_STARTS_AT_TOP
float2 coord = input.uv2.xy;
#else
float2 coord = input.uv.xy;
#endif
//Get view space position and view vector
float4 viewSpacePosition = GetViewSpacePosition(coord);
float3 viewVector = normalize(viewSpacePosition.xyz);
//Get voxel space position
float4 voxelSpacePosition = mul(CameraToWorld, viewSpacePosition);
voxelSpacePosition = mul(SEGIWorldToVoxel, voxelSpacePosition);
voxelSpacePosition = mul(SEGIVoxelProjection, voxelSpacePosition);
voxelSpacePosition.xyz = voxelSpacePosition.xyz * 0.5 + 0.5;
//Prepare for cone trace
float2 dither = rand(coord + (float)FrameSwitch * 0.011734);
float3 worldNormal = normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0);
float3 voxelOrigin = voxelSpacePosition.xyz + worldNormal.xyz * 0.003 * ConeTraceBias * 1.25 / SEGIVoxelScaleFactor;
float3 gi = float3(0.0, 0.0, 0.0);
float4 traceResult = float4(0,0,0,0);
const float phi = 1.618033988;
const float gAngle = phi * PI * 1.0;
//Get blue noise
float2 noiseCoord = (input.uv.xy * _MainTex_TexelSize.zw) / (64.0).xx;
float blueNoise = tex2Dlod(NoiseTexture, float4(noiseCoord, 0.0, 0.0)).x;
//Trace GI cones
int numSamples = TraceDirections;
for (int i = 0; i < numSamples; i++)
{
float fi = (float)i + blueNoise * StochasticSampling;
float fiN = fi / numSamples;
float longitude = gAngle * fi;
float latitude = asin(fiN * 2.0 - 1.0);
float3 kernel;
kernel.x = cos(latitude) * cos(longitude);
kernel.z = cos(latitude) * sin(longitude);
kernel.y = sin(latitude);
kernel = normalize(kernel + worldNormal.xyz * 1.0);
traceResult += ConeTrace(voxelOrigin.xyz, kernel.xyz, worldNormal.xyz, coord, dither.y, TraceSteps, ConeSize, 1.0, 1.0);
}
traceResult /= numSamples;
gi = traceResult.rgb * 20.0;
float fadeout = saturate((distance(voxelSpacePosition.xyz, float3(0.5, 0.5, 0.5)) - 0.5f) * 5.0);
float3 fakeGI = saturate(dot(worldNormal, float3(0, 1, 0)) * 0.5 + 0.5) * SEGISkyColor.rgb * 5.0;
gi.rgb = lerp(gi.rgb, fakeGI, fadeout);
gi *= 0.75 + (float)HalfResolution * 0.25;
return float4(gi, 1.0);
}
ENDCG
}
Pass //1 Bilateral Blur
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float2 Kernel;
float DepthTolerance;
sampler2D DepthNormalsLow;
sampler2D _CameraGBufferTexture2;
sampler2D DepthLow;
int SourceScale;
float4 frag(v2f input) : COLOR0
{
float4 blurred = float4(0.0, 0.0, 0.0, 0.0);
float validWeights = 0.0;
float depth = LinearEyeDepth(tex2D(_CameraDepthTexture, input.uv.xy).x);
half3 normal = normalize(tex2D(_CameraGBufferTexture2, input.uv.xy).rgb * 2.0 - 1.0);
float thresh = 0.26;
float3 viewPosition = GetViewSpacePosition(input.uv.xy).xyz;
float3 viewVector = normalize(viewPosition);
float NdotV = 1.0 / (saturate(dot(-viewVector, normal.xyz)) + 0.1);
thresh *= 1.0 + NdotV * 2.0;
for (int i = -4; i <= 4; i++)
{
float2 offs = Kernel.xy * (i) * _MainTex_TexelSize.xy * 1.0;
float sampleDepth = LinearEyeDepth(tex2Dlod(_CameraDepthTexture, float4(input.uv.xy + offs.xy * 1, 0, 0)).x);
half3 sampleNormal = normalize(tex2Dlod(_CameraGBufferTexture2, float4(input.uv.xy + offs.xy * 1, 0, 0)).rgb * 2.0 - 1.0);
float weight = saturate(1.0 - abs(depth - sampleDepth) / thresh);
weight *= pow(saturate(dot(sampleNormal, normal)), 24.0);
float4 blurSample = tex2Dlod(_MainTex, float4(input.uv.xy + offs.xy, 0, 0)).rgba;
blurred += blurSample * weight;
validWeights += weight;
}
blurred /= validWeights + 0.001;
return blurred;
}
ENDCG
}
Pass //2 Blend with scene
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
sampler2D _CameraGBufferTexture2;
sampler2D _CameraGBufferTexture1;
sampler2D GITexture;
sampler2D Reflections;
float4x4 CameraToWorld;
int DoReflections;
int HalfResolution;
float4 frag(v2f input) : COLOR0
{
#if UNITY_UV_STARTS_AT_TOP
float2 coord = input.uv2.xy;
#else
float2 coord = input.uv.xy;
#endif
float4 albedoTex = tex2D(_CameraGBufferTexture0, input.uv.xy);
float3 albedo = albedoTex.rgb;
float3 gi = tex2D(GITexture, input.uv.xy).rgb;
float3 scene = tex2D(_MainTex, input.uv.xy).rgb;
float3 reflections = tex2D(Reflections, input.uv.xy).rgb;
float3 result = scene + gi * albedoTex.a * albedoTex.rgb;
if (DoReflections > 0)
{
float4 viewSpacePosition = GetViewSpacePosition(coord);
float3 viewVector = normalize(viewSpacePosition.xyz);
float4 worldViewVector = mul(CameraToWorld, float4(viewVector.xyz, 0.0));
float4 spec = tex2D(_CameraGBufferTexture1, coord);
float smoothness = spec.a;
float3 specularColor = spec.rgb;
float3 worldNormal = normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0);
float3 reflectionKernel = reflect(worldViewVector.xyz, worldNormal);
float3 fresnel = pow(saturate(dot(worldViewVector.xyz, reflectionKernel.xyz)) * (smoothness * 0.5 + 0.5), 5.0);
fresnel = lerp(fresnel, (1.0).xxx, specularColor.rgb);
fresnel *= saturate(smoothness * 4.0);
result = lerp(result, reflections, fresnel);
}
return float4(result, 1.0);
}
ENDCG
}
Pass //3 Temporal blend (with unity motion vectors)
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
sampler2D GITexture;
sampler2D PreviousDepth;
sampler2D CurrentDepth;
sampler2D PreviousLocalWorldPos;
float4 CameraPosition;
float4 CameraPositionPrev;
float4x4 ProjectionPrev;
float4x4 ProjectionPrevInverse;
float4x4 WorldToCameraPrev;
float4x4 CameraToWorldPrev;
float4x4 CameraToWorld;
float DeltaTime;
float BlendWeight;
float4 frag(v2f input) : COLOR0
{
float3 gi = tex2D(_MainTex, input.uv.xy).rgb;
float depth = GetDepthTexture(input.uv.xy);
float4 currentPos = float4(input.uv.x * 2.0 - 1.0, input.uv.y * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
float4 fragpos = mul(ProjectionMatrixInverse, currentPos);
fragpos = mul(CameraToWorld, fragpos);
fragpos /= fragpos.w;
float4 thisWorldPosition = fragpos;
float2 motionVectors = tex2Dlod(_CameraMotionVectorsTexture, float4(input.uv.xy, 0.0, 0.0)).xy;
float2 reprojCoord = input.uv.xy - motionVectors.xy;
float prevDepth = (tex2Dlod(PreviousDepth, float4(reprojCoord + _MainTex_TexelSize.xy * 0.0, 0.0, 0.0)).x);
#if defined(UNITY_REVERSED_Z)
prevDepth = 1.0 - prevDepth;
#endif
float4 previousWorldPosition = mul(ProjectionPrevInverse, float4(reprojCoord.xy * 2.0 - 1.0, prevDepth * 2.0 - 1.0, 1.0));
previousWorldPosition = mul(CameraToWorldPrev, previousWorldPosition);
previousWorldPosition /= previousWorldPosition.w;
float blendWeight = BlendWeight;
float posSimilarity = saturate(1.0 - distance(previousWorldPosition.xyz, thisWorldPosition.xyz) * 1.0);
blendWeight = lerp(1.0, blendWeight, posSimilarity);
float3 minPrev = float3(10000, 10000, 10000);
float3 maxPrev = float3(0, 0, 0);
float3 s0 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.5, 0.5), 0, 0)).rgb;
minPrev = s0;
maxPrev = s0;
s0 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.5, -0.5), 0, 0)).rgb;
minPrev = min(minPrev, s0);
maxPrev = max(maxPrev, s0);
s0 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(-0.5, 0.5), 0, 0)).rgb;
minPrev = min(minPrev, s0);
maxPrev = max(maxPrev, s0);
s0 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(-0.5, -0.5), 0, 0)).rgb;
minPrev = min(minPrev, s0);
maxPrev = max(maxPrev, s0);
float3 prevGI = tex2Dlod(PreviousGITexture, float4(reprojCoord, 0.0, 0.0)).rgb;
prevGI = lerp(prevGI, clamp(prevGI, minPrev, maxPrev), 0.25);
gi = lerp(prevGI, gi, float3(blendWeight, blendWeight, blendWeight));
float3 result = gi;
return float4(result, 1.0);
}
ENDCG
}
Pass //4 Specular/reflections trace
{
ZTest Always
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4x4 CameraToWorld;
sampler2D _CameraGBufferTexture1;
sampler2D _CameraGBufferTexture2;
sampler3D SEGIVolumeTexture1;
int FrameSwitch;
float4 frag(v2f input) : SV_Target
{
#if UNITY_UV_STARTS_AT_TOP
float2 coord = input.uv2.xy;
#else
float2 coord = input.uv.xy;
#endif
float4 spec = tex2D(_CameraGBufferTexture1, coord);
float4 viewSpacePosition = GetViewSpacePosition(coord);
float3 viewVector = normalize(viewSpacePosition.xyz);
float4 worldViewVector = mul(CameraToWorld, float4(viewVector.xyz, 0.0));
float4 voxelSpacePosition = mul(CameraToWorld, viewSpacePosition);
float3 worldPosition = voxelSpacePosition.xyz;
voxelSpacePosition = mul(SEGIWorldToVoxel, voxelSpacePosition);
voxelSpacePosition = mul(SEGIVoxelProjection, voxelSpacePosition);
voxelSpacePosition.xyz = voxelSpacePosition.xyz * 0.5 + 0.5;
float3 worldNormal = normalize(tex2D(_CameraGBufferTexture2, coord).rgb * 2.0 - 1.0);
float3 voxelOrigin = voxelSpacePosition.xyz + worldNormal.xyz * 0.006 * ConeTraceBias * 1.25 / SEGIVoxelScaleFactor;
float2 dither = rand(coord + (float)FrameSwitch * 0.11734);
float smoothness = spec.a * 0.5;
float3 specularColor = spec.rgb;
float4 reflection = (0.0).xxxx;
float3 reflectionKernel = reflect(worldViewVector.xyz, worldNormal);
float3 fresnel = pow(saturate(dot(worldViewVector.xyz, reflectionKernel.xyz)) * (smoothness * 0.5 + 0.5), 5.0);
fresnel = lerp(fresnel, (1.0).xxx, specularColor.rgb);
voxelOrigin += worldNormal.xyz * 0.002 * 1.25 / SEGIVoxelScaleFactor;
reflection = SpecularConeTrace(voxelOrigin.xyz, reflectionKernel.xyz, worldNormal.xyz, smoothness, coord, dither.x);
float3 skyReflection = (reflection.a * 1.0 * SEGISkyColor);
reflection.rgb = reflection.rgb * 0.7 + skyReflection.rgb * 2.4015 * SkyReflectionIntensity;
return float4(reflection.rgb, 1.0);
}
ENDCG
}
Pass //5 Get camera depth texture
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 frag(v2f input) : COLOR0
{
float2 coord = input.uv.xy;
float4 tex = tex2D(_CameraDepthTexture, coord);
return tex;
}
ENDCG
}
Pass //6 Get camera normals texture
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 frag(v2f input) : COLOR0
{
float2 coord = input.uv.xy;
float4 tex = tex2D(_CameraDepthNormalsTexture, coord);
return tex;
}
ENDCG
}
Pass //7 Visualize GI
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
sampler2D GITexture;
float4 frag(v2f input) : COLOR0
{
float4 albedoTex = tex2D(_CameraGBufferTexture0, input.uv.xy);
float3 albedo = albedoTex.rgb;
float3 gi = tex2D(GITexture, input.uv.xy).rgb;
return float4(gi, 1.0);
}
ENDCG
}
Pass //8 Write black
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 frag(v2f input) : COLOR0
{
return float4(0.0, 0.0, 0.0, 1.0);
}
ENDCG
}
Pass //9 Visualize slice of GI Volume (CURRENTLY UNUSED)
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float LayerToVisualize;
int MipLevelToVisualize;
sampler3D SEGIVolumeTexture1;
float4 frag(v2f input) : COLOR0
{
return float4(tex3D(SEGIVolumeTexture1, float3(input.uv.xy, LayerToVisualize)).rgb, 1.0);
}
ENDCG
}
Pass //10 Visualize voxels (trace through GI volumes)
{
ZTest Always
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4x4 CameraToWorld;
sampler2D _CameraGBufferTexture2;
float4 CameraPosition;
float4 frag(v2f input) : SV_Target
{
#if UNITY_UV_STARTS_AT_TOP
float2 coord = input.uv2.xy;
#else
float2 coord = input.uv.xy;
#endif
float4 viewSpacePosition = GetViewSpacePosition(coord);
float3 viewVector = normalize(viewSpacePosition.xyz);
float4 worldViewVector = mul(CameraToWorld, float4(viewVector.xyz, 0.0));
float4 voxelCameraPosition = mul(SEGIWorldToVoxel, float4(CameraPosition.xyz, 1.0));
voxelCameraPosition = mul(SEGIVoxelProjection, voxelCameraPosition);
voxelCameraPosition.xyz = voxelCameraPosition.xyz * 0.5 + 0.5;
float4 result = VisualConeTrace(voxelCameraPosition.xyz, worldViewVector.xyz);
return float4(result.rgb, 1.0);
}
ENDCG
}
Pass //11 Bilateral upsample
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float2 Kernel;
float DepthTolerance;
sampler2D DepthNormalsLow;
sampler2D DepthLow;
int SourceScale;
sampler2D CurrentDepth;
sampler2D CurrentNormal;
float4 frag(v2f input) : COLOR0
{
float4 blurred = float4(0.0, 0.0, 0.0, 0.0);
float4 blurredDumb = float4(0.0, 0.0, 0.0, 0.0);
float validWeights = 0.0;
float depth = LinearEyeDepth(tex2D(_CameraDepthTexture, input.uv.xy).x);
half3 normal = DecodeViewNormalStereo(tex2D(_CameraDepthNormalsTexture, input.uv.xy));
float thresh = 0.26;
float3 viewPosition = GetViewSpacePosition(input.uv.xy).xyz;
float3 viewVector = normalize(viewPosition);
float NdotV = 1.0 / (saturate(dot(-viewVector, normal.xyz)) + 0.1);
thresh *= 1.0 + NdotV * 2.0;
float4 sample00 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 0.0) * 1.0, 0.0, 0.0));
float4 sample10 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 0.0) * 1.0, 0.0, 0.0));
float4 sample11 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 1.0) * 1.0, 0.0, 0.0));
float4 sample01 = tex2Dlod(_MainTex, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 1.0) * 1.0, 0.0, 0.0));
float4 depthSamples = float4(0,0,0,0);
depthSamples.x = LinearEyeDepth(tex2Dlod(CurrentDepth, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 0.0), 0, 0)).x);
depthSamples.y = LinearEyeDepth(tex2Dlod(CurrentDepth, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 0.0), 0, 0)).x);
depthSamples.z = LinearEyeDepth(tex2Dlod(CurrentDepth, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 1.0), 0, 0)).x);
depthSamples.w = LinearEyeDepth(tex2Dlod(CurrentDepth, float4(input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 1.0), 0, 0)).x);
half3 normal00 = DecodeViewNormalStereo(tex2D(CurrentNormal, input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 0.0)));
half3 normal10 = DecodeViewNormalStereo(tex2D(CurrentNormal, input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 0.0)));
half3 normal11 = DecodeViewNormalStereo(tex2D(CurrentNormal, input.uv.xy + _MainTex_TexelSize.xy * float2(1.0, 1.0)));
half3 normal01 = DecodeViewNormalStereo(tex2D(CurrentNormal, input.uv.xy + _MainTex_TexelSize.xy * float2(0.0, 1.0)));
float4 depthWeights = saturate(1.0 - abs(depthSamples - depth.xxxx) / thresh);
float4 normalWeights = float4(0,0,0,0);
normalWeights.x = pow(saturate(dot(normal00, normal)), 24.0);
normalWeights.y = pow(saturate(dot(normal10, normal)), 24.0);
normalWeights.z = pow(saturate(dot(normal11, normal)), 24.0);
normalWeights.w = pow(saturate(dot(normal01, normal)), 24.0);
float4 weights = depthWeights * normalWeights;
float weightSum = dot(weights, float4(1.0, 1.0, 1.0, 1.0));
if (weightSum < 0.01)
{
weightSum = 4.0;
weights = (1.0).xxxx;
}
weights /= weightSum;
float2 fractCoord = frac(input.uv.xy * _MainTex_TexelSize.zw * 1.0);
float4 filteredX0 = lerp(sample00 * weights.x, sample10 * weights.y, fractCoord.x);
float4 filteredX1 = lerp(sample01 * weights.w, sample11 * weights.z, fractCoord.x);
float4 filtered = lerp(filteredX0, filteredX1, fractCoord.y);
return filtered * 3.0;
return blurred;
}
ENDCG
}
Pass //12 Temporal blending without motion vectors (for legacy support)
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
sampler2D GITexture;
sampler2D PreviousDepth;
sampler2D CurrentDepth;
sampler2D PreviousLocalWorldPos;
float4 CameraPosition;
float4 CameraPositionPrev;
float4x4 ProjectionPrev;
float4x4 ProjectionPrevInverse;
float4x4 WorldToCameraPrev;
float4x4 CameraToWorldPrev;
float4x4 CameraToWorld;
float DeltaTime;
float BlendWeight;
float4 frag(v2f input) : COLOR0
{
float3 gi = tex2D(_MainTex, input.uv.xy).rgb;
float2 depthLookupCoord = round(input.uv.xy * _MainTex_TexelSize.zw) * _MainTex_TexelSize.xy;
depthLookupCoord = input.uv.xy;
//float depth = tex2Dlod(_CameraDepthTexture, float4(depthLookupCoord, 0.0, 0.0)).x;
float depth = GetDepthTexture(depthLookupCoord);
float4 currentPos = float4(input.uv.x * 2.0 - 1.0, input.uv.y * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
float4 fragpos = mul(ProjectionMatrixInverse, currentPos);
float4 thisViewPos = fragpos;
fragpos = mul(CameraToWorld, fragpos);
fragpos /= fragpos.w;
float4 thisWorldPosition = fragpos;
fragpos.xyz += CameraPosition.xyz * DeltaTime;
float4 prevPos = fragpos;
prevPos.xyz -= CameraPositionPrev.xyz * DeltaTime;
prevPos = mul(WorldToCameraPrev, prevPos);
prevPos = mul(ProjectionPrev, prevPos);
prevPos /= prevPos.w;
float2 diff = currentPos.xy - prevPos.xy;
float2 reprojCoord = input.uv.xy - diff.xy * 0.5;
float2 previousTexcoord = input.uv.xy + diff.xy * 0.5;
float blendWeight = BlendWeight;
float prevDepth = (tex2Dlod(PreviousDepth, float4(reprojCoord + _MainTex_TexelSize.xy * 0.0, 0.0, 0.0)).x);
float4 previousWorldPosition = mul(ProjectionPrevInverse, float4(reprojCoord.xy * 2.0 - 1.0, prevDepth * 2.0 - 1.0, 1.0));
previousWorldPosition = mul(CameraToWorldPrev, previousWorldPosition);
previousWorldPosition /= previousWorldPosition.w;
if (distance(previousWorldPosition.xyz, thisWorldPosition.xyz) > 0.1 || reprojCoord.x > 1.0 || reprojCoord.x < 0.0 || reprojCoord.y > 1.0 || reprojCoord.y < 0.0)
{
blendWeight = 1.0;
}
float3 prevGI = tex2D(PreviousGITexture, reprojCoord).rgb;
gi = lerp(prevGI, gi, float3(blendWeight, blendWeight, blendWeight));
float3 result = gi;
return float4(result, 1.0);
}
ENDCG
}
}
Fallback off
}