@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,470 @@
1
+ import {
2
+ bindToValue,
3
+ Color,
4
+ CreateRenderTargetOptions,
5
+ ExtendedShaderMaterial,
6
+ ExtendedShaderPass,
7
+ FrameFadePlugin,
8
+ GBufferPlugin,
9
+ generateUiConfig,
10
+ HalfFloatType,
11
+ ICamera,
12
+ IPassID,
13
+ IPipelinePass,
14
+ IRenderManager,
15
+ IScene,
16
+ IViewerEvent,
17
+ IViewerPlugin,
18
+ IWebGLRenderer,
19
+ LinearSRGBColorSpace,
20
+ NearestFilter,
21
+ now,
22
+ PickingPlugin,
23
+ PipelinePassPlugin,
24
+ RGBAFormat,
25
+ serialize,
26
+ shaderUtils,
27
+ ThreeViewer,
28
+ uiFolderContainer,
29
+ UiObjectConfig,
30
+ uiSlider,
31
+ uiToggle,
32
+ uniform,
33
+ Vector2,
34
+ Vector3,
35
+ WebGLRenderTarget,
36
+ } from 'threepipe'
37
+ import dofCombine from './shaders/dofCombine.glsl'
38
+ // import dofBlurBox from './shaders/dofBlurBox.glsl'
39
+ import dofPoissonBox from './shaders/dofPoissonBox.glsl'
40
+ import poissonDiskSamples from './shaders/poissonDiskSamples.glsl'
41
+ import dofComputeCoC from './shaders/dofComputeCoC.glsl'
42
+ import dofExpandCoC from './shaders/dofExpandCoC.glsl'
43
+
44
+ const passId = 'depthOfField'
45
+ type DepthOfFieldPassId = typeof passId
46
+
47
+ /**
48
+ * Depth Of Field Plugin
49
+ *
50
+ * Adds a depth of field effect with configurable focal point.
51
+ * @category Plugins
52
+ */
53
+ export class DepthOfFieldPlugin
54
+ extends PipelinePassPlugin<DepthOfFieldPluginPass<DepthOfFieldPassId>, DepthOfFieldPassId> {
55
+ static readonly PluginType = 'DepthOfField'
56
+ static readonly OldPluginType = 'DepthOfFieldPlugin' // todo swap
57
+ readonly passId = passId
58
+
59
+ @serialize()
60
+ @uiToggle()
61
+ enableEdit = false
62
+
63
+ @serialize('focalPoint') // todo uiConfig
64
+ protected _focalPointHit: Vector3 = new Vector3(0, 0, 0)
65
+
66
+ @serialize()
67
+ crossFadeTime = 200 // ms
68
+
69
+ // readonly materialExtension: MaterialExtension = uiConfigMaterialExtension(this._getUiConfig.bind(this), DepthOfFieldPlugin.PluginType)
70
+
71
+ private _focalPointHitTime = 0
72
+
73
+ protected get _frameFadeTime() { return this.crossFadeTime * 2.5 } // ms
74
+
75
+ constructor(enabled = true, enableEdit = false) {
76
+ super()
77
+ this.enabled = enabled
78
+ this.enableEdit = enableEdit
79
+ this._onObjectHit = this._onObjectHit.bind(this)
80
+ this.setDirty = this.setDirty.bind(this)
81
+ }
82
+
83
+ // todo make this a property.
84
+ setFocalPoint(p: Vector3, fade = true, showGizmo = false) {
85
+ this._focalPointHit.copy(p)
86
+ if (fade) this._viewer?.getPlugin<FrameFadePlugin>('FrameFadePlugin')?.startTransition(this._frameFadeTime)
87
+ if (showGizmo) this._focalPointHitTime = now()
88
+ this.setDirty()
89
+ }
90
+ getFocalPoint() {
91
+ return this._focalPointHit
92
+ }
93
+
94
+ protected _onObjectHit(e: any) {
95
+ if (!this._pass || !e.intersects.intersect || this.isDisabled() || !this.enableEdit) return
96
+ this._focalPointHit.copy(e.intersects.intersect.point)
97
+ this._focalPointHitTime = e.time
98
+ e.intersects.selectedObject = null // this will prevent ObjectPicker to select the object
99
+ this._viewer?.getPlugin(FrameFadePlugin)?.startTransition(this._frameFadeTime)
100
+ this.setDirty()
101
+ }
102
+
103
+ protected _createPass() {
104
+ const pass = new DepthOfFieldPluginPass(this.passId)
105
+ return pass
106
+ }
107
+
108
+ dependencies = [GBufferPlugin] // todo use gbufferUnpackExtension from render manager to support depth buffer plugin as well.
109
+
110
+ onAdded(viewer: ThreeViewer) {
111
+ super.onAdded(viewer)
112
+ viewer.addEventListener('addPlugin', this._onPluginAdd)
113
+ viewer.addEventListener('removePlugin', this._onPluginRemove)
114
+ this._onPluginAdd({plugin: viewer.getPlugin(GBufferPlugin)})
115
+ this._onPluginAdd({plugin: viewer.getPlugin(PickingPlugin)})
116
+ // viewer.materialManager.registerMaterialExtension(this.materialExtension)
117
+ }
118
+
119
+ onRemove(viewer: ThreeViewer) {
120
+ viewer.removeEventListener('addPlugin', this._onPluginAdd)
121
+ viewer.removeEventListener('removePlugin', this._onPluginRemove)
122
+ this._onPluginRemove({plugin: viewer.getPlugin(GBufferPlugin)})
123
+ this._onPluginRemove({plugin: viewer.getPlugin(PickingPlugin)})
124
+ super.onRemove(viewer)
125
+ // viewer.materialManager.unregisterMaterialExtension(this.materialExtension)
126
+ }
127
+ private _onPluginAdd = (e: {plugin?: IViewerPlugin} & Partial<IViewerEvent>)=>{ // not really required since gbuffer is now a dependency
128
+ const plugin = e.plugin
129
+ if (!plugin) return
130
+ if (plugin.constructor?.PluginType === GBufferPlugin.PluginType) {
131
+ const gbuffer = plugin as GBufferPlugin
132
+ // gbuffer.registerGBufferUpdater(this.constructor.PluginType, this.updateGBufferFlags.bind(this))
133
+ this._pass?.material.registerMaterialExtensions([gbuffer.unpackExtension])
134
+ this._pass?.computeCocMaterial.registerMaterialExtensions([gbuffer.unpackExtension])
135
+ this._pass?.expandCocMaterial.registerMaterialExtensions([gbuffer.unpackExtension])
136
+ }
137
+ if (plugin.constructor?.PluginType === PickingPlugin.PluginType) {
138
+ const picking = plugin as PickingPlugin
139
+ picking.addEventListener('hitObject', this._onObjectHit)
140
+ }
141
+ }
142
+ private _onPluginRemove = (e: {plugin?: IViewerPlugin} & Partial<IViewerEvent>)=>{ // not really required since gbuffer is now a dependency
143
+ const plugin = e.plugin
144
+ if (!plugin) return
145
+ if (plugin.constructor?.PluginType === GBufferPlugin.PluginType) {
146
+ const gbuffer = e.plugin as GBufferPlugin
147
+ // gbuffer.unregisterGBufferUpdater(this.constructor.PluginType)
148
+ this._pass?.material.unregisterMaterialExtensions([gbuffer.unpackExtension])
149
+ this._pass?.computeCocMaterial.unregisterMaterialExtensions([gbuffer.unpackExtension])
150
+ this._pass?.expandCocMaterial.unregisterMaterialExtensions([gbuffer.unpackExtension])
151
+ }
152
+ if (plugin.constructor?.PluginType === PickingPlugin.PluginType) {
153
+ const picking = e.plugin as PickingPlugin
154
+ picking.removeEventListener('hitObject', this._onObjectHit)
155
+ }
156
+ }
157
+
158
+ uiConfig: UiObjectConfig = {
159
+ type: 'folder',
160
+ label: 'DepthOfField',
161
+ onChange: this.setDirty.bind(this),
162
+ children: [
163
+ ...generateUiConfig(this) || [],
164
+ ()=>this._pass?.uiConfig,
165
+ ],
166
+ }
167
+
168
+ private _tempVec = new Vector3()
169
+ protected _beforeRender(scene: IScene, camera: ICamera, renderManager: IRenderManager): boolean {
170
+ if (!super._beforeRender(scene, camera, renderManager)) return false
171
+ const pass = this.pass
172
+ if (!pass) return false
173
+
174
+ if (pass.dofBlurMaterial.uniforms.frameCount) // blur mat could change.
175
+ renderManager?.updateShaderProperties(pass.dofBlurMaterial)
176
+
177
+ const cam = camera
178
+ if (!cam) return false
179
+
180
+ cam.updateMatrixWorld(true)
181
+
182
+ cam.updateShaderProperties(pass.material) // for cameraNearFar
183
+
184
+ cam.getWorldPosition(this._tempVec)
185
+ this._tempVec.subVectors(this._focalPointHit, this._tempVec)
186
+
187
+ pass.focalDepthRange.x = this._tempVec.length()
188
+ pass.focalDepthRange.x *= cam.getWorldDirection(new Vector3()).dot(this._tempVec.normalize())
189
+
190
+ let crossAlpha = (now() - this._focalPointHitTime) / this.crossFadeTime
191
+ crossAlpha = 1. - Math.min(1, Math.max(0, crossAlpha))
192
+ if (Math.abs(crossAlpha - pass.crossAlpha) > 0.01) {
193
+ pass.crossAlpha = crossAlpha
194
+ this.setDirty()
195
+ }
196
+ if (crossAlpha > 0) {
197
+ const projectedPoint = this._tempVec.copy(this._focalPointHit).project(cam).addScalar(1).divideScalar(2)
198
+ pass.crossCenter.set(projectedPoint.x, projectedPoint.y)
199
+
200
+ pass.computeCocMaterial.uniformsNeedUpdate = true // for cameraNearFar
201
+ pass.expandCocMaterial.uniformsNeedUpdate = true
202
+ }
203
+ return true
204
+ }
205
+
206
+ // region to be done or removed
207
+
208
+ // static AddDepthOfFieldData(material: IMaterial, params?: IMaterialUserData['DepthOfField'], setDirty = true): IMaterialUserData['DepthOfField']|null {
209
+ // const ud = material?.userData
210
+ // if (!ud) return null
211
+ // if (!ud[DepthOfFieldPlugin.PluginType]) {
212
+ // ud[DepthOfFieldPlugin.PluginType] = {}
213
+ // }
214
+ // const data = ud[DepthOfFieldPlugin.PluginType]!
215
+ // data.enable = true
216
+ // params && Object.assign(data, params)
217
+ // if (setDirty && material.setDirty) material.setDirty()
218
+ // return data
219
+ // }
220
+
221
+ // updateGBufferFlags(data: Vector4, c: GBufferUpdaterContext): void {
222
+ // if (!c.material || !c.material.userData) return
223
+ // const disabled = c.material.userData[DepthOfFieldPlugin.PluginType]?.enable === false ||
224
+ // c.material.userData.pluginsDisabled
225
+ // const x = disabled ? 0 : 1
226
+ // data.w = updateBit(data.w, 4, x)
227
+ // }
228
+
229
+ /*
230
+ get depthRange() {
231
+ return this.pass?.focalDepthRange.y ?? 0
232
+ }
233
+
234
+ set depthRange(v: number) {
235
+ if (this.pass) this.pass.focalDepthRange.y = v
236
+ this.setDirty()
237
+ }
238
+
239
+ get nearBlurScale() {
240
+ return this.pass?.nearFarBlurScale.x ?? 0
241
+ }
242
+ set nearBlurScale(v: number) {
243
+ if (this.pass) this.pass.nearFarBlurScale.x = v
244
+ this.setDirty()
245
+ }
246
+
247
+ get farBlurScale() {
248
+ return this.pass?.nearFarBlurScale.y ?? 0
249
+ }
250
+ set farBlurScale(v: number) {
251
+ if (this.pass) this.pass.nearFarBlurScale.y = v
252
+ this.setDirty()
253
+ }
254
+ */
255
+
256
+ /**
257
+ * This uiConfig is added to each material by extension
258
+ * @param material
259
+ * @private
260
+ */
261
+ // private _getUiConfig(material: IMaterial) {
262
+ // const config: UiObjectConfig = {
263
+ // type: 'folder',
264
+ // label: 'DepthOfField',
265
+ // children: [
266
+ // {
267
+ // type: 'checkbox',
268
+ // label: 'Enabled',
269
+ // get value() {
270
+ // return material.userData[DepthOfFieldPlugin.PluginType]?.enable ?? true
271
+ // },
272
+ // set value(v) {
273
+ // let data = material.userData[DepthOfFieldPlugin.PluginType]
274
+ // if (v === data?.enable) return
275
+ // if (!data) data = DepthOfFieldPlugin.AddDepthOfFieldData(material, undefined, false)!
276
+ // data.enable = v
277
+ // material.setDirty()
278
+ // config.uiRefresh?.(true, 'postFrame')
279
+ // },
280
+ // onChange: this.setDirty,
281
+ // },
282
+ // ],
283
+ // }
284
+ // return config
285
+ // }
286
+
287
+ // endregion
288
+ }
289
+
290
+ // const dofBlurMaterialBox = new ExtendedShaderMaterial({
291
+ // uniforms: {
292
+ // cocTexture: {value: null},
293
+ // colorTexture: {value: null},
294
+ // colorTextureSize: {value: new Vector2()},
295
+ // direction: {value: new Vector2()},
296
+ // },
297
+ // vertexShader: shaderUtils.defaultVertex,
298
+ // fragmentShader: dofBlurBox,
299
+ // }, ['colorTexture', 'cocTexture'])
300
+ const dofBlurMaterialPoisson = new ExtendedShaderMaterial({
301
+ uniforms: {
302
+ colorTexture: {value: null},
303
+ colorTextureSize: {value: new Vector2()},
304
+ direction: {value: new Vector2()},
305
+ frameCount: {value: 0},
306
+ blurRadius: {value: 16},
307
+ },
308
+ vertexShader: shaderUtils.defaultVertex,
309
+ fragmentShader: poissonDiskSamples + '\n' + dofPoissonBox,
310
+ defines: {
311
+ ['DOF_MODE']: 1,
312
+ },
313
+ }, ['colorTexture'])
314
+
315
+ @uiFolderContainer('DepthOfField Pass')
316
+ export class DepthOfFieldPluginPass<Tid extends IPassID> extends ExtendedShaderPass implements IPipelinePass<Tid> {
317
+ uiConfig?: UiObjectConfig = undefined
318
+ before = ['progressive', 'screen']
319
+ after = ['render']
320
+ required = ['render']
321
+
322
+ public computeCocMaterial: ExtendedShaderMaterial
323
+ public expandCocMaterial: ExtendedShaderMaterial
324
+ public dofBlurMaterial: ExtendedShaderMaterial = dofBlurMaterialPoisson
325
+ // public dofBlurMaterialBox: ShaderMaterialEncodingSupport = dofBlurMaterialBox
326
+
327
+ @bindToValue({obj: 'focalDepthRange', key: 'y', onChange: 'setDirty'})
328
+ @uiSlider('Depth Range', [0.25, 3])
329
+ depthRange: number
330
+
331
+ @bindToValue({obj: 'nearFarBlurScale', key: 'x', onChange: 'setDirty'})
332
+ @uiSlider('Near Blur Scale', [0, 1])
333
+ nearBlurScale: number
334
+
335
+ @bindToValue({obj: 'nearFarBlurScale', key: 'y', onChange: 'setDirty'})
336
+ @uiSlider('Far Blur Scale', [0, 1])
337
+ farBlurScale: number
338
+
339
+ @serialize()
340
+ @uniform() nearFarBlurScale = new Vector2(0.25, 0.25)
341
+ @serialize()
342
+ @uniform() focalDepthRange = new Vector2(0.5, 1.5)
343
+
344
+ @uniform() crossCenter = new Vector2(0.5, 0.5)
345
+
346
+ @uniform() crossRadius = 0.04
347
+ @uniform() crossAlpha = 1
348
+ @uniform() crossColor = new Color(0xff9900)
349
+ public readonly passId: Tid
350
+
351
+ constructor(pid: Tid) {
352
+ super({
353
+ uniforms: {
354
+ colorTexture: {value: null},
355
+ blurTexture: {value: null},
356
+ cocTexture: {value: null},
357
+ cocTextureSize: {value: new Vector2()},
358
+ cameraNearFar: {value: new Vector2()},
359
+ },
360
+ vertexShader: shaderUtils.defaultVertex,
361
+ fragmentShader: dofCombine,
362
+ }, 'colorTexture', 'cocTexture', 'blurTexture')
363
+ this.passId = pid
364
+
365
+ this.computeCocMaterial = new ExtendedShaderMaterial({
366
+ uniforms: {
367
+ colorTexture: {value: null},
368
+ cameraNearFar: this.uniforms.cameraNearFar,
369
+ nearFarBlurScale: this.uniforms.nearFarBlurScale,
370
+ focalDepthRange: this.uniforms.focalDepthRange,
371
+ },
372
+ vertexShader: shaderUtils.defaultVertex,
373
+ fragmentShader: dofComputeCoC,
374
+ }, ['colorTexture'])
375
+ // this.computeCocMaterial.uniforms.NearFarBlurScale.value.copy(this.nearFarBlurScale_)
376
+
377
+ this.expandCocMaterial = new ExtendedShaderMaterial({
378
+ uniforms: {
379
+ colorTexture: {value: null},
380
+ colorTextureSize: {value: new Vector2()},
381
+ direction: {value: new Vector2()},
382
+ nearFarBlurScale: this.uniforms.nearFarBlurScale,
383
+ },
384
+ vertexShader: shaderUtils.defaultVertex,
385
+ fragmentShader: dofExpandCoC,
386
+ }, ['colorTexture'])
387
+
388
+ // todo
389
+ // this.clear = true
390
+
391
+ }
392
+
393
+ render(renderer: IWebGLRenderer, writeBuffer?: WebGLRenderTarget | null, readBuffer?: WebGLRenderTarget, deltaTime?: number, maskActive?: boolean) {
394
+ if (!this.enabled) return
395
+ if (!readBuffer) {
396
+ console.error('DepthOfFieldPluginPass: No readBuffer')
397
+ return
398
+ }
399
+ const renderManager = renderer.renderManager
400
+
401
+ // const oldClearColor = renderer.getClearColor(new Color())
402
+ // const oldClearAlpha = renderer.getClearAlpha()
403
+ // const oldAutoClear = renderer.autoClear
404
+ // renderer.autoClear = true
405
+
406
+ const halfPars: CreateRenderTargetOptions = {
407
+ minFilter: NearestFilter, magFilter: NearestFilter,
408
+ // type: UnsignedByteType,
409
+ // encoding: RGBM16Encoding,
410
+ type: HalfFloatType,
411
+ // encoding: LinearEncoding,
412
+ colorSpace: LinearSRGBColorSpace,
413
+ sizeMultiplier: 0.5,
414
+ // samples: 1,
415
+ format: RGBAFormat,
416
+ depthBuffer: false,
417
+ generateMipmaps: false,
418
+ }
419
+
420
+ const cocRt = renderManager.getTempTarget(halfPars)
421
+ const renderTargetBlurTemp = renderManager.getTempTarget(halfPars)
422
+
423
+ // 1. Downsample the Original texture, and store coc in the alpha channel
424
+ this.computeCocMaterial.uniforms.colorTexture.value = readBuffer.texture
425
+ renderManager.blit(cocRt, {material: this.computeCocMaterial})
426
+
427
+ // 2. Blur Near field coc
428
+ this.expandCocMaterial.uniforms.colorTexture.value = cocRt.texture
429
+ this.expandCocMaterial.uniforms.direction.value.set(1, 0)
430
+ renderManager.blit(renderTargetBlurTemp, {material: this.expandCocMaterial})
431
+ this.expandCocMaterial.uniforms.colorTexture.value = renderTargetBlurTemp.texture
432
+ this.expandCocMaterial.uniforms.direction.value.set(0, 1)
433
+ renderManager.blit(cocRt, {material: this.expandCocMaterial})
434
+
435
+ // 3. Blur Dof
436
+ // if (this.dofBlurType_ === 1) {
437
+ if (this.dofBlurMaterial.uniforms.frameCount) {
438
+ this.dofBlurMaterial.uniforms.colorTexture.value = cocRt.texture
439
+ renderManager.blit(renderTargetBlurTemp, {material: this.dofBlurMaterial})
440
+ } else {
441
+ const renderTargetBlurTemp2 = renderManager.getTempTarget(halfPars)
442
+ this.dofBlurMaterial.uniforms.cocTexture.value = cocRt.texture
443
+ this.dofBlurMaterial.uniforms.colorTexture.value = cocRt.texture
444
+ this.dofBlurMaterial.uniforms.direction.value.set(1, 0)
445
+ renderManager.blit(renderTargetBlurTemp2, {material: this.dofBlurMaterial})
446
+ this.dofBlurMaterial.uniforms.colorTexture.value = renderTargetBlurTemp2.texture
447
+ this.dofBlurMaterial.uniforms.direction.value.set(0, 1)
448
+ renderManager.blit(renderTargetBlurTemp, {material: this.dofBlurMaterial})
449
+ renderManager.releaseTempTarget(renderTargetBlurTemp2)
450
+ }
451
+
452
+ // 4. Dof Combine
453
+ this.material.uniforms.blurTexture.value = renderTargetBlurTemp.texture
454
+ this.material.uniforms.cocTexture.value = cocRt.texture
455
+ super.render(renderer, writeBuffer, readBuffer, deltaTime, maskActive)
456
+
457
+ renderManager.releaseTempTarget(cocRt)
458
+ renderManager.releaseTempTarget(renderTargetBlurTemp)
459
+
460
+ }
461
+
462
+ }
463
+
464
+ // declare module 'threepipe' {
465
+ // interface IMaterialUserData {
466
+ // [DepthOfFieldPlugin.PluginType]?: {
467
+ // enable?: boolean
468
+ // }
469
+ // }
470
+ // }