@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,527 @@
1
+ import {
2
+ BasicShadowMap,
3
+ BufferGeometry,
4
+ glsl,
5
+ IDisposable,
6
+ Layers,
7
+ MaterialExtension,
8
+ Mesh,
9
+ NoBlending,
10
+ NoColorSpace,
11
+ onChange,
12
+ PhysicalMaterial, RenderManager,
13
+ RGBAFormat,
14
+ serialize,
15
+ ShaderMaterial2,
16
+ shaderReplaceString, shaderUtils,
17
+ SimpleEventDispatcher,
18
+ Texture,
19
+ ThreeSerialization,
20
+ ThreeViewer,
21
+ UnsignedByteType,
22
+ Vector2,
23
+ Vector3,
24
+ WebGLRenderTarget,
25
+ } from 'threepipe'
26
+ import {RandomizedDirectionalLight} from "./RandomizedDirectionalLight";
27
+ import {FSShadowMaterial} from "./FSShadowMaterial";
28
+ import seperableShadowBlur from './shaders/seperableShadowBlur.glsl'
29
+
30
+ const ShaderChunk = RenderManager.ShaderChunk
31
+
32
+ export class ShadowMapBaker extends SimpleEventDispatcher<'shadowBaking'|'shadowBaked'> implements IDisposable {
33
+ enabled = true // do not serialize // this is to disable baking for a while, toggling this does not reset the baked shadow map
34
+
35
+ get attachedMesh(): Mesh<BufferGeometry, PhysicalMaterial> | undefined {
36
+ return this._attachedMesh
37
+ }
38
+
39
+ set attachedMesh(value: Mesh<BufferGeometry, PhysicalMaterial> | undefined) {
40
+ if (this._attachedMesh !== value) {
41
+ this._attachedMesh && this.cleanupMaterial()
42
+ this._attachedMesh = value
43
+ this._attachedMesh && this._updateMaterial()
44
+ }
45
+ }
46
+ private _shadowMat: FSShadowMaterial
47
+ private _shadowBlurMat: ShaderMaterial2
48
+
49
+ get target(): WebGLRenderTarget | undefined {
50
+ return this._target
51
+ }
52
+
53
+ private _target?: WebGLRenderTarget
54
+
55
+ get light(): RandomizedDirectionalLight {
56
+ return this._light
57
+ }
58
+
59
+ @serialize('randomizedLight') private readonly _light: RandomizedDirectionalLight
60
+ private readonly _lightLayer = 5 // todo: get free one from the viewer.
61
+ private _viewer: ThreeViewer
62
+
63
+ constructor(viewer: ThreeViewer) {
64
+ super()
65
+ this._viewer = viewer
66
+ const light = new RandomizedDirectionalLight(0xffffff, 10, {
67
+ near: 1.5,
68
+ far: 20,
69
+ bias: 0.0,
70
+ frustumSize: 4,
71
+ width: 1024,
72
+ height: 1024,
73
+ enabled: true,
74
+ radius: 10,
75
+ normalBias: 0,
76
+ }, {
77
+ direction: new Vector3(0.2, 1, 0.2).normalize(),
78
+ spread: 0.9,
79
+ focus: 1.0,
80
+ distanceScale: 20,
81
+ minDistanceScale: new Vector3(10, 10, 10),
82
+ normalDirection: new Vector3(0, 1, 0),
83
+ })
84
+ light.shadow.camera.updateProjectionMatrix()
85
+ light.layers.disableAll()
86
+ light.layers.set(this._lightLayer)
87
+
88
+ this._light = light
89
+ viewer.scene.addObject(this._light, {addToRoot: true})
90
+
91
+ this._shadowMat = new FSShadowMaterial({
92
+ color: '#ffffff',
93
+ toneMapped: false,
94
+ depthWrite: false,
95
+ depthTest: false,
96
+ premultipliedAlpha: false,
97
+ opacity: 1,
98
+ transparent: false,
99
+ blending: NoBlending,
100
+ })
101
+
102
+ this._shadowBlurMat = new ShaderMaterial2({
103
+ uniforms: {
104
+ 'colorTexture': {value: null},
105
+ 'step': {value: 0.1},
106
+ 'size': {value: new Vector2(0.5, 0.5)},
107
+ 'direction': {value: new Vector2(0.5, 0.5)},
108
+ },
109
+
110
+ vertexShader: shaderUtils.defaultVertex,
111
+
112
+ fragmentShader: seperableShadowBlur,
113
+ })
114
+ }
115
+
116
+ dispose() {
117
+ this._shadowMat.dispose()
118
+ this._shadowBlurMat.dispose()
119
+ if (this._target) this._viewer.renderManager.disposeTarget(this._target)
120
+ this._target = undefined
121
+ this._light.dispose() // removes the light from the scene and frees its shadow map
122
+ this.reset()
123
+ }
124
+
125
+ public cleanupMaterial() {
126
+ this._updateMaterial(true)
127
+ }
128
+
129
+ private _frameNumber = 0
130
+
131
+ @onChange(ShadowMapBaker.prototype.reset)
132
+ @serialize() maxFrameNumber = 400
133
+
134
+ @onChange(ShadowMapBaker.prototype.reset)
135
+ @serialize() smoothShadow = false
136
+
137
+ @serialize()
138
+ @onChange(ShadowMapBaker.prototype.reset)
139
+ shadowMapType = BasicShadowMap // this is fine since we are randomizing
140
+
141
+ private _attachedMesh: Mesh<BufferGeometry, PhysicalMaterial> | undefined
142
+
143
+ @onChange(ShadowMapBaker.prototype._groundMapModeChanged)
144
+ @serialize() groundMapMode: 'aoMap' | 'map' | 'alphaMap' = 'aoMap'
145
+
146
+ @serialize()
147
+ @onChange(ShadowMapBaker.prototype._alphaVignetteChanged)
148
+ alphaVignette = true // only works for transparent and transmissive materials.
149
+
150
+ @serialize()
151
+ @onChange(ShadowMapBaker.prototype._alphaVignetteChanged)
152
+ alphaVignetteAxis = 'xy' // x or y or xy.
153
+
154
+ private _groundMapModeChanged() {
155
+ if (this._attachedMesh) {
156
+ this.cleanupMaterial()
157
+ this._updateMaterial()
158
+ if (this.groundMapMode === 'alphaMap') this._attachedMesh.material.transparent = true
159
+ else this._attachedMesh.material.transparent = false
160
+ }
161
+ this.reset()
162
+ }
163
+ private _alphaVignetteChanged() {
164
+ this.materialExtension?.setDirty?.()
165
+ this._viewer?.setDirty()
166
+ }
167
+
168
+ fromJSON(data: any, meta?: any, _internal: boolean = false): this {
169
+ if(!_internal) {
170
+ return ThreeSerialization.Deserialize(data, this, meta, false)
171
+ }else {
172
+ ThreeSerialization.Deserialize(data, this, meta, true)
173
+ this.reset()
174
+ }
175
+ return this
176
+ }
177
+
178
+ reset(): void {
179
+ // this._updateMaterial()
180
+ this._frameNumber = 0
181
+ }
182
+
183
+ get frameNumber() {
184
+ return this._frameNumber
185
+ }
186
+
187
+ shadowAutoUpdate = true
188
+
189
+ private _bakeCounter = 0
190
+
191
+ @serialize() maxBakeCount = Infinity
192
+
193
+ autoUpdateShadow(): boolean {
194
+ if (this.shadowAutoUpdate) {
195
+ if (this._bakeCounter >= this.maxBakeCount) return false
196
+ if (!this.updateShadow()) return false
197
+ if (this._frameNumber === this.maxFrameNumber) {
198
+ this.dispatchEvent({type: 'shadowBaked', bakeCount: ++this._bakeCounter})
199
+ } else if (this._frameNumber > 0 && this._frameNumber < this.maxFrameNumber) {
200
+ this.dispatchEvent({type: 'shadowBaking', progress: this._frameNumber / this.maxFrameNumber, bakeCount: this._bakeCounter})
201
+ }
202
+ return true
203
+ }
204
+ return false
205
+ }
206
+
207
+ updateShadow(): boolean {
208
+ if (!this.enabled) return false
209
+ const mesh = this._attachedMesh
210
+ if (!mesh) return false
211
+ if (++this._frameNumber > this.maxFrameNumber) return false
212
+
213
+ const customSize = 1024
214
+ if (!this._target) {
215
+ this._target = this._viewer.renderManager.createTarget({ // todo: dispose somewhere
216
+ // type: HalfFloatType,
217
+ type: UnsignedByteType,
218
+ depthBuffer: false,
219
+ size: customSize ? new Vector2(customSize, customSize) : undefined,
220
+ sizeMultiplier: customSize ? undefined : 1,
221
+ // encoding: sRGBEncoding,
222
+ colorSpace: NoColorSpace,
223
+ format: RGBAFormat,
224
+ }) as WebGLRenderTarget
225
+ // this._target.texture.userData.serializableRenderTarget = true // not working properly and exported image size is high, see BaseRenderer
226
+ }
227
+
228
+ if (this._frameNumber < 3)
229
+ this._light.randomizePosition(0, 1, 0)
230
+ else
231
+ this._light.randomizePosition(this._frameNumber)
232
+ mesh.castShadow = false
233
+ const renderer = this._viewer.renderManager.renderer
234
+ const shadowMap = renderer.shadowMap
235
+
236
+ const shadowMapType = shadowMap.type
237
+ const shadowMapNeedsUpdate = shadowMap.needsUpdate
238
+ const shadowMapAutoUpdate = shadowMap.autoUpdate
239
+ shadowMap.type = this.shadowMapType
240
+ shadowMap.needsUpdate = true
241
+ shadowMap.autoUpdate = false
242
+
243
+ const scene = this._viewer.scene
244
+ // disable all other lights
245
+ const sceneLightLayer = new Layers()
246
+ sceneLightLayer.disableAll()
247
+ scene.traverse((o: any) => {
248
+ if (o.isLight && o !== this._light) {
249
+ o.userData.__gp_layers = o.layers
250
+ o.layers = sceneLightLayer
251
+ }
252
+ })
253
+ const cam = scene.mainCamera
254
+ if ((cam.layers.mask & 1 << this._lightLayer) !== 0) throw 'Camera can render pseudo directional light, check layers'
255
+
256
+ cam.layers.enable(this._lightLayer)
257
+ mesh.layers.disable(this._lightLayer)
258
+
259
+ // scene.modelObject.traverse((o: any)=>{
260
+ // if (o.assetType === 'widget') {
261
+ // o.traverse((o1: any)=> {
262
+ // o1.castShadow = false
263
+ // })
264
+ // }
265
+ // })
266
+
267
+ renderer.renderWithModes({
268
+ shadowMapRender:true,
269
+ backgroundRender:false,
270
+ sceneRender:false,
271
+ }, ()=> renderer.render(scene, scene.mainCamera))
272
+ // scene.modelObject.traverse((o: any)=>{
273
+ // if (o.assetType === 'widget') {
274
+ // o.castShadow = false
275
+ // }
276
+ // })
277
+
278
+ cam.layers.disable(this._lightLayer)
279
+
280
+ // Only render light layer
281
+ const camLayers = cam.layers.mask
282
+ cam.layers.set(this._lightLayer)
283
+ mesh.layers.enable(this._lightLayer)
284
+
285
+ const lastThisTarget = (customSize ? this._viewer.renderManager.getTempTarget({
286
+ type: UnsignedByteType,
287
+ depthBuffer: false,
288
+ size: new Vector2(customSize, customSize),
289
+ // encoding: sRGBEncoding,
290
+ colorSpace: NoColorSpace,
291
+ format: RGBAFormat,
292
+ }) : this._viewer.renderManager.composerTarget) as WebGLRenderTarget
293
+
294
+ const texture = lastThisTarget.texture as Texture
295
+ const lastThisTargetColorSpace = texture.colorSpace
296
+ texture.colorSpace = NoColorSpace
297
+
298
+ this._viewer.renderManager.blit(lastThisTarget, {clear: true, source: this._target.texture})
299
+
300
+ {
301
+
302
+ // const currentEnvironment = scene.modelObject.environment
303
+ // const currentBackground = scene.modelObject.background
304
+ const currentMaterial = mesh.material
305
+ const currentFrustumCulled = mesh.frustumCulled
306
+ const currentRt = renderer.getRenderTarget()
307
+ const activeCubeFace = renderer.getActiveCubeFace()
308
+ const activeMipLevel = renderer.getActiveMipmapLevel()
309
+ // scene.modelObject.background = null
310
+ // scene.modelObject.environment = null
311
+ mesh.material = this._shadowMat as any
312
+ mesh.frustumCulled = false
313
+ renderer.setRenderTarget(this._target)
314
+
315
+ const useMovingAverage = false
316
+ this._shadowMat.opacity = useMovingAverage ? 1. / this.maxFrameNumber : Math.max(1. / this.maxFrameNumber, 1. / this._frameNumber)
317
+ this._shadowMat.lastFrameTexture = lastThisTarget.texture
318
+ this._shadowMat.needsUpdate = true
319
+
320
+ renderer.renderWithModes({
321
+ shadowMapRender:false,
322
+ backgroundRender:false,
323
+ opaqueRender:true,
324
+ transparentRender:false,
325
+ transmissionRender:false,
326
+ }, ()=> renderer.render(scene, scene.mainCamera))
327
+
328
+ renderer.setRenderTarget(currentRt, activeCubeFace, activeMipLevel)
329
+ mesh.frustumCulled = currentFrustumCulled
330
+ mesh.material = currentMaterial
331
+ // scene.modelObject.environment = currentEnvironment
332
+ // scene.modelObject.background = currentBackground
333
+
334
+ if (this.smoothShadow)
335
+ this._applySmoothFilter(this._target, lastThisTarget as WebGLRenderTarget)
336
+ }
337
+
338
+ lastThisTarget.texture.colorSpace = lastThisTargetColorSpace
339
+ if (customSize) {
340
+ this._viewer.renderManager.releaseTempTarget(lastThisTarget)
341
+ }
342
+
343
+ mesh.layers.disable(this._lightLayer)
344
+ cam.layers.mask = camLayers
345
+
346
+ // enable all other lights
347
+ scene.modelObject.traverse((o: any) => {
348
+ if (o.isLight && o !== this._light.lightObject) {
349
+ o.layers = o.userData.__gp_layers
350
+ delete o.userData.__gp_layers
351
+ }
352
+ })
353
+
354
+ shadowMap.type = shadowMapType
355
+ shadowMap.needsUpdate = shadowMapNeedsUpdate
356
+ shadowMap.autoUpdate = shadowMapAutoUpdate
357
+
358
+ mesh.castShadow = true
359
+
360
+ if (this._frameNumber < 3 || this._frameNumber > Math.min(100, this.maxFrameNumber)
361
+ && this._frameNumber % 4 === 0
362
+ ) {
363
+ // if (this._attachedMesh)
364
+ // this.materialExtension.extraUniforms!.transitionOpacity.value = Math.min(1, 4. * this._frameNumber / this.maxFrameNumber)
365
+
366
+ this._updateMaterial()
367
+ // this._viewer.setDirty()
368
+ // mesh.dispatchEvent({type: 'materialUpdate'})
369
+ mesh.material.setDirty()
370
+ }
371
+
372
+ return true
373
+ }
374
+
375
+ private _updateMaterial(isNull = false) {
376
+ if (!this._attachedMesh) return
377
+ if (isNull) {
378
+ if (this._attachedMesh.material.alphaMap === this._target?.texture) {
379
+ this._attachedMesh.material.alphaMap = null
380
+ }
381
+ if (this._attachedMesh.material.aoMap === this._target?.texture) {
382
+ this._attachedMesh.material.aoMap = null
383
+ }
384
+ if (this._attachedMesh.material.map === this._target?.texture) {
385
+ this._attachedMesh.material.map = null
386
+ }
387
+
388
+ } else if (this._target) {
389
+ if (this.groundMapMode === 'alphaMap') {
390
+ this._attachedMesh.material.alphaMap = this._target.texture
391
+ }
392
+ if (this.groundMapMode === 'aoMap') {
393
+ this._attachedMesh.material.aoMap = this._target.texture
394
+ }
395
+ if (this.groundMapMode === 'map') {
396
+ this._attachedMesh.material.map = this._target.texture
397
+ }
398
+ }
399
+
400
+ // needed in DepthNormalPass when rendering to depth
401
+ if (this._attachedMesh.material) {
402
+ this._attachedMesh.material.userData.ALPHA_I_RGBA_PACKING = !isNull && this.groundMapMode === 'alphaMap'
403
+ this._attachedMesh.material.alphaTest = !isNull && this.groundMapMode === 'alphaMap' ? 0.01 : 0
404
+
405
+ this._attachedMesh.material.needsUpdate = true
406
+ }
407
+ // console.log(this._attachedMesh.material, target)
408
+ }
409
+
410
+ private _applySmoothFilter(shadowReadBuffer: WebGLRenderTarget, shadowWriteBuffer: WebGLRenderTarget) {
411
+ this._shadowBlurMat.uniforms.colorTexture.value = shadowReadBuffer.texture
412
+ this._shadowBlurMat.uniforms.direction.value.set(1, 0)
413
+ this._shadowBlurMat.uniforms.size.value.set(shadowReadBuffer.width, shadowReadBuffer.height)
414
+ this._viewer.renderManager.blit(shadowWriteBuffer, {material: this._shadowBlurMat})
415
+
416
+ this._shadowBlurMat.uniforms.colorTexture.value = shadowWriteBuffer.texture
417
+ this._shadowBlurMat.uniforms.direction.value.set(0, 1)
418
+ this._shadowBlurMat.uniforms.size.value.set(shadowWriteBuffer.width, shadowWriteBuffer.height)
419
+ this._viewer.renderManager.blit(shadowReadBuffer, {material: this._shadowBlurMat})
420
+ }
421
+
422
+ readonly materialExtension: MaterialExtension = {
423
+
424
+ parsFragmentSnippet: (_renderer, _material)=> {
425
+ return glsl`
426
+ uniform float transitionOpacity;
427
+ `
428
+ },
429
+
430
+ extraUniforms: {
431
+ transitionOpacity: {
432
+ value: 1,
433
+ },
434
+ },
435
+
436
+ shaderExtender: (shader, _material, renderer) => {
437
+ if (this.groundMapMode === 'aoMap') {
438
+
439
+ shader.fragmentShader = shaderReplaceString(shader.fragmentShader,
440
+ '#include <aomap_fragment>',
441
+ shaderReplaceString(ShaderChunk.aomap_fragment,
442
+ 'float ambientOcclusion = ( texture2D( aoMap, vAoMapUv ).r - 1.0 ) * aoMapIntensity + 1.0;',
443
+ 'float ambientOcclusion = ( mix(1., unpackRGBAToDepth(texture2D( aoMap, vAoMapUv ) ), transitionOpacity) - 1.0) * aoMapIntensity + 1.0;'
444
+ ))
445
+ // material.materialObject.transparent = false
446
+
447
+ } else if (this.groundMapMode === 'map') {
448
+
449
+ shader.fragmentShader = shaderReplaceString(shader.fragmentShader, '#include <map_fragment>',
450
+ shaderReplaceString(ShaderChunk.map_fragment,
451
+ 'diffuseColor *= sampledDiffuseColor',
452
+ 'float groundShadow = mix(1., unpackRGBAToDepth(sampledDiffuseColor), transitionOpacity); diffuseColor.rgb *= groundShadow; diffuseColor.a *= max(0., 1.-groundShadow) * transitionOpacity;'
453
+ ))
454
+ // material.materialObject.transparent = false
455
+
456
+ } else if (this.groundMapMode === 'alphaMap') {
457
+
458
+ shader.fragmentShader = shaderReplaceString(shader.fragmentShader, '#include <alphamap_fragment>',
459
+ shaderReplaceString(ShaderChunk.alphamap_fragment,
460
+ 'texture2D( alphaMap, vAlphaMapUv ).g',
461
+ '1. - unpackRGBAToDepth( texture2D( alphaMap, vAlphaMapUv ) )',
462
+ {replaceAll: true},
463
+ ))
464
+ // material.materialObject.transparent = true
465
+
466
+ }
467
+
468
+ shader.fragmentShader = shaderReplaceString(shader.fragmentShader, '#include <opaque_fragment>',
469
+ glsl`#include <opaque_fragment>
470
+ #ifndef OPAQUE
471
+ #ifdef USE_AOMAP
472
+ #if NUM_DIR_LIGHT_SHADOWS > 0
473
+ // TODO find a better solution
474
+ float alphaMod = length(reflectedLight.directDiffuse)*4./float(NUM_DIR_LIGHT_SHADOWS);
475
+ #else
476
+ float alphaMod = 1.;
477
+ #endif
478
+ float t1 = 1. - ambientOcclusion;
479
+ float t2 = max(1. - alphaMod, 0.);
480
+ float t = t1 + t2;
481
+ gl_FragColor.a *= max(t, 0.);
482
+ #endif
483
+ #endif
484
+ `)
485
+
486
+ if (this.alphaVignette && renderer.capabilities.isWebGL2) {
487
+ (shader as any).defines.USE_UV = ''
488
+ shader.fragmentShader = shaderReplaceString(shader.fragmentShader, '#include <opaque_fragment>',
489
+ glsl`#include <opaque_fragment>
490
+ #ifndef OPAQUE
491
+ float weight = 0.;
492
+ #ifdef USE_UV // why are we checking for this? this is always supposed to be true
493
+ weight = 2.*abs(length(0.5 - vUv.${this.alphaVignetteAxis}));
494
+ #endif
495
+ #if defined(USE_LIGHTMAP) || defined(USE_AOMAP)
496
+ weight = 2.*abs(length(0.5 - vAoMapUv.${this.alphaVignetteAxis}));
497
+ #endif
498
+ weight = min(1., max(0., weight))-0.5;
499
+ weight = min(1., max(0., 1.0-2.*weight));
500
+ weight = pow(weight, 1.5);
501
+ gl_FragColor.a *= weight;
502
+ //gl_FragColor.rgb /= max(0.01, weight);
503
+ gl_FragColor = saturate(gl_FragColor);
504
+ //gl_FragColor.a = 0.5;
505
+ #endif
506
+ `)
507
+ }
508
+ },
509
+ computeCacheKey: ()=>{
510
+ return this.groundMapMode + '.' + this.alphaVignette + '.' + this.alphaVignetteAxis
511
+ },
512
+ onObjectRender: (_object, material) => {
513
+ if (material.userData.gMapMode !== this.groundMapMode) {
514
+ material.userData.gMapMode = this.groundMapMode
515
+ material.needsUpdate = true
516
+ }
517
+ },
518
+ isCompatible: material => {
519
+ return (material).isPhysicalMaterial
520
+ },
521
+ }
522
+
523
+ // render(mesh: Mesh<BufferGeometry, PhysicalMaterial) {
524
+ // this._viewer.renderManager.renderModel(mesh, this._viewer.scene.activeCamera!)
525
+ // }
526
+ }
527
+
@@ -0,0 +1,16 @@
1
+
2
+ uniform mat4 lastProjectionViewMatrix;
3
+ uniform mat4 currentProjectionViewMatrix;
4
+
5
+ vec2 computeScreenSpaceVelocity(const in vec3 worldPosition) {
6
+ vec4 currentPositionClip = currentProjectionViewMatrix * vec4(worldPosition, 1.0);
7
+ vec4 prevPositionClip = lastProjectionViewMatrix * vec4(worldPosition, 1.0);
8
+
9
+ vec2 currentPositionNDC = currentPositionClip.xy / currentPositionClip.w;
10
+ vec2 prevPositionNDC = prevPositionClip.xy / prevPositionClip.w;
11
+
12
+ if(prevPositionNDC.x >= 1.0 || prevPositionNDC.x <= -1.0 || prevPositionNDC.x >= 1.0 || prevPositionNDC.y <= -1.0) {
13
+ return vec2(0.0);
14
+ }
15
+ return 0.5 * (currentPositionNDC - prevPositionNDC);
16
+ }
@@ -0,0 +1,9 @@
1
+
2
+ uniform mat4 inverseViewMatrix;
3
+
4
+ vec3 getWorldPositionFromViewZ(const in vec2 uv, const in float viewDepth) {
5
+ vec2 uv_ = 2. * uv - 1.;
6
+ float xe = -(uv_.x + projection[2][0]) * viewDepth/projection[0][0];
7
+ float ye = -(uv_.y + projection[2][1]) * viewDepth/projection[1][1];
8
+ return (inverseViewMatrix * vec4(xe, ye, viewDepth, 1.)).xyz;
9
+ }
@@ -0,0 +1,19 @@
1
+ vec2 poisson_disk_samples[16];
2
+ void setPds(){
3
+ poisson_disk_samples[0] = vec2(-0.399691779231, 0.728591545584);
4
+ poisson_disk_samples[1] = vec2(-0.48622557676, -0.84016533712);
5
+ poisson_disk_samples[2] = vec2(0.770309468987, -0.24906070432);
6
+ poisson_disk_samples[3] = vec2(0.556596796154, 0.820359876432);
7
+ poisson_disk_samples[4] = vec2(-0.933902004071, 0.0600539051593);
8
+ poisson_disk_samples[5] = vec2(0.330144964342, 0.207477293384);
9
+ poisson_disk_samples[6] = vec2(0.289013230975, -0.686749271417);
10
+ poisson_disk_samples[7] = vec2(-0.0832470893559, -0.187351643125);
11
+ poisson_disk_samples[8] = vec2(-0.296314525615, 0.254474834305);
12
+ poisson_disk_samples[9] = vec2(-0.850977666059, 0.484642744689);
13
+ poisson_disk_samples[10] = vec2(0.829287915319, 0.2345063545);
14
+ poisson_disk_samples[11] = vec2(-0.773042143899, -0.543741521254);
15
+ poisson_disk_samples[12] = vec2(0.0561133030864, 0.928419742597);
16
+ poisson_disk_samples[13] = vec2(-0.205799249508, -0.562072714492);
17
+ poisson_disk_samples[14] = vec2(-0.526991665882, -0.193690188118);
18
+ poisson_disk_samples[15] = vec2(-0.051789270667, -0.935374050821);
19
+ }
@@ -0,0 +1,54 @@
1
+
2
+ #include <randomHelpers>
3
+
4
+ #ifndef D_sceneBoundingRadius
5
+ #define D_sceneBoundingRadius
6
+ uniform float sceneBoundingRadius;
7
+ #endif
8
+
9
+ #ifndef D_frameCount
10
+ #define D_frameCount
11
+ uniform float frameCount;
12
+ #endif
13
+
14
+
15
+ varying vec4 vRefUv;
16
+ uniform sampler2D tRefDiffuse;
17
+ uniform vec2 tRefDiffuseSize;
18
+ //uniform sampler2D tRefDepth;
19
+ float getSpecularMIPLevel(const in float roughness, const in float maxMIPLevel) {
20
+ float sigma = PI * roughness * roughness / (1.0 + roughness);
21
+ float desiredMIPLevel = maxMIPLevel + log2(sigma);
22
+ // clamp to allowable LOD ranges.
23
+ return clamp(desiredMIPLevel, 0.0, maxMIPLevel);
24
+ }
25
+
26
+ vec4 getReflectionColor(const in float roughness, const in float depthModifier){
27
+ float mip = getSpecularMIPLevel(roughness + depthModifier, 4.0);
28
+ vec4 color = texture2D(tRefDiffuse, vRefUv.xy/vRefUv.w, mip);
29
+ float blurDist = saturate(2.0 / (1. + pow(abs(vViewPosition.z), 0.25))) * roughness * 64. * color.a;
30
+
31
+ float rnd = PI2 * interleavedGradientNoise( vUv.xy, frameCount );
32
+ vec4 rotationMatrix = vec4(cos(rnd), -sin(rnd), 0.,0.);
33
+ rotationMatrix.z = -rotationMatrix.y;
34
+ rotationMatrix.w = rotationMatrix.x;
35
+
36
+ vec3 colorSum = color.rgb * color.a;
37
+ float weightSum = 0.001 + color.a;
38
+ vec2 ofs;
39
+
40
+ setPds(); // sets poisson_disk_samples
41
+
42
+ #pragma unroll_loop_start
43
+ for ( int i = 0; i < 16; i ++ ) {
44
+ ofs = poisson_disk_samples[UNROLLED_LOOP_INDEX];
45
+ ofs = vec2(dot(ofs, rotationMatrix.xy), dot(ofs, rotationMatrix.zw) );
46
+ ofs = vRefUv.xy + vRefUv.w * blurDist * ofs / tRefDiffuseSize.xy;
47
+ color = texture2D(tRefDiffuse, ofs / vRefUv.w, mip);
48
+ colorSum += color.rgb * color.a;
49
+ weightSum += color.a;
50
+ }
51
+ #pragma unroll_loop_end
52
+
53
+ return vec4(colorSum / weightSum, 1.0);
54
+ }
@@ -0,0 +1,28 @@
1
+ varying vec2 vUv;
2
+ uniform sampler2D colorTexture;
3
+ uniform sampler2D maskTexture;
4
+ uniform vec2 texSize;
5
+ uniform vec2 direction;
6
+
7
+ float gaussianPdf(in float x, in float sigma) {
8
+ return 0.39894 * exp( -0.5 * x * x/( sigma * sigma))/sigma;
9
+ }
10
+
11
+ void main() {
12
+ vec2 invSize = 1.0 / texSize;
13
+ float fSigma = float(SIGMA);
14
+ float weightSum = gaussianPdf(0.0, fSigma);
15
+ vec4 mask = texture2D( maskTexture, vUv);
16
+ vec3 diffuseSum = texture2D( colorTexture, vUv).rgb * weightSum;
17
+ for( int i = 1; i < KERNEL_RADIUS; i ++ ) {
18
+ float x = float(i);
19
+ float w = gaussianPdf(x, fSigma);
20
+ vec2 uvOffset = direction * invSize * x;
21
+ vec3 sample1 = texture2D( colorTexture, vUv + uvOffset).rgb;
22
+ vec3 sample2 = texture2D( colorTexture, vUv - uvOffset).rgb;
23
+ diffuseSum += (sample1 + sample2) * w;
24
+ weightSum += 2.0 * w;
25
+ }
26
+ //gl_FragColor = vec4(diffuseSum/weightSum * mask.r, 1.0);
27
+ gl_FragColor = vec4(diffuseSum/weightSum, 1.0);
28
+ }
@@ -0,0 +1,21 @@
1
+ #include <packing>
2
+
3
+ uniform sampler2D colorTexture;
4
+ uniform vec2 size;
5
+ uniform vec2 direction;
6
+ uniform float step;
7
+
8
+ varying vec2 vUv;
9
+
10
+ void main() {
11
+
12
+ float sum = 0.0;
13
+ vec2 uvDelta = step * direction / size;
14
+
15
+ sum += unpackRGBAToDepth(texture2D(colorTexture, vUv - 1. * uvDelta)) * 0.3333;
16
+ sum += unpackRGBAToDepth(texture2D(colorTexture, vec2(vUv.x, vUv.y))) * 0.3333;
17
+ sum += unpackRGBAToDepth(texture2D(colorTexture, vUv + 1. * uvDelta)) * 0.3333;
18
+
19
+ gl_FragColor = packDepthToRGBA(sum);
20
+
21
+ }