@threepipe/webgi-plugins 0.6.4 → 0.7.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 (110) hide show
  1. package/CHANGELOG.md +93 -0
  2. package/README.md +13 -23
  3. package/THIRD_PARTY_NOTICES.md +243 -0
  4. package/dist/dependencies.txt +1 -1
  5. package/dist/index.d.ts +1 -1
  6. package/dist/index.js +4536 -18
  7. package/dist/index.js.map +1 -1
  8. package/dist/index.mjs +4545 -18
  9. package/dist/index.mjs.map +1 -1
  10. package/dist/plugins/buffer/VelocityBufferPlugin.d.ts.map +1 -1
  11. package/dist/plugins/extras/AdvancedGroundPlugin.d.ts +3 -2
  12. package/dist/plugins/extras/AdvancedGroundPlugin.d.ts.map +1 -1
  13. package/dist/plugins/extras/AnisotropyPlugin.d.ts.map +1 -1
  14. package/dist/plugins/extras/WatchHandsPlugin.d.ts.map +1 -1
  15. package/dist/plugins/index.d.ts +24 -24
  16. package/dist/plugins/postprocessing/SSGIPlugin.d.ts +3 -2
  17. package/dist/plugins/postprocessing/SSGIPlugin.d.ts.map +1 -1
  18. package/dist/plugins/postprocessing/SSReflectionPlugin.d.ts +1 -1
  19. package/dist/utils/ShadowMapBaker.d.ts +1 -1
  20. package/dist/utils/ShadowMapBaker.d.ts.map +1 -1
  21. package/docs/assets/hierarchy.js +1 -0
  22. package/docs/assets/highlight.css +22 -0
  23. package/docs/assets/icons.js +18 -0
  24. package/docs/assets/icons.svg +1 -0
  25. package/docs/assets/main.js +60 -0
  26. package/docs/assets/navigation.js +1 -0
  27. package/docs/assets/search.js +1 -0
  28. package/docs/assets/style.css +1633 -0
  29. package/docs/classes/AdvancedGroundPlugin.html +69 -0
  30. package/docs/classes/AnisotropyPlugin.html +40 -0
  31. package/docs/classes/BloomPlugin.html +37 -0
  32. package/docs/classes/BloomPluginPass.html +34 -0
  33. package/docs/classes/DepthOfFieldPlugin.html +41 -0
  34. package/docs/classes/DepthOfFieldPluginPass.html +37 -0
  35. package/docs/classes/OutlinePlugin.html +75 -0
  36. package/docs/classes/OutlineRenderPass.html +25 -0
  37. package/docs/classes/SSContactShadowsPlugin.html +37 -0
  38. package/docs/classes/SSGIPlugin.html +42 -0
  39. package/docs/classes/SSGIPluginPass.html +44 -0
  40. package/docs/classes/SSReflectionPlugin.html +43 -0
  41. package/docs/classes/SSReflectionPluginPass.html +45 -0
  42. package/docs/classes/SSVelocityMaterial.html +7 -0
  43. package/docs/classes/TemporalAAPlugin.html +37 -0
  44. package/docs/classes/TemporalAAPluginPass.html +33 -0
  45. package/docs/classes/VelocityBufferPlugin.html +41 -0
  46. package/docs/classes/WatchHandsPlugin.html +39 -0
  47. package/docs/hierarchy.html +1 -0
  48. package/docs/index.html +1 -0
  49. package/docs/types/OutlinePluginPass.html +1 -0
  50. package/docs/types/OutlinePluginTarget.html +1 -0
  51. package/docs/types/SSGIPluginTarget.html +1 -0
  52. package/docs/types/SSReflectionPluginTarget.html +1 -0
  53. package/docs/types/VelocityBufferPluginPass.html +1 -0
  54. package/docs/types/VelocityBufferPluginTarget.html +1 -0
  55. package/docs/variables/anisotropyGLTFExtension.html +1 -0
  56. package/docs/variables/webgiPlugins.html +1 -0
  57. package/package.json +23 -22
  58. package/src/global.d.ts +27 -0
  59. package/src/index.ts +1 -0
  60. package/src/passes/BilateralFilterPass.ts +62 -0
  61. package/src/passes/shaders/ssaoBilateral.glsl +89 -0
  62. package/src/plugins/buffer/VelocityBufferPlugin.ts +270 -0
  63. package/src/plugins/buffer/shaders/VelocityBufferPlugin.mat.frag.glsl +46 -0
  64. package/src/plugins/buffer/shaders/VelocityBufferPlugin.mat.vert.glsl +51 -0
  65. package/src/plugins/buffer/shaders/VelocityBufferPlugin.unpack.glsl +8 -0
  66. package/src/plugins/extras/AdvancedGroundPlugin.ts +385 -0
  67. package/src/plugins/extras/AnisotropyPlugin.ts +519 -0
  68. package/src/plugins/extras/WatchHandsPlugin.ts +156 -0
  69. package/src/plugins/extras/shaders/anisotropyBsdf.glsl +64 -0
  70. package/src/plugins/extras/shaders/anisotropyTBN.glsl +35 -0
  71. package/src/plugins/index.ts +38 -0
  72. package/src/plugins/postprocessing/BloomPlugin.ts +374 -0
  73. package/src/plugins/postprocessing/DepthOfFieldPlugin.ts +470 -0
  74. package/src/plugins/postprocessing/OutlinePlugin.ts +476 -0
  75. package/src/plugins/postprocessing/SSContactShadowsPlugin.ts +374 -0
  76. package/src/plugins/postprocessing/SSGIPlugin.ts +597 -0
  77. package/src/plugins/postprocessing/SSReflectionPlugin.ts +563 -0
  78. package/src/plugins/postprocessing/TemporalAAPlugin.ts +301 -0
  79. package/src/plugins/postprocessing/shaders/dofBlurBox.glsl +46 -0
  80. package/src/plugins/postprocessing/shaders/dofCombine.glsl +68 -0
  81. package/src/plugins/postprocessing/shaders/dofComputeCoC.glsl +21 -0
  82. package/src/plugins/postprocessing/shaders/dofExpandCoC.glsl +56 -0
  83. package/src/plugins/postprocessing/shaders/dofPoissonBox.glsl +90 -0
  84. package/src/plugins/postprocessing/shaders/giPatch.glsl +53 -0
  85. package/src/plugins/postprocessing/shaders/hdrBloom.glsl +101 -0
  86. package/src/plugins/postprocessing/shaders/outline.glsl +60 -0
  87. package/src/plugins/postprocessing/shaders/outlineDepthFrag.glsl +22 -0
  88. package/src/plugins/postprocessing/shaders/outlineDepthVert.glsl +26 -0
  89. package/src/plugins/postprocessing/shaders/poissonDiskSamples.glsl +19 -0
  90. package/src/plugins/postprocessing/shaders/samplePointHelpers.glsl +3 -0
  91. package/src/plugins/postprocessing/shaders/ssrPatch.glsl +145 -0
  92. package/src/plugins/postprocessing/shaders/ssreflectionMain.glsl +69 -0
  93. package/src/plugins/postprocessing/shaders/ssrt.glsl +118 -0
  94. package/src/utils/FSShadowMaterial.ts +65 -0
  95. package/src/utils/RandomizedDirectionalLight.ts +288 -0
  96. package/src/utils/Reflector2.ts +309 -0
  97. package/src/utils/ShadowMapBaker.ts +527 -0
  98. package/src/utils/shaders/computeScreenSpaceVelocity.glsl +16 -0
  99. package/src/utils/shaders/getWorldPositionFromViewZ.glsl +9 -0
  100. package/src/utils/shaders/poissonDiskSamples.glsl +19 -0
  101. package/src/utils/shaders/reflectorSample.glsl +54 -0
  102. package/src/utils/shaders/seperableBlurShaderMasked.glsl +28 -0
  103. package/src/utils/shaders/seperableShadowBlur.glsl +21 -0
  104. package/src/utils/shaders/ssreflection.glsl +141 -0
  105. package/src/utils/shaders/ssrtao.glsl +275 -0
  106. package/src/utils/shaders/temporalaa.glsl +233 -0
  107. package/src/utils/shaders.ts +24 -0
  108. package/tsconfig.json +39 -44
  109. package/dist/version.d.ts +0 -2
  110. package/dist/version.d.ts.map +0 -1
@@ -0,0 +1,597 @@
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
+ SSAOPlugin,
26
+ Texture,
27
+ TextureDataType,
28
+ ThreeViewer,
29
+ uiConfig,
30
+ uiConfigMaterialExtension,
31
+ uiFolderContainer,
32
+ uiImage,
33
+ uiSlider,
34
+ uiToggle,
35
+ uniform,
36
+ UnsignedByteType,
37
+ ValOrFunc,
38
+ Vector2,
39
+ Vector3,
40
+ WebGLRenderTarget,
41
+ } from 'threepipe'
42
+ // import ssrtaoShader from './shaders/SSGIPlugin.pass.glsl' // todo
43
+ // import ssgiPatch from './shaders/SSGIPlugin.patch.glsl'
44
+ import samplePointHelpers from './shaders/samplePointHelpers.glsl'
45
+ import ssrtShader from './shaders/ssrt.glsl'
46
+ import ssgiPatch from './shaders/giPatch.glsl'
47
+ // import ssrtaoShader from './shaders/ssrtao.glsl'
48
+ import {BilateralFilterPass} from "../../passes/BilateralFilterPass";
49
+ import {getShaders} from "../../utils/shaders";
50
+ import {VelocityBufferPlugin} from "../buffer/VelocityBufferPlugin";
51
+
52
+ export type SSGIPluginTarget = WebGLRenderTarget
53
+
54
+ const passId = 'ssrtgi'
55
+ type SSGIPassId = typeof passId
56
+
57
+ /**
58
+ * SSGI Plugin
59
+ *
60
+ * Adds screen space reflections to the scene.
61
+ * @category Plugins
62
+ */
63
+ @uiFolderContainer('SSGI Plugin')
64
+ export class SSGIPlugin
65
+ extends PipelinePassPlugin<SSGIPluginPass, SSGIPassId> {
66
+
67
+ readonly passId = passId
68
+ public static readonly PluginType = 'SSGIPlugin'
69
+ public static readonly OldPluginType = 'SSGI'
70
+
71
+ dependencies = [GBufferPlugin, SSAAPlugin]
72
+
73
+ target?: SSGIPluginTarget
74
+ @uiImage('SSGI Buffer', {readOnly: true}) texture?: Texture
75
+
76
+ @uiConfig(undefined, {unwrapContents: true}) declare protected _pass?: SSGIPluginPass
77
+
78
+ // @onChange2(SSGIPlugin.prototype._createTarget)
79
+ // @uiDropdown('Buffer Type', threeConstMappings.TextureDataType.uiConfig)
80
+ readonly bufferType: TextureDataType // cannot be changed after creation (for now)
81
+
82
+ // @onChange2(SSGIPlugin.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
+ bufferType: TextureDataType = UnsignedByteType,
89
+ sizeMultiplier = 1,
90
+ enabled = true,
91
+ ) {
92
+ super()
93
+ this.enabled = enabled
94
+ this.bufferType = bufferType
95
+ this.sizeMultiplier = sizeMultiplier
96
+ }
97
+
98
+ protected _createTarget(recreate = true) {
99
+ if (!this._viewer) return
100
+ if (recreate) this._disposeTarget()
101
+ if (!this.target)
102
+ this.target = this._viewer.renderManager.createTarget<SSGIPluginTarget>(
103
+ {
104
+ depthBuffer: false,
105
+ type: this.bufferType,
106
+ sizeMultiplier: this.sizeMultiplier,
107
+ // magFilter: NearestFilter,
108
+ // minFilter: NearestFilter,
109
+ // generateMipmaps: false,
110
+ // encoding: LinearEncoding,
111
+ })
112
+ this.texture = this.target.texture
113
+ this.texture.name = 'ssgiBuffer'
114
+
115
+ // if (this._pass) this._pass.target = this.target
116
+ }
117
+
118
+ protected _disposeTarget() {
119
+ if (!this._viewer) return
120
+ if (this.target) {
121
+ this._viewer.renderManager.disposeTarget(this.target)
122
+ this.target = undefined
123
+ }
124
+ this.texture = undefined
125
+ }
126
+
127
+ private _gbufferUnpackExtension = undefined as MaterialExtension|undefined
128
+ private _gbufferUnpackExtensionChanged = ()=>{
129
+ if (!this._pass || !this._viewer) throw new Error('SSGIPlugin: pass/viewer not created yet')
130
+ const newExtension = this._viewer.renderManager.gbufferUnpackExtension
131
+ if (this._gbufferUnpackExtension === newExtension) return
132
+ if (this._gbufferUnpackExtension) this._pass.setGBufferUnpackExtension(undefined)
133
+ this._gbufferUnpackExtension = newExtension
134
+ if (this._gbufferUnpackExtension) this._pass.setGBufferUnpackExtension(this._gbufferUnpackExtension)
135
+ else this._viewer.console.warn('SSGIPlugin: GBuffer unpack extension removed')
136
+ }
137
+
138
+ protected _createPass() {
139
+ if (!this._viewer) throw new Error('SSGIPlugin: viewer not set')
140
+ if (!this._viewer.renderManager.gbufferTarget || !this._viewer.renderManager.gbufferUnpackExtension)
141
+ throw new Error('SSGIPlugin: GBuffer target not created. GBufferPlugin is required.')
142
+ this._createTarget(true)
143
+ return new SSGIPluginPass(this.passId, ()=>this.target)
144
+ }
145
+
146
+ onAdded(viewer: ThreeViewer) {
147
+ super.onAdded(viewer)
148
+ // todo why?
149
+ if (viewer.getPlugin('Ground') || viewer.getPlugin('BaseGroundPlugin')) console.warn('GroundPlugin must be added after SSGIPlugin')
150
+
151
+ // todo updateGBufferFlags like in SSAOPlugin
152
+
153
+ this._gbufferUnpackExtensionChanged()
154
+ viewer.renderManager.addEventListener('gbufferUnpackExtensionChanged', this._gbufferUnpackExtensionChanged)
155
+ viewer.addEventListener('preFrame', this._releaseSsao)
156
+ viewer.forPlugin(VelocityBufferPlugin, (vbp) => {
157
+ this._pass?.material.registerMaterialExtensions([vbp.unpackExtension])
158
+ }, (vbp)=>{
159
+ this._pass?.material?.unregisterMaterialExtensions([vbp.unpackExtension])
160
+ }, this)
161
+ }
162
+
163
+ onRemove(viewer: ThreeViewer): void {
164
+ this._disposeTarget()
165
+ viewer.renderManager.removeEventListener('gbufferUnpackExtensionChanged', this._gbufferUnpackExtensionChanged)
166
+ viewer.removeEventListener('preFrame', this._releaseSsao)
167
+ viewer.getPlugin<SSAOPlugin>('SSAOPlugin')?.enable(SSGIPlugin.PluginType)
168
+ return super.onRemove(viewer)
169
+ }
170
+
171
+ fromJSON(data: any, meta?: any): this|null|Promise<this|null> {
172
+ // legacy
173
+ if (data.passes?.ssrtgi) {
174
+ data = {...data}
175
+ data.pass = data.passes.ssrtgi
176
+ delete data.passes
177
+ if (data.pass.enabled !== undefined) data.enabled = data.pass.enabled
178
+ }
179
+ return super.fromJSON(data, meta)
180
+ }
181
+
182
+ // _beforeRender disables SSAO while SSGI renders. It does not run once the pass is off, so the
183
+ // key is given back here: SSAO is usable again when SSGI is disabled (enable() is a no-op otherwise).
184
+ private _releaseSsao = () => {
185
+ if (this.isDisabled()) this._viewer?.getPlugin<SSAOPlugin>('SSAOPlugin')?.enable(SSGIPlugin.PluginType)
186
+ }
187
+
188
+ protected _beforeRender(_?: IScene, _1?: ICamera, _2?: IRenderManager): boolean {
189
+ if (!this._viewer) return false
190
+ const e = super._beforeRender(_, _1, _2)
191
+ if (e) { // && !lastEnabled) {
192
+ const ssao = this._viewer?.getPlugin<SSAOPlugin>('SSAOPlugin')
193
+ if (ssao && !ssao.isDisabled()) {
194
+ // const c = confirm('SSAO Plugin needs to be disabled to enable SSRTGI or SSRTAO. Disable now?')
195
+ // if (!c) {
196
+ // this.enabled = false
197
+ // e = false // for lastEnabled
198
+ // } else
199
+ ssao.disable(SSGIPlugin.PluginType)
200
+ }
201
+ }
202
+ // lastEnabled = e
203
+ if(!e) return e
204
+
205
+ const progressive = this._viewer.getPlugin(ProgressivePlugin)
206
+ this._pass?.updateShaderProperties([progressive])
207
+
208
+ return e
209
+ }
210
+
211
+ /**
212
+ * @deprecated use {@link target} instead
213
+ */
214
+ get rtgiTarget() {
215
+ console.warn('SSGIPlugin: rtgiTarget is deprecated, use target instead')
216
+ return this.target
217
+ }
218
+
219
+ }
220
+
221
+ @uiFolderContainer('SSGI Pass')
222
+ export class SSGIPluginPass extends ExtendedShaderPass implements IPipelinePass {
223
+ before = ['render']
224
+ after = ['gbuffer', 'depth']
225
+ required = ['render', 'progressive'] // gbuffer required check done in plugin.
226
+
227
+ @serialize() readonly bilateralPass: BilateralFilterPass
228
+
229
+ @uiToggle('GI Enabled')
230
+ @serialize()
231
+ @matDefineBool('SSGI_ENABLED', undefined, undefined, SSGIPluginPass.prototype.setDirty, false)
232
+ giEnabled = true // todo make getter/setter for legacy ssgiEnabled
233
+
234
+ @uiSlider('Intensity', [0, 4])
235
+ @serialize() @uniform() intensity = 2
236
+
237
+ @uiSlider('Power', [0, 3])
238
+ @serialize() @uniform() power = 1.1
239
+
240
+ @uiToggle('Auto radius') @serialize() @uniform() autoRadius = true
241
+
242
+ @uiSlider('Object Radius', [0.01, 10.0])
243
+ @serialize() @uniform() objectRadius = 1
244
+
245
+ @uiSlider('Tolerance', [0.1, 5])
246
+ @serialize() @uniform() tolerance = 1
247
+
248
+ @uiSlider('Bias', [-0.3, 0.3])
249
+ @serialize() @uniform() bias = 0.001
250
+
251
+ @uiSlider('Falloff', [0.0001, 4])
252
+ @serialize() @uniform() falloff = 0.7
253
+
254
+ // todo
255
+ // @uiSlider('Roughness Factor', [0.1, 5])
256
+ // @serialize() @uniform({propKey: 'ssgiRoughnessFactor'}) roughnessFactor = 1
257
+
258
+ @uiSlider('Ray Count', [1, 5], 1, {tags: ['performance']})
259
+ @serialize() @uniform() rayCount = 4
260
+
261
+ @uiSlider('Step count', [1, 16], 1, {tags: ['performance']})
262
+ @serialize()
263
+ @matDefine('RTAO_STEP_COUNT', undefined, undefined, SSGIPluginPass.prototype.setDirty)
264
+ stepCount = 8
265
+
266
+ @uiToggle('Smooth Enabled')
267
+ @serialize() smoothEnabled = true
268
+
269
+ @uiToggle('Render with Camera')
270
+ renderWithCamera = true
271
+
272
+ // todo
273
+ // @uiSlider('Low Quality Frames', [0, 4], 1)
274
+ // @serialize()
275
+ // @matDefine('SSRTAO_LOW_QUALITY_FRAMES', undefined, undefined, SSGIPluginPass.prototype.setDirty)
276
+ // lowQualityFrames = 0
277
+ //
278
+ // @uiToggle('Ignore front rays')
279
+ // @serialize()
280
+ // @matDefineBool('SSRTAO_MASK_FRONT_RAYS', undefined, undefined, SSGIPluginPass.prototype.setDirty)
281
+ // maskFrontRays = true
282
+ //
283
+ // @uiSlider('Mask front rays factor', [-1, 1], 0.01, (that: SSGIPluginPass)=>({hidden: () => !that.maskFrontRays}))
284
+ // @serialize() @uniform() maskFrontFactor = -0.2
285
+
286
+ @uiSlider('Split', [0, 1], 0.01, {tags: ['debug']})
287
+ @serialize() @uniform({propKey: 'ssrtaoSplitX', onChange: SSGIPluginPass.prototype.setDirty})
288
+ split = 0
289
+
290
+ public readonly passId: IPassID
291
+
292
+ constructor(pid: IPassID,
293
+ public target?: ValOrFunc<WebGLRenderTarget|undefined>,
294
+ giEnabled = true) {
295
+ super({
296
+ defines: {
297
+ ['RTAO_STEP_COUNT']: 16,
298
+ // ['SSRTAO_LOW_QUALITY_FRAMES']: 2,
299
+ // ['SSRTAO_MASK_FRONT_RAYS']: true,
300
+ ['PERSPECTIVE_CAMERA']: 1, // set in PerspectiveCamera2
301
+ // ['CHECK_GBUFFER_FLAG']: 0, // todo like ssao plugin
302
+ },
303
+ uniforms: {
304
+ // tDiffuse: {value: null},// todo
305
+ // tNormalDepth: {value: null},// todo
306
+
307
+ // smoothEnabled: {value: false},
308
+ // smoothSigma: {value: new Vector4(0.5, 0.2, 4., 0.4)},
309
+ // smoothScale: {value: new Vector4(1, 10, 1, 8)},
310
+
311
+ tLastThis: {value: null},
312
+ tLastFrame: {value: null}, // set in progressive plugin
313
+ // TODO required?
314
+ screenSize: {value: new Vector2(0, 0)}, // set in ExtendedRenderMaterial
315
+ currentFrameCount: {value: 0}, // set in RenderManager
316
+ frameCount: {value: 0}, // set in RenderManager
317
+ cameraNearFar: {value: new Vector2(0.1, 1000)}, // set in PerspectiveCamera2
318
+ cameraPositionWorld: {value: new Vector3()}, // set in PerspectiveCamera2
319
+ projection: {value: new Matrix4()}, // set in PerspectiveCamera2
320
+ // saoBiasEpsilon: {value: new Vector3(1, 1, 1)},
321
+
322
+ opacity: {value: 1}, // todo?
323
+ intensity: {value: 0.},
324
+ objectRadius: {value: 0},
325
+ autoRadius: {value: giEnabled ? false : true},
326
+ rayCount: {value: 0.1},
327
+ bias: {value: 0.015},
328
+ falloff: {value: 0.7},
329
+ power: {value: 1.1},
330
+ // maskFrontFactor: {value: -0.1},
331
+ tolerance: {value: 0},
332
+ // ssrtaoRoughnessFactor: {value: 1},
333
+ sceneBoundingRadius: {value: 0}, // todo do same in SSAO
334
+
335
+ // reprojection/velocity
336
+ currentProjectionViewMatrix: {value: new Matrix4()},
337
+ lastProjectionViewMatrix: {value: new Matrix4()},
338
+ inverseViewMatrix: {value: new Matrix4()},
339
+
340
+ // split mode
341
+ ssrtaoSplitX: {value: 0.5},
342
+ },
343
+
344
+ vertexShader: shaderUtils.defaultVertex,
345
+
346
+ fragmentShader: `
347
+ varying vec2 vUv;
348
+
349
+ // for gbuffer
350
+ #include <packing>
351
+ #define THREE_PACKING_INCLUDED
352
+ #include <cameraHelpers>
353
+ #include <randomHelpers>
354
+ ${samplePointHelpers}
355
+
356
+ ${ssrtShader}
357
+
358
+ ${getShaders().calculateGI}
359
+ `,
360
+
361
+ }, 'tDiffuse', 'tLastThis', 'tLastFrame')
362
+ this.passId = pid
363
+ this.needsSwap = true
364
+
365
+ this.clear = true // todo
366
+ // this._getUiConfig = this._getUiConfig.bind(this)
367
+
368
+ this.giEnabled = giEnabled
369
+ this.bilateralPass = new BilateralFilterPass(this.target, 'rgba')
370
+
371
+ // this._multiplyPass = new GenericBlendTexturePass(this._target.texture as any, 'c = vec4((1.0-b.r) * a.xyz, a.a);')
372
+ // this._multiplyPass = new GenericBlendTexturePass(this._target.texture as any, 'c = vec4(pow(a.rgb, vec3(a.a*10.)), 1.0);')
373
+ // this._multiplyPass = new GenericBlendTexturePass(this._target.texture as any, 'c = vec4(b.rgb, 0.)+a;')
374
+ // this._multiplyPass = new GenericBlendTexturePass(this._target.texture as any, 'c = a;')
375
+ // this._multiplyPass = new GenericBlendTexturePass(this._target.texture as any, 'c = vec4(0.);')
376
+ // this._multiplyPass = new GenericBlendTexturePass(this._target.texture as any, 'c = vec4(vec3(pow(max(0.,1.-b.r), b.g*10.)), b.a);')
377
+ }
378
+
379
+ render(renderer: IWebGLRenderer, writeBuffer: WebGLRenderTarget, readBuffer: WebGLRenderTarget, deltaTime: number, maskActive: boolean) {
380
+ if (!this.enabled) return
381
+ this.needsSwap = false
382
+ // console.log(renderer.renderManager.frameCount)
383
+ if (!this.renderWithCamera && renderer.renderManager.frameCount < 2) {
384
+ return
385
+ }
386
+
387
+ const target = getOrCall(this.target)
388
+ if (!target) {
389
+ console.warn('SSGIPluginPass: target not defined.')
390
+ return
391
+ }
392
+
393
+ renderer.renderManager.blit(writeBuffer, {
394
+ source: target.texture,
395
+ })
396
+ if (!this.material.defines.HAS_GBUFFER) {
397
+ console.warn('SSGIPluginPass: DepthNormalBuffer required for ssrtao/ssgi')
398
+ }
399
+
400
+ this.uniforms.tLastThis.value = writeBuffer.texture
401
+
402
+ super.render(renderer, target, readBuffer, deltaTime, maskActive)
403
+
404
+ if (this.smoothEnabled) {
405
+ this.bilateralPass.render(renderer, writeBuffer, readBuffer, deltaTime, maskActive)
406
+ }
407
+
408
+ this.uniforms.lastProjectionViewMatrix.value.copy(this.uniforms.currentProjectionViewMatrix.value)
409
+
410
+ // if (this._multiplyPass) {
411
+ // this._multiplyPass.uniforms.tDiffuse2.value = this._target.texture as any
412
+ // this._multiplyPass.render(renderer, writeBuffer, readBuffer, deltaTime, maskActive)
413
+ // this.needsSwap = true
414
+ // }
415
+ }
416
+
417
+ beforeRender(scene: IScene, camera: ICamera, renderManager: IRenderManager) {
418
+ if (!this.enabled) return
419
+ this.updateShaderProperties([scene, camera, renderManager])
420
+ this.uniforms.currentProjectionViewMatrix.value.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse)
421
+ this.uniforms.inverseViewMatrix.value.copy(camera.matrixWorld)
422
+ }
423
+
424
+ readonly materialExtension: MaterialExtension = { // todo set priority wrt ssr etc
425
+ extraUniforms: {
426
+ tSSGIMap: ()=>({value: getOrCall(this.target)?.texture ?? null}),
427
+ ssaoPower: this.material.uniforms.power,
428
+ ssgiIntensity: this.material.uniforms.intensity,
429
+ ssrtaoSplitX: this.material.uniforms.ssrtaoSplitX,
430
+ // ssrMaskFrontFactor: this.material.uniforms.maskFrontFactor,
431
+ },
432
+ extraDefines: {
433
+ ['SSGI_ENABLED']: ()=>this.material.defines.SSGI_ENABLED,
434
+ // ['RTAO_STEP_COUNT']: ()=>this.material.defines.SRTAOSTEP_COUNT,
435
+ // ['SSRTAO_LOW_QUALITY_FRAMES']: ()=>this.material.defines.SSRTAO_LOW_QUALITY_FRAMES,
436
+ // ['SSRTAO_MASK_FRONT_RAYS']: ()=>this.material.defines.SSRTAO_MASK_FRONT_RAYS,
437
+ // ['PERSPECTIVE_CAMERA']: ()=>this.material.defines.PERSPECTIVE_CAMERA,
438
+ // todo gbuffer stuff
439
+ },
440
+ shaderExtender: (shader, _material, _renderer) => {
441
+ if (!shader.defines?.SSRTAO_ENABLED) return
442
+ const ls = `
443
+
444
+ ${ssgiPatch}
445
+
446
+ #if defined(SSGI_ENABLED) && SSGI_ENABLED > 0
447
+ // reflectedLight.directDiffuse = vec3(0.);
448
+ // reflectedLight.indirectDiffuse = vec3(0.);
449
+ // reflectedLight.directSpecular = vec3(0.);
450
+ // reflectedLight.indirectSpecular = vec3(0.);
451
+ #endif
452
+ `
453
+ shader.fragmentShader = shaderReplaceString(shader.fragmentShader,
454
+ 'vec3 totalDiffuse =', ls, {prepend: true})
455
+
456
+ shader.fragmentShader = shaderReplaceString(shader.fragmentShader, '#include <aomap_fragment>', '')
457
+
458
+ // ;(shader as any).defines.USE_UV = '' // todo
459
+
460
+ // this._gbufferUnpackExtension?.shaderExtender?.(shader, _material, _renderer)
461
+ },
462
+ onObjectRender: (_object, material, renderer: IWebGLRenderer) => {
463
+ // const opaque = !material.transparent && (material as MeshPhysicalMaterial).transmission < 0.001
464
+ let x: any = this.enabled// && opaque
465
+ && (this.renderWithCamera || renderer.renderManager.frameCount > 1) &&
466
+ renderer.userData.screenSpaceRendering !== false &&
467
+ !material.userData?.pluginsDisabled &&
468
+ !material.userData?.ssgiDisabled && // the material UI toggle; the two below are older flags
469
+ !material.userData?.ssrtaoDisabled &&
470
+ !material.userData?.ssaoDisabled ? this.split > 0 ? 2 : 1 : 0
471
+
472
+ if (material.defines!.SSRTAO_ENABLED !== x) {
473
+ material.defines!.SSRTAO_ENABLED = x
474
+ material.needsUpdate = true
475
+ }
476
+ // x = material.userData?.ssreflNonPhysical ? 1 : 0
477
+ // if (material.defines!.SSR_NON_PHYSICAL !== x) {
478
+ // material.defines!.SSR_NON_PHYSICAL = x
479
+ // material.needsUpdate = true
480
+ // }
481
+ },
482
+ parsFragmentSnippet: (_renderer)=>glsl`
483
+ #if defined(SSRTAO_ENABLED) && SSRTAO_ENABLED > 0
484
+ uniform float ssaoPower;
485
+ uniform float ssgiIntensity;
486
+ uniform sampler2D tSSGIMap;
487
+ #if defined(SSRTAO_ENABLED) && SSRTAO_ENABLED == 2
488
+ uniform float ssrtaoSplitX;
489
+ #endif
490
+ ${getTexelDecoding('tSSGIMap', getOrCall(this.target)?.texture.colorSpace)}
491
+ ${/*tLastFrame not requried in material extension
492
+ // uniform sampler2D tLastFrame;
493
+ ${getTexelDecoding('tLastFrame', this.material.uniforms!.tLastFrame.value?.colorSpace)}
494
+ */''}
495
+ #include <simpleCameraHelpers>
496
+ #endif
497
+ `,
498
+ computeCacheKey: () => {
499
+ const tex = getOrCall(this.target)?.texture
500
+ return (this.enabled ? '1' : '0') + tex?.colorSpace + tex?.uuid + Object.values(this.material.defines).map(v=>v + '').join(',')
501
+ },
502
+ uuid: SSGIPlugin.PluginType,
503
+ ...uiConfigMaterialExtension(this._getUiConfig.bind(this), SSGIPlugin.PluginType),
504
+ isCompatible: material => {
505
+ return (material as PhysicalMaterial).isPhysicalMaterial
506
+ },
507
+ }
508
+
509
+ /**
510
+ * Returns a uiConfig to toggle SSGI on a material.
511
+ * This uiConfig is added to each material by extension
512
+ * @param material
513
+ * @private
514
+ */
515
+ protected _getUiConfig(material: IMaterial) {
516
+ return {
517
+ type: 'folder',
518
+ label: 'SSGI',
519
+ children: [
520
+ {
521
+ type: 'checkbox',
522
+ label: 'Enabled',
523
+ get value() {
524
+ return !(material.userData.ssgiDisabled ?? false)
525
+ },
526
+ set value(v) {
527
+ if (v === !(material.userData.ssgiDisabled ?? false)) return
528
+ material.userData.ssgiDisabled = !v
529
+ material.setDirty()
530
+ },
531
+ onChange: this.setDirty,
532
+ },
533
+ // todo ssao cast
534
+ // {
535
+ // type: 'checkbox',
536
+ // label: 'Non Physical',
537
+ // get value() {
538
+ // return material.userData.ssreflNonPhysical ?? false
539
+ // },
540
+ // set value(v) {
541
+ // if (v === (material.userData.ssreflNonPhysical ?? false)) return
542
+ // material.userData.ssreflNonPhysical = v
543
+ // material.setDirty()
544
+ // },
545
+ // onChange: this.setDirty,
546
+ // },
547
+ ],
548
+ }
549
+ }
550
+
551
+ set uniformsNeedUpdate(value: true) {
552
+ this.material.uniformsNeedUpdate = value
553
+ this.setDirty()
554
+ }
555
+
556
+ protected _gbufferUnpackExtension: MaterialExtension|undefined
557
+ setGBufferUnpackExtension(extension: MaterialExtension|undefined) {
558
+ if (this._gbufferUnpackExtension === extension) return
559
+ if (this._gbufferUnpackExtension) {
560
+ this.material.unregisterMaterialExtensions([this._gbufferUnpackExtension])
561
+ this.bilateralPass.material.unregisterMaterialExtensions([this._gbufferUnpackExtension])
562
+ }
563
+ this._gbufferUnpackExtension = extension
564
+ if (this._gbufferUnpackExtension) {
565
+ this.material.registerMaterialExtensions([this._gbufferUnpackExtension])
566
+ this.bilateralPass.material.registerMaterialExtensions([this._gbufferUnpackExtension])
567
+ }
568
+
569
+ if (!this._gbufferUnpackExtension) return
570
+
571
+ // todo not possible to remove it?
572
+ // Object.assign(this.materialExtension.extraUniforms!, this._gbufferUnpackExtension.extraUniforms)
573
+ // Object.assign(this.materialExtension.extraDefines!, this._gbufferUnpackExtension.extraDefines)
574
+ }
575
+
576
+ // todo update gbuffer flag like in AO?
577
+
578
+ setDirty() {
579
+ this.materialExtension?.setDirty?.()
580
+ super.setDirty();
581
+ }
582
+
583
+ }
584
+
585
+ declare module 'threepipe' {
586
+ interface IMaterialUserData {
587
+ /**
588
+ * Disable SSGIPlugin for this material.
589
+ */
590
+ ssgiDisabled?: boolean // default false
591
+ ssreflNonPhysical?: boolean // default false
592
+ // [SSGIPlugin.PluginType]?: {
593
+ // enable?: boolean
594
+ // nonPhysical?: boolean
595
+ // }
596
+ }
597
+ }