@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,563 @@
1
+ import {
2
+ ExtendedShaderPass,
3
+ GBufferPlugin,
4
+ getOrCall,
5
+ getTexelDecoding,
6
+ glsl,
7
+ ICamera,
8
+ IMaterial,
9
+ IPassID,
10
+ IPipelinePass,
11
+ IRenderManager,
12
+ IScene,
13
+ IWebGLRenderer,
14
+ matDefine,
15
+ matDefineBool,
16
+ MaterialExtension,
17
+ Matrix4,
18
+ PhysicalMaterial,
19
+ PipelinePassPlugin,
20
+ ProgressivePlugin,
21
+ serialize,
22
+ shaderReplaceString,
23
+ shaderUtils,
24
+ SSAAPlugin,
25
+ Texture,
26
+ TextureDataType,
27
+ ThreeViewer,
28
+ uiConfig,
29
+ uiConfigMaterialExtension,
30
+ uiFolderContainer,
31
+ uiImage,
32
+ uiSlider,
33
+ uiToggle,
34
+ uiVector,
35
+ uniform,
36
+ UnsignedByteType,
37
+ ValOrFunc,
38
+ Vector2,
39
+ Vector3,
40
+ WebGLRenderTarget,
41
+ } from 'threepipe'
42
+ // import ssreflPass from './shaders/SSReflectionPlugin.pass.glsl' // todo
43
+ // import ssreflPatch from './shaders/SSReflectionPlugin.patch.glsl'
44
+ import samplePointHelpers from './shaders/samplePointHelpers.glsl'
45
+ import ssrtShader from './shaders/ssrt.glsl'
46
+ // import ssrShader from './shaders/ssreflection.glsl'
47
+ import ssreflPatch from './shaders/ssrPatch.glsl'
48
+ import ssrShaderMain from './shaders/ssreflectionMain.glsl'
49
+ import {getShaders} from "../../utils/shaders";
50
+ import {VelocityBufferPlugin} from "../buffer/VelocityBufferPlugin";
51
+
52
+ export type SSReflectionPluginTarget = WebGLRenderTarget
53
+
54
+ const passId = 'ssrefl'
55
+ type SSReflectionPassId = typeof passId
56
+
57
+ /**
58
+ * SSReflection Plugin
59
+ *
60
+ * Adds screen space reflections to the scene.
61
+ * @category Plugins
62
+ */
63
+ @uiFolderContainer('SSReflection Plugin')
64
+ export class SSReflectionPlugin
65
+ extends PipelinePassPlugin<SSReflectionPluginPass, SSReflectionPassId> {
66
+
67
+ readonly passId = passId
68
+ public static readonly PluginType = 'SSReflectionPlugin'
69
+ public static readonly OldPluginType = 'SSReflection'
70
+
71
+ dependencies = [GBufferPlugin, SSAAPlugin]
72
+
73
+ target?: SSReflectionPluginTarget
74
+ @uiImage('SSReflection Buffer', {readOnly: true, hideOnEmpty: true}) texture?: Texture
75
+
76
+ @uiConfig(undefined, {unwrapContents: true}) declare protected _pass?: SSReflectionPluginPass
77
+
78
+ // @onChange2(SSReflectionPlugin.prototype._createTarget)
79
+ // @uiDropdown('Buffer Type', threeConstMappings.TextureDataType.uiConfig)
80
+ readonly bufferType: TextureDataType // cannot be changed after creation (for now)
81
+
82
+ // @onChange2(SSReflectionPlugin.prototype._createTarget)
83
+ // @uiSlider('Buffer Size Multiplier', [0.25, 2.0], 0.25)
84
+ readonly sizeMultiplier: number // cannot be changed after creation (for now)
85
+
86
+
87
+ constructor(
88
+ // previously inlineSSR
89
+ public readonly inlineShaderRayTrace = true, // todo: we need roughness in gbuffer for proper inline. this has other advantages also like we can do blurring and denoising etc
90
+ bufferType: TextureDataType = UnsignedByteType, // only when inline is false
91
+ sizeMultiplier = 1, // only when inline is false
92
+ enabled = true,
93
+ ) {
94
+ super()
95
+ this.enabled = enabled
96
+ this.bufferType = bufferType
97
+ this.sizeMultiplier = sizeMultiplier
98
+ }
99
+
100
+ protected _createTarget(recreate = true) {
101
+ if (!this._viewer) return
102
+ if (recreate) this._disposeTarget()
103
+ if (this.inlineShaderRayTrace) return // no need to create.
104
+ if (!this.target)
105
+ this.target = this._viewer.renderManager.createTarget<SSReflectionPluginTarget>(
106
+ {
107
+ depthBuffer: false,
108
+ type: this.bufferType,
109
+ sizeMultiplier: this.sizeMultiplier,
110
+ // magFilter: NearestFilter,
111
+ // minFilter: NearestFilter,
112
+ // generateMipmaps: false,
113
+ // encoding: LinearEncoding,
114
+ })
115
+ this.texture = this.target.texture
116
+ this.texture.name = 'ssreflBuffer'
117
+
118
+ // if (this._pass) this._pass.target = this.target
119
+ }
120
+
121
+ protected _disposeTarget() {
122
+ if (!this._viewer) return
123
+ if (this.target) {
124
+ this._viewer.renderManager.disposeTarget(this.target)
125
+ this.target = undefined
126
+ }
127
+ this.texture = undefined
128
+ }
129
+
130
+ private _gbufferUnpackExtension = undefined as MaterialExtension|undefined
131
+ private _gbufferUnpackExtensionChanged = ()=>{
132
+ if (!this._pass || !this._viewer) throw new Error('SSReflectionPlugin: pass/viewer not created yet')
133
+ const newExtension = this._viewer.renderManager.gbufferUnpackExtension
134
+ if (this._gbufferUnpackExtension === newExtension) return
135
+ if (this._gbufferUnpackExtension) this._pass.setGBufferUnpackExtension(undefined)
136
+ this._gbufferUnpackExtension = newExtension
137
+ if (this._gbufferUnpackExtension) this._pass.setGBufferUnpackExtension(this._gbufferUnpackExtension)
138
+ else this._viewer.console.warn('SSReflectionPlugin: GBuffer unpack extension removed')
139
+ }
140
+
141
+ protected _createPass() {
142
+ if (!this._viewer) throw new Error('SSReflectionPlugin: viewer not set')
143
+ if (!this._viewer.renderManager.gbufferTarget || !this._viewer.renderManager.gbufferUnpackExtension)
144
+ throw new Error('SSReflectionPlugin: GBuffer target not created. GBufferPlugin is required.')
145
+ this._createTarget(true)
146
+ return new SSReflectionPluginPass(this.passId, ()=>this.target, this.inlineShaderRayTrace)
147
+ }
148
+
149
+ onAdded(viewer: ThreeViewer) {
150
+ super.onAdded(viewer)
151
+ // todo why?
152
+ if (viewer.getPlugin('Ground') || viewer.getPlugin('BaseGroundPlugin')) console.warn('GroundPlugin must be added after SSReflectionPlugin')
153
+ this._gbufferUnpackExtensionChanged()
154
+ viewer.renderManager.addEventListener('gbufferUnpackExtensionChanged', this._gbufferUnpackExtensionChanged)
155
+ viewer.forPlugin(VelocityBufferPlugin, (vbp) => {
156
+ this._pass?.material.registerMaterialExtensions([vbp.unpackExtension])
157
+ }, (vbp)=>{
158
+ this._pass?.material?.unregisterMaterialExtensions([vbp.unpackExtension])
159
+ }, this)
160
+ }
161
+
162
+ onRemove(viewer: ThreeViewer): void {
163
+ this._disposeTarget()
164
+ viewer.renderManager.removeEventListener('gbufferUnpackExtensionChanged', this._gbufferUnpackExtensionChanged)
165
+ return super.onRemove(viewer)
166
+ }
167
+
168
+ protected _beforeRender(_?: IScene, _1?: ICamera, _2?: IRenderManager): boolean {
169
+ if (!this._viewer) return false
170
+ const e = super._beforeRender(_, _1, _2)
171
+ if(!e) return e
172
+ const progressive = this._viewer.getPlugin(ProgressivePlugin)
173
+ this._pass?.updateShaderProperties([progressive])
174
+ return e
175
+ }
176
+
177
+ /**
178
+ * @deprecated use {@link target} instead
179
+ */
180
+ get ssrTarget() {
181
+ console.warn('SSReflectionPlugin: ssrTarget is deprecated, use target instead')
182
+ return this.target
183
+ }
184
+
185
+ fromJSON(data: any, meta?: any): this|null|Promise<this|null> {
186
+ // legacy
187
+ if (data.passes?.ssr) {
188
+ data = {...data}
189
+ data.pass = data.passes.ssr
190
+ delete data.passes
191
+ if (data.pass.enabled !== undefined) data.enabled = data.pass.enabled
192
+ }
193
+ return super.fromJSON(data, meta)
194
+ }
195
+
196
+ _viewerListeners = {
197
+ preRender: ()=>{
198
+ if(!this._viewer || this.isDisabled()) return
199
+ if(!this._pass) return
200
+
201
+ const renderCamera = this._viewer.scene.renderCamera
202
+ this._pass.uniforms.currentProjectionViewMatrix.value.multiplyMatrices(renderCamera.projectionMatrix, renderCamera.matrixWorldInverse)
203
+ this._pass.uniforms.inverseViewMatrix.value.copy(renderCamera.matrixWorld)
204
+ },
205
+ postRender: ()=>{
206
+ if(!this._viewer || this.isDisabled()) return
207
+ if(!this._pass) return
208
+
209
+ this._pass.uniforms.lastProjectionViewMatrix.value.copy(this._pass.uniforms.currentProjectionViewMatrix.value)
210
+ }
211
+ }
212
+
213
+ }
214
+
215
+ @uiFolderContainer('SSReflection Pass')
216
+ export class SSReflectionPluginPass extends ExtendedShaderPass implements IPipelinePass {
217
+ before = ['render']
218
+ after = ['gbuffer', 'depth']
219
+ required = ['render', 'progressive'] // gbuffer required check done in plugin.
220
+
221
+ @uiSlider('Intensity', [0, 4])
222
+ @serialize() @uniform() intensity = 1
223
+
224
+ @uiVector('Boost')
225
+ @serialize() @uniform() boost = new Vector3(1, 1, 1)
226
+
227
+ @uiSlider('Object Radius', [0.01, 2.])
228
+ @serialize() @uniform() objectRadius = 1
229
+
230
+ @uiToggle('Auto radius') @serialize() @uniform() autoRadius = true
231
+
232
+ @uiSlider('Power', [0, 3])
233
+ @serialize() @uniform() power = 1.1
234
+
235
+ @uiSlider('Tolerance', [0.1, 5])
236
+ @serialize() @uniform() tolerance = 0.5
237
+
238
+ @uiSlider('Roughness Factor', [0.1, 1.25])
239
+ @serialize() @uniform({propKey: 'ssrRoughnessFactor'}) roughnessFactor = 1
240
+
241
+ @uiSlider('Step count', [1, 32], 1, {tags: ['performance']})
242
+ @serialize()
243
+ @matDefine('SSR_STEP_COUNT', undefined, undefined, SSReflectionPluginPass.prototype.setDirty)
244
+ stepCount = 16
245
+
246
+ @uiSlider('Ray count', [1, 8], 1, {tags: ['performance']})
247
+ @serialize()
248
+ @matDefine('SSR_RAY_COUNT', undefined, undefined, SSReflectionPluginPass.prototype.setDirty)
249
+ rayCount = 1
250
+
251
+ @uiToggle('Ray Blend MAX')
252
+ @serialize()
253
+ @matDefineBool('SSR_RAY_BLEND_MAX', undefined, undefined, SSReflectionPluginPass.prototype.setDirty, true)
254
+ rayBlendMax = false
255
+
256
+ setDirty() {
257
+ this.materialExtension?.setDirty?.()
258
+ super.setDirty();
259
+ }
260
+
261
+ @uiSlider('Low Quality Frames', [0, 4], 1)
262
+ @serialize()
263
+ @matDefine('SSR_LOW_QUALITY_FRAMES', undefined, undefined, SSReflectionPluginPass.prototype.setDirty)
264
+ lowQualityFrames = 0
265
+
266
+ @uiToggle('Ignore front rays')
267
+ @serialize()
268
+ @matDefineBool('SSR_MASK_FRONT_RAYS', undefined, undefined, SSReflectionPluginPass.prototype.setDirty)
269
+ maskFrontRays = true
270
+
271
+ @uiSlider('Mask front rays factor', [-1, 1], 0.01, (that: SSReflectionPluginPass)=>({hidden: () => !that.maskFrontRays}))
272
+ @serialize() @uniform() maskFrontFactor = -0.2
273
+ public readonly passId: IPassID
274
+
275
+ @uiSlider('Split', [0, 1], 0.01, {tags: ['debug']})
276
+ @serialize() @uniform({propKey: 'ssrSplitX', onChange: SSReflectionPluginPass.prototype.setDirty})
277
+ split = 0
278
+
279
+ constructor(pid: IPassID,
280
+ public target?: ValOrFunc<WebGLRenderTarget|undefined>,
281
+ public readonly inlineShaderRayTrace = true) {
282
+ super({
283
+ defines: {
284
+ ['SSR_STEP_COUNT']: 16,
285
+ ['SSR_RAY_COUNT']: 4,
286
+ ['SSR_RAY_BLEND_MAX']: 0,
287
+ ['SSR_LOW_QUALITY_FRAMES']: 0, // todo: only on mobile
288
+ ['SSR_INLINE']: inlineShaderRayTrace ? '1' : '0',
289
+ // ['SSR_NON_PHYSICAL']: '0', only in inline, so it should not be here
290
+ ['SSR_MASK_FRONT_RAYS']: true,
291
+ ['PERSPECTIVE_CAMERA']: 1, // set in PerspectiveCamera2
292
+ // ['SSREFL_PACKING']: 1, // 1 is (r: ssrefl, gba: depth), 2 is (rgb: ssrefl, a: 1), 3 is (rgba: packed_ssrefl), 4 is (rgb: packed_ssrefl, a: 1)
293
+ },
294
+ uniforms: {
295
+ tLastThis: {value: null},
296
+ tLastFrame: {value: null}, // set in progressive plugin
297
+ // TODO required?
298
+ screenSize: {value: new Vector2(0, 0)}, // set in ExtendedRenderMaterial
299
+ currentFrameCount: {value: 0}, // set in RenderManager
300
+ frameCount: {value: 0}, // set in RenderManager
301
+ cameraNearFar: {value: new Vector2(0.1, 1000)}, // set in PerspectiveCamera2
302
+ cameraPositionWorld: {value: new Vector3()}, // set in PerspectiveCamera2
303
+ projection: {value: new Matrix4()}, // set in PerspectiveCamera2
304
+ // saoBiasEpsilon: {value: new Vector3(1, 1, 1)},
305
+
306
+ opacity: {value: 1}, // todo?
307
+ intensity: {value: 0.},
308
+ boost: {value: new Vector3(0, 0, 0)},
309
+ objectRadius: {value: 0},
310
+ autoRadius: {value: false},
311
+ power: {value: 0},
312
+ maskFrontFactor: {value: -0.1},
313
+ tolerance: {value: 0},
314
+ ssrRoughnessFactor: {value: 1},
315
+ sceneBoundingRadius: {value: 0}, // todo do same in SSAO
316
+
317
+ // reprojection/velocity
318
+ currentProjectionViewMatrix: {value: new Matrix4()},
319
+ lastProjectionViewMatrix: {value: new Matrix4()},
320
+ inverseViewMatrix: {value: new Matrix4()},
321
+
322
+ // split mode
323
+ ssrSplitX: {value: 0.5},
324
+ },
325
+
326
+ vertexShader: shaderUtils.defaultVertex,
327
+
328
+ fragmentShader: `
329
+ varying vec2 vUv;
330
+
331
+ // for gbuffer
332
+ #include <packing>
333
+ #define THREE_PACKING_INCLUDED
334
+ #include <cameraHelpers>
335
+ #include <randomHelpers>
336
+ ${samplePointHelpers}
337
+
338
+ ${ssrtShader}
339
+
340
+ ${getShaders().ssReflection}
341
+
342
+ ${ssrShaderMain}
343
+ `,
344
+
345
+ }, 'tDiffuse', 'tLastThis', 'tLastFrame')
346
+ this.passId = pid
347
+
348
+ this.needsSwap = false
349
+ this.clear = true // todo
350
+
351
+ if (inlineShaderRayTrace) {
352
+ // inline... todo test non inline
353
+ Object.assign(this.materialExtension.extraUniforms!, {
354
+ tNormalDepth: this.material.uniforms.tNormalDepth,
355
+ tLastFrame: this.material.uniforms.tLastFrame,
356
+ objectRadius: this.material.uniforms.objectRadius,
357
+ autoRadius: this.material.uniforms.autoRadius,
358
+ tolerance: this.material.uniforms.tolerance,
359
+ ssrRoughnessFactor: this.material.uniforms.ssrRoughnessFactor,
360
+ frameCount: this.material.uniforms.frameCount,
361
+ currentFrameCount: this.material.uniforms.currentFrameCount,
362
+ projection: this.material.uniforms.projection,
363
+ cameraPositionWorld: this.material.uniforms.cameraPositionWorld,
364
+ cameraNearFar: this.material.uniforms.cameraNearFar,
365
+
366
+ sceneBoundingRadius: this.material.uniforms.sceneBoundingRadius,
367
+
368
+ // reprojection
369
+ currentProjectionViewMatrix: this.material.uniforms.currentProjectionViewMatrix,
370
+ lastProjectionViewMatrix: this.material.uniforms.lastProjectionViewMatrix,
371
+ inverseViewMatrix: this.material.uniforms.inverseViewMatrix,
372
+
373
+ ssrSplitX: this.material.uniforms.ssrSplitX,
374
+ })
375
+ }
376
+ // this._getUiConfig = this._getUiConfig.bind(this)
377
+ }
378
+
379
+ render(renderer: IWebGLRenderer, writeBuffer: WebGLRenderTarget, readBuffer: WebGLRenderTarget, deltaTime: number, maskActive: boolean) {
380
+ if (!this.enabled) return
381
+ if (this.inlineShaderRayTrace) {
382
+ this.needsSwap = false
383
+ return
384
+ }
385
+ const target = getOrCall(this.target)
386
+ if (!target) {
387
+ console.warn('SSReflectionPluginPass: target not defined. It must be set when inlineShaderRayTrace is false')
388
+ return
389
+ }
390
+ // this._updateParameters()
391
+ if (!this.material.defines.HAS_GBUFFER) {
392
+ console.warn('SSReflectionPluginPass: DepthNormalBuffer required for ssrefl')
393
+ }
394
+ renderer.renderManager.blit(writeBuffer, {
395
+ source: target.texture,
396
+ })
397
+ this.uniforms.tLastThis.value = writeBuffer.texture
398
+ super.render(renderer, target, readBuffer, deltaTime, maskActive)
399
+
400
+ this.needsSwap = false
401
+ }
402
+
403
+ beforeRender(scene: IScene, camera: ICamera, renderManager: IRenderManager) {
404
+ if (!this.enabled) return
405
+ this.updateShaderProperties([scene, camera, renderManager])
406
+ }
407
+
408
+ readonly materialExtension: MaterialExtension = {
409
+ extraUniforms: {
410
+ tSSReflMap: ()=>({value: getOrCall(this.target)?.texture ?? null}),
411
+ ssrPower: this.material.uniforms.power,
412
+ ssrIntensity: this.material.uniforms.intensity,
413
+ ssrMaskFrontFactor: this.material.uniforms.maskFrontFactor,
414
+ ssrBoost: this.material.uniforms.boost,
415
+ },
416
+ extraDefines: {
417
+ ['SSR_STEP_COUNT']: ()=>this.material.defines.SSR_STEP_COUNT,
418
+ ['SSR_RAY_COUNT']: ()=>this.material.defines.SSR_RAY_COUNT,
419
+ ['SSR_RAY_BLEND_MAX']: ()=>this.material.defines.SSR_RAY_BLEND_MAX,
420
+ ['SSR_LOW_QUALITY_FRAMES']: ()=>this.material.defines.SSR_LOW_QUALITY_FRAMES,
421
+ ['SSR_INLINE']: ()=>this.material.defines.SSR_INLINE,
422
+ ['SSR_MASK_FRONT_RAYS']: ()=>this.material.defines.SSR_MASK_FRONT_RAYS,
423
+ ['PERSPECTIVE_CAMERA']: ()=>this.material.defines.PERSPECTIVE_CAMERA,
424
+ // todo gbuffer stuff
425
+ },
426
+ shaderExtender: (shader, _material, _renderer) => {
427
+ if (!shader.defines?.SSREFL_ENABLED) return
428
+ const ls = `
429
+
430
+ ${ssreflPatch}
431
+
432
+ // reflectedLight.directDiffuse = vec3(0.);
433
+ // reflectedLight.indirectDiffuse = vec3(0.);
434
+ // reflectedLight.directSpecular = vec3(0.);
435
+ // reflectedLight.indirectSpecular = vec3(0.);
436
+
437
+ `
438
+ shader.fragmentShader = shaderReplaceString(shader.fragmentShader,
439
+ '#glMarker beforeModulation', ls, {prepend: true})
440
+
441
+ this._gbufferUnpackExtension?.shaderExtender?.(shader, _material, _renderer)
442
+ },
443
+ onObjectRender: (_object, material, renderer: IWebGLRenderer) => {
444
+ let x: any = this.enabled &&
445
+ renderer.userData.screenSpaceRendering !== false &&
446
+ !material.userData?.pluginsDisabled &&
447
+ !material.userData?.ssreflDisabled ? this.split > 0 ? 2 : 1 : 0
448
+
449
+ if (material.defines!.SSREFL_ENABLED !== x) {
450
+ material.defines!.SSREFL_ENABLED = x
451
+ material.needsUpdate = true
452
+ }
453
+ x = material.userData?.ssreflNonPhysical ? 1 : 0
454
+ if (material.defines!.SSR_NON_PHYSICAL !== x) {
455
+ material.defines!.SSR_NON_PHYSICAL = x
456
+ material.needsUpdate = true
457
+ }
458
+ },
459
+ parsFragmentSnippet: ()=>glsl`
460
+ #if defined(SSREFL_ENABLED) && SSREFL_ENABLED > 0
461
+ uniform float ssrPower;
462
+ uniform float ssrIntensity;
463
+ uniform float ssrMaskFrontFactor;
464
+ uniform vec3 ssrBoost;
465
+ uniform sampler2D tSSReflMap;
466
+ uniform sampler2D tLastFrame;
467
+ ${getTexelDecoding('tSSReflMap', getOrCall(this.target)?.texture.colorSpace)}
468
+ ${getTexelDecoding('tLastFrame', this.material.uniforms!.tLastFrame.value?.colorSpace)}
469
+ ${!this.inlineShaderRayTrace ? '' : `
470
+ #define THREE_PACKING_INCLUDED // by threejs
471
+ #include <cameraHelpers>
472
+ #include <randomHelpers>
473
+ ${samplePointHelpers}
474
+ ${ssrtShader}
475
+ ${getShaders().ssReflection}
476
+ `}
477
+ #include <simpleCameraHelpers>
478
+ #endif
479
+ `,
480
+ computeCacheKey: () => {
481
+ return (this.enabled ? '1' : '0') + getOrCall(this.target)?.texture?.colorSpace + Object.values(this.material.defines).map(v=>v + '').join(',')
482
+ },
483
+ uuid: SSReflectionPlugin.PluginType,
484
+ ...uiConfigMaterialExtension(this._getUiConfig.bind(this), SSReflectionPlugin.PluginType),
485
+ isCompatible: material => {
486
+ return (material as PhysicalMaterial).isPhysicalMaterial
487
+ },
488
+ }
489
+
490
+ /**
491
+ * Returns a uiConfig to toggle SSReflection on a material.
492
+ * This uiConfig is added to each material by extension
493
+ * @param material
494
+ * @private
495
+ */
496
+ protected _getUiConfig(material: IMaterial) {
497
+ return {
498
+ type: 'folder',
499
+ label: 'SSReflection',
500
+ children: [
501
+ {
502
+ type: 'checkbox',
503
+ label: 'Enabled',
504
+ get value() {
505
+ return !(material.userData.ssreflDisabled ?? false)
506
+ },
507
+ set value(v) {
508
+ if (v === !(material.userData.ssreflDisabled ?? false)) return
509
+ material.userData.ssreflDisabled = !v
510
+ material.setDirty()
511
+ },
512
+ onChange: this.setDirty,
513
+ },
514
+ {
515
+ type: 'checkbox',
516
+ label: 'Non Physical',
517
+ get value() {
518
+ return material.userData.ssreflNonPhysical ?? false
519
+ },
520
+ set value(v) {
521
+ if (v === (material.userData.ssreflNonPhysical ?? false)) return
522
+ material.userData.ssreflNonPhysical = v
523
+ material.setDirty()
524
+ },
525
+ onChange: this.setDirty,
526
+ },
527
+ ],
528
+ }
529
+ }
530
+
531
+ set uniformsNeedUpdate(value: true) {
532
+ this.material.uniformsNeedUpdate = value
533
+ this.setDirty()
534
+ }
535
+
536
+ protected _gbufferUnpackExtension: MaterialExtension|undefined
537
+ setGBufferUnpackExtension(extension: MaterialExtension|undefined) {
538
+ if (this._gbufferUnpackExtension === extension) return
539
+ if (this._gbufferUnpackExtension) this.material.unregisterMaterialExtensions([this._gbufferUnpackExtension])
540
+ this._gbufferUnpackExtension = extension
541
+ if (this._gbufferUnpackExtension) this.material.registerMaterialExtensions([this._gbufferUnpackExtension])
542
+
543
+ if (!this._gbufferUnpackExtension) return
544
+
545
+ // todo not possible to remove it?
546
+ Object.assign(this.materialExtension.extraUniforms!, this._gbufferUnpackExtension.extraUniforms)
547
+ Object.assign(this.materialExtension.extraDefines!, this._gbufferUnpackExtension.extraDefines)
548
+ }
549
+ }
550
+
551
+ declare module 'threepipe' {
552
+ interface IMaterialUserData {
553
+ /**
554
+ * Disable SSReflectionPlugin for this material.
555
+ */
556
+ ssreflDisabled?: boolean // default false
557
+ ssreflNonPhysical?: boolean // default false
558
+ // [SSReflectionPlugin.PluginType]?: {
559
+ // enable?: boolean
560
+ // nonPhysical?: boolean
561
+ // }
562
+ }
563
+ }