@threepipe/webgi-plugins 0.6.4 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/CHANGELOG.md +99 -0
  2. package/LICENSE +133 -16
  3. package/README.md +29 -26
  4. package/THIRD_PARTY_NOTICES.md +243 -0
  5. package/dist/dependencies.txt +1 -1
  6. package/dist/index.d.ts +1 -1
  7. package/dist/index.js +3907 -18
  8. package/dist/index.js.map +1 -1
  9. package/dist/index.mjs +3824 -18
  10. package/dist/index.mjs.map +1 -1
  11. package/dist/passes/BilateralFilterPass.d.ts +1 -1
  12. package/dist/passes/BilateralFilterPass.d.ts.map +1 -1
  13. package/dist/plugins/buffer/VelocityBufferPlugin.d.ts.map +1 -1
  14. package/dist/plugins/extras/AdvancedGroundPlugin.d.ts +4 -3
  15. package/dist/plugins/extras/AdvancedGroundPlugin.d.ts.map +1 -1
  16. package/dist/plugins/extras/AnisotropyPlugin.d.ts +5 -4
  17. package/dist/plugins/extras/AnisotropyPlugin.d.ts.map +1 -1
  18. package/dist/plugins/extras/WatchHandsPlugin.d.ts +1 -1
  19. package/dist/plugins/extras/WatchHandsPlugin.d.ts.map +1 -1
  20. package/dist/plugins/index.d.ts +23 -23
  21. package/dist/plugins/postprocessing/BloomPlugin.d.ts.map +1 -1
  22. package/dist/plugins/postprocessing/DepthOfFieldPlugin.d.ts.map +1 -1
  23. package/dist/plugins/postprocessing/OutlinePlugin.d.ts +1 -1
  24. package/dist/plugins/postprocessing/OutlinePlugin.d.ts.map +1 -1
  25. package/dist/plugins/postprocessing/SSContactShadowsPlugin.d.ts.map +1 -1
  26. package/dist/plugins/postprocessing/SSGIPlugin.d.ts +3 -2
  27. package/dist/plugins/postprocessing/SSGIPlugin.d.ts.map +1 -1
  28. package/dist/plugins/postprocessing/SSReflectionPlugin.d.ts +1 -1
  29. package/dist/plugins/postprocessing/SSReflectionPlugin.d.ts.map +1 -1
  30. package/dist/plugins/postprocessing/TemporalAAPlugin.d.ts.map +1 -1
  31. package/dist/utils/FSShadowMaterial.d.ts.map +1 -1
  32. package/dist/utils/RandomizedDirectionalLight.d.ts.map +1 -1
  33. package/dist/utils/Reflector2.d.ts +4 -4
  34. package/dist/utils/Reflector2.d.ts.map +1 -1
  35. package/dist/utils/ShadowMapBaker.d.ts +1 -1
  36. package/dist/utils/ShadowMapBaker.d.ts.map +1 -1
  37. package/docs/.nojekyll +1 -0
  38. package/docs/assets/hierarchy.js +1 -0
  39. package/docs/assets/highlight.css +22 -0
  40. package/docs/assets/icons.js +18 -0
  41. package/docs/assets/icons.svg +1 -0
  42. package/docs/assets/main.js +60 -0
  43. package/docs/assets/navigation.js +1 -0
  44. package/docs/assets/search.js +1 -0
  45. package/docs/assets/style.css +1648 -0
  46. package/docs/classes/AdvancedGroundPlugin.html +736 -0
  47. package/docs/classes/AnisotropyPlugin.html +432 -0
  48. package/docs/classes/BloomPlugin.html +466 -0
  49. package/docs/classes/BloomPluginPass.html +323 -0
  50. package/docs/classes/DepthOfFieldPlugin.html +493 -0
  51. package/docs/classes/DepthOfFieldPluginPass.html +340 -0
  52. package/docs/classes/OutlinePlugin.html +696 -0
  53. package/docs/classes/OutlineRenderPass.html +252 -0
  54. package/docs/classes/SSContactShadowsPlugin.html +421 -0
  55. package/docs/classes/SSGIPlugin.html +488 -0
  56. package/docs/classes/SSGIPluginPass.html +404 -0
  57. package/docs/classes/SSReflectionPlugin.html +495 -0
  58. package/docs/classes/SSReflectionPluginPass.html +410 -0
  59. package/docs/classes/SSVelocityMaterial.html +110 -0
  60. package/docs/classes/TemporalAAPlugin.html +447 -0
  61. package/docs/classes/TemporalAAPluginPass.html +337 -0
  62. package/docs/classes/VelocityBufferPlugin.html +480 -0
  63. package/docs/classes/WatchHandsPlugin.html +443 -0
  64. package/docs/hierarchy.html +28 -0
  65. package/docs/index.html +38 -0
  66. package/docs/types/OutlinePluginPass.html +32 -0
  67. package/docs/types/OutlinePluginTarget.html +32 -0
  68. package/docs/types/SSGIPluginTarget.html +32 -0
  69. package/docs/types/SSReflectionPluginTarget.html +32 -0
  70. package/docs/types/VelocityBufferPluginPass.html +32 -0
  71. package/docs/types/VelocityBufferPluginTarget.html +32 -0
  72. package/docs/variables/anisotropyGLTFExtension.html +43 -0
  73. package/docs/variables/webgiPlugins.html +32 -0
  74. package/package.json +23 -22
  75. package/src/global.d.ts +27 -0
  76. package/src/index.ts +1 -0
  77. package/src/passes/BilateralFilterPass.ts +62 -0
  78. package/src/passes/shaders/ssaoBilateral.glsl +89 -0
  79. package/src/plugins/buffer/VelocityBufferPlugin.ts +270 -0
  80. package/src/plugins/buffer/shaders/VelocityBufferPlugin.mat.frag.glsl +46 -0
  81. package/src/plugins/buffer/shaders/VelocityBufferPlugin.mat.vert.glsl +51 -0
  82. package/src/plugins/buffer/shaders/VelocityBufferPlugin.unpack.glsl +8 -0
  83. package/src/plugins/extras/AdvancedGroundPlugin.ts +385 -0
  84. package/src/plugins/extras/AnisotropyPlugin.ts +519 -0
  85. package/src/plugins/extras/WatchHandsPlugin.ts +156 -0
  86. package/src/plugins/extras/shaders/anisotropyBsdf.glsl +64 -0
  87. package/src/plugins/extras/shaders/anisotropyTBN.glsl +35 -0
  88. package/src/plugins/index.ts +38 -0
  89. package/src/plugins/postprocessing/BloomPlugin.ts +377 -0
  90. package/src/plugins/postprocessing/DepthOfFieldPlugin.ts +470 -0
  91. package/src/plugins/postprocessing/OutlinePlugin.ts +476 -0
  92. package/src/plugins/postprocessing/SSContactShadowsPlugin.ts +374 -0
  93. package/src/plugins/postprocessing/SSGIPlugin.ts +597 -0
  94. package/src/plugins/postprocessing/SSReflectionPlugin.ts +563 -0
  95. package/src/plugins/postprocessing/TemporalAAPlugin.ts +301 -0
  96. package/src/plugins/postprocessing/shaders/dofBlurBox.glsl +46 -0
  97. package/src/plugins/postprocessing/shaders/dofCombine.glsl +68 -0
  98. package/src/plugins/postprocessing/shaders/dofComputeCoC.glsl +21 -0
  99. package/src/plugins/postprocessing/shaders/dofExpandCoC.glsl +56 -0
  100. package/src/plugins/postprocessing/shaders/dofPoissonBox.glsl +90 -0
  101. package/src/plugins/postprocessing/shaders/giPatch.glsl +53 -0
  102. package/src/plugins/postprocessing/shaders/hdrBloom.glsl +101 -0
  103. package/src/plugins/postprocessing/shaders/outline.glsl +60 -0
  104. package/src/plugins/postprocessing/shaders/outlineDepthFrag.glsl +22 -0
  105. package/src/plugins/postprocessing/shaders/outlineDepthVert.glsl +26 -0
  106. package/src/plugins/postprocessing/shaders/poissonDiskSamples.glsl +19 -0
  107. package/src/plugins/postprocessing/shaders/samplePointHelpers.glsl +3 -0
  108. package/src/plugins/postprocessing/shaders/ssrPatch.glsl +145 -0
  109. package/src/plugins/postprocessing/shaders/ssreflectionMain.glsl +69 -0
  110. package/src/plugins/postprocessing/shaders/ssrt.glsl +118 -0
  111. package/src/utils/FSShadowMaterial.ts +65 -0
  112. package/src/utils/RandomizedDirectionalLight.ts +288 -0
  113. package/src/utils/Reflector2.ts +309 -0
  114. package/src/utils/ShadowMapBaker.ts +527 -0
  115. package/src/utils/shaders/computeScreenSpaceVelocity.glsl +16 -0
  116. package/src/utils/shaders/getWorldPositionFromViewZ.glsl +9 -0
  117. package/src/utils/shaders/poissonDiskSamples.glsl +19 -0
  118. package/src/utils/shaders/reflectorSample.glsl +54 -0
  119. package/src/utils/shaders/seperableBlurShaderMasked.glsl +28 -0
  120. package/src/utils/shaders/seperableShadowBlur.glsl +21 -0
  121. package/src/utils/shaders/ssreflection.glsl +141 -0
  122. package/src/utils/shaders/ssrtao.glsl +275 -0
  123. package/src/utils/shaders/temporalaa.glsl +233 -0
  124. package/src/utils/shaders.ts +24 -0
  125. package/tsconfig.json +40 -44
  126. package/dist/version.d.ts +0 -2
  127. package/dist/version.d.ts.map +0 -1
@@ -0,0 +1,141 @@
1
+
2
+ //https://web.archive.org/web/20170808071110/http://graphics.cs.williams.edu/papers/AlchemyHPG11/AlchemyHPG2011-present.pdf
3
+
4
+ //uniform float opacity;
5
+ //uniform sampler2D tDiffuse;
6
+ //uniform sampler2D tLastFrame;
7
+ uniform float currentFrameCount;
8
+ #ifndef D_frameCount
9
+ #define D_frameCount
10
+ uniform float frameCount;
11
+ #endif
12
+ uniform float objectRadius;
13
+ uniform float radius;
14
+ //uniform float power;
15
+ //uniform float bias;
16
+ //uniform float falloff;
17
+ uniform float tolerance;
18
+ uniform float ssrRoughnessFactor;
19
+ uniform bool autoRadius;
20
+ //uniform bool giEnabled;
21
+
22
+ #ifndef D_sceneBoundingRadius
23
+ #define D_sceneBoundingRadius
24
+ uniform float sceneBoundingRadius;
25
+ #endif
26
+
27
+ #if SSREFL_ENABLED == 2 // split mode
28
+ uniform float ssrSplitX;
29
+ #endif
30
+
31
+ #ifdef HAS_VELOCITY_BUFFER
32
+ #pragma <velocity_unpack>
33
+ #else
34
+ #define HAS_VELOCITY_BUFFER 0
35
+ #endif
36
+ #if HAS_VELOCITY_BUFFER == 0
37
+ #include <computeScreenSpaceVelocity>
38
+ #include <getWorldPositionFromViewZ>
39
+ #endif
40
+
41
+ vec3 ComputeReflectionL(vec3 N, vec2 E, vec3 V, float rough){
42
+ // vec3 L;
43
+ // L = reflect(normalize(V), N);
44
+ // return L;
45
+
46
+ float rough4 = rough *rough *rough *rough;
47
+ // importance sampling
48
+ float phi = 2.0 * PI * E.x;
49
+ // float cos_theta = sqrt((1.0 - E.y) / (1.0 + (rough4 - 1.0) * E.y)); // ggx, NOISY
50
+ float cos_theta = pow(max(E.y, 0.000001), rough4 / (2.0 - rough4));// blinn
51
+ float sin_theta = sqrt(max(0., 1.0 - cos_theta * cos_theta));
52
+ vec3 half_vec = vec3(sin_theta * cos(phi), sin_theta * sin(phi), cos_theta);
53
+ vec3 tangentX = normalize(cross(abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0), N));
54
+ vec3 tangentY = cross(N, tangentX);
55
+ half_vec = half_vec.x * tangentX + half_vec.y * tangentY + half_vec.z * N;
56
+
57
+ // in view space
58
+ vec3 ray_dir = (2.0 * dot(V, half_vec)) * half_vec - V;
59
+ // ray_dir = normalize(ray_dir);
60
+
61
+ return ray_dir;
62
+
63
+ }
64
+
65
+ vec2 GetRandomE(float seed){
66
+
67
+ vec2 rand_e;// random
68
+ rand_e.x = interleavedGradientNoise(gl_FragCoord.xy, frameCount*34. + seed);
69
+ rand_e.y = fract(rand_e.x * 38.65435);
70
+ // https://www.slideshare.net/DICEStudio/stochastic-screenspace-reflections#p=67
71
+ rand_e.y = mix(rand_e.y, 1.0, 0.7);
72
+ return rand_e;
73
+
74
+ }
75
+
76
+ vec4 calculateSSR(in float seed, in vec3 screenPos, in vec3 normal, in float radiusFactor, in float roughness){
77
+
78
+ // if(roughness > 0.9) return vec4(0.);
79
+
80
+ vec3 viewPos = screenToView3(screenPos.xy, screenPos.z);
81
+ normal = normalize(normal);
82
+ vec2 E = GetRandomE(seed);
83
+ vec3 L = ComputeReflectionL(normal, E, -normalize(viewPos), roughness * ssrRoughnessFactor);
84
+
85
+ L = normalize(L);
86
+ // L *= sign(dot(L, normal));
87
+
88
+ float cameraDist = length(cameraPositionWorld);
89
+
90
+ float rayLen = objectRadius*sceneBoundingRadius;
91
+ rayLen = autoRadius ?
92
+ // length(viewPos - screenToView3(screenPos.xy + objectRadius/10., screenPos.z)):
93
+ // mix((cameraNearFar.y) + viewPos.z, -viewPos.z - cameraNearFar.x, L.z * 0.5 + 0.5)*objectRadius:
94
+ min(max(mix(
95
+ max(0.0, (cameraDist + rayLen) + viewPos.z),
96
+ max(0.0, -viewPos.z - max(0.0, cameraDist - rayLen)),
97
+ L.z * 0.5 + 0.5), rayLen *0.1), rayLen*5.) :
98
+ rayLen;
99
+ // rayLen = mix((cameraDist + objectRadius) + viewPos.z, -viewPos.z - (cameraDist - objectRadius), L.z * 0.5 + 0.5);
100
+
101
+ rayLen *= radiusFactor;
102
+ // rayLen = 10.;
103
+
104
+ float r = interleavedGradientNoise(gl_FragCoord.xy, frameCount + seed);
105
+
106
+ // jitter rayLen
107
+ // rayLen = rayLen * (.75 + 0.5 * (random(r)));//mix(rayLen*(1.-.5), rayLen*(1.+.5), random(r));
108
+
109
+ rayLen = max(rayLen, 0.001);
110
+
111
+ int steps = SSR_STEP_COUNT / (currentFrameCount < float(SSR_LOW_QUALITY_FRAMES) ? 2 : 1);
112
+ vec3 state = vec3(0.,(r+0.5)/float(steps),2.);
113
+ viewPos += normal * max(-0.0001*viewPos.z, 0.001);
114
+
115
+ vec3 screenHitP = traceRay(viewPos, L * rayLen, tolerance * rayLen, state, steps);
116
+ // return vec4(screenHitP.x);
117
+ // return vec4(-viewPos.z,0,0,1);
118
+ if(state.z < 0.99){
119
+
120
+ if(currentFrameCount < 1.){
121
+ #if HAS_VELOCITY_BUFFER == 0
122
+ vec3 worldPosition = getWorldPositionFromViewZ(screenHitP.xy, screenHitP.z);
123
+ vec2 screenSpaceVelocity = computeScreenSpaceVelocity(worldPosition);
124
+ #else
125
+ vec2 screenSpaceVelocity = getVelocity(screenHitP.xy);
126
+ #endif
127
+ screenHitP.xy -= screenSpaceVelocity;
128
+ }
129
+
130
+ vec3 hitColor = (tLastFrameTexelToLinear(texture2D( tLastFrame , screenHitP.xy))).rgb;
131
+ // vec3 hitColor = tDiffuseTexelToLinear(texture2D( tDiffuse, screenHitP.xy )).rgb;
132
+ // vec3 hitNormal = getViewNormal(screenHitP.xy);
133
+
134
+ float ssrWeight = 1.;
135
+
136
+ return vec4(hitColor*ssrWeight, 1.);
137
+ }
138
+
139
+ return vec4(0.);
140
+ }
141
+
@@ -0,0 +1,275 @@
1
+
2
+ //https://web.archive.org/web/20170808071110/http://graphics.cs.williams.edu/papers/AlchemyHPG11/AlchemyHPG2011-present.pdf
3
+
4
+ //uniform float opacity;
5
+ //varying vec2 vUv;
6
+ //#ifndef D_frameCount
7
+ //#define D_frameCount
8
+ //uniform float frameCount;
9
+ //#endif
10
+ uniform float currentFrameCount;
11
+ uniform float intensity;
12
+ uniform float objectRadius;
13
+ //uniform float radius;
14
+ uniform float rayCount;
15
+ uniform float power;
16
+ uniform float bias;
17
+ uniform float falloff;
18
+ uniform float tolerance;
19
+ uniform bool autoRadius;
20
+
21
+ uniform vec2 screenSize;
22
+
23
+ #ifndef D_sceneBoundingRadius
24
+ #define D_sceneBoundingRadius
25
+ uniform float sceneBoundingRadius;
26
+ #endif
27
+
28
+ #ifdef HAS_VELOCITY_BUFFER
29
+ #pragma <velocity_unpack>
30
+ #else
31
+ #define HAS_VELOCITY_BUFFER 0
32
+ #endif
33
+ #if HAS_VELOCITY_BUFFER == 0
34
+ #include <computeScreenSpaceVelocity>
35
+ #include <getWorldPositionFromViewZ>
36
+ #endif
37
+
38
+ vec3 ComputeUniformL(vec3 N, vec2 E){
39
+ vec3 L;
40
+ L.xy = E;
41
+ L.z = interleavedGradientNoise(gl_FragCoord.xy, currentFrameCount*5.);
42
+ L = L * 2. - 1.;
43
+ return L;
44
+ }
45
+
46
+ vec2 GetRandomE(float seed){
47
+ vec2 rand_e;
48
+ rand_e.x = random3(vec3(gl_FragCoord.xy, currentFrameCount + seed));
49
+ rand_e.y = random3(vec3(gl_FragCoord.yx, rand_e.x + (currentFrameCount)*7.));
50
+ return rand_e;
51
+ }
52
+
53
+ float saturate2(float v, float mx){
54
+ return max(0., min(mx, v));
55
+ }
56
+
57
+ vec4 calculateGI(in float seed, in vec3 screenPos, in vec3 normal, in float radiusFactor){
58
+
59
+ vec3 viewPos = screenToView3(screenPos.xy, screenPos.z);
60
+
61
+ normal = normalize(normal);
62
+ vec2 E = GetRandomE(seed);
63
+ vec3 L = ComputeUniformL(normal, E);
64
+
65
+ L = normalize(L);
66
+ L *= sign(dot(L, normal));
67
+
68
+ float cameraDist = length(cameraPositionWorld);
69
+
70
+ // float rayLen = autoRadius ?
71
+ // length(viewPos - screenToView3(screenPos.xy + objectRadius/10., screenPos.z)):
72
+ // mix((cameraNearFar.y) + viewPos.z, -viewPos.z - cameraNearFar.x, L.z * 0.5 + 0.5)*objectRadius;
73
+
74
+ float rayLen = objectRadius*sceneBoundingRadius;
75
+ rayLen = autoRadius ?
76
+ // length(viewPos - screenToView3(screenPos.xy + objectRadius/10., screenPos.z)):
77
+ // mix((cameraNearFar.y) + viewPos.z, -viewPos.z - cameraNearFar.x, L.z * 0.5 + 0.5)*objectRadius:
78
+ min(max(mix(
79
+ max(0.0, (cameraDist + rayLen) + viewPos.z),
80
+ max(0.0, -viewPos.z - max(0.0, cameraDist - rayLen)),
81
+ L.z * 0.5 + 0.5), rayLen *0.1), rayLen*5.) :
82
+ rayLen;
83
+
84
+ // rayLen = min(-viewPos.z, rayLen);
85
+ // rayLen = mix((cameraDist + objectRadius) + viewPos.z, -viewPos.z - (cameraDist - objectRadius), L.z * 0.5 + 0.5);
86
+
87
+ rayLen *= radiusFactor;
88
+
89
+ // float r = interleavedGradientNoise(gl_FragCoord.xy, currentFrameCount*14. + seed) + 0.05;
90
+ float r = interleavedGradientNoise(gl_FragCoord.xy, currentFrameCount*14. + seed) + 0.05;
91
+
92
+ // jitter rayLen
93
+ // rayLen = rayLen * (.75 + 0.5 * (random(r)));//mix(rayLen*(1.-.5), rayLen*(1.+.5), random(r));
94
+
95
+ rayLen = max(rayLen, 0.001);
96
+
97
+ vec3 state = vec3(1.,(r+0.5)/float(RTAO_STEP_COUNT),2.);
98
+ viewPos += normal * max(-0.01*viewPos.z, 0.001);
99
+ vec3 screenHitP = traceRay(viewPos, L * rayLen, tolerance * rayLen, state, RTAO_STEP_COUNT);
100
+
101
+ vec3 viewHitP = screenToView3(screenHitP.xy, screenHitP.z);
102
+ vec3 LRes = viewHitP - viewPos;
103
+ if(state.z > 1.) LRes = vec3(9999999.);
104
+ float dist = length(LRes) * falloff;
105
+
106
+ float EPS = 0.01;
107
+ float zBias = (viewPos.z) * bias;
108
+ float ao = (max(dot(normal, L) + zBias, 0.)) / (dist*dist + EPS);
109
+
110
+ #if defined(SSGI_ENABLED) && SSGI_ENABLED > 0
111
+
112
+ if(currentFrameCount < 1.){
113
+ #if HAS_VELOCITY_BUFFER == 0
114
+ vec3 worldPosition = getWorldPositionFromViewZ(screenHitP.xy, screenHitP.z);
115
+ vec2 screenSpaceVelocity = computeScreenSpaceVelocity(worldPosition);
116
+ #else
117
+ vec2 screenSpaceVelocity = getVelocity(screenHitP.xy);
118
+ #endif
119
+ screenHitP.xy -= screenSpaceVelocity;
120
+ }
121
+
122
+ vec3 hitColor = tLastFrameTexelToLinear(texture2D(tLastFrame, screenHitP.xy)).rgb;
123
+ // vec3 hitColor = tDiffuseTexelToLinear(texture2D( tDiffuse, screenHitP.xy )).rgb;
124
+ vec3 hitNormal = getViewNormal(screenHitP.xy);
125
+ float giWeight = 1.;
126
+ giWeight = saturate2(giWeight / (dist+EPS), 1.);
127
+ giWeight *= saturate2((dot(normal, L)), 1.0);
128
+ giWeight *= saturate2((dot(hitNormal, -L)), 1.0);
129
+ // giWeight *= saturate2((1.-dot(hitNormal, normal) ), 1.1);
130
+
131
+ return vec4(hitColor*giWeight, ao);
132
+ #endif
133
+
134
+ return vec4(0,0,0,ao);
135
+ }
136
+
137
+
138
+ float normpdf(in float x, in float sigma)
139
+ {
140
+ return exp(-0.5*x*x/(sigma*sigma));
141
+ }
142
+ //float normpdf(in float x, in float sigma)
143
+ //{
144
+ // return 0.39894*exp(-0.5*x*x/(sigma*sigma))/sigma;
145
+ //}
146
+ //
147
+
148
+
149
+ //uniform bool smoothEnabled; // last frame bilateral denoise
150
+ //uniform vec4 smoothSigma; // color, depth, pixel, normal
151
+ //uniform vec4 smoothScale; // color, depth, pixel, normal
152
+ //#define BILATERAL_KERNEL 2
153
+
154
+ //uniform vec4 smoothModes; // depthScale, 0, 0,
155
+ vec4 getLastThis(sampler2D tex, float depth, vec3 normal){
156
+ // vec4 smoothSigma = vec4(5,10,2,2);
157
+ vec2 direction = vec2(1,1);
158
+
159
+ vec4 color = clamp(tLastThisTexelToLinear(texture2D(tex, vUv.xy)), 0., 5.);
160
+
161
+ // float depth;
162
+ // vec3 normal;
163
+ // getDepthNormal(vUv, depth, normal);
164
+
165
+ // if(!smoothEnabled || frameCount < 60. || vUv.x > 0.5)
166
+ return color;
167
+
168
+ // direction *= vec2(int(frameCount)%2, int(frameCount+1.)%2);
169
+
170
+ // float Z = 1.0;
171
+ // vec4 final_colour = Z * color;
172
+ // float factor;
173
+ // vec2 nuv;
174
+ // vec4 cc, np; float dp; vec3 nor; // cc is color, np is position, dp is depth, nor is normal
175
+ // direction /= screenSize.xy;
176
+ //
177
+ // // -1, 1, -2, 2, -3, 3
178
+ // for (int i = 0; i < BILATERAL_KERNEL; ++i)
179
+ // {
180
+ //
181
+ // direction *= -1.;
182
+ // nuv = vUv + direction * float( i/2 + 1 ); // clamp to screen border
183
+ // getDepthNormal(nuv, dp, nor);
184
+ // if(dp > 0.99) continue;
185
+ //
186
+ // cc = clamp(texture2D(tex, nuv), 0., 5.); // clamp(texsample(tDiffuse, float(i), float(j)), 0., 1.);
187
+ //
188
+ // factor = 1.;
189
+ //
190
+ // factor *= normpdf( length(cc-color) * smoothScale.x, smoothSigma.x); //color
191
+ // factor *= normpdf( sqrt(abs(dp-depth)) * smoothScale.y, smoothSigma.y); //depth
192
+ // factor *= normpdf( float( i/2 + 1 ) * smoothScale.z, smoothSigma.z); //pixel distance
193
+ // factor *= normpdf( (1.-dot(normal, nor)) * smoothScale.w, smoothSigma.w); //normal
194
+ //// factor *= normpdf(sqrt(length(np-pos))*smoothScale.y, smoothSigma.y); // position.
195
+ //
196
+ // Z += factor;
197
+ // final_colour += factor*cc;
198
+ //
199
+ // }
200
+ //
201
+ // final_colour /= Z;
202
+ // return final_colour;
203
+ }
204
+
205
+ void main() {
206
+
207
+ // vec4 texel = texture2D( tDiffuse, vUv );
208
+ float depth;
209
+ vec3 normal;
210
+ // float alpha = opacity;
211
+ getDepthNormal(vUv, depth, normal);
212
+
213
+ if (depth > 0.99) {
214
+ discard;
215
+
216
+ gl_FragColor = getLastThis(tLastThis, depth, normal);
217
+
218
+ return;
219
+ }
220
+
221
+ float viewZ = depthToViewZ(depth);
222
+ vec3 screenPos = vec3(vUv.x, vUv.y, viewZ);
223
+
224
+ vec4 gi = vec4(0.);
225
+ // screenPos.z += 0.001;
226
+ gi += calculateGI(8., screenPos, normal, 1.);
227
+ if(rayCount > 1.5)
228
+ gi = max(gi, calculateGI(2., screenPos, normal, 0.4));
229
+ if(rayCount > 2.5)
230
+ gi = max(gi, calculateGI(3., screenPos, normal, 1.5));
231
+ if(rayCount > 3.5)
232
+ gi = max(gi, calculateGI(1., screenPos, normal, 0.6));
233
+ if(rayCount > 4.5)
234
+ gi = max(gi, calculateGI(3., screenPos, normal, 1.));
235
+
236
+ // gi += calculateGI(3., screenPos, normal, 1.);
237
+ // gi += calculateGI(4., screenPos, normal, 1.);
238
+ // gi += calculateGI(5., screenPos, normal, 1.);
239
+ // gi += calculateGI(6., screenPos, normal, 1.);
240
+ // gi += calculateGI(7., screenPos, normal, 1.);
241
+ // gi = gi / 6.;
242
+ // gi = gi / 3.;
243
+
244
+ // gl_FragColor = vec4(texel) * (1.-ao);
245
+
246
+ gi.a = min(1., gi.a);
247
+ gi.a = max(0., gi.a);
248
+ // gi = ao;
249
+ // gi *= intensity/1.;
250
+
251
+ gi.rgb = min(vec3(3.), gi.rgb);
252
+ gi.rgb = max(vec3(0.), gi.rgb);
253
+
254
+ if(currentFrameCount < 3.){
255
+ gl_FragColor = gi;
256
+ return;
257
+ }
258
+
259
+ gl_FragColor = (texture2D( tLastThis, vUv ));
260
+ // gl_FragColor = getLastThis(tLastThis, depth, normal);
261
+
262
+ // if(gi.a < 0.001){
263
+ // gl_FragColor.rgb = gl_FragColor.rgb;
264
+ // gl_FragColor.a = (((gl_FragColor.a) * frameCount)/(frameCount+1.));
265
+ // }else {
266
+ // gl_FragColor = ((gi + (gl_FragColor) * frameCount)/(frameCount+1.));
267
+ // }
268
+
269
+ gl_FragColor = ((gi + (gl_FragColor) * currentFrameCount)/(currentFrameCount+1.));
270
+
271
+ // todo: encodings??
272
+
273
+ // #include <colorspace_fragment>
274
+
275
+ }
@@ -0,0 +1,233 @@
1
+ #include <common>
2
+ #include <gbuffer_unpack>
3
+
4
+ #ifdef HAS_VELOCITY_BUFFER
5
+ #pragma <velocity_unpack>
6
+ #else
7
+ #define HAS_VELOCITY_BUFFER 0
8
+ #endif
9
+
10
+ #include <cameraHelpers>
11
+
12
+ varying vec2 vUv;
13
+ uniform vec2 previousRTSize;
14
+ uniform vec2 jitterSample;
15
+ uniform vec2 feedBack;
16
+ uniform bool firstFrame;
17
+
18
+ //float getViewZ( const in float depth ) {
19
+ // #if PERSPECTIVE_CAMERA == 1
20
+ // return perspectiveDepthToViewZ( depth, cameraNearFar.x, cameraNearFar.y );
21
+ // #else
22
+ // return orthoDepthToViewZ( depth, cameraNearFar.x, cameraNearFar.y );
23
+ // #endif
24
+ //}
25
+
26
+ vec3 find_closest_fragment_3x3(const in vec2 uv) {
27
+ const vec3 offset = vec3(-1.0, 1.0, 0.0);
28
+ vec2 texelSize = 1.0/previousRTSize;
29
+
30
+ vec3 dtr = vec3( 1, 1, getDepth( uv + offset.yy * texelSize) );
31
+ vec3 dtc = vec3( 0, 1, getDepth( uv + offset.zy * texelSize) );
32
+ vec3 dtl = vec3( -1, 1, getDepth( uv + offset.xy * texelSize) );
33
+
34
+ vec3 dml = vec3(-1, 0, getDepth( uv + offset.xz * texelSize) );
35
+ vec3 dmc = vec3( 0, 0, getDepth( uv ) );
36
+ vec3 dmr = vec3( 1, 0, getDepth( uv + offset.yz * texelSize) );
37
+
38
+ vec3 dbl = vec3(-1, -1, getDepth( uv + offset.xx * texelSize) );
39
+ vec3 dbc = vec3( 0, -1, getDepth( uv + offset.zx * texelSize) );
40
+ vec3 dbr = vec3( 1, -1, getDepth( uv + offset.yx * texelSize) );
41
+
42
+ vec3 dmin = dtl;
43
+ if ( dmin.z > dtc.z ) dmin = dtc;
44
+ if ( dmin.z > dtr.z ) dmin = dtr;
45
+
46
+ if ( dmin.z > dml.z ) dmin = dml;
47
+ if ( dmin.z > dmc.z ) dmin = dmc;
48
+ if ( dmin.z > dmr.z ) dmin = dmr;
49
+
50
+ if ( dmin.z > dbl.z ) dmin = dbl;
51
+ if ( dmin.z > dbc.z ) dmin = dbc;
52
+ if ( dmin.z > dbr.z ) dmin = dbr;
53
+
54
+ return vec3(uv + texelSize.xy * dmin.xy, dmin.z);
55
+ }
56
+
57
+ vec3 find_closest_fragment_5tap(const in vec2 uv)
58
+ {
59
+ vec2 texelSize = 1.0/previousRTSize;
60
+ vec2 offset = vec2(1.0, -1.0);
61
+
62
+ vec3 dtl = vec3(-1, 1, getDepth( uv + offset.yx * texelSize) );
63
+ vec3 dtr = vec3( 1, 1, getDepth( uv + offset.xx * texelSize) );
64
+
65
+ vec3 dmc = vec3( 0, 0, getDepth( uv) );
66
+
67
+ vec3 dbl = vec3(-1, -1, getDepth( uv + offset.yy * texelSize) );
68
+ vec3 dbr = vec3( 1, -1, getDepth( uv + offset.xy * texelSize) );
69
+
70
+ vec3 dmin = dtl;
71
+ if ( dmin.z > dtr.z ) dmin = dtr;
72
+ if ( dmin.z > dmc.z ) dmin = dmc;
73
+
74
+ if ( dmin.z > dbl.z ) dmin = dbl;
75
+ if ( dmin.z > dbr.z ) dmin = dbr;
76
+
77
+ return vec3(uv + dmin.xy * texelSize, dmin.z);
78
+ }
79
+
80
+ vec4 clip_aabb(const in vec4 aabb_min, const in vec4 aabb_max, vec4 p )
81
+ {
82
+ const float FLT_EPS = 1e-8;
83
+ vec4 p_clip = 0.5 * (aabb_max + aabb_min);
84
+ vec4 e_clip = 0.5 * (aabb_max - aabb_min) + FLT_EPS;
85
+
86
+ vec4 v_clip = p - p_clip;
87
+ vec4 v_unit = abs(v_clip / e_clip);
88
+ float ma_unit = max(v_unit.x, max(v_unit.y, v_unit.z));
89
+
90
+ if (ma_unit > 1.0)
91
+ return p_clip + v_clip / ma_unit;
92
+ else return p;
93
+ }
94
+
95
+ #if HAS_VELOCITY_BUFFER == 0 || defined(DEBUG_VELOCITY)
96
+ #include <computeScreenSpaceVelocity>
97
+ #include <getWorldPositionFromViewZ>
98
+ #endif
99
+
100
+ vec4 currentRTTexelToLinear1(vec4 a){
101
+ if(isinf(a.x) || isinf(a.y) || isinf(a.z) || isinf(a.w)){
102
+ return vec4(1.);
103
+ }
104
+ return currentRTTexelToLinear(a);
105
+ }
106
+ vec4 computeTAA(const in vec2 uv, const in vec2 screenSpaceVelocity, const in float feedbackScale) {
107
+ // vec2 jitterOffset = jitterSample/previousRTSize; // todo
108
+ vec2 uvUnJitter = uv;
109
+
110
+ vec4 currentColor = currentRTTexelToLinear(texture2D(currentRT, uvUnJitter));
111
+ vec4 previousColor = previousRTTexelToLinear(texture2D(previousRT, uv - screenSpaceVelocity));
112
+ const vec3 offset = vec3(1., -1., 0.);
113
+ vec2 texelSize = 1./previousRTSize;
114
+
115
+ float texelSpeed = length( screenSpaceVelocity );
116
+
117
+ // todo pick only the neighbors which are not background?
118
+ vec4 tl = currentRTTexelToLinear1(texture2D(currentRT, uvUnJitter + offset.yx * texelSize));
119
+ vec4 tc = currentRTTexelToLinear1(texture2D(currentRT, uvUnJitter + offset.zx * texelSize));
120
+ vec4 tr = currentRTTexelToLinear1(texture2D(currentRT, uvUnJitter + offset.xx * texelSize));
121
+ vec4 ml = currentRTTexelToLinear1(texture2D(currentRT, uvUnJitter + offset.yz * texelSize));
122
+ vec4 mc = currentColor;
123
+ vec4 mr = currentRTTexelToLinear1(texture2D(currentRT, uvUnJitter + offset.xz * texelSize));
124
+ vec4 bl = currentRTTexelToLinear1(texture2D(currentRT, uvUnJitter + offset.yy * texelSize));
125
+ vec4 bc = currentRTTexelToLinear1(texture2D(currentRT, uvUnJitter + offset.zy * texelSize));
126
+ vec4 br = currentRTTexelToLinear1(texture2D(currentRT, uvUnJitter + offset.xy * texelSize));
127
+
128
+ // Sharpen the centre sample for the clamp box below: unsharp mask with the diagonal neighbours,
129
+ // mc + k * (4 mc - (tl + tr + bl + br)), k = 0.2718.
130
+ mc = max(vec4(0.0), mc + 0.2718 * (4.0 * mc - (tl + tr + bl + br)));
131
+
132
+ vec4 min5 = min(tc, min(ml, min(mc, min(mr, bc))));
133
+ vec4 max5 = max(tc, max(ml, max(mc, max(mr, bc))));
134
+
135
+ vec4 cmin = min(min5, min(tl, min(tr, min(bl, br))));
136
+ vec4 cmax = max(min5, max(tl, max(tr, max(bl, br))));;
137
+
138
+ cmin = 0.5 * (cmin + min5);
139
+ cmax = 0.5 * (cmax + max5);
140
+ previousColor = clip_aabb(cmin, cmax, previousColor);
141
+
142
+ float lum0 = luminance(currentColor.rgb);
143
+ float lum1 = luminance(previousColor.rgb);
144
+ float unbiased_diff = abs(lum0 - lum1) / max(lum0, max(lum1, 0.2));
145
+ float unbiased_weight = 1.0 - unbiased_diff;
146
+ float unbiased_weight_sqr = unbiased_weight * unbiased_weight;
147
+ float k_feedback = mix(feedBack.x, feedBack.y, unbiased_weight_sqr);
148
+
149
+ return mix(currentColor, previousColor, clamp(k_feedback * feedbackScale, 0., 1.));
150
+
151
+ }
152
+
153
+ void main() {
154
+
155
+ // gl_FragColor.rgb = vec3(getDepth( vUv ));
156
+ // gl_FragColor.a = 1.;
157
+ // return;
158
+ // vec2 jitterOffset = jitterSample/previousRTSize;
159
+
160
+ #if HAS_VELOCITY_BUFFER == 0 // todo why not using closest fragment in velocity buffer?
161
+ #if QUALITY == 1
162
+ vec3 c_frag = find_closest_fragment_3x3(vUv);
163
+ #else
164
+ vec3 c_frag = find_closest_fragment_5tap(vUv);
165
+ #endif
166
+ #else
167
+ vec3 c_frag = vec3(vUv, 0.);
168
+ #endif
169
+
170
+ bool bg = firstFrame;
171
+
172
+ #if BACKGROUND_TAA // this is required for edge artifacts in msaa
173
+
174
+ // float d = getDepth(vUv);
175
+ float d = c_frag.z;
176
+ float edgef = min(1., max(0., 1.- (d*100. - 99.)));
177
+
178
+ #else
179
+
180
+ bg = bg || c_frag.z > 0.999;
181
+
182
+ #endif
183
+
184
+ if( bg ) {
185
+
186
+ gl_FragColor = currentRTTexelToLinear1(texture2D(currentRT, vUv));
187
+
188
+ } else {
189
+ #if HAS_VELOCITY_BUFFER == 0
190
+ // #if LINEAR_DEPTH == 0
191
+ // float sampleViewZ = getViewZ( c_frag.z );
192
+ // #else
193
+ float sampleViewZ = mix(-cameraNearFar.x, -cameraNearFar.y, c_frag.z);
194
+ // #endif
195
+ vec3 worldPosition = getWorldPositionFromViewZ(c_frag.xy, sampleViewZ);
196
+ vec2 screenSpaceVelocity = computeScreenSpaceVelocity(worldPosition);
197
+ #else
198
+ vec2 screenSpaceVelocity = getVelocity(c_frag.xy);
199
+ #endif
200
+
201
+ // float previousDepth = getDepth(vUv - screenSpaceVelocity);
202
+
203
+ // screenSpaceVelocity *= min(1., edgef);
204
+ // screenSpaceVelocity *= (d >= 0.99) ? 0. : 1.;
205
+ // screenSpaceVelocity *= abs(d-previousDepth) > 0.01 ? 0. : 1.;
206
+ // screenSpaceVelocity *= 0.;
207
+
208
+ // todo add velocity scale also
209
+ #if BACKGROUND_TAA
210
+ gl_FragColor = computeTAA(vUv, screenSpaceVelocity * edgef, edgef);
211
+ #else
212
+ gl_FragColor = computeTAA(vUv, screenSpaceVelocity, 1.);
213
+ #endif
214
+
215
+ // gl_FragColor = firstFrame /* || previousDepth > 0.999*/ ? currentRTTexelToLinear1(texture2D(currentRT, vUv)) : computeTAA(vUv, screenSpaceVelocity, edgef);
216
+ // gl_FragColor = vec4(1. - max(0., (d - 0.9) * 10.0), 0., 0., 1.0);
217
+ // gl_FragColor = vec4(10. * length(screenSpaceVelocity));
218
+ // gl_FragColor = vec4(abs(d-previousDepth) > 0.1, 0., 0., 1.);
219
+ // gl_FragColor = vec4(abs(d-previousDepth) > 0.1, 0., 0., 1.);
220
+
221
+ }
222
+
223
+ #include <colorspace_fragment>
224
+
225
+ #ifdef DEBUG_VELOCITY
226
+ float sampleViewZ = mix(-cameraNearFar.x, -cameraNearFar.y, c_frag.z);
227
+ vec3 worldPosition = getWorldPositionFromViewZ(c_frag.xy, sampleViewZ);
228
+ vec2 screenSpaceVelocity = computeScreenSpaceVelocity(worldPosition);
229
+ // screenSpaceVelocity *= min(1., edgef);
230
+ gl_FragColor = vec4(10. * length(screenSpaceVelocity), 0., 0., 1.);
231
+ #endif
232
+
233
+ }
@@ -0,0 +1,24 @@
1
+ import computeScreenSpaceVelocity from './shaders/computeScreenSpaceVelocity.glsl'
2
+ import getWorldPositionFromViewZ from './shaders/getWorldPositionFromViewZ.glsl'
3
+ import temporalaa from './shaders/temporalaa.glsl'
4
+ import ssreflection from './shaders/ssreflection.glsl'
5
+ import ssrtao from './shaders/ssrtao.glsl'
6
+ import {RenderManager} from "threepipe";
7
+
8
+ const shadersUtils2 = {
9
+ computeScreenSpaceVelocity: computeScreenSpaceVelocity,
10
+ getWorldPositionFromViewZ: getWorldPositionFromViewZ,
11
+ temporalAA: temporalaa,
12
+ ssReflection: ssreflection,
13
+ calculateGI: ssrtao,
14
+
15
+ ['__inited']: false,
16
+ }
17
+
18
+ export function getShaders(){
19
+ if(!shadersUtils2.__inited){
20
+ shadersUtils2.__inited = true
21
+ Object.assign(RenderManager.ShaderChunk, shadersUtils2)
22
+ }
23
+ return shadersUtils2
24
+ }