@react-three/postprocessing 2.19.1 → 3.0.0-rc.1

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 (270) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +81 -81
  3. package/dist/index.d.ts +1 -38
  4. package/dist/index.js +554 -88
  5. package/dist/index.js.map +1 -1
  6. package/package.json +68 -96
  7. package/src/EffectComposer.test.tsx +126 -0
  8. package/src/EffectComposer.tsx +200 -0
  9. package/src/Selection.tsx +46 -0
  10. package/src/effects/ASCII.tsx +135 -0
  11. package/src/effects/Autofocus.tsx +153 -0
  12. package/src/effects/Bloom.tsx +6 -0
  13. package/src/effects/BrightnessContrast.tsx +4 -0
  14. package/src/effects/ChromaticAberration.tsx +5 -0
  15. package/src/effects/ColorAverage.tsx +15 -0
  16. package/src/effects/ColorDepth.tsx +4 -0
  17. package/src/effects/Depth.tsx +4 -0
  18. package/src/effects/DepthOfField.tsx +89 -0
  19. package/src/effects/DotScreen.tsx +4 -0
  20. package/src/effects/FXAA.tsx +4 -0
  21. package/src/effects/Glitch.tsx +40 -0
  22. package/src/effects/GodRays.tsx +16 -0
  23. package/src/effects/Grid.tsx +21 -0
  24. package/src/effects/HueSaturation.tsx +4 -0
  25. package/src/effects/LUT.tsx +26 -0
  26. package/{dist/effects/LensFlare.js → src/effects/LensFlare.tsx} +611 -556
  27. package/src/effects/N8AO.tsx +81 -0
  28. package/src/effects/Noise.tsx +4 -0
  29. package/src/effects/Outline.tsx +117 -0
  30. package/src/effects/Pixelation.tsx +15 -0
  31. package/{dist/effects/Ramp.js → src/effects/Ramp.tsx} +150 -156
  32. package/src/effects/SMAA.tsx +4 -0
  33. package/src/effects/SSAO.tsx +41 -0
  34. package/src/effects/ScanlineEffect.tsx +7 -0
  35. package/src/effects/SelectiveBloom.tsx +126 -0
  36. package/src/effects/Sepia.tsx +4 -0
  37. package/src/effects/ShockWave.tsx +4 -0
  38. package/src/effects/Texture.tsx +23 -0
  39. package/src/effects/TiltShift.tsx +4 -0
  40. package/{dist/effects/TiltShift2.js → src/effects/TiltShift2.tsx} +90 -98
  41. package/src/effects/ToneMapping.tsx +6 -0
  42. package/src/effects/Vignette.tsx +4 -0
  43. package/{dist/effects/Water.js → src/effects/Water.tsx} +35 -38
  44. package/src/index.ts +40 -0
  45. package/src/util.tsx +55 -0
  46. package/dist/EffectComposer.cjs +0 -137
  47. package/dist/EffectComposer.cjs.map +0 -1
  48. package/dist/EffectComposer.d.ts +0 -27
  49. package/dist/EffectComposer.js +0 -137
  50. package/dist/EffectComposer.js.map +0 -1
  51. package/dist/EffectComposer.test.d.ts +0 -4
  52. package/dist/Selection.cjs +0 -36
  53. package/dist/Selection.cjs.map +0 -1
  54. package/dist/Selection.d.ts +0 -16
  55. package/dist/Selection.js +0 -36
  56. package/dist/Selection.js.map +0 -1
  57. package/dist/compat.cjs +0 -77
  58. package/dist/compat.cjs.map +0 -1
  59. package/dist/compat.js +0 -60
  60. package/dist/compat.js.map +0 -1
  61. package/dist/effects/ASCII.cjs +0 -113
  62. package/dist/effects/ASCII.cjs.map +0 -1
  63. package/dist/effects/ASCII.d.ts +0 -18
  64. package/dist/effects/ASCII.js +0 -113
  65. package/dist/effects/ASCII.js.map +0 -1
  66. package/dist/effects/Autofocus.cjs +0 -126
  67. package/dist/effects/Autofocus.cjs.map +0 -1
  68. package/dist/effects/Autofocus.d.ts +0 -22
  69. package/dist/effects/Autofocus.js +0 -109
  70. package/dist/effects/Autofocus.js.map +0 -1
  71. package/dist/effects/Bloom.cjs +0 -9
  72. package/dist/effects/Bloom.cjs.map +0 -1
  73. package/dist/effects/Bloom.d.ts +0 -6
  74. package/dist/effects/Bloom.js +0 -9
  75. package/dist/effects/Bloom.js.map +0 -1
  76. package/dist/effects/BrightnessContrast.cjs +0 -7
  77. package/dist/effects/BrightnessContrast.cjs.map +0 -1
  78. package/dist/effects/BrightnessContrast.d.ts +0 -10
  79. package/dist/effects/BrightnessContrast.js +0 -7
  80. package/dist/effects/BrightnessContrast.js.map +0 -1
  81. package/dist/effects/ChromaticAberration.cjs +0 -7
  82. package/dist/effects/ChromaticAberration.cjs.map +0 -1
  83. package/dist/effects/ChromaticAberration.d.ts +0 -13
  84. package/dist/effects/ChromaticAberration.js +0 -7
  85. package/dist/effects/ChromaticAberration.js.map +0 -1
  86. package/dist/effects/ColorAverage.cjs +0 -11
  87. package/dist/effects/ColorAverage.cjs.map +0 -1
  88. package/dist/effects/ColorAverage.d.ts +0 -8
  89. package/dist/effects/ColorAverage.js +0 -11
  90. package/dist/effects/ColorAverage.js.map +0 -1
  91. package/dist/effects/ColorDepth.cjs +0 -7
  92. package/dist/effects/ColorDepth.cjs.map +0 -1
  93. package/dist/effects/ColorDepth.d.ts +0 -9
  94. package/dist/effects/ColorDepth.js +0 -7
  95. package/dist/effects/ColorDepth.js.map +0 -1
  96. package/dist/effects/Depth.cjs +0 -7
  97. package/dist/effects/Depth.cjs.map +0 -1
  98. package/dist/effects/Depth.d.ts +0 -9
  99. package/dist/effects/Depth.js +0 -7
  100. package/dist/effects/Depth.js.map +0 -1
  101. package/dist/effects/DepthOfField.cjs +0 -74
  102. package/dist/effects/DepthOfField.cjs.map +0 -1
  103. package/dist/effects/DepthOfField.d.ts +0 -25
  104. package/dist/effects/DepthOfField.js +0 -74
  105. package/dist/effects/DepthOfField.js.map +0 -1
  106. package/dist/effects/DotScreen.cjs +0 -7
  107. package/dist/effects/DotScreen.cjs.map +0 -1
  108. package/dist/effects/DotScreen.d.ts +0 -10
  109. package/dist/effects/DotScreen.js +0 -7
  110. package/dist/effects/DotScreen.js.map +0 -1
  111. package/dist/effects/FXAA.cjs +0 -7
  112. package/dist/effects/FXAA.cjs.map +0 -1
  113. package/dist/effects/FXAA.d.ts +0 -8
  114. package/dist/effects/FXAA.js +0 -7
  115. package/dist/effects/FXAA.js.map +0 -1
  116. package/dist/effects/Glitch.cjs +0 -31
  117. package/dist/effects/Glitch.cjs.map +0 -1
  118. package/dist/effects/Glitch.d.ts +0 -30
  119. package/dist/effects/Glitch.js +0 -31
  120. package/dist/effects/Glitch.js.map +0 -1
  121. package/dist/effects/GodRays.cjs +0 -15
  122. package/dist/effects/GodRays.cjs.map +0 -1
  123. package/dist/effects/GodRays.d.ts +0 -21
  124. package/dist/effects/GodRays.js +0 -15
  125. package/dist/effects/GodRays.js.map +0 -1
  126. package/dist/effects/Grid.cjs +0 -18
  127. package/dist/effects/Grid.cjs.map +0 -1
  128. package/dist/effects/Grid.d.ts +0 -12
  129. package/dist/effects/Grid.js +0 -18
  130. package/dist/effects/Grid.js.map +0 -1
  131. package/dist/effects/HueSaturation.cjs +0 -7
  132. package/dist/effects/HueSaturation.cjs.map +0 -1
  133. package/dist/effects/HueSaturation.d.ts +0 -10
  134. package/dist/effects/HueSaturation.js +0 -7
  135. package/dist/effects/HueSaturation.js.map +0 -1
  136. package/dist/effects/LUT.cjs +0 -20
  137. package/dist/effects/LUT.cjs.map +0 -1
  138. package/dist/effects/LUT.d.ts +0 -9
  139. package/dist/effects/LUT.js +0 -20
  140. package/dist/effects/LUT.js.map +0 -1
  141. package/dist/effects/LensFlare.cjs +0 -573
  142. package/dist/effects/LensFlare.cjs.map +0 -1
  143. package/dist/effects/LensFlare.d.ts +0 -55
  144. package/dist/effects/LensFlare.js.map +0 -1
  145. package/dist/effects/N8AO/BlueNoise.cjs +0 -4
  146. package/dist/effects/N8AO/BlueNoise.cjs.map +0 -1
  147. package/dist/effects/N8AO/BlueNoise.js +0 -5
  148. package/dist/effects/N8AO/BlueNoise.js.map +0 -1
  149. package/dist/effects/N8AO/DepthDownSample.cjs +0 -183
  150. package/dist/effects/N8AO/DepthDownSample.cjs.map +0 -1
  151. package/dist/effects/N8AO/DepthDownSample.js +0 -166
  152. package/dist/effects/N8AO/DepthDownSample.js.map +0 -1
  153. package/dist/effects/N8AO/EffectCompositer.cjs +0 -278
  154. package/dist/effects/N8AO/EffectCompositer.cjs.map +0 -1
  155. package/dist/effects/N8AO/EffectCompositer.js +0 -261
  156. package/dist/effects/N8AO/EffectCompositer.js.map +0 -1
  157. package/dist/effects/N8AO/EffectShader.cjs +0 -236
  158. package/dist/effects/N8AO/EffectShader.cjs.map +0 -1
  159. package/dist/effects/N8AO/EffectShader.js +0 -219
  160. package/dist/effects/N8AO/EffectShader.js.map +0 -1
  161. package/dist/effects/N8AO/FullScreenTriangle.cjs +0 -59
  162. package/dist/effects/N8AO/FullScreenTriangle.cjs.map +0 -1
  163. package/dist/effects/N8AO/FullScreenTriangle.js +0 -42
  164. package/dist/effects/N8AO/FullScreenTriangle.js.map +0 -1
  165. package/dist/effects/N8AO/N8AOPostPass.cjs +0 -502
  166. package/dist/effects/N8AO/N8AOPostPass.cjs.map +0 -1
  167. package/dist/effects/N8AO/N8AOPostPass.js +0 -485
  168. package/dist/effects/N8AO/N8AOPostPass.js.map +0 -1
  169. package/dist/effects/N8AO/PoissionBlur.cjs +0 -186
  170. package/dist/effects/N8AO/PoissionBlur.cjs.map +0 -1
  171. package/dist/effects/N8AO/PoissionBlur.js +0 -169
  172. package/dist/effects/N8AO/PoissionBlur.js.map +0 -1
  173. package/dist/effects/N8AO/index.cjs +0 -60
  174. package/dist/effects/N8AO/index.cjs.map +0 -1
  175. package/dist/effects/N8AO/index.d.ts +0 -18
  176. package/dist/effects/N8AO/index.js +0 -60
  177. package/dist/effects/N8AO/index.js.map +0 -1
  178. package/dist/effects/Noise.cjs +0 -7
  179. package/dist/effects/Noise.cjs.map +0 -1
  180. package/dist/effects/Noise.d.ts +0 -9
  181. package/dist/effects/Noise.js +0 -7
  182. package/dist/effects/Noise.js.map +0 -1
  183. package/dist/effects/Outline.cjs +0 -100
  184. package/dist/effects/Outline.cjs.map +0 -1
  185. package/dist/effects/Outline.d.ts +0 -30
  186. package/dist/effects/Outline.js +0 -100
  187. package/dist/effects/Outline.js.map +0 -1
  188. package/dist/effects/Pixelation.cjs +0 -11
  189. package/dist/effects/Pixelation.cjs.map +0 -1
  190. package/dist/effects/Pixelation.d.ts +0 -6
  191. package/dist/effects/Pixelation.js +0 -11
  192. package/dist/effects/Pixelation.js.map +0 -1
  193. package/dist/effects/Ramp.cjs +0 -156
  194. package/dist/effects/Ramp.cjs.map +0 -1
  195. package/dist/effects/Ramp.d.ts +0 -88
  196. package/dist/effects/Ramp.js.map +0 -1
  197. package/dist/effects/SMAA.cjs +0 -7
  198. package/dist/effects/SMAA.cjs.map +0 -1
  199. package/dist/effects/SMAA.d.ts +0 -10
  200. package/dist/effects/SMAA.js +0 -7
  201. package/dist/effects/SMAA.js.map +0 -1
  202. package/dist/effects/SSAO.cjs +0 -37
  203. package/dist/effects/SSAO.cjs.map +0 -1
  204. package/dist/effects/SSAO.d.ts +0 -30
  205. package/dist/effects/SSAO.js +0 -37
  206. package/dist/effects/SSAO.js.map +0 -1
  207. package/dist/effects/SSR/index.cjs +0 -33
  208. package/dist/effects/SSR/index.cjs.map +0 -1
  209. package/dist/effects/SSR/index.d.ts +0 -55
  210. package/dist/effects/SSR/index.js +0 -33
  211. package/dist/effects/SSR/index.js.map +0 -1
  212. package/dist/effects/SSR/screen-space-reflections.cjs +0 -1699
  213. package/dist/effects/SSR/screen-space-reflections.cjs.map +0 -1
  214. package/dist/effects/SSR/screen-space-reflections.js +0 -1699
  215. package/dist/effects/SSR/screen-space-reflections.js.map +0 -1
  216. package/dist/effects/ScanlineEffect.cjs +0 -7
  217. package/dist/effects/ScanlineEffect.cjs.map +0 -1
  218. package/dist/effects/ScanlineEffect.d.ts +0 -9
  219. package/dist/effects/ScanlineEffect.js +0 -7
  220. package/dist/effects/ScanlineEffect.js.map +0 -1
  221. package/dist/effects/SelectiveBloom.cjs +0 -104
  222. package/dist/effects/SelectiveBloom.cjs.map +0 -1
  223. package/dist/effects/SelectiveBloom.d.ts +0 -20
  224. package/dist/effects/SelectiveBloom.js +0 -104
  225. package/dist/effects/SelectiveBloom.js.map +0 -1
  226. package/dist/effects/Sepia.cjs +0 -7
  227. package/dist/effects/Sepia.cjs.map +0 -1
  228. package/dist/effects/Sepia.d.ts +0 -9
  229. package/dist/effects/Sepia.js +0 -7
  230. package/dist/effects/Sepia.js.map +0 -1
  231. package/dist/effects/ShockWave.cjs +0 -7
  232. package/dist/effects/ShockWave.cjs.map +0 -1
  233. package/dist/effects/ShockWave.d.ts +0 -6
  234. package/dist/effects/ShockWave.js +0 -7
  235. package/dist/effects/ShockWave.js.map +0 -1
  236. package/dist/effects/Texture.cjs +0 -21
  237. package/dist/effects/Texture.cjs.map +0 -1
  238. package/dist/effects/Texture.d.ts +0 -11
  239. package/dist/effects/Texture.js +0 -21
  240. package/dist/effects/Texture.js.map +0 -1
  241. package/dist/effects/TiltShift.cjs +0 -7
  242. package/dist/effects/TiltShift.cjs.map +0 -1
  243. package/dist/effects/TiltShift.d.ts +0 -17
  244. package/dist/effects/TiltShift.js +0 -7
  245. package/dist/effects/TiltShift.js.map +0 -1
  246. package/dist/effects/TiltShift2.cjs +0 -98
  247. package/dist/effects/TiltShift2.cjs.map +0 -1
  248. package/dist/effects/TiltShift2.d.ts +0 -30
  249. package/dist/effects/TiltShift2.js.map +0 -1
  250. package/dist/effects/ToneMapping.cjs +0 -7
  251. package/dist/effects/ToneMapping.cjs.map +0 -1
  252. package/dist/effects/ToneMapping.d.ts +0 -19
  253. package/dist/effects/ToneMapping.js +0 -7
  254. package/dist/effects/ToneMapping.js.map +0 -1
  255. package/dist/effects/Vignette.cjs +0 -7
  256. package/dist/effects/Vignette.cjs.map +0 -1
  257. package/dist/effects/Vignette.d.ts +0 -12
  258. package/dist/effects/Vignette.js +0 -7
  259. package/dist/effects/Vignette.js.map +0 -1
  260. package/dist/effects/Water.cjs +0 -38
  261. package/dist/effects/Water.cjs.map +0 -1
  262. package/dist/effects/Water.d.ts +0 -15
  263. package/dist/effects/Water.js.map +0 -1
  264. package/dist/index.cjs +0 -89
  265. package/dist/index.cjs.map +0 -1
  266. package/dist/util.cjs +0 -69
  267. package/dist/util.cjs.map +0 -1
  268. package/dist/util.d.ts +0 -13
  269. package/dist/util.js +0 -52
  270. package/dist/util.js.map +0 -1
@@ -1,1699 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const postprocessing = require("postprocessing");
4
- const THREE = require("three");
5
- const compat = require("../../compat.cjs");
6
- const boxBlur = (
7
- /* glsl */
8
- `
9
- uniform float blur;
10
- uniform float blurSharpness;
11
- uniform int blurKernel;
12
-
13
- vec3 denoise(
14
- vec3 center,
15
- sampler2D tex,
16
- vec2 uv,
17
- vec2 invTexSize,
18
- float blur,
19
- float blurSharpness,
20
- int blurKernel
21
- ) {
22
- vec3 color, col;
23
- float total, weight;
24
-
25
- for (int x = -blurKernel; x <= blurKernel; x++) {
26
- for (int y=-blurKernel; y<=blurKernel; y++) {
27
- col = textureLod(tex, uv + vec2(x,y) * invTexSize, 0.0).rgb;
28
- weight = 1.0-abs(dot(col - center, vec3(0.25)));
29
- weight = pow(weight, blurSharpness);
30
- color += col * weight;
31
- total += weight;
32
- }
33
- }
34
-
35
- return color / total;
36
- }
37
- `
38
- );
39
- const finalSSRShader = (
40
- /* glsl */
41
- `
42
- #define MODE_DEFAULT 0
43
- #define MODE_REFLECTIONS 1
44
- #define MODE_RAW_REFLECTION 2
45
- #define MODE_BLURRED_REFLECTIONS 3
46
- #define MODE_INPUT 4
47
- #define MODE_BLUR_MIX 5
48
- #define FLOAT_EPSILON 0.00001
49
- // uniform sampler2D inputTexture;
50
- uniform sampler2D reflectionsTexture;
51
- // uniform float samples;
52
-
53
- ${boxBlur}
54
-
55
- void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) {
56
- vec4 reflectionsTexel=texture2D(reflectionsTexture, vUv);
57
- ivec2 size = textureSize(reflectionsTexture, 0);
58
- vec2 invTexSize= 1.0 / vec2(size.x, size.y);
59
- vec3 reflectionClr = reflectionsTexel.xyz;
60
- if (blur > FLOAT_EPSILON) {
61
- vec3 blurredReflectionsColor = denoise(
62
- reflectionsTexel.rgb,
63
- reflectionsTexture,
64
- vUv,
65
- invTexSize,
66
- blur,
67
- blurSharpness,
68
- blurKernel
69
- );
70
- reflectionClr = mix(reflectionClr, blurredReflectionsColor.rgb, blur);
71
- }
72
-
73
- #if RENDER_MODE == MODE_DEFAULT
74
- outputColor = vec4(inputColor.rgb+reflectionClr, 1.0);
75
- #endif
76
- #if RENDER_MODE == MODE_REFLECTIONS
77
- outputColor = vec4(reflectionClr, 1.0);
78
- #endif
79
- #if RENDER_MODE == MODE_RAW_REFLECTION
80
- outputColor = vec4(reflectionsTexel.xyz, 1.0);
81
- #endif
82
- #if RENDER_MODE == MODE_BLURRED_REFLECTIONS
83
- outputColor = vec4(blurredReflectionsTexel.xyz, 1.0);
84
- #endif
85
- #if RENDER_MODE == MODE_INPUT
86
- outputColor = vec4(inputColor.xyz, 1.0);
87
- #endif
88
- #if RENDER_MODE == MODE_BLUR_MIX
89
- outputColor = vec4(vec3(blur), 1.0);
90
- #endif
91
- }
92
- `
93
- );
94
- const helperFunctions = (
95
- /* glsl */
96
- `
97
- vec3 getViewPosition(const float depth) {
98
- float clipW= _projectionMatrix[2][3] * depth + _projectionMatrix[3][3];
99
- vec4 clipPosition = vec4((vec3(vUv, depth) - 0.5) * 2.0, 1.0);
100
- clipPosition *= clipW;
101
- return(_inverseProjectionMatrix * clipPosition).xyz;
102
- }
103
-
104
- float getViewZ(const in float depth) {
105
- #ifdef PERSPECTIVE_CAMERA
106
- return perspectiveDepthToViewZ(depth, cameraNear, cameraFar);
107
- #else
108
- return orthographicDepthToViewZ(depth, cameraNear, cameraFar);
109
- #endif
110
- }
111
-
112
- vec3 screenSpaceToWorldSpace(const vec2 uv,const float depth){
113
- vec4 ndc = vec4((uv.x - 0.5) * 2.0,(uv.y - 0.5)* 2.0, (depth - 0.5) * 2.0, 1.0);
114
- vec4 clip= _inverseProjectionMatrix*ndc;
115
- vec4 view = cameraMatrixWorld * (clip / clip.w);
116
- return view.xyz;
117
- }
118
-
119
- #define Scale (vec3(0.8, 0.8, 0.8))
120
- #define K (19.19)
121
-
122
- vec3 hash(vec3 a) {
123
- a = fract(a * Scale);
124
- a += dot(a, a.yxz + K);
125
- return fract((a.xxy + a.yxx) * a.zyx);
126
- }
127
-
128
- float fresnel_dielectric_cos(float cosi, float eta){
129
- float c = abs(cosi);
130
- float g = eta * eta - 1.0 + c* c;
131
- float result;
132
-
133
- if (g > 0.0){
134
- g = sqrt(g);
135
- float A = (g - c) / (g + c);
136
- float B = (c* (g + c) - 1.0) / (c * (g - c) + 1.0);
137
- result = 0.5 * A * A * (1.0 + B * B);
138
- } else {
139
- result = 1.0;
140
- }
141
-
142
- return result;
143
- }
144
-
145
- float fresnel_dielectric(vec3 Incoming, vec3 Normal, float eta){
146
- float cosine = dot(Incoming, Normal);
147
- return min(1.0, 5.0 * fresnel_dielectric_cos(cosine, eta));
148
- }
149
- `
150
- );
151
- const trCompose = (
152
- /* glsl */
153
- `
154
- #define INV_EULER 0.36787944117144233
155
-
156
- alpha = velocityDisocclusion < FLOAT_EPSILON ? (alpha + 0.0075) : 0.0;
157
- alpha = clamp(alpha, 0.0, 1.0);
158
- bool needsBlur = !didReproject || velocityDisocclusion > 0.5;
159
-
160
- #ifdef boxBlur
161
- if (needsBlur) inputColor = boxBlurredColor;
162
- #endif
163
-
164
- if (alpha == 1.0) {
165
- outputColor = accumulatedColor;
166
- } else {
167
- float m = mix(alpha, 1.0, blend);
168
- if (needsBlur) m = 0.0;
169
- outputColor = accumulatedColor * m + inputColor * (1.0 - m);
170
- }
171
- `
172
- );
173
- class MRTMaterial extends THREE.ShaderMaterial {
174
- constructor() {
175
- super({
176
- type: "MRTMaterial",
177
- defines: {
178
- USE_UV: "",
179
- TEMPORAL_RESOLVE: ""
180
- },
181
- uniforms: {
182
- opacity: new THREE.Uniform(1),
183
- normalMap: new THREE.Uniform(null),
184
- normalScale: new THREE.Uniform(new THREE.Vector2(1, 1)),
185
- uvTransform: new THREE.Uniform(new THREE.Matrix3()),
186
- roughness: new THREE.Uniform(1),
187
- roughnessMap: new THREE.Uniform(null)
188
- },
189
- vertexShader: (
190
- /* glsl */
191
- `
192
- #ifdef USE_MRT
193
- varying vec2 vHighPrecisionZW;
194
- #endif
195
- #define NORMAL
196
- #if defined(FLAT_SHADED) || defined(USE_BUMPMAP) || defined(TANGENTSPACE_NORMALMAP)
197
- varying vec3 vViewPosition;
198
- #endif
199
- #include <common>
200
- #include <uv_pars_vertex>
201
- #include <displacementmap_pars_vertex>
202
- #include <normal_pars_vertex>
203
- #include <morphtarget_pars_vertex>
204
- #include <skinning_pars_vertex>
205
- #include <logdepthbuf_pars_vertex>
206
- #include <clipping_planes_pars_vertex>
207
- #ifdef USE_UV
208
- ${THREE.REVISION.replace(/\D+/g, "") >= 151 ? "uniform mat3 uvTransform;" : ""}
209
- #endif
210
- void main() {
211
- #include <uv_vertex>
212
- #include <beginnormal_vertex>
213
- #include <morphnormal_vertex>
214
- #include <skinbase_vertex>
215
- #include <skinnormal_vertex>
216
- #include <defaultnormal_vertex>
217
- #include <normal_vertex>
218
- #include <begin_vertex>
219
- #include <morphtarget_vertex>
220
- #include <skinning_vertex>
221
- #include <displacementmap_vertex>
222
- #include <project_vertex>
223
- #include <logdepthbuf_vertex>
224
- #include <clipping_planes_vertex>
225
- #if defined(FLAT_SHADED) || defined(USE_BUMPMAP) || defined(TANGENTSPACE_NORMALMAP)
226
- vViewPosition = -mvPosition.xyz;
227
- #endif
228
- #ifdef USE_MRT
229
- vHighPrecisionZW = gl_Position.zw;
230
- #endif
231
- #ifdef USE_UV
232
- vUv = (uvTransform * vec3(uv, 1)).xy;
233
- #endif
234
- }
235
- `
236
- ),
237
- fragmentShader: (
238
- /* glsl */
239
- `
240
- #define NORMAL
241
- #if defined(FLAT_SHADED) || defined(USE_BUMPMAP) || defined(TANGENTSPACE_NORMALMAP)
242
- varying vec3 vViewPosition;
243
- #endif
244
- #include <packing>
245
- #include <uv_pars_fragment>
246
- #include <normal_pars_fragment>
247
- #include <bumpmap_pars_fragment>
248
- #include <normalmap_pars_fragment>
249
- #include <logdepthbuf_pars_fragment>
250
- #include <clipping_planes_pars_fragment>
251
- #include <roughnessmap_pars_fragment>
252
-
253
- #ifdef USE_MRT
254
- layout(location = 0) out vec4 gNormal;
255
- layout(location = 1) out vec4 gDepth;
256
- varying vec2 vHighPrecisionZW;
257
- #endif
258
- uniform float roughness;
259
- void main() {
260
- #include <clipping_planes_fragment>
261
- #include <logdepthbuf_fragment>
262
- #include <normal_fragment_begin>
263
- #include <normal_fragment_maps>
264
-
265
- float roughnessFactor = roughness;
266
-
267
- if (roughness > 10.0e9){
268
- roughnessFactor = 1.;
269
- } else {
270
- #ifdef useRoughnessMap
271
- vec4 texelRoughness = texture2D(roughnessMap, vUv);
272
- // reads channel G, compatible with a combined OcclusionRoughnessMetallic (RGB) texture
273
- roughnessFactor *= texelRoughness.g;
274
- #endif
275
- }
276
-
277
- vec3 normalColor = packNormalToRGB(normal);
278
- #ifdef USE_MRT
279
- float fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;
280
- vec4 depthColor = packDepthToRGBA(fragCoordZ);
281
- gNormal = vec4(normalColor, roughnessFactor);
282
- gDepth = depthColor;
283
- #else
284
- gl_FragColor = vec4(normalColor, roughnessFactor);
285
- #endif
286
- }
287
- `
288
- ),
289
- toneMapped: false
290
- });
291
- this.normalMapType = THREE.TangentSpaceNormalMap;
292
- this.normalScale = new THREE.Vector2(1, 1);
293
- Object.defineProperty(this, "glslVersion", {
294
- get() {
295
- return "USE_MRT" in this.defines ? THREE.GLSL3 : null;
296
- },
297
- set(_) {
298
- }
299
- });
300
- }
301
- }
302
- const vertexShader = (
303
- /* glsl */
304
- `
305
- varying vec2 vUv;
306
-
307
- void main() {
308
- vUv = position.xy * 0.5 + 0.5;
309
- gl_Position = vec4(position.xy, 1.0, 1.0);
310
- }
311
- `
312
- );
313
- const fragmentShader = (
314
- /* glsl */
315
- `
316
- varying vec2 vUv;
317
- uniform sampler2D inputTexture;
318
- uniform sampler2D accumulatedTexture;
319
- uniform sampler2D normalTexture;
320
- uniform sampler2D depthTexture;
321
- uniform sampler2D envMap;
322
- uniform mat4 _projectionMatrix;
323
- uniform mat4 _inverseProjectionMatrix;
324
- uniform mat4 cameraMatrixWorld;
325
- uniform float cameraNear;
326
- uniform float cameraFar;
327
- uniform float rayDistance;
328
- uniform float intensity;
329
- uniform float maxDepthDifference;
330
- uniform float roughnessFade;
331
- uniform float maxRoughness;
332
- uniform float fade;
333
- uniform float thickness;
334
- uniform float ior;
335
- uniform float samples;
336
- uniform float jitter;
337
- uniform float jitterRoughness;
338
-
339
- #define INVALID_RAY_COORDS vec2(-1.0);
340
-
341
- #define EARLY_OUT_COLOR vec4(0.0, 0.0, 0.0, 1.0)
342
- #define FLOAT_EPSILON 0.00001
343
- float nearMinusFar;
344
- float nearMulFar;
345
- float farMinusNear;
346
-
347
- #include <packing>
348
-
349
- ${helperFunctions}
350
-
351
- vec2 RayMarch(vec3 dir, inout vec3 hitPos, inout float rayHitDepthDifference);
352
- vec2 BinarySearch(in vec3 dir, inout vec3 hitPos, inout float rayHitDepthDifference);
353
- float fastGetViewZ(const in float depth);
354
- vec3 getIBLRadiance(const in vec3 viewDir, const in vec3 normal, const in float roughness);
355
-
356
- void main() {
357
- vec4 depthTexel = textureLod(depthTexture, vUv, 0.0);
358
-
359
- if (dot(depthTexel.rgb, depthTexel.rgb) < FLOAT_EPSILON) {
360
- gl_FragColor = EARLY_OUT_COLOR;
361
- return;
362
- }
363
-
364
- float unpackedDepth = unpackRGBAToDepth(depthTexel);
365
- vec4 normalTexel = textureLod(normalTexture, vUv, 0.0);
366
- float roughness = normalTexel.a;
367
- float specular = 1.0 - roughness;
368
-
369
- nearMinusFar = cameraNear - cameraFar;
370
- nearMulFar = cameraNear * cameraFar;
371
- farMinusNear = cameraFar - cameraNear;
372
-
373
- normalTexel.rgb = unpackRGBToNormal(normalTexel.rgb);
374
-
375
- float depth = fastGetViewZ(unpackedDepth);
376
- vec3 viewPos = getViewPosition(depth);
377
- vec3 viewDir = normalize(viewPos);
378
- vec3 viewNormal = normalTexel.xyz;
379
- vec3 worldPos = screenSpaceToWorldSpace(vUv, unpackedDepth);
380
-
381
- vec3 jitt=vec3(0.0);
382
- if (jitterRoughness != 0.0 || jitter!=0.0){
383
- vec3 randomJitter = hash(50.0 * samples * worldPos) - 0.5;
384
- float spread= ((2.0 - specular) + roughness * jitterRoughness);
385
- float jitterMix = jitter * 0.25 + jitterRoughness * roughness;
386
- if (jitterMix > 1.0) jitterMix = 1.0;
387
- jitt = mix(vec3(0.0), randomJitter * spread, jitterMix);
388
- }
389
-
390
- viewNormal += jitt;
391
- float fresnelFactor = fresnel_dielectric(viewDir, viewNormal, ior);
392
- vec3 iblRadiance = getIBLRadiance(-viewDir, viewNormal, 0.0) * fresnelFactor;
393
- float lastFrameAlpha = textureLod(accumulatedTexture, vUv, 0.0).a;
394
- if (roughness > maxRoughness || (roughness > 1.0 - FLOAT_EPSILON && roughnessFade > 1.0 - FLOAT_EPSILON)) {
395
- gl_FragColor=vec4(iblRadiance,lastFrameAlpha);
396
- return;
397
- }
398
-
399
- vec3 reflected = reflect(viewDir, viewNormal);
400
- vec3 rayDir = reflected *- viewPos.z;
401
- vec3 hitPos = viewPos;
402
- float rayHitDepthDifference;
403
- vec2 coords = RayMarch(rayDir, hitPos, rayHitDepthDifference);
404
- if (coords.x == -1.0){
405
- gl_FragColor=vec4(iblRadiance, lastFrameAlpha);
406
- return;
407
- }
408
-
409
- vec4 SSRTexel = textureLod(inputTexture, coords.xy, 0.0);
410
- vec4 SSRTexelReflected = textureLod(accumulatedTexture, coords.xy, 0.0);
411
- vec3 SSR = SSRTexel.rgb + SSRTexelReflected.rgb;
412
- float roughnessFactor = mix(specular, 1.0, max(0.0, 1.0 - roughnessFade));
413
- vec2 coordsNDC = (coords.xy * 2.0 - 1.0);
414
- float screenFade = 0.1;
415
- float maxDimension = min(1.0, max(abs(coordsNDC.x), abs(coordsNDC.y)));
416
- float reflectionIntensity = 1.0 - (max(0.0, maxDimension - screenFade) / (1.0 - screenFade));
417
- reflectionIntensity = max(0.0, reflectionIntensity);
418
- vec3 finalSSR = mix(iblRadiance, SSR, reflectionIntensity) * roughnessFactor;
419
-
420
- if (fade != 0.0) {
421
- vec3 hitWorldPos = screenSpaceToWorldSpace(coords, rayHitDepthDifference);
422
- float reflectionDistance = distance(hitWorldPos, worldPos) + 1.0;
423
- float opacity = 1.0 / (reflectionDistance * fade * 0.1);
424
- if(opacity > 1.0) opacity=1.0;
425
- finalSSR *= opacity;
426
- }
427
-
428
- finalSSR *= fresnelFactor * intensity;
429
- finalSSR = min(vec3(1.0), finalSSR);
430
- float alpha = hitPos.z == 1.0 ? 1.0 : SSRTexelReflected.a;
431
- alpha = min(lastFrameAlpha, alpha);
432
- gl_FragColor = vec4(finalSSR, alpha);
433
- }
434
-
435
- vec2 RayMarch(vec3 dir, inout vec3 hitPos, inout float rayHitDepthDifference) {
436
- dir=normalize(dir);
437
- dir *= rayDistance / float(steps);
438
- float depth;
439
- vec4 projectedCoord;
440
- vec4 lastProjectedCoord;
441
- float unpackedDepth;
442
- vec4 depthTexel;
443
-
444
- for (int i = 0; i < steps; i++) {
445
- hitPos += dir;
446
- projectedCoord = _projectionMatrix * vec4(hitPos, 1.0);
447
- projectedCoord.xy /= projectedCoord.w;
448
- projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
449
-
450
- #ifndef missedRays
451
- if (
452
- projectedCoord.x < 0.0 ||
453
- projectedCoord.x > 1.0 ||
454
- projectedCoord.y < 0.0 ||
455
- projectedCoord.y > 1.0
456
- ) {
457
- return INVALID_RAY_COORDS;
458
- }
459
- #endif
460
-
461
- depthTexel = textureLod(depthTexture, projectedCoord.xy, 0.0);
462
- unpackedDepth = unpackRGBAToDepth(depthTexel);
463
- depth = fastGetViewZ(unpackedDepth);
464
- rayHitDepthDifference = depth - hitPos.z;
465
-
466
- if (rayHitDepthDifference >= 0.0 && rayHitDepthDifference < thickness){
467
- #if refineSteps == 0
468
- if (dot(depthTexel.rgb, depthTexel.rgb) < FLOAT_EPSILON) return INVALID_RAY_COORDS;
469
- #else
470
- return BinarySearch(dir, hitPos, rayHitDepthDifference);
471
- #endif
472
- }
473
-
474
- #ifndef missedRays
475
- if (hitPos.z > 0.0) return INVALID_RAY_COORDS;
476
- #endif
477
-
478
- lastProjectedCoord = projectedCoord;
479
- }
480
-
481
- hitPos.z = 1.0;
482
-
483
- #ifndef missedRays
484
- return INVALID_RAY_COORDS;
485
- #endif
486
-
487
- rayHitDepthDifference = unpackedDepth;
488
-
489
- return projectedCoord.xy;
490
- }
491
-
492
- vec2 BinarySearch(in vec3 dir, inout vec3 hitPos, inout float rayHitDepthDifference) {
493
- float depth;
494
- vec4 projectedCoord;
495
- vec2 lastMinProjectedCoordXY;
496
- float unpackedDepth;
497
- vec4 depthTexel;
498
-
499
- for (int i = 0; i < refineSteps; i++){
500
- projectedCoord = _projectionMatrix * vec4(hitPos, 1.0);
501
- projectedCoord.xy /= projectedCoord.w;
502
- projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
503
- depthTexel = textureLod(depthTexture, projectedCoord.xy, 0.0);
504
- unpackedDepth = unpackRGBAToDepth(depthTexel);
505
- depth = fastGetViewZ(unpackedDepth);
506
- rayHitDepthDifference = depth - hitPos.z;
507
- dir *= 0.5;
508
-
509
- if (rayHitDepthDifference > 0.0) {
510
- hitPos -= dir;
511
- } else {
512
- hitPos += dir;
513
- }
514
- }
515
-
516
- if (dot(depthTexel.rgb, depthTexel.rgb) < FLOAT_EPSILON) return INVALID_RAY_COORDS;
517
- if (abs(rayHitDepthDifference) > maxDepthDifference) return INVALID_RAY_COORDS;
518
-
519
- projectedCoord = _projectionMatrix*vec4(hitPos, 1.0);
520
- projectedCoord.xy /= projectedCoord.w;
521
- projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
522
- rayHitDepthDifference = unpackedDepth;
523
- return projectedCoord.xy;
524
- }
525
-
526
- float fastGetViewZ(const in float depth){
527
- #ifdef PERSPECTIVE_CAMERA
528
- return nearMulFar / (farMinusNear * depth - cameraFar);
529
- #else
530
- return depth * nearMinusFar - cameraNear;
531
- #endif
532
- }
533
-
534
- #include <common>
535
- #include <cube_uv_reflection_fragment>
536
-
537
- vec3 getIBLRadiance(const in vec3 viewDir, const in vec3 normal, const in float roughness){
538
- #if defined(ENVMAP_TYPE_CUBE_UV)
539
- vec3 reflectVec = reflect(-viewDir, normal);
540
- reflectVec = normalize(mix(reflectVec, normal,roughness * roughness));
541
- reflectVec = inverseTransformDirection(reflectVec, viewMatrix);
542
- vec4 envMapColor = textureCubeUV(envMap, reflectVec, roughness);
543
- return envMapColor.rgb * intensity;
544
- #else
545
- return vec3(0.0);
546
- #endif
547
- }
548
- `
549
- );
550
- class ReflectionsMaterial extends THREE.ShaderMaterial {
551
- constructor() {
552
- super({
553
- type: "ReflectionsMaterial",
554
- uniforms: {
555
- inputTexture: new THREE.Uniform(null),
556
- accumulatedTexture: new THREE.Uniform(null),
557
- normalTexture: new THREE.Uniform(null),
558
- depthTexture: new THREE.Uniform(null),
559
- _projectionMatrix: new THREE.Uniform(new THREE.Matrix4()),
560
- _inverseProjectionMatrix: new THREE.Uniform(new THREE.Matrix4()),
561
- cameraMatrixWorld: new THREE.Uniform(new THREE.Matrix4()),
562
- cameraNear: new THREE.Uniform(0),
563
- cameraFar: new THREE.Uniform(0),
564
- rayDistance: new THREE.Uniform(0),
565
- intensity: new THREE.Uniform(0),
566
- roughnessFade: new THREE.Uniform(0),
567
- fade: new THREE.Uniform(0),
568
- thickness: new THREE.Uniform(0),
569
- ior: new THREE.Uniform(0),
570
- maxDepthDifference: new THREE.Uniform(0),
571
- jitter: new THREE.Uniform(0),
572
- jitterRoughness: new THREE.Uniform(0),
573
- maxRoughness: new THREE.Uniform(0),
574
- samples: new THREE.Uniform(0),
575
- envMap: new THREE.Uniform(null),
576
- envMapPosition: new THREE.Uniform(new THREE.Vector3()),
577
- envMapSize: new THREE.Uniform(new THREE.Vector3()),
578
- viewMatrix: new THREE.Uniform(new THREE.Matrix4())
579
- },
580
- defines: {
581
- steps: 20,
582
- refineSteps: 5,
583
- CUBEUV_TEXEL_WIDTH: 0,
584
- CUBEUV_TEXEL_HEIGHT: 0,
585
- CUBEUV_MAX_MIP: 0,
586
- vWorldPosition: "worldPos"
587
- },
588
- fragmentShader,
589
- vertexShader,
590
- toneMapped: false,
591
- depthWrite: false,
592
- depthTest: false
593
- });
594
- }
595
- }
596
- const getVisibleChildren = (object) => {
597
- const queue = [object];
598
- const objects = [];
599
- while (queue.length !== 0) {
600
- const mesh = queue.shift();
601
- if (mesh.material)
602
- objects.push(mesh);
603
- for (const c of mesh.children) {
604
- if (c.visible)
605
- queue.push(c);
606
- }
607
- }
608
- return objects;
609
- };
610
- const generateCubeUVSize = (parameters) => {
611
- const imageHeight = parameters.envMapCubeUVHeight;
612
- if (imageHeight === null)
613
- return null;
614
- const maxMip = Math.log2(imageHeight) - 2;
615
- const texelHeight = 1 / imageHeight;
616
- const texelWidth = 1 / (3 * Math.max(Math.pow(2, maxMip), 7 * 16));
617
- return {
618
- texelWidth,
619
- texelHeight,
620
- maxMip
621
- };
622
- };
623
- const setupEnvMap = (reflectionsMaterial, envMap, envMapCubeUVHeight) => {
624
- reflectionsMaterial.uniforms.envMap.value = envMap;
625
- const envMapCubeUVSize = generateCubeUVSize({
626
- envMapCubeUVHeight
627
- });
628
- reflectionsMaterial.defines.ENVMAP_TYPE_CUBE_UV = "";
629
- reflectionsMaterial.defines.CUBEUV_TEXEL_WIDTH = envMapCubeUVSize.texelWidth;
630
- reflectionsMaterial.defines.CUBEUV_TEXEL_HEIGHT = envMapCubeUVSize.texelHeight;
631
- reflectionsMaterial.defines.CUBEUV_MAX_MIP = envMapCubeUVSize.maxMip + ".0";
632
- reflectionsMaterial.needsUpdate = true;
633
- };
634
- const isWebGL2Available = () => {
635
- try {
636
- const canvas = document.createElement("canvas");
637
- return !!(window.WebGL2RenderingContext && canvas.getContext("webgl2"));
638
- } catch (e) {
639
- return false;
640
- }
641
- };
642
- class ReflectionsPass extends postprocessing.Pass {
643
- constructor(ssrEffect, options = {}) {
644
- super("ReflectionsPass");
645
- this.ssrEffect = void 0;
646
- this.cachedMaterials = /* @__PURE__ */ new WeakMap();
647
- this.USE_MRT = false;
648
- this.webgl1DepthPass = null;
649
- this.visibleMeshes = [];
650
- this.ssrEffect = ssrEffect;
651
- this._scene = ssrEffect._scene;
652
- this._camera = ssrEffect._camera;
653
- this.fullscreenMaterial = new ReflectionsMaterial();
654
- if (ssrEffect._camera.isPerspectiveCamera)
655
- this.fullscreenMaterial.defines.PERSPECTIVE_CAMERA = "";
656
- const width = options.width || typeof window !== "undefined" ? window.innerWidth : 2e3;
657
- const height = options.height || typeof window !== "undefined" ? window.innerHeight : 1e3;
658
- this.renderTarget = new THREE.WebGLRenderTarget(width, height, {
659
- minFilter: THREE.LinearFilter,
660
- magFilter: THREE.LinearFilter,
661
- type: THREE.HalfFloatType,
662
- depthBuffer: false
663
- });
664
- this.renderPass = new postprocessing.RenderPass(this._scene, this._camera);
665
- this.USE_MRT = isWebGL2Available();
666
- if (this.USE_MRT) {
667
- this.gBuffersRenderTarget = new compat.WebGLMultipleRenderTargets(width, height, 2, {
668
- minFilter: THREE.LinearFilter,
669
- magFilter: THREE.LinearFilter
670
- });
671
- this.normalTexture = this.gBuffersRenderTarget.texture[0];
672
- this.depthTexture = this.gBuffersRenderTarget.texture[1];
673
- } else {
674
- this.webgl1DepthPass = new postprocessing.DepthPass(this._scene, this._camera);
675
- this.webgl1DepthPass.renderTarget.minFilter = THREE.LinearFilter;
676
- this.webgl1DepthPass.renderTarget.magFilter = THREE.LinearFilter;
677
- this.webgl1DepthPass.renderTarget.texture.minFilter = THREE.LinearFilter;
678
- this.webgl1DepthPass.renderTarget.texture.magFilter = THREE.LinearFilter;
679
- this.webgl1DepthPass.setSize(
680
- typeof window !== "undefined" ? window.innerWidth : 2e3,
681
- typeof window !== "undefined" ? window.innerHeight : 1e3
682
- );
683
- this.gBuffersRenderTarget = new THREE.WebGLRenderTarget(width, height, {
684
- minFilter: THREE.LinearFilter,
685
- magFilter: THREE.LinearFilter
686
- });
687
- this.normalTexture = this.gBuffersRenderTarget.texture;
688
- this.depthTexture = this.webgl1DepthPass.texture;
689
- }
690
- this.fullscreenMaterial.uniforms.normalTexture.value = this.normalTexture;
691
- this.fullscreenMaterial.uniforms.depthTexture.value = this.depthTexture;
692
- this.fullscreenMaterial.uniforms.accumulatedTexture.value = this.ssrEffect.temporalResolvePass.accumulatedTexture;
693
- this.fullscreenMaterial.uniforms.cameraMatrixWorld.value = this._camera.matrixWorld;
694
- this.fullscreenMaterial.uniforms._projectionMatrix.value = this._camera.projectionMatrix;
695
- this.fullscreenMaterial.uniforms._inverseProjectionMatrix.value = this._camera.projectionMatrixInverse;
696
- }
697
- setSize(width, height) {
698
- this.renderTarget.setSize(width * this.ssrEffect.resolutionScale, height * this.ssrEffect.resolutionScale);
699
- this.gBuffersRenderTarget.setSize(width * this.ssrEffect.resolutionScale, height * this.ssrEffect.resolutionScale);
700
- this.fullscreenMaterial.uniforms.accumulatedTexture.value = this.ssrEffect.temporalResolvePass.accumulatedTexture;
701
- this.fullscreenMaterial.needsUpdate = true;
702
- }
703
- dispose() {
704
- this.renderTarget.dispose();
705
- this.gBuffersRenderTarget.dispose();
706
- this.renderPass.dispose();
707
- if (!this.USE_MRT)
708
- this.webgl1DepthPass.dispose();
709
- this.fullscreenMaterial.dispose();
710
- this.normalTexture = null;
711
- this.depthTexture = null;
712
- this.velocityTexture = null;
713
- }
714
- keepMaterialMapUpdated(mrtMaterial, originalMaterial, prop, define) {
715
- if (this.ssrEffect[define]) {
716
- if (originalMaterial[prop] !== mrtMaterial[prop]) {
717
- mrtMaterial[prop] = originalMaterial[prop];
718
- mrtMaterial.uniforms[prop].value = originalMaterial[prop];
719
- if (originalMaterial[prop]) {
720
- mrtMaterial.defines[define] = "";
721
- } else {
722
- delete mrtMaterial.defines[define];
723
- }
724
- mrtMaterial.needsUpdate = true;
725
- }
726
- } else if (mrtMaterial[prop] !== void 0) {
727
- mrtMaterial[prop] = void 0;
728
- mrtMaterial.uniforms[prop].value = void 0;
729
- delete mrtMaterial.defines[define];
730
- mrtMaterial.needsUpdate = true;
731
- }
732
- }
733
- setMRTMaterialInScene() {
734
- this.visibleMeshes = getVisibleChildren(this._scene);
735
- for (const c of this.visibleMeshes) {
736
- if (c.material) {
737
- const originalMaterial = c.material;
738
- let [cachedOriginalMaterial, mrtMaterial] = this.cachedMaterials.get(c) || [];
739
- if (originalMaterial !== cachedOriginalMaterial) {
740
- if (mrtMaterial)
741
- mrtMaterial.dispose();
742
- mrtMaterial = new MRTMaterial();
743
- if (this.USE_MRT)
744
- mrtMaterial.defines.USE_MRT = "";
745
- mrtMaterial.normalScale = originalMaterial.normalScale;
746
- mrtMaterial.uniforms.normalScale.value = originalMaterial.normalScale;
747
- const map = originalMaterial.map || originalMaterial.normalMap || originalMaterial.roughnessMap || originalMaterial.metalnessMap;
748
- if (map)
749
- mrtMaterial.uniforms.uvTransform.value = map.matrix;
750
- this.cachedMaterials.set(c, [originalMaterial, mrtMaterial]);
751
- }
752
- this.keepMaterialMapUpdated(mrtMaterial, originalMaterial, "normalMap", "useNormalMap");
753
- this.keepMaterialMapUpdated(mrtMaterial, originalMaterial, "roughnessMap", "useRoughnessMap");
754
- mrtMaterial.uniforms.roughness.value = this.ssrEffect.selection.size === 0 || this.ssrEffect.selection.has(c) ? originalMaterial.roughness || 0 : 1e11;
755
- c.material = mrtMaterial;
756
- }
757
- }
758
- }
759
- unsetMRTMaterialInScene() {
760
- for (const c of this.visibleMeshes) {
761
- var _c$material;
762
- if (((_c$material = c.material) == null ? void 0 : _c$material.type) === "MRTMaterial") {
763
- c.visible = true;
764
- const [originalMaterial] = this.cachedMaterials.get(c);
765
- c.material = originalMaterial;
766
- }
767
- }
768
- }
769
- render(renderer, inputBuffer) {
770
- this.setMRTMaterialInScene();
771
- renderer.setRenderTarget(this.gBuffersRenderTarget);
772
- this.renderPass.render(renderer, this.gBuffersRenderTarget);
773
- this.unsetMRTMaterialInScene();
774
- if (!this.USE_MRT)
775
- this.webgl1DepthPass.renderPass.render(renderer, this.webgl1DepthPass.renderTarget);
776
- this.fullscreenMaterial.uniforms.inputTexture.value = inputBuffer.texture;
777
- this.fullscreenMaterial.uniforms.samples.value = this.ssrEffect.temporalResolvePass.samples;
778
- this.fullscreenMaterial.uniforms.cameraNear.value = this._camera.near;
779
- this.fullscreenMaterial.uniforms.cameraFar.value = this._camera.far;
780
- this.fullscreenMaterial.uniforms.viewMatrix.value.copy(this._camera.matrixWorldInverse);
781
- renderer.setRenderTarget(this.renderTarget);
782
- renderer.render(this.scene, this.camera);
783
- }
784
- }
785
- const defaultSSROptions = {
786
- intensity: 1,
787
- exponent: 1,
788
- distance: 10,
789
- fade: 0,
790
- roughnessFade: 1,
791
- thickness: 10,
792
- ior: 1.45,
793
- maxRoughness: 1,
794
- maxDepthDifference: 10,
795
- blend: 0.9,
796
- correction: 1,
797
- correctionRadius: 1,
798
- blur: 0.5,
799
- blurKernel: 1,
800
- blurSharpness: 10,
801
- jitter: 0,
802
- jitterRoughness: 0,
803
- steps: 20,
804
- refineSteps: 5,
805
- missedRays: true,
806
- useNormalMap: true,
807
- useRoughnessMap: true,
808
- resolutionScale: 1,
809
- velocityResolutionScale: 1
810
- };
811
- const temporalResolve = (
812
- /* glsl */
813
- `
814
- uniform sampler2D inputTexture;
815
- uniform sampler2D accumulatedTexture;
816
- uniform sampler2D velocityTexture;
817
- uniform sampler2D lastVelocityTexture;
818
- uniform float blend;
819
- uniform float correction;
820
- uniform float exponent;
821
- uniform float samples;
822
- uniform vec2 invTexSize;
823
- uniform mat4 curInverseProjectionMatrix;
824
- uniform mat4 curCameraMatrixWorld;
825
- uniform mat4 prevInverseProjectionMatrix;
826
- uniform mat4 prevCameraMatrixWorld;
827
- varying vec2 vUv;
828
-
829
- #define MAX_NEIGHBOR_DEPTH_DIFFERENCE 0.001
830
- #define FLOAT_EPSILON 0.00001
831
- #define FLOAT_ONE_MINUS_EPSILON 0.99999
832
-
833
- vec3 transformexponent;
834
- vec3 undoColorTransformExponent;
835
-
836
- vec3 transformColor(vec3 color) {
837
- if (exponent == 1.0) return color;
838
- return pow(abs(color), transformexponent);
839
- }
840
-
841
- vec3 undoColorTransform(vec3 color) {
842
- if (exponent == 1.0) return color;
843
- return max(pow(abs(color), undoColorTransformExponent), vec3(0.0));
844
- }
845
-
846
- void main() {
847
- if (exponent != 1.0){
848
- transformexponent = vec3(1.0 / exponent);
849
- undoColorTransformExponent = vec3(exponent);
850
- }
851
-
852
- vec4 inputTexel = textureLod(inputTexture, vUv, 0.0);
853
- vec4 accumulatedTexel;
854
- vec3 inputColor = transformColor(inputTexel.rgb);
855
- vec3 accumulatedColor;
856
- float alpha = inputTexel.a;
857
- float velocityDisocclusion;
858
- bool didReproject = false;
859
-
860
- #ifdef boxBlur
861
- vec3 boxBlurredColor = inputTexel.rgb;
862
- #endif
863
-
864
- vec4 velocity = textureLod(velocityTexture, vUv, 0.0);
865
- bool isMoving = alpha < 1.0 || dot(velocity.xy, velocity.xy) > 0.0;
866
- if (isMoving) {
867
- vec3 minNeighborColor = inputColor;
868
- vec3 maxNeighborColor = inputColor;
869
- vec3 col;
870
- vec2 neighborUv;
871
- vec2 reprojectedUv = vUv-velocity.xy;
872
- vec4 lastVelocity = textureLod(lastVelocityTexture, reprojectedUv, 0.0);
873
- float depth = velocity.b;
874
- float closestDepth = depth;
875
- float lastClosestDepth = lastVelocity.b;
876
- float neighborDepth;
877
- float lastNeighborDepth;
878
-
879
- for (int x = -correctionRadius; x <= correctionRadius; x++) {
880
- for (int y = -correctionRadius; y <= correctionRadius; y++) {
881
- if (x != 0 || y != 0) {
882
- neighborUv = vUv + vec2(x,y) * invTexSize;
883
- vec4 neigborVelocity = textureLod(velocityTexture, neighborUv, 0.0);
884
- neighborDepth = neigborVelocity.b;
885
- col = textureLod(inputTexture, neighborUv, 0.0).xyz;
886
- int absX = abs(x);
887
- int absY = abs(y);
888
-
889
- #ifdef dilation
890
- if (absX == 1 && absY == 1) {
891
- if (neighborDepth > closestDepth) {
892
- velocity=neigborVelocity;
893
- closestDepth=neighborDepth;
894
- }
895
-
896
- vec4 lastNeighborVelocity = textureLod(velocityTexture, vUv + vec2(x, y) * invTexSize, 0.0);
897
- lastNeighborDepth = lastNeighborVelocity.b;
898
-
899
- if (neighborDepth > closestDepth) {
900
- lastVelocity = lastNeighborVelocity;
901
- lastClosestDepth = lastNeighborDepth;
902
- }
903
- }
904
- #endif
905
-
906
- if (abs(depth-neighborDepth) < MAX_NEIGHBOR_DEPTH_DIFFERENCE) {
907
- #ifdef boxBlur
908
- if (absX <= 2 && absY <= 2) boxBlurredColor += col;
909
- #endif
910
-
911
- col = transformColor(col);
912
- minNeighborColor = min(col, minNeighborColor);
913
- maxNeighborColor = max(col, maxNeighborColor);
914
- }
915
- }
916
- }
917
- }
918
-
919
- float velocityLength = length(lastVelocity.xy - velocity.xy);
920
- velocityDisocclusion = (velocityLength - 0.000005) * 10.0;
921
- velocityDisocclusion *= velocityDisocclusion;
922
- reprojectedUv = vUv - velocity.xy;
923
-
924
- #ifdef boxBlur
925
- float pxRadius = correctionRadius > 5 ? 121.0 : pow(float(correctionRadius * 2 + 1), 2.0);
926
- boxBlurredColor /= pxRadius;
927
- boxBlurredColor = transformColor(boxBlurredColor);
928
- #endif
929
-
930
- if (
931
- reprojectedUv.x >=0.0 &&
932
- reprojectedUv.x <= 1.0 &&
933
- reprojectedUv.y >= 0.0 &&
934
- reprojectedUv.y <= 1.0
935
- ) {
936
- accumulatedTexel = textureLod(accumulatedTexture, reprojectedUv, 0.0);
937
- accumulatedColor = transformColor(accumulatedTexel.rgb);
938
- vec3 clampedColor = clamp(accumulatedColor, minNeighborColor, maxNeighborColor);
939
- accumulatedColor = mix(accumulatedColor, clampedColor, correction);
940
- didReproject = true;
941
- } else {
942
- #ifdef boxBlur
943
- accumulatedColor=boxBlurredColor;
944
- #else
945
- accumulatedColor=inputColor;
946
- #endif
947
- }
948
-
949
- if (velocity.r > FLOAT_ONE_MINUS_EPSILON && velocity.g > FLOAT_ONE_MINUS_EPSILON) {
950
- alpha = 0.0;
951
- velocityDisocclusion = 1.0;
952
- }
953
- } else {
954
- accumulatedColor = transformColor(textureLod(accumulatedTexture, vUv, 0.0).rgb);
955
- }
956
-
957
- vec3 outputColor = inputColor;
958
-
959
- #include <custom_compose_shader>
960
-
961
- gl_FragColor = vec4(undoColorTransform(outputColor), alpha);
962
- }
963
- `
964
- );
965
- class TemporalResolveMaterial extends THREE.ShaderMaterial {
966
- constructor(customComposeShader) {
967
- const fragmentShader2 = temporalResolve.replace("#include <custom_compose_shader>", customComposeShader);
968
- super({
969
- type: "TemporalResolveMaterial",
970
- uniforms: {
971
- inputTexture: new THREE.Uniform(null),
972
- accumulatedTexture: new THREE.Uniform(null),
973
- velocityTexture: new THREE.Uniform(null),
974
- lastVelocityTexture: new THREE.Uniform(null),
975
- samples: new THREE.Uniform(1),
976
- blend: new THREE.Uniform(0.5),
977
- correction: new THREE.Uniform(1),
978
- exponent: new THREE.Uniform(1),
979
- invTexSize: new THREE.Uniform(new THREE.Vector2())
980
- },
981
- defines: {
982
- correctionRadius: 1
983
- },
984
- vertexShader,
985
- fragmentShader: fragmentShader2
986
- });
987
- }
988
- }
989
- const prev_skinning_pars_vertex = (
990
- /* glsl */
991
- `
992
- #ifdef USE_SKINNING
993
- #ifdef BONE_TEXTURE
994
- uniform sampler2D prevBoneTexture;
995
- mat4 getPrevBoneMatrix( const in float i ) {
996
- float j = i * 4.0;
997
- float x = mod( j, float( boneTextureSize ) );
998
- float y = floor( j / float( boneTextureSize ) );
999
- float dx = 1.0 / float( boneTextureSize );
1000
- float dy = 1.0 / float( boneTextureSize );
1001
- y = dy * ( y + 0.5 );
1002
- vec4 v1 = texture2D( prevBoneTexture, vec2( dx * ( x + 0.5 ), y ) );
1003
- vec4 v2 = texture2D( prevBoneTexture, vec2( dx * ( x + 1.5 ), y ) );
1004
- vec4 v3 = texture2D( prevBoneTexture, vec2( dx * ( x + 2.5 ), y ) );
1005
- vec4 v4 = texture2D( prevBoneTexture, vec2( dx * ( x + 3.5 ), y ) );
1006
- mat4 bone = mat4( v1, v2, v3, v4 );
1007
- return bone;
1008
- }
1009
- #else
1010
- uniform mat4 prevBoneMatrices[ MAX_BONES ];
1011
- mat4 getPrevBoneMatrix( const in float i ) {
1012
- mat4 bone = prevBoneMatrices[ int(i) ];
1013
- return bone;
1014
- }
1015
- #endif
1016
- #endif
1017
- `
1018
- );
1019
- const velocity_vertex = (
1020
- /* glsl */
1021
- `
1022
- vec3 transformed;
1023
-
1024
- // Get the normal
1025
- ${THREE.ShaderChunk.skinbase_vertex}
1026
- ${THREE.ShaderChunk.beginnormal_vertex}
1027
- ${THREE.ShaderChunk.skinnormal_vertex}
1028
- ${THREE.ShaderChunk.defaultnormal_vertex}
1029
-
1030
- // Get the current vertex position
1031
- transformed = vec3( position );
1032
- ${THREE.ShaderChunk.skinning_vertex}
1033
- newPosition = velocityMatrix * vec4( transformed, 1.0 );
1034
-
1035
- // Get the previous vertex position
1036
- transformed = vec3( position );
1037
- ${THREE.ShaderChunk.skinbase_vertex.replace(/mat4 /g, "").replace(/getBoneMatrix/g, "getPrevBoneMatrix")}
1038
- ${THREE.ShaderChunk.skinning_vertex.replace(/vec4 /g, "")}
1039
- prevPosition = prevVelocityMatrix * vec4( transformed, 1.0 );
1040
-
1041
- gl_Position = newPosition;
1042
- `
1043
- );
1044
- class VelocityMaterial extends THREE.ShaderMaterial {
1045
- constructor() {
1046
- super({
1047
- uniforms: {
1048
- prevVelocityMatrix: {
1049
- value: new THREE.Matrix4()
1050
- },
1051
- velocityMatrix: {
1052
- value: new THREE.Matrix4()
1053
- },
1054
- prevBoneTexture: {
1055
- value: null
1056
- },
1057
- interpolateGeometry: {
1058
- value: 0
1059
- },
1060
- intensity: {
1061
- value: 1
1062
- },
1063
- boneTexture: {
1064
- value: null
1065
- },
1066
- alphaTest: {
1067
- value: 0
1068
- },
1069
- map: {
1070
- value: null
1071
- },
1072
- alphaMap: {
1073
- value: null
1074
- },
1075
- opacity: {
1076
- value: 1
1077
- }
1078
- },
1079
- vertexShader: (
1080
- /* glsl */
1081
- `
1082
- #define MAX_BONES 1024
1083
-
1084
- ${THREE.ShaderChunk.skinning_pars_vertex}
1085
- ${prev_skinning_pars_vertex}
1086
-
1087
- uniform mat4 velocityMatrix;
1088
- uniform mat4 prevVelocityMatrix;
1089
- uniform float interpolateGeometry;
1090
- varying vec4 prevPosition;
1091
- varying vec4 newPosition;
1092
- varying vec2 vHighPrecisionZW;
1093
-
1094
- void main() {
1095
-
1096
- ${velocity_vertex}
1097
-
1098
- vHighPrecisionZW = gl_Position.zw;
1099
-
1100
- }`
1101
- ),
1102
- fragmentShader: (
1103
- /* glsl */
1104
- `
1105
- uniform float intensity;
1106
- varying vec4 prevPosition;
1107
- varying vec4 newPosition;
1108
- varying vec2 vHighPrecisionZW;
1109
-
1110
- void main() {
1111
- #ifdef FULL_MOVEMENT
1112
- gl_FragColor = vec4( 1., 1., 1. - gl_FragCoord.z, 0. );
1113
- return;
1114
- #endif
1115
-
1116
- vec2 pos0 = (prevPosition.xy / prevPosition.w) * 0.5 + 0.5;
1117
- vec2 pos1 = (newPosition.xy / newPosition.w) * 0.5 + 0.5;
1118
-
1119
- vec2 vel = pos1 - pos0;
1120
-
1121
- float fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;
1122
-
1123
- gl_FragColor = vec4( vel, 1. - fragCoordZ, 0. );
1124
-
1125
- }`
1126
- )
1127
- });
1128
- this.isVelocityMaterial = true;
1129
- }
1130
- }
1131
- const backgroundColor = new THREE.Color(0);
1132
- const updateProperties = ["visible", "wireframe", "side"];
1133
- class VelocityPass extends postprocessing.Pass {
1134
- constructor(scene, camera) {
1135
- var _window, _window2;
1136
- super("VelocityPass");
1137
- this.cachedMaterials = /* @__PURE__ */ new WeakMap();
1138
- this.lastCameraTransform = {
1139
- position: new THREE.Vector3(),
1140
- quaternion: new THREE.Quaternion()
1141
- };
1142
- this.visibleMeshes = [];
1143
- this.renderedMeshesThisFrame = 0;
1144
- this.renderedMeshesLastFrame = 0;
1145
- this._scene = scene;
1146
- this._camera = camera;
1147
- this.renderTarget = new THREE.WebGLRenderTarget(
1148
- ((_window = window) == null ? void 0 : _window.innerWidth) || 1e3,
1149
- ((_window2 = window) == null ? void 0 : _window2.innerHeight) || 1e3,
1150
- {
1151
- type: THREE.HalfFloatType
1152
- }
1153
- );
1154
- }
1155
- setVelocityMaterialInScene() {
1156
- this.renderedMeshesThisFrame = 0;
1157
- this.visibleMeshes = getVisibleChildren(this._scene);
1158
- for (const c of this.visibleMeshes) {
1159
- var _c$skeleton2;
1160
- const originalMaterial = c.material;
1161
- let [cachedOriginalMaterial, velocityMaterial] = this.cachedMaterials.get(c) || [];
1162
- if (originalMaterial !== cachedOriginalMaterial) {
1163
- var _c$skeleton;
1164
- velocityMaterial = new VelocityMaterial();
1165
- velocityMaterial.lastMatrixWorld = new THREE.Matrix4();
1166
- c.material = velocityMaterial;
1167
- if ((_c$skeleton = c.skeleton) != null && _c$skeleton.boneTexture)
1168
- this.saveBoneTexture(c);
1169
- this.cachedMaterials.set(c, [originalMaterial, velocityMaterial]);
1170
- }
1171
- velocityMaterial.uniforms.velocityMatrix.value.multiplyMatrices(this._camera.projectionMatrix, c.modelViewMatrix);
1172
- if (c.userData.needsUpdatedReflections || originalMaterial.map instanceof THREE.VideoTexture) {
1173
- if (!("FULL_MOVEMENT" in velocityMaterial.defines))
1174
- velocityMaterial.needsUpdate = true;
1175
- velocityMaterial.defines.FULL_MOVEMENT = "";
1176
- } else {
1177
- if ("FULL_MOVEMENT" in velocityMaterial.defines) {
1178
- delete velocityMaterial.defines.FULL_MOVEMENT;
1179
- velocityMaterial.needsUpdate = true;
1180
- }
1181
- }
1182
- c.visible = this.cameraMovedThisFrame || !c.matrixWorld.equals(velocityMaterial.lastMatrixWorld) || c.skeleton || "FULL_MOVEMENT" in velocityMaterial.defines;
1183
- c.material = velocityMaterial;
1184
- if (!c.visible)
1185
- continue;
1186
- this.renderedMeshesThisFrame++;
1187
- for (const prop of updateProperties)
1188
- velocityMaterial[prop] = originalMaterial[prop];
1189
- if ((_c$skeleton2 = c.skeleton) != null && _c$skeleton2.boneTexture) {
1190
- velocityMaterial.defines.USE_SKINNING = "";
1191
- velocityMaterial.defines.BONE_TEXTURE = "";
1192
- velocityMaterial.uniforms.boneTexture.value = c.skeleton.boneTexture;
1193
- }
1194
- }
1195
- }
1196
- saveBoneTexture(object) {
1197
- let boneTexture = object.material.uniforms.prevBoneTexture.value;
1198
- if (boneTexture && boneTexture.image.width === object.skeleton.boneTexture.width) {
1199
- boneTexture = object.material.uniforms.prevBoneTexture.value;
1200
- boneTexture.image.data.set(object.skeleton.boneTexture.image.data);
1201
- } else {
1202
- var _boneTexture;
1203
- (_boneTexture = boneTexture) == null ? void 0 : _boneTexture.dispose();
1204
- const boneMatrices = object.skeleton.boneTexture.image.data.slice();
1205
- const size = object.skeleton.boneTexture.image.width;
1206
- boneTexture = new THREE.DataTexture(boneMatrices, size, size, THREE.RGBAFormat, THREE.FloatType);
1207
- object.material.uniforms.prevBoneTexture.value = boneTexture;
1208
- boneTexture.needsUpdate = true;
1209
- }
1210
- }
1211
- unsetVelocityMaterialInScene() {
1212
- for (const c of this.visibleMeshes) {
1213
- if (c.material.isVelocityMaterial) {
1214
- var _c$skeleton3;
1215
- c.visible = true;
1216
- c.material.lastMatrixWorld.copy(c.matrixWorld);
1217
- c.material.uniforms.prevVelocityMatrix.value.multiplyMatrices(this._camera.projectionMatrix, c.modelViewMatrix);
1218
- if ((_c$skeleton3 = c.skeleton) != null && _c$skeleton3.boneTexture)
1219
- this.saveBoneTexture(c);
1220
- c.material = this.cachedMaterials.get(c)[0];
1221
- }
1222
- }
1223
- }
1224
- setSize(width, height) {
1225
- this.renderTarget.setSize(width, height);
1226
- }
1227
- renderVelocity(renderer) {
1228
- renderer.setRenderTarget(this.renderTarget);
1229
- if (this.renderedMeshesThisFrame > 0) {
1230
- const { background } = this._scene;
1231
- this._scene.background = backgroundColor;
1232
- renderer.render(this._scene, this._camera);
1233
- this._scene.background = background;
1234
- } else {
1235
- renderer.clearColor();
1236
- }
1237
- }
1238
- checkCameraMoved() {
1239
- const moveDist = this.lastCameraTransform.position.distanceToSquared(this._camera.position);
1240
- const rotateDist = 8 * (1 - this.lastCameraTransform.quaternion.dot(this._camera.quaternion));
1241
- if (moveDist > 1e-6 || rotateDist > 1e-6) {
1242
- this.lastCameraTransform.position.copy(this._camera.position);
1243
- this.lastCameraTransform.quaternion.copy(this._camera.quaternion);
1244
- return true;
1245
- }
1246
- return false;
1247
- }
1248
- render(renderer) {
1249
- this.cameraMovedThisFrame = this.checkCameraMoved();
1250
- this.setVelocityMaterialInScene();
1251
- if (this.renderedMeshesThisFrame > 0 || this.renderedMeshesLastFrame > 0)
1252
- this.renderVelocity(renderer);
1253
- this.unsetVelocityMaterialInScene();
1254
- this.renderedMeshesLastFrame = this.renderedMeshesThisFrame;
1255
- }
1256
- }
1257
- const zeroVec2 = new THREE.Vector2();
1258
- class TemporalResolvePass extends postprocessing.Pass {
1259
- constructor(scene, camera, customComposeShader, options = {}) {
1260
- super("TemporalResolvePass");
1261
- this.velocityPass = null;
1262
- this.velocityResolutionScale = 1;
1263
- this.samples = 1;
1264
- this.lastCameraTransform = {
1265
- position: new THREE.Vector3(),
1266
- quaternion: new THREE.Quaternion()
1267
- };
1268
- this._scene = scene;
1269
- this._camera = camera;
1270
- this.renderTarget = new THREE.WebGLRenderTarget(1, 1, {
1271
- minFilter: THREE.LinearFilter,
1272
- magFilter: THREE.LinearFilter,
1273
- type: THREE.HalfFloatType,
1274
- depthBuffer: false
1275
- });
1276
- this.velocityPass = new VelocityPass(scene, camera);
1277
- this.fullscreenMaterial = new TemporalResolveMaterial(customComposeShader);
1278
- this.fullscreenMaterial.defines.correctionRadius = options.correctionRadius || 1;
1279
- if (options.dilation)
1280
- this.fullscreenMaterial.defines.dilation = "";
1281
- if (options.boxBlur)
1282
- this.fullscreenMaterial.defines.boxBlur = "";
1283
- this.setupFramebuffers(1, 1);
1284
- this.checkCanUseSharedVelocityTexture();
1285
- }
1286
- dispose() {
1287
- if (this._scene.userData.velocityTexture === this.velocityPass.renderTarget.texture) {
1288
- delete this._scene.userData.velocityTexture;
1289
- delete this._scene.userData.lastVelocityTexture;
1290
- }
1291
- this.renderTarget.dispose();
1292
- this.accumulatedTexture.dispose();
1293
- this.fullscreenMaterial.dispose();
1294
- this.velocityPass.dispose();
1295
- }
1296
- setSize(width, height) {
1297
- this.renderTarget.setSize(width, height);
1298
- this.velocityPass.setSize(width * this.velocityResolutionScale, height * this.velocityResolutionScale);
1299
- this.velocityPass.renderTarget.texture.minFilter = this.velocityResolutionScale === 1 ? THREE.NearestFilter : THREE.LinearFilter;
1300
- this.velocityPass.renderTarget.texture.magFilter = this.velocityResolutionScale === 1 ? THREE.NearestFilter : THREE.LinearFilter;
1301
- this.velocityPass.renderTarget.texture.needsUpdate = true;
1302
- this.fullscreenMaterial.uniforms.invTexSize.value.set(1 / width, 1 / height);
1303
- this.setupFramebuffers(width, height);
1304
- }
1305
- setupFramebuffers(width, height) {
1306
- if (this.accumulatedTexture)
1307
- this.accumulatedTexture.dispose();
1308
- if (this.lastVelocityTexture)
1309
- this.lastVelocityTexture.dispose();
1310
- this.accumulatedTexture = new THREE.FramebufferTexture(width, height, THREE.RGBAFormat);
1311
- this.accumulatedTexture.minFilter = THREE.LinearFilter;
1312
- this.accumulatedTexture.magFilter = THREE.LinearFilter;
1313
- this.accumulatedTexture.type = THREE.HalfFloatType;
1314
- this.lastVelocityTexture = new THREE.FramebufferTexture(
1315
- width * this.velocityResolutionScale,
1316
- height * this.velocityResolutionScale,
1317
- THREE.RGBAFormat
1318
- );
1319
- this.lastVelocityTexture.minFilter = this.velocityResolutionScale === 1 ? THREE.NearestFilter : THREE.LinearFilter;
1320
- this.lastVelocityTexture.magFilter = this.velocityResolutionScale === 1 ? THREE.NearestFilter : THREE.LinearFilter;
1321
- this.lastVelocityTexture.type = THREE.HalfFloatType;
1322
- this.fullscreenMaterial.uniforms.accumulatedTexture.value = this.accumulatedTexture;
1323
- this.fullscreenMaterial.uniforms.lastVelocityTexture.value = this.lastVelocityTexture;
1324
- this.fullscreenMaterial.needsUpdate = true;
1325
- }
1326
- checkCanUseSharedVelocityTexture() {
1327
- const canUseSharedVelocityTexture = this._scene.userData.velocityTexture && this.velocityPass.renderTarget.texture !== this._scene.userData.velocityTexture;
1328
- if (canUseSharedVelocityTexture) {
1329
- if (this.velocityPass.renderTarget.texture === this.fullscreenMaterial.uniforms.velocityTexture.value) {
1330
- this.fullscreenMaterial.uniforms.lastVelocityTexture.value = this._scene.userData.lastVelocityTexture;
1331
- this.fullscreenMaterial.uniforms.velocityTexture.value = this._scene.userData.velocityTexture;
1332
- this.fullscreenMaterial.needsUpdate = true;
1333
- }
1334
- } else {
1335
- if (this.velocityPass.renderTarget.texture !== this.fullscreenMaterial.uniforms.velocityTexture.value) {
1336
- this.fullscreenMaterial.uniforms.velocityTexture.value = this.velocityPass.renderTarget.texture;
1337
- this.fullscreenMaterial.uniforms.lastVelocityTexture.value = this.lastVelocityTexture;
1338
- this.fullscreenMaterial.needsUpdate = true;
1339
- if (!this._scene.userData.velocityTexture) {
1340
- this._scene.userData.velocityTexture = this.velocityPass.renderTarget.texture;
1341
- this._scene.userData.lastVelocityTexture = this.lastVelocityTexture;
1342
- }
1343
- }
1344
- }
1345
- return this.velocityPass.renderTarget.texture !== this.fullscreenMaterial.uniforms.velocityTexture.value;
1346
- }
1347
- checkNeedsResample() {
1348
- const moveDist = this.lastCameraTransform.position.distanceToSquared(this._camera.position);
1349
- const rotateDist = 8 * (1 - this.lastCameraTransform.quaternion.dot(this._camera.quaternion));
1350
- if (moveDist > 1e-6 || rotateDist > 1e-6) {
1351
- this.samples = 1;
1352
- this.lastCameraTransform.position.copy(this._camera.position);
1353
- this.lastCameraTransform.quaternion.copy(this._camera.quaternion);
1354
- }
1355
- }
1356
- render(renderer) {
1357
- this.samples++;
1358
- this.checkNeedsResample();
1359
- this.fullscreenMaterial.uniforms.samples.value = this.samples;
1360
- renderer.setRenderTarget(this.renderTarget);
1361
- renderer.render(this.scene, this.camera);
1362
- if (Number(THREE.REVISION) >= 165) {
1363
- renderer.copyFramebufferToTexture(this.accumulatedTexture, zeroVec2);
1364
- renderer.setRenderTarget(this.velocityPass.renderTarget);
1365
- renderer.copyFramebufferToTexture(this.lastVelocityTexture, zeroVec2);
1366
- } else {
1367
- renderer.copyFramebufferToTexture(zeroVec2, this.accumulatedTexture);
1368
- renderer.setRenderTarget(this.velocityPass.renderTarget);
1369
- renderer.copyFramebufferToTexture(zeroVec2, this.accumulatedTexture);
1370
- }
1371
- }
1372
- }
1373
- const halton = function halton2(index, base) {
1374
- let fraction = 1;
1375
- let result = 0;
1376
- while (index > 0) {
1377
- fraction /= base;
1378
- result += fraction * (index % base);
1379
- index = ~~(index / base);
1380
- }
1381
- return result;
1382
- };
1383
- const generateHalton23Points = (count) => {
1384
- const data = [];
1385
- let i = 1;
1386
- const end = i + count;
1387
- for (; i < end; i++) {
1388
- data.push([halton(i, 2) - 0.5, halton(i, 3) - 0.5]);
1389
- }
1390
- return data;
1391
- };
1392
- function escapeRegExp(string) {
1393
- return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1394
- }
1395
- const worldposReplace = (
1396
- /* glsl */
1397
- `
1398
- #if defined( USE_ENVMAP ) || defined( ) || defined ( USE_SHADOWMAP )
1399
- vec4 worldPosition = modelMatrix * vec4( transformed, 1.0 );
1400
-
1401
- #ifdef BOX_PROJECTED_ENV_MAP
1402
- vWorldPosition = worldPosition.xyz;
1403
- #endif
1404
- #endif
1405
- `
1406
- );
1407
- const boxProjectDefinitions = (
1408
- /* glsl */
1409
- `
1410
- #ifdef BOX_PROJECTED_ENV_MAP
1411
- uniform vec3 envMapSize;
1412
- uniform vec3 envMapPosition;
1413
- varying vec3 vWorldPosition;
1414
-
1415
- vec3 parallaxCorrectNormal( vec3 v, vec3 cubeSize, vec3 cubePos ) {
1416
- vec3 nDir = normalize( v );
1417
-
1418
- vec3 rbmax = ( .5 * cubeSize + cubePos - vWorldPosition ) / nDir;
1419
- vec3 rbmin = ( -.5 * cubeSize + cubePos - vWorldPosition ) / nDir;
1420
-
1421
- vec3 rbminmax;
1422
-
1423
- rbminmax.x = ( nDir.x > 0. ) ? rbmax.x : rbmin.x;
1424
- rbminmax.y = ( nDir.y > 0. ) ? rbmax.y : rbmin.y;
1425
- rbminmax.z = ( nDir.z > 0. ) ? rbmax.z : rbmin.z;
1426
-
1427
- float correction = min( min( rbminmax.x, rbminmax.y ), rbminmax.z );
1428
- vec3 boxIntersection = vWorldPosition + nDir * correction;
1429
-
1430
- return boxIntersection - cubePos;
1431
- }
1432
- #endif
1433
- `
1434
- );
1435
- const getIBLIrradiance_patch = (
1436
- /* glsl */
1437
- `
1438
- #ifdef BOX_PROJECTED_ENV_MAP
1439
- worldNormal = parallaxCorrectNormal( worldNormal, envMapSize, envMapPosition );
1440
- #endif
1441
- `
1442
- );
1443
- const getIBLRadiance_patch = (
1444
- /* glsl */
1445
- `
1446
- #ifdef BOX_PROJECTED_ENV_MAP
1447
- reflectVec = parallaxCorrectNormal( reflectVec, envMapSize, envMapPosition );
1448
- #endif
1449
- `
1450
- );
1451
- function useBoxProjectedEnvMap(shader, envMapPosition, envMapSize) {
1452
- shader.defines.BOX_PROJECTED_ENV_MAP = "";
1453
- shader.uniforms.envMapPosition = {
1454
- value: envMapPosition
1455
- };
1456
- shader.uniforms.envMapSize = {
1457
- value: envMapSize
1458
- };
1459
- const line1 = new RegExp(
1460
- escapeRegExp("vec3 worldNormal = inverseTransformDirection ( normal , viewMatrix ) ;").replaceAll(" ", "\\s*"),
1461
- "g"
1462
- );
1463
- const line2 = new RegExp(
1464
- escapeRegExp("reflectVec = inverseTransformDirection ( reflectVec , viewMatrix ) ;").replaceAll(" ", "\\s*"),
1465
- "g"
1466
- );
1467
- shader.vertexShader = "varying vec3 vWorldPosition;\n" + shader.vertexShader.replace("#include <worldpos_vertex>", worldposReplace);
1468
- shader.fragmentShader = boxProjectDefinitions + "\n" + shader.fragmentShader.replace("#include <envmap_physical_pars_fragment>", THREE.ShaderChunk.envmap_physical_pars_fragment).replace(
1469
- line1,
1470
- `vec3 worldNormal = inverseTransformDirection( normal, viewMatrix );
1471
- ${getIBLIrradiance_patch}`
1472
- ).replace(
1473
- line2,
1474
- `reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
1475
- ${getIBLRadiance_patch}`
1476
- );
1477
- }
1478
- const noResetSamplesProperties = ["blur", "blurSharpness", "blurKernel"];
1479
- const defaultCubeRenderTarget = new THREE.WebGLCubeRenderTarget(1);
1480
- let pmremGenerator;
1481
- class SSREffect extends postprocessing.Effect {
1482
- /**
1483
- * @param {THREE.Scene} scene The scene of the SSR effect
1484
- * @param {THREE.Camera} camera The camera with which SSR is being rendered
1485
- * @param {SSROptions} [options] The optional options for the SSR effect
1486
- */
1487
- constructor(scene, camera, options = defaultSSROptions) {
1488
- super("SSREffect", finalSSRShader, {
1489
- type: "FinalSSRMaterial",
1490
- uniforms: /* @__PURE__ */ new Map([
1491
- ["reflectionsTexture", new THREE.Uniform(null)],
1492
- ["blur", new THREE.Uniform(0)],
1493
- ["blurSharpness", new THREE.Uniform(0)],
1494
- ["blurKernel", new THREE.Uniform(0)]
1495
- ]),
1496
- defines: /* @__PURE__ */ new Map([["RENDER_MODE", "0"]])
1497
- });
1498
- this.haltonSequence = generateHalton23Points(1024);
1499
- this.haltonIndex = 0;
1500
- this.selection = new postprocessing.Selection();
1501
- this.lastSize = void 0;
1502
- this.cubeCamera = new THREE.CubeCamera(1e-3, 1e3, defaultCubeRenderTarget);
1503
- this.usingBoxProjectedEnvMap = false;
1504
- this._scene = scene;
1505
- this._camera = camera;
1506
- const trOptions = {
1507
- boxBlur: true,
1508
- dilation: true
1509
- };
1510
- options = { ...defaultSSROptions, ...options, ...trOptions };
1511
- this.temporalResolvePass = new TemporalResolvePass(scene, camera, trCompose, options);
1512
- this.uniforms.get("reflectionsTexture").value = this.temporalResolvePass.renderTarget.texture;
1513
- this.reflectionsPass = new ReflectionsPass(this, options);
1514
- this.temporalResolvePass.fullscreenMaterial.uniforms.inputTexture.value = this.reflectionsPass.renderTarget.texture;
1515
- this.lastSize = {
1516
- width: options.width,
1517
- height: options.height,
1518
- resolutionScale: options.resolutionScale,
1519
- velocityResolutionScale: options.velocityResolutionScale
1520
- };
1521
- this.setSize(options.width, options.height);
1522
- this.makeOptionsReactive(options);
1523
- }
1524
- makeOptionsReactive(options) {
1525
- let needsUpdate = false;
1526
- const reflectionPassFullscreenMaterialUniforms = this.reflectionsPass.fullscreenMaterial.uniforms;
1527
- const reflectionPassFullscreenMaterialUniformsKeys = Object.keys(reflectionPassFullscreenMaterialUniforms);
1528
- for (const key of Object.keys(options)) {
1529
- Object.defineProperty(this, key, {
1530
- get() {
1531
- return options[key];
1532
- },
1533
- set(value) {
1534
- if (options[key] === value && needsUpdate)
1535
- return;
1536
- options[key] = value;
1537
- if (!noResetSamplesProperties.includes(key)) {
1538
- this.setSize(this.lastSize.width, this.lastSize.height, true);
1539
- }
1540
- switch (key) {
1541
- case "resolutionScale":
1542
- this.setSize(this.lastSize.width, this.lastSize.height);
1543
- break;
1544
- case "velocityResolutionScale":
1545
- this.temporalResolvePass.velocityResolutionScale = value;
1546
- this.setSize(this.lastSize.width, this.lastSize.height, true);
1547
- break;
1548
- case "blur":
1549
- this.uniforms.get("blur").value = value;
1550
- break;
1551
- case "blurSharpness":
1552
- this.uniforms.get("blurSharpness").value = value;
1553
- break;
1554
- case "blurKernel":
1555
- this.uniforms.get("blurKernel").value = value;
1556
- break;
1557
- case "steps":
1558
- this.reflectionsPass.fullscreenMaterial.defines.steps = parseInt(value);
1559
- this.reflectionsPass.fullscreenMaterial.needsUpdate = needsUpdate;
1560
- break;
1561
- case "refineSteps":
1562
- this.reflectionsPass.fullscreenMaterial.defines.refineSteps = parseInt(value);
1563
- this.reflectionsPass.fullscreenMaterial.needsUpdate = needsUpdate;
1564
- break;
1565
- case "missedRays":
1566
- if (value) {
1567
- this.reflectionsPass.fullscreenMaterial.defines.missedRays = "";
1568
- } else {
1569
- delete this.reflectionsPass.fullscreenMaterial.defines.missedRays;
1570
- }
1571
- this.reflectionsPass.fullscreenMaterial.needsUpdate = needsUpdate;
1572
- break;
1573
- case "correctionRadius":
1574
- this.temporalResolvePass.fullscreenMaterial.defines.correctionRadius = Math.round(value);
1575
- this.temporalResolvePass.fullscreenMaterial.needsUpdate = needsUpdate;
1576
- break;
1577
- case "blend":
1578
- this.temporalResolvePass.fullscreenMaterial.uniforms.blend.value = value;
1579
- break;
1580
- case "correction":
1581
- this.temporalResolvePass.fullscreenMaterial.uniforms.correction.value = value;
1582
- break;
1583
- case "exponent":
1584
- this.temporalResolvePass.fullscreenMaterial.uniforms.exponent.value = value;
1585
- break;
1586
- case "distance":
1587
- reflectionPassFullscreenMaterialUniforms.rayDistance.value = value;
1588
- default:
1589
- if (reflectionPassFullscreenMaterialUniformsKeys.includes(key)) {
1590
- reflectionPassFullscreenMaterialUniforms[key].value = value;
1591
- }
1592
- }
1593
- }
1594
- });
1595
- this[key] = options[key];
1596
- }
1597
- needsUpdate = true;
1598
- }
1599
- setSize(width, height, force = false) {
1600
- if (!force && width === this.lastSize.width && height === this.lastSize.height && this.resolutionScale === this.lastSize.resolutionScale && this.velocityResolutionScale === this.lastSize.velocityResolutionScale)
1601
- return;
1602
- this.temporalResolvePass.setSize(width, height);
1603
- this.reflectionsPass.setSize(width, height);
1604
- this.lastSize = {
1605
- width,
1606
- height,
1607
- resolutionScale: this.resolutionScale,
1608
- velocityResolutionScale: this.velocityResolutionScale
1609
- };
1610
- }
1611
- generateBoxProjectedEnvMapFallback(renderer, position = new THREE.Vector3(), size = new THREE.Vector3(), envMapSize = 512) {
1612
- this.cubeCamera.renderTarget.dispose();
1613
- this.cubeCamera.renderTarget = new THREE.WebGLCubeRenderTarget(envMapSize);
1614
- this.cubeCamera.position.copy(position);
1615
- this.cubeCamera.updateMatrixWorld();
1616
- this.cubeCamera.update(renderer, this._scene);
1617
- if (!pmremGenerator) {
1618
- pmremGenerator = new THREE.PMREMGenerator(renderer);
1619
- pmremGenerator.compileCubemapShader();
1620
- }
1621
- const envMap = pmremGenerator.fromCubemap(this.cubeCamera.renderTarget.texture).texture;
1622
- envMap.minFilter = THREE.LinearFilter;
1623
- envMap.magFilter = THREE.LinearFilter;
1624
- const reflectionsMaterial = this.reflectionsPass.fullscreenMaterial;
1625
- useBoxProjectedEnvMap(reflectionsMaterial, position, size);
1626
- reflectionsMaterial.fragmentShader = reflectionsMaterial.fragmentShader.replace("vec3 worldPos", "worldPos").replace("varying vec3 vWorldPosition;", "vec3 worldPos;");
1627
- reflectionsMaterial.uniforms.envMapPosition.value.copy(position);
1628
- reflectionsMaterial.uniforms.envMapSize.value.copy(size);
1629
- setupEnvMap(reflectionsMaterial, envMap, envMapSize);
1630
- this.usingBoxProjectedEnvMap = true;
1631
- return envMap;
1632
- }
1633
- setIBLRadiance(iblRadiance, renderer) {
1634
- this._scene.traverse((c) => {
1635
- if (c.material) {
1636
- var _renderer$properties$;
1637
- const uniforms = (_renderer$properties$ = renderer.properties.get(c.material)) == null ? void 0 : _renderer$properties$.uniforms;
1638
- if (uniforms && "disableIBLRadiance" in uniforms) {
1639
- uniforms.disableIBLRadiance.value = iblRadiance;
1640
- }
1641
- }
1642
- });
1643
- }
1644
- deleteBoxProjectedEnvMapFallback() {
1645
- const reflectionsMaterial = this.reflectionsPass.fullscreenMaterial;
1646
- reflectionsMaterial.uniforms.envMap.value = null;
1647
- reflectionsMaterial.fragmentShader = reflectionsMaterial.fragmentShader.replace("worldPos = ", "vec3 worldPos = ");
1648
- delete reflectionsMaterial.defines.BOX_PROJECTED_ENV_MAP;
1649
- reflectionsMaterial.needsUpdate = true;
1650
- this.usingBoxProjectedEnvMap = false;
1651
- }
1652
- dispose() {
1653
- super.dispose();
1654
- this.reflectionsPass.dispose();
1655
- this.temporalResolvePass.dispose();
1656
- }
1657
- update(renderer, inputBuffer) {
1658
- if (!this.usingBoxProjectedEnvMap && this._scene.environment) {
1659
- const reflectionsMaterial = this.reflectionsPass.fullscreenMaterial;
1660
- let envMap = null;
1661
- this._scene.traverse((c) => {
1662
- if (!envMap && c.material && !c.material.envMap) {
1663
- const properties = renderer.properties.get(c.material);
1664
- if ("envMap" in properties && properties.envMap instanceof THREE.Texture)
1665
- envMap = properties.envMap;
1666
- }
1667
- });
1668
- if (envMap) {
1669
- const envMapCubeUVHeight = this._scene.environment.image.height;
1670
- setupEnvMap(reflectionsMaterial, envMap, envMapCubeUVHeight);
1671
- }
1672
- }
1673
- this.haltonIndex = (this.haltonIndex + 1) % this.haltonSequence.length;
1674
- const [x, y] = this.haltonSequence[this.haltonIndex];
1675
- const { width, height } = this.lastSize;
1676
- this.temporalResolvePass.velocityPass.render(renderer);
1677
- if (this._camera.setViewOffset)
1678
- this._camera.setViewOffset(width, height, x, y, width, height);
1679
- this.reflectionsPass.render(renderer, inputBuffer);
1680
- this.temporalResolvePass.render(renderer);
1681
- this._camera.clearViewOffset();
1682
- }
1683
- static patchDirectEnvIntensity(envMapIntensity = 0) {
1684
- if (envMapIntensity === 0) {
1685
- THREE.ShaderChunk.envmap_physical_pars_fragment = THREE.ShaderChunk.envmap_physical_pars_fragment.replace(
1686
- "vec3 getIBLRadiance( const in vec3 viewDir, const in vec3 normal, const in float roughness ) {",
1687
- "vec3 getIBLRadiance( const in vec3 viewDir, const in vec3 normal, const in float roughness ) { return vec3(0.0);"
1688
- );
1689
- } else {
1690
- THREE.ShaderChunk.envmap_physical_pars_fragment = THREE.ShaderChunk.envmap_physical_pars_fragment.replace(
1691
- "vec4 envMapColor = textureCubeUV( envMap, reflectVec, roughness );",
1692
- "vec4 envMapColor = textureCubeUV( envMap, reflectVec, roughness ) * " + envMapIntensity.toFixed(5) + ";"
1693
- );
1694
- }
1695
- }
1696
- }
1697
- exports.SSREffect = SSREffect;
1698
- exports.defaultSSROptions = defaultSSROptions;
1699
- //# sourceMappingURL=screen-space-reflections.cjs.map