@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,64 @@
1
+ uniform float anisotropyFactor;
2
+ uniform float anisotropyNoise;
3
+ #if ANISOTROPY_TEX_MODE == 0
4
+ uniform float anisotropyDirection;
5
+ #else
6
+ uniform sampler2D anisotropyDirectionMap;
7
+ varying vec2 vAnisotropy2MapUv;
8
+ #endif
9
+ const float MIN_ROUGHNESS = 0.05;
10
+
11
+ // Adapted from getReflectedVector() in Filament: https://github.com/google/filament/blob/v1.12.0/shaders/src/light_indirect.fs#L139-L152
12
+ // Filament, Apache License 2.0, Copyright (C) 2015 The Android Open Source Project
13
+ vec3 indirectAnisotropyBentNormal(const in vec3 normal, const in vec3 viewDir, const in float roughness, const in vec3 anisotropicT, const in vec3 anisotropicB) {
14
+ vec3 aDirection = anisotropyFactor >= 0.0 ? anisotropicB : anisotropicT;
15
+ vec3 aTangent = cross(aDirection, viewDir);
16
+ vec3 aNormal = cross(aTangent, aDirection);
17
+ float bendFactor = abs(anisotropyFactor) * saturate(5.0 * max(roughness, MIN_ROUGHNESS));
18
+ return normalize(mix(normal, aNormal, bendFactor));
19
+ }
20
+
21
+ // ShaderChunk.bsdfs
22
+ // Anisotropic lobe adapted from anisotropicLobe() in Filament: https://github.com/google/filament/blob/v1.12.0/shaders/src/shading_model_standard.fs#L31-L58
23
+ // Filament, Apache License 2.0, Copyright (C) 2015 The Android Open Source Project
24
+ vec3 BRDF_GGX_Anisotropy( const in vec3 lightDir, const in vec3 viewDir, const in vec3 normal, const in vec3 f0, const in float f90, const in float roughness, const in vec3 anisotropicT, const in vec3 anisotropicB ) {
25
+
26
+ float alpha = pow2( roughness ); // UE4's roughness
27
+
28
+ vec3 halfDir = normalize( lightDir + viewDir );
29
+
30
+ float dotNL = saturate( dot( normal, lightDir ) );
31
+ float dotNV = saturate( dot( normal, viewDir ) );
32
+ float dotNH = saturate( dot( normal, halfDir ) );
33
+ float dotVH = saturate( dot( viewDir, halfDir ) );
34
+
35
+ float dotTV = dot(anisotropicT, viewDir) ;
36
+ float dotBV = dot(anisotropicB, viewDir) ;
37
+ float dotTL = dot(anisotropicT, lightDir) ;
38
+ float dotBL = dot(anisotropicB, lightDir) ;
39
+ float dotTH = dot(anisotropicT, halfDir) ;
40
+ float dotBH = dot(anisotropicB, halfDir) ;
41
+
42
+ // Anisotropic parameters: at and ab are the roughness along the tangent and bitangent
43
+ // to simplify materials, we derive them from a single roughness parameter
44
+ // Kulla 2017, "Revisiting Physically Based Shading at Imageworks"
45
+ // float at = max(alpha * (1.0 + anisotropyFactor), MIN_ROUGHNESS);
46
+ // float ab = max(alpha * (1.0 - anisotropyFactor), MIN_ROUGHNESS);
47
+
48
+ // slide 26, Disney 2012, "Physically Based Shading at Disney"
49
+ // https://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_notes_v3.pdf
50
+ float aspect = sqrt(1.0 - min(1.-MIN_ROUGHNESS, abs(anisotropyFactor) * 0.9));
51
+ if (anisotropyFactor > 0.0) aspect = 1.0 / aspect;
52
+ float at = roughness * aspect;
53
+ float ab = roughness / aspect;
54
+
55
+ // specular anisotropic BRDF
56
+ vec3 F = F_Schlick( f0, f90, dotVH );
57
+
58
+ float V = V_GGX_SmithCorrelated_Anisotropic( at, ab, dotTV, dotBV, dotTL, dotBL, dotNV, dotNL );
59
+
60
+ float D = D_GGX_Anisotropic( at, ab, dotTH, dotBH, dotNH );
61
+
62
+ return F * ( V * D );
63
+
64
+ }
@@ -0,0 +1,35 @@
1
+
2
+ float rnd = (random2(vUv.xy, frameCount)-0.5) * anisotropyNoise * material.roughness;
3
+
4
+ #if ANISOTROPY_TEX_MODE < 2
5
+ #if ANISOTROPY_TEX_MODE == 0 // CONSTANT rotation
6
+ float rot = saturate(anisotropyDirection) ;
7
+ //vec3 anisotropicT = normalize(mix(tbn[0], tbn[1], rot));
8
+ #else // ROTATION map
9
+ float rot = (anisotropyDirectionMapTexelToLinear(texture2D(anisotropyDirectionMap, vAnisotropy2MapUv)).r);
10
+ #endif
11
+ rot = rot * 2. * PI + rnd;
12
+ vec2 rot2 = vec2(sin(rot), cos(rot));
13
+ // Tangent rotation as in Cycles node_principled_bsdf.osl (Blender Foundation, Apache License 2.0):
14
+ // https://github.com/blender/cycles/blob/main/src/kernel/osl/shaders/node_principled_bsdf.osl
15
+
16
+ #else // DIRECTION map
17
+ vec2 rot2 = (anisotropyDirectionMapTexelToLinear(texture2D(anisotropyDirectionMap, vAnisotropy2MapUv)).rg * 2. - 1.) + vec2(rnd, rnd);
18
+ rot2 = normalize(rot2);
19
+
20
+ const float anisoSpecMultiplier = 0.25;
21
+
22
+ float matSpecAniso = (length(material.specularColor.rgb))*2.*PI;
23
+ rot2 = mix(rot2, vec2(sin(matSpecAniso), cos(matSpecAniso)), anisoSpecMultiplier);
24
+ rot2 = normalize(rot2);
25
+
26
+ #endif
27
+
28
+
29
+ vec3 anisotropicT = (tbn[0] * rot2.x + tbn[1] * rot2.y);
30
+
31
+
32
+ // reproject on normal plane
33
+ anisotropicT = normalize(anisotropicT - normal * dot(anisotropicT, normal));
34
+ vec3 anisotropicB = normalize(cross(normal, anisotropicT));
35
+
@@ -0,0 +1,38 @@
1
+ import {VelocityBufferPlugin} from './buffer/VelocityBufferPlugin'
2
+ import {BloomPlugin} from './postprocessing/BloomPlugin'
3
+ import {DepthOfFieldPlugin} from './postprocessing/DepthOfFieldPlugin'
4
+ import {SSContactShadowsPlugin} from './postprocessing/SSContactShadowsPlugin'
5
+ import {SSReflectionPlugin} from './postprocessing/SSReflectionPlugin'
6
+ import {TemporalAAPlugin} from './postprocessing/TemporalAAPlugin'
7
+ import {OutlinePlugin} from "./postprocessing/OutlinePlugin";
8
+ import {SSGIPlugin} from "./postprocessing/SSGIPlugin";
9
+ import {AnisotropyPlugin} from "./extras/AnisotropyPlugin";
10
+ import {AdvancedGroundPlugin} from "./extras/AdvancedGroundPlugin";
11
+ import {WatchHandsPlugin} from "./extras/WatchHandsPlugin";
12
+
13
+ export {TemporalAAPlugin, TemporalAAPluginPass} from './postprocessing/TemporalAAPlugin'
14
+ export {VelocityBufferPlugin, SSVelocityMaterial} from './buffer/VelocityBufferPlugin'
15
+ export type {VelocityBufferPluginPass, VelocityBufferPluginTarget} from './buffer/VelocityBufferPlugin'
16
+ export {BloomPlugin, BloomPluginPass} from './postprocessing/BloomPlugin'
17
+ export {SSReflectionPlugin, SSReflectionPluginPass, type SSReflectionPluginTarget} from './postprocessing/SSReflectionPlugin'
18
+ export {SSGIPlugin, SSGIPluginPass, type SSGIPluginTarget} from './postprocessing/SSGIPlugin'
19
+ export {SSContactShadowsPlugin} from './postprocessing/SSContactShadowsPlugin'
20
+ export {DepthOfFieldPlugin, DepthOfFieldPluginPass} from './postprocessing/DepthOfFieldPlugin'
21
+ export {OutlinePlugin, OutlineRenderPass, type OutlinePluginPass, type OutlinePluginTarget} from './postprocessing/OutlinePlugin'
22
+ export {AnisotropyPlugin, anisotropyGLTFExtension} from "./extras/AnisotropyPlugin";
23
+ export {AdvancedGroundPlugin} from "./extras/AdvancedGroundPlugin";
24
+ export {WatchHandsPlugin} from "./extras/WatchHandsPlugin";
25
+
26
+ export const webgiPlugins = [
27
+ TemporalAAPlugin,
28
+ VelocityBufferPlugin,
29
+ BloomPlugin,
30
+ SSReflectionPlugin,
31
+ SSContactShadowsPlugin,
32
+ DepthOfFieldPlugin,
33
+ OutlinePlugin,
34
+ SSGIPlugin,
35
+ AnisotropyPlugin,
36
+ AdvancedGroundPlugin,
37
+ WatchHandsPlugin,
38
+ ]
@@ -0,0 +1,374 @@
1
+ import {
2
+ AdditiveBlending,
3
+ CopyShader,
4
+ ExtendedShaderPass,
5
+ GBufferPlugin,
6
+ GBufferUpdater,
7
+ GBufferUpdaterContext,
8
+ generateUiConfig,
9
+ HalfFloatType,
10
+ IMaterial,
11
+ IMaterialUserData,
12
+ IPassID,
13
+ IPipelinePass,
14
+ IViewerEvent,
15
+ IWebGLRenderer,
16
+ matDefineBool,
17
+ MaterialExtension,
18
+ NoBlending,
19
+ onChange,
20
+ onChange2,
21
+ PipelinePassPlugin,
22
+ serialize,
23
+ ThreeViewer,
24
+ uiConfig,
25
+ uiConfigMaterialExtension,
26
+ uiFolderContainer,
27
+ UiObjectConfig,
28
+ uiSlider,
29
+ uiToggle,
30
+ uniform,
31
+ updateBit,
32
+ Vector2,
33
+ Vector4,
34
+ WebGLRenderTarget,
35
+ } from 'threepipe'
36
+ import hdrBloom from './shaders/hdrBloom.glsl'
37
+
38
+ const passId = 'bloom'
39
+ type BloomPassId = typeof passId
40
+
41
+ /**
42
+ * Bloom Plugin
43
+ *
44
+ * Adds HDR Bloom post-processing effect to the scene
45
+ * @category Plugins
46
+ */
47
+ export class BloomPlugin
48
+ extends PipelinePassPlugin<BloomPluginPass<BloomPassId>, BloomPassId>
49
+ implements GBufferUpdater {
50
+ static readonly PluginType = 'Bloom'
51
+ static readonly OldPluginType = 'BloomPlugin' // todo swap
52
+ readonly passId = passId
53
+
54
+ readonly materialExtension: MaterialExtension = uiConfigMaterialExtension(this._getUiConfig.bind(this), BloomPlugin.PluginType)
55
+
56
+ protected _createPass() {
57
+ const pass = new BloomPluginPass(this.passId, Math.min(8, this._viewer?.renderManager.maxHDRIntensity || 8))
58
+ return pass
59
+ }
60
+
61
+ constructor(enabled = true) {
62
+ super()
63
+ this.enabled = enabled
64
+ }
65
+
66
+ @uiConfig(undefined, {unwrapContents: true}) declare protected _pass?: BloomPluginPass<BloomPassId>
67
+
68
+ dependencies = [GBufferPlugin]
69
+
70
+ onAdded(viewer: ThreeViewer) {
71
+ super.onAdded(viewer)
72
+ const gbuffer = viewer.getPlugin(GBufferPlugin)
73
+ if (gbuffer) {
74
+ gbuffer.registerGBufferUpdater(this.constructor.PluginType, this.updateGBufferFlags.bind(this))
75
+ this._pass!.material.registerMaterialExtensions([gbuffer.unpackExtension])
76
+ } else viewer.addEventListener('addPlugin', this._onPluginAdd) // todo subscribe to remove plugin
77
+ viewer.materialManager.registerMaterialExtension(this.materialExtension)
78
+ }
79
+ private _onPluginAdd = (e: IViewerEvent)=>{ // not really required since gbuffer is now a dependency
80
+ if (e.plugin?.constructor?.PluginType !== GBufferPlugin.PluginType) return
81
+ const gbuffer = e.plugin as GBufferPlugin
82
+ gbuffer.registerGBufferUpdater(this.constructor.PluginType, this.updateGBufferFlags.bind(this))
83
+ this._pass?.material.registerMaterialExtensions([gbuffer.unpackExtension])
84
+ this._viewer?.removeEventListener('addPlugin', this._onPluginAdd)
85
+ }
86
+ onRemove(viewer: ThreeViewer) {
87
+ viewer.removeEventListener('addPlugin', this._onPluginAdd)
88
+ const gbuffer = viewer.getPlugin(GBufferPlugin)
89
+ gbuffer?.unregisterGBufferUpdater(this.constructor.PluginType)
90
+ gbuffer && this._pass?.material.unregisterMaterialExtensions([gbuffer.unpackExtension])
91
+ viewer.materialManager.unregisterMaterialExtension(this.materialExtension)
92
+ super.onRemove(viewer)
93
+ }
94
+
95
+ updateGBufferFlags(data: Vector4, c: GBufferUpdaterContext): void {
96
+ if (!c.material || !c.material.userData) return
97
+ const disabled = c.material.userData[BloomPlugin.PluginType]?.enable === false ||
98
+ c.material.userData.pluginsDisabled
99
+ const x = disabled ? 0 : 1
100
+ data.w = updateBit(data.w, 2, x)
101
+ }
102
+
103
+ uiConfig: UiObjectConfig = {
104
+ type: 'folder',
105
+ label: 'Bloom Plugin',
106
+ onChange: this.setDirty.bind(this),
107
+ children: [
108
+ ...generateUiConfig(this) || [],
109
+ ],
110
+ }
111
+
112
+ static AddBloomData(material: IMaterial, params?: IMaterialUserData['Bloom'], setDirty = true): IMaterialUserData['Bloom']|null {
113
+ const ud = material?.userData
114
+ if (!ud) return null
115
+ if (!ud[BloomPlugin.PluginType]) {
116
+ ud[BloomPlugin.PluginType] = {}
117
+ }
118
+ const data = ud[BloomPlugin.PluginType]!
119
+ data.enable = true
120
+ params && Object.assign(data, params)
121
+ if (setDirty && material.setDirty) material.setDirty()
122
+ return data
123
+ }
124
+
125
+ /**
126
+ * This uiConfig is added to each material by extension
127
+ * @param material
128
+ * @private
129
+ */
130
+ private _getUiConfig(material: IMaterial) {
131
+ const config: UiObjectConfig = {
132
+ type: 'folder',
133
+ label: 'Bloom',
134
+ children: [
135
+ {
136
+ type: 'checkbox',
137
+ label: 'Enabled',
138
+ get value() {
139
+ return material.userData[BloomPlugin.PluginType]?.enable ?? true
140
+ },
141
+ set value(v) {
142
+ let data = material.userData[BloomPlugin.PluginType]
143
+ if (v === data?.enable) return
144
+ if (!data) data = BloomPlugin.AddBloomData(material, undefined, false)!
145
+ data.enable = v
146
+ material.setDirty()
147
+ config.uiRefresh?.(true, 'postFrame')
148
+ },
149
+ onChange: this.setDirty,
150
+ },
151
+ ],
152
+ }
153
+ return config
154
+ }
155
+ }
156
+
157
+ @uiFolderContainer('Bloom Pass')
158
+ export class BloomPluginPass<Tid extends IPassID> extends ExtendedShaderPass implements IPipelinePass<Tid> {
159
+ uiConfig?: UiObjectConfig = undefined
160
+ before = ['screen']
161
+ after = ['render', 'progressive']
162
+ required = ['render']
163
+ public readonly passId: Tid
164
+
165
+ constructor(pid: Tid, maxIntensity = 16) {
166
+ super({
167
+ vertexShader: CopyShader.vertexShader,
168
+ defines: {
169
+ ['PASS_STEP']: 1,
170
+ ['MAX_INTENSITY']: Math.min(maxIntensity, 16),
171
+ },
172
+ uniforms: {
173
+ tSource: {value: null},
174
+ tDiffuse: {value: null},
175
+ // intensity: {value: 0.2},
176
+ opacity: {value: 1.0},
177
+ // prefilter: {value: new Vector4(1, 0.5, 0, 0)},
178
+ tDiffuseSize: {value: new Vector2()},
179
+ weight: {value: 1},
180
+ // tNormalDepth: {value: null},
181
+ // tGBufferFlags: {value: null},
182
+ },
183
+ fragmentShader: hdrBloom,
184
+ }, 'tDiffuse', 'tSource')
185
+ this.passId = pid
186
+ this._updateWeights = this._updateWeights.bind(this)
187
+ this._thresholdsUpdated = this._thresholdsUpdated.bind(this)
188
+ this._updateWeights()
189
+ this._thresholdsUpdated()
190
+ this.clear = true
191
+
192
+ // for tweakpane UI. todo: check if required and why
193
+ // ;(this as any).userData = {setDirty: ()=>{
194
+ // this.setDirty()
195
+ // }}
196
+
197
+ }
198
+
199
+ @uniform() prefilter = new Vector4(2, 0.5, 0, 0)
200
+
201
+ @uiSlider('Threshold', [0, 2])
202
+ @onChange(BloomPluginPass.prototype._thresholdsUpdated)
203
+ @serialize() threshold = 2
204
+
205
+ @uiSlider('Soft Threshold', [0, 1])
206
+ @onChange(BloomPluginPass.prototype._thresholdsUpdated)
207
+ @serialize() softThreshold = 0.5
208
+
209
+ @uiSlider('Intensity', [0, 3])
210
+ @serialize() @uniform() intensity = 0.2
211
+
212
+ @uiToggle('Background Bloom')
213
+ @serialize()
214
+ @matDefineBool('BACKGROUND_BLOOM')
215
+ backgroundBloom = false
216
+
217
+ @uiSlider('Iterations', [2, 7], 1)
218
+ @onChange2(BloomPluginPass.prototype._updateWeights)
219
+ @serialize() bloomIterations = 4
220
+
221
+ private _currentIterations = 0 // could be less than bloomIterations based on canvas size
222
+
223
+ @uiSlider('Radius', [0, 1], 0.01)
224
+ @onChange2(BloomPluginPass.prototype._updateWeights)
225
+ @serialize() radius = 0.6
226
+
227
+ @uiSlider('Power', [0.2, 10], 0.01)
228
+ @onChange2(BloomPluginPass.prototype._updateWeights)
229
+ @serialize() power = 1
230
+
231
+ private _thresholdsUpdated() {
232
+ this.prefilter.x = this.threshold
233
+ this.prefilter.y = this.softThreshold
234
+ this.prefilter.z = 2 * this.prefilter.x * this.prefilter.y
235
+ this.prefilter.w = this.uniforms?.prefilter ? .125 / (this.uniforms.prefilter.value.z + 0.00001) : 0
236
+ }
237
+
238
+ @uiToggle('Debug')
239
+ bloomDebug = false
240
+
241
+ private _weights: any = []
242
+
243
+ render(renderer: IWebGLRenderer, writeBuffer?: WebGLRenderTarget | null, readBuffer?: WebGLRenderTarget, deltaTime?: number, maskActive?: boolean) {
244
+ const renderManager = renderer.renderManager
245
+ // prefilter
246
+ this.material.defines.PASS_STEP = 0
247
+
248
+ this.clear = true
249
+
250
+ this.needsSwap = false
251
+
252
+ const source = readBuffer
253
+ if (!source) {
254
+ console.warn('BloomPluginPass: No source to read from')
255
+ return
256
+ }
257
+ this.needsSwap = true
258
+
259
+ let sizeMultiplier = 0.5 // todo are we starting with 0.25? this should be 1 maybe? but more memory...
260
+ let width = source.width * sizeMultiplier
261
+ let height = source.height * sizeMultiplier
262
+ const textures: any[] = []
263
+ let currentDestination = renderManager.getTempTarget({sizeMultiplier: 1, type: HalfFloatType}) as any as WebGLRenderTarget
264
+ textures.push(currentDestination)
265
+ let currentSource = source
266
+ this.material.needsUpdate = true
267
+ this.material.uniforms.weight.value = this._weights[0]
268
+
269
+
270
+ super.render(renderer, currentDestination, currentSource, deltaTime, maskActive)
271
+
272
+ currentSource = currentDestination
273
+
274
+ const ci = this._currentIterations
275
+
276
+ let i = 1
277
+ const iter = Math.max(2, this.bloomIterations)
278
+ for (; i < iter; i++) {
279
+ width /= 2
280
+ height /= 2
281
+ sizeMultiplier /= 2
282
+ if (height < 2 || width < 2) {
283
+ break
284
+ }
285
+ currentDestination = renderManager.getTempTarget({sizeMultiplier, type: HalfFloatType}) as any as WebGLRenderTarget
286
+ textures.push(currentDestination)
287
+
288
+ this.material.defines.PASS_STEP = 1
289
+
290
+ let modifiedWeight = this._weights[i]
291
+ // if(i > 1)
292
+ {
293
+ modifiedWeight = this._weights[i - 1] !== 0 ? this._weights[i] / this._weights[i - 1] : this._weights[i]
294
+ }
295
+ this.material.uniforms.weight.value = modifiedWeight
296
+ this.material.needsUpdate = true
297
+ super.render(renderer, currentDestination, currentSource, deltaTime, maskActive)
298
+
299
+ currentSource = currentDestination
300
+ this._currentIterations = i + 1
301
+ }
302
+
303
+ // console.log(this._currentIterations)
304
+
305
+ if (ci !== this._currentIterations)
306
+ this._updateWeights(false)
307
+
308
+ this.clear = false
309
+
310
+ const oldAutoClear = renderer.autoClear
311
+ renderer.autoClear = false
312
+ for (i -= 2; i >= 0; i--) {
313
+ currentDestination = textures[i]
314
+ textures[i] = undefined
315
+
316
+ this.material.defines.PASS_STEP = 2
317
+ this.material.transparent = true
318
+ this.material.blending = AdditiveBlending
319
+ // this.material.blendSrc = OneFactor;
320
+ // this.material.blendDst = OneFactor;
321
+ this.material.needsUpdate = true
322
+ renderer.autoClear = false
323
+ super.render(renderer, currentDestination, currentSource, deltaTime, maskActive)
324
+ this.material.blending = NoBlending
325
+
326
+ renderManager.releaseTempTarget(currentSource as any)
327
+
328
+ currentSource = currentDestination
329
+ }
330
+
331
+ this.clear = true
332
+
333
+ renderer.autoClear = oldAutoClear
334
+
335
+ renderer.autoClear = true
336
+
337
+ if (this.bloomDebug) {
338
+ this.material.defines.PASS_STEP = 4
339
+ this.material.needsUpdate = true
340
+ super.render(renderer, writeBuffer, currentSource, deltaTime, maskActive)
341
+ } else {
342
+ this.uniforms.tSource.value = source.texture
343
+ this.material.defines.PASS_STEP = 3
344
+ this.material.needsUpdate = true
345
+ super.render(renderer, writeBuffer, currentSource, deltaTime, maskActive)
346
+ this.uniforms.tSource.value = null
347
+ }
348
+ renderManager.releaseTempTarget(currentSource as any)
349
+ }
350
+
351
+ private _updateWeights(setDirty = true) {
352
+ if (!this._weights) return // for first time onChange.
353
+ const radius = Math.max(Math.min(this.radius, 1), 0)
354
+ const iter = Math.max(2, this._currentIterations || this.bloomIterations)
355
+ const delta = 1 / (iter - 1)
356
+ for (let i = 0; i < iter; i++) {
357
+ let f = i * delta + 0.1
358
+ let oneMinusF = 1.2 - f
359
+ f = Math.pow(f, this.power)
360
+ oneMinusF = Math.pow(oneMinusF, this.power)
361
+ this._weights[i] = oneMinusF * (1 - radius) + f * radius
362
+ }
363
+ if (setDirty !== false) this.setDirty()
364
+ }
365
+
366
+ }
367
+
368
+ declare module 'threepipe' {
369
+ interface IMaterialUserData {
370
+ [BloomPlugin.PluginType]?: {
371
+ enable?: boolean
372
+ }
373
+ }
374
+ }