@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,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,377 @@
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({onChange: BloomPluginPass.prototype.setDirty}) intensity = 0.2
211
+
212
+ @uiToggle('Background Bloom')
213
+ @serialize()
214
+ @matDefineBool('BACKGROUND_BLOOM', undefined, undefined, BloomPluginPass.prototype.setDirty)
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
+ // the prefilter vector changes in place, so its uniform setter does not run: render again
237
+ this.setDirty()
238
+ }
239
+
240
+ @uiToggle('Debug')
241
+ @onChange2(BloomPluginPass.prototype.setDirty)
242
+ bloomDebug = false
243
+
244
+ private _weights: any = []
245
+
246
+ render(renderer: IWebGLRenderer, writeBuffer?: WebGLRenderTarget | null, readBuffer?: WebGLRenderTarget, deltaTime?: number, maskActive?: boolean) {
247
+ const renderManager = renderer.renderManager
248
+ // prefilter
249
+ this.material.defines.PASS_STEP = 0
250
+
251
+ this.clear = true
252
+
253
+ this.needsSwap = false
254
+
255
+ const source = readBuffer
256
+ if (!source) {
257
+ console.warn('BloomPluginPass: No source to read from')
258
+ return
259
+ }
260
+ this.needsSwap = true
261
+
262
+ let sizeMultiplier = 0.5 // todo are we starting with 0.25? this should be 1 maybe? but more memory...
263
+ let width = source.width * sizeMultiplier
264
+ let height = source.height * sizeMultiplier
265
+ const textures: any[] = []
266
+ let currentDestination = renderManager.getTempTarget({sizeMultiplier: 1, type: HalfFloatType}) as any as WebGLRenderTarget
267
+ textures.push(currentDestination)
268
+ let currentSource = source
269
+ this.material.needsUpdate = true
270
+ this.material.uniforms.weight.value = this._weights[0]
271
+
272
+
273
+ super.render(renderer, currentDestination, currentSource, deltaTime, maskActive)
274
+
275
+ currentSource = currentDestination
276
+
277
+ const ci = this._currentIterations
278
+
279
+ let i = 1
280
+ const iter = Math.max(2, this.bloomIterations)
281
+ for (; i < iter; i++) {
282
+ width /= 2
283
+ height /= 2
284
+ sizeMultiplier /= 2
285
+ if (height < 2 || width < 2) {
286
+ break
287
+ }
288
+ currentDestination = renderManager.getTempTarget({sizeMultiplier, type: HalfFloatType}) as any as WebGLRenderTarget
289
+ textures.push(currentDestination)
290
+
291
+ this.material.defines.PASS_STEP = 1
292
+
293
+ let modifiedWeight = this._weights[i]
294
+ // if(i > 1)
295
+ {
296
+ modifiedWeight = this._weights[i - 1] !== 0 ? this._weights[i] / this._weights[i - 1] : this._weights[i]
297
+ }
298
+ this.material.uniforms.weight.value = modifiedWeight
299
+ this.material.needsUpdate = true
300
+ super.render(renderer, currentDestination, currentSource, deltaTime, maskActive)
301
+
302
+ currentSource = currentDestination
303
+ this._currentIterations = i + 1
304
+ }
305
+
306
+ // console.log(this._currentIterations)
307
+
308
+ if (ci !== this._currentIterations)
309
+ this._updateWeights(false)
310
+
311
+ this.clear = false
312
+
313
+ const oldAutoClear = renderer.autoClear
314
+ renderer.autoClear = false
315
+ for (i -= 2; i >= 0; i--) {
316
+ currentDestination = textures[i]
317
+ textures[i] = undefined
318
+
319
+ this.material.defines.PASS_STEP = 2
320
+ this.material.transparent = true
321
+ this.material.blending = AdditiveBlending
322
+ // this.material.blendSrc = OneFactor;
323
+ // this.material.blendDst = OneFactor;
324
+ this.material.needsUpdate = true
325
+ renderer.autoClear = false
326
+ super.render(renderer, currentDestination, currentSource, deltaTime, maskActive)
327
+ this.material.blending = NoBlending
328
+
329
+ renderManager.releaseTempTarget(currentSource as any)
330
+
331
+ currentSource = currentDestination
332
+ }
333
+
334
+ this.clear = true
335
+
336
+ renderer.autoClear = oldAutoClear
337
+
338
+ renderer.autoClear = true
339
+
340
+ if (this.bloomDebug) {
341
+ this.material.defines.PASS_STEP = 4
342
+ this.material.needsUpdate = true
343
+ super.render(renderer, writeBuffer, currentSource, deltaTime, maskActive)
344
+ } else {
345
+ this.uniforms.tSource.value = source.texture
346
+ this.material.defines.PASS_STEP = 3
347
+ this.material.needsUpdate = true
348
+ super.render(renderer, writeBuffer, currentSource, deltaTime, maskActive)
349
+ this.uniforms.tSource.value = null
350
+ }
351
+ renderManager.releaseTempTarget(currentSource as any)
352
+ }
353
+
354
+ private _updateWeights(setDirty = true) {
355
+ if (!this._weights) return // for first time onChange.
356
+ const radius = Math.max(Math.min(this.radius, 1), 0)
357
+ const iter = Math.max(2, this._currentIterations || this.bloomIterations)
358
+ const delta = 1 / (iter - 1)
359
+ for (let i = 0; i < iter; i++) {
360
+ let f = i * delta + 0.1
361
+ let oneMinusF = 1.2 - f
362
+ f = Math.pow(f, this.power)
363
+ oneMinusF = Math.pow(oneMinusF, this.power)
364
+ this._weights[i] = oneMinusF * (1 - radius) + f * radius
365
+ }
366
+ if (setDirty !== false) this.setDirty()
367
+ }
368
+
369
+ }
370
+
371
+ declare module 'threepipe' {
372
+ interface IMaterialUserData {
373
+ [BloomPlugin.PluginType]?: {
374
+ enable?: boolean
375
+ }
376
+ }
377
+ }