@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
package/package.json CHANGED
@@ -1,13 +1,11 @@
1
1
  {
2
2
  "name": "@threepipe/webgi-plugins",
3
- "version": "0.6.4",
4
3
  "description": "WebGi - Realistic Rendering Plugins for ThreePipe.",
5
- "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
- "types": "dist/index.d.ts",
8
- "sources": "src/index.ts",
9
- "browser": "dist/index.js",
10
- "type": "module",
4
+ "version": "0.8.0",
5
+ "dependencies": {},
6
+ "peerDependencies": {
7
+ "threepipe": ">=0.5.1"
8
+ },
11
9
  "exports": {
12
10
  ".": {
13
11
  "types": "./dist/index.d.ts",
@@ -19,17 +17,23 @@
19
17
  "require": "./dist/"
20
18
  }
21
19
  },
20
+ "type": "module",
21
+ "main": "dist/index.js",
22
+ "module": "dist/index.mjs",
23
+ "types": "dist/index.d.ts",
22
24
  "files": [
25
+ "dist",
26
+ "src",
27
+ "README.md",
28
+ "CHANGELOG.md",
29
+ "docs",
30
+ "tsconfig.json",
23
31
  "LICENSE",
24
32
  "NOTICE",
25
- "README.md",
26
- "dist",
27
- "tsconfig.json"
33
+ "THIRD_PARTY_NOTICES.md"
28
34
  ],
29
- "repository": {
30
- "type": "git",
31
- "url": "git://github.com/repalash/threepipe-webgi.git"
32
- },
35
+ "author": "repalash <palash@shaders.app> (https://repalash.com)",
36
+ "license": "SEE LICENSE IN LICENSE",
33
37
  "keywords": [
34
38
  "3d",
35
39
  "three.js",
@@ -42,17 +46,14 @@
42
46
  "webgi",
43
47
  "canvas"
44
48
  ],
45
- "author": "repalash <palash@shaders.app> (https://repalash.com)",
46
- "license": "SEE LICENSE IN LICENSE",
47
49
  "bugs": {
48
50
  "url": "https://github.com/repalash/threepipe/issues"
49
51
  },
50
- "homepage": "https://webgi.dev/",
51
- "peerDependencies": {
52
- "threepipe": ">=0.4.0"
52
+ "homepage": "https://threepipe.org/package/webgi-plugins.html",
53
+ "repository": {
54
+ "type": "git",
55
+ "url": "git://github.com/repalash/threepipe.git",
56
+ "directory": "plugins/webgi-plugins"
53
57
  },
54
- "browserslist": [
55
- "defaults"
56
- ],
57
58
  "sideEffects": false
58
59
  }
@@ -0,0 +1,27 @@
1
+ declare module '*.glsl' {
2
+ const content: string
3
+ export default content
4
+ }
5
+ declare module '*.vert' {
6
+ const content: string
7
+ export default content
8
+ }
9
+ declare module '*.frag' {
10
+ const content: string
11
+ export default content
12
+ }
13
+ // declare module '*.module.css' {
14
+ // const content: any
15
+ // export default content
16
+ // export const stylesheet: string
17
+ // }
18
+ declare module '*.css?inline' {
19
+ const content: string
20
+ export default content
21
+ }
22
+
23
+ // export {}
24
+
25
+ // hack for typedoc
26
+ // eslint-disable-next-line @typescript-eslint/naming-convention
27
+ // declare type OffscreenCanvas = HTMLCanvasElement
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './plugins'
@@ -0,0 +1,62 @@
1
+ import ssaoBilateral from './shaders/ssaoBilateral.glsl'
2
+ import {
3
+ CopyShader,
4
+ ExtendedShaderPass, getOrCall,
5
+ IPass,
6
+ IWebGLRenderer,
7
+ serialize,
8
+ uniform, ValOrFunc,
9
+ Vector2,
10
+ WebGLRenderTarget
11
+ } from "threepipe";
12
+
13
+ export class BilateralFilterPass extends ExtendedShaderPass implements IPass {
14
+ @serialize() @uniform() smoothEnabled = true
15
+ @serialize() @uniform() edgeSharpness = 0.1
16
+ // @serialize() @uniform() smoothSigma = new Vector4(1, 0.2, 4., 1)
17
+ // @serialize() @uniform() smoothScale = new Vector4(0, 10, 1, 0)
18
+
19
+ constructor(public target?: ValOrFunc<WebGLRenderTarget|undefined>, sourceAccessor = 'rgba') {
20
+ super({
21
+ vertexShader: CopyShader.vertexShader,
22
+ fragmentShader: `
23
+ // for gbuffer
24
+ #include <packing>
25
+ #define THREE_PACKING_INCLUDED
26
+ ` + ssaoBilateral,
27
+ uniforms: {
28
+ bilDirection: {value: new Vector2(1, 0)},
29
+ // tNormalDepth: {value: null},
30
+ tDiffuse: {value: null},
31
+ tDiffuseSize: {value: new Vector2()},
32
+ },
33
+ defines: {
34
+ ['B_SRC_ACCESSOR']: sourceAccessor,
35
+ },
36
+ }, 'tDiffuse')
37
+ this.clear = false
38
+ this.needsSwap = false
39
+ }
40
+
41
+ render(renderer: IWebGLRenderer, writeBuffer: WebGLRenderTarget, _readBuffer: WebGLRenderTarget, deltaTime: number, maskActive: boolean) {
42
+ if (!this.enabled) return
43
+ const target = getOrCall(this.target)
44
+ if(!target) return
45
+
46
+ this.uniforms.bilDirection.value.set(1, 0)
47
+
48
+ this.uniforms.tDiffuse.value = target.texture
49
+ this.uniforms.tDiffuseSize.value.set(this.uniforms.tDiffuse.value?.image.width || 0, this.uniforms.tDiffuse.value?.image.height || 0)
50
+ super.render(renderer, writeBuffer, target, deltaTime, maskActive)
51
+
52
+ this.uniforms.bilDirection.value.set(0, 1)
53
+ this.uniforms.tDiffuse.value = writeBuffer.texture
54
+ this.uniforms.tDiffuseSize.value.set(this.uniforms.tDiffuse.value?.image.width || 0, this.uniforms.tDiffuse.value?.image.height || 0)
55
+ super.render(renderer, target, writeBuffer, deltaTime, maskActive)
56
+
57
+ // this.uniforms.bilDirection.value.set(-1, -1)
58
+ // this.uniforms.tDiffuse2.value = target.texture
59
+ // super.render(renderer, writeBuffer, readBuffer, deltaTime, maskActive)
60
+
61
+ }
62
+ }
@@ -0,0 +1,89 @@
1
+
2
+ uniform vec2 tDiffuseSize; //tDiffuse is the color buffer
3
+ //uniform vec2 tDiffuse2Size;
4
+ uniform vec2 bilDirection;
5
+ varying vec2 vUv;
6
+
7
+ uniform bool smoothEnabled;
8
+
9
+ uniform float edgeSharpness;
10
+
11
+ vec4 bilaterialAO(){
12
+ vec4 color = clamp((texture2D(tDiffuse, vUv.xy)).B_SRC_ACCESSOR, 0., 5.);
13
+
14
+ if(!smoothEnabled) return color;
15
+
16
+ float depth; vec3 normal;
17
+ getDepthNormal(vUv.xy, depth, normal);
18
+
19
+ float gaussianWeights[4];
20
+ gaussianWeights[0] = 0.153170;
21
+ gaussianWeights[1] = 0.144893;
22
+ gaussianWeights[2] = 0.122649;
23
+ gaussianWeights[3] = 0.092902;
24
+
25
+ float Z = gaussianWeights[0] + 0.03;
26
+ vec4 final_colour = Z * color;
27
+ vec2 nuv;
28
+ vec4 cc; float dp; vec3 nor; // cc is color, np is position, dp is depth, nor is normal
29
+ vec2 direction = bilDirection / tDiffuseSize.xy;
30
+
31
+
32
+ // -1, 1, -2, 2, -3, 3
33
+
34
+ #pragma unroll_loop_start
35
+ for (int i = 0; i < 6; i++){
36
+
37
+ direction *= -1.;
38
+ nuv = vUv + 2. * direction * float( UNROLLED_LOOP_INDEX / 2 + 1 ); // clamp to screen border
39
+ getDepthNormal(nuv, dp, nor);
40
+
41
+ if(dp < 0.999) {
42
+
43
+ float normalCloseness = dot(normal, nor);
44
+ normalCloseness *= normalCloseness;
45
+ float normalError = (1.0 - normalCloseness) * 8.;
46
+ float normalWeight = max((1.0 - normalError * edgeSharpness), 0.00);
47
+
48
+ float depthWeight = max(0.0, 1.0 - edgeSharpness * 4000. * abs(depth - dp));
49
+
50
+ float kernelWeight = gaussianWeights[UNROLLED_LOOP_INDEX / 2] + 0.03;
51
+ float bilateralWeight = kernelWeight * depthWeight * normalWeight;
52
+
53
+ Z += bilateralWeight;
54
+
55
+ cc = clamp((texture2D(tDiffuse, nuv)).B_SRC_ACCESSOR, 0., 5.);// clamp(texsample(tDiffuse, float(i), float(j)), 0., 1.);
56
+ final_colour += bilateralWeight*cc;
57
+
58
+ }
59
+
60
+ }
61
+ #pragma unroll_loop_end
62
+
63
+ final_colour /= Z;
64
+ // final_colour.a = color.a;
65
+ return final_colour;
66
+ }
67
+
68
+
69
+ void main() {
70
+
71
+ vec4 ao = clamp(bilaterialAO(), vec4(0.), vec4(1.));
72
+ // ao.a = 1.;
73
+ gl_FragColor = ao;
74
+
75
+ }
76
+
77
+ //float normpdf(in float x, in float sigma){
78
+ // return exp(-0.5*x*x/(sigma*sigma));
79
+ // // return 0.39894*exp(-0.5*x*x/(sigma*sigma))/sigma;
80
+ //}
81
+ //uniform vec4 smoothSigma; // color, depth, pixel, normal
82
+ //uniform vec4 smoothScale; // color, depth, pixel, normal
83
+ //factor = 1.;
84
+ //factor *= normpdf( length(cc-color) * smoothScale.x, smoothSigma.x); //color
85
+ //factor *= normpdf(sqrt(abs(dp-depth)) * smoothScale.y, smoothSigma.y); //depth
86
+ //factor *= normpdf( float( i/2 + 1 ) * smoothScale.z, smoothSigma.z); //pixel distance
87
+ //factor *= normpdf((1.-dot(normal, nor)) * smoothScale.w, smoothSigma.w); //normal
88
+ //factor *= normpdf(sqrt(length(np-pos))*smoothScale.y, smoothSigma.y); // position.
89
+
@@ -0,0 +1,270 @@
1
+ import {
2
+ BufferGeometry,
3
+ Camera,
4
+ Color,
5
+ DoubleSide,
6
+ GBufferRenderPass,
7
+ ICamera,
8
+ IObject3D,
9
+ IRenderManager,
10
+ IScene,
11
+ IUniform,
12
+ IWebGLRenderer,
13
+ LinearSRGBColorSpace,
14
+ MaterialExtension,
15
+ Matrix3,
16
+ Matrix4,
17
+ NoBlending,
18
+ PipelinePassPlugin,
19
+ Scene,
20
+ ShaderMaterial,
21
+ shaderReplaceString,
22
+ Texture,
23
+ TextureDataType,
24
+ ThreeViewer,
25
+ uiFolderContainer,
26
+ uiImage,
27
+ Group,
28
+ UnsignedByteType,
29
+ Vector2,
30
+ WebGLRenderer,
31
+ WebGLRenderTarget,
32
+ } from 'threepipe'
33
+ import VelocityBufferUnpack from './shaders/VelocityBufferPlugin.unpack.glsl'
34
+ import ssVelocityVert from './shaders/VelocityBufferPlugin.mat.vert.glsl'
35
+ import ssVelocityFrag from './shaders/VelocityBufferPlugin.mat.frag.glsl'
36
+ import {TemporalAAPlugin} from '../postprocessing/TemporalAAPlugin'
37
+
38
+ // type VelocityBufferPluginTarget = WebGLRenderTarget
39
+ export type VelocityBufferPluginTarget = WebGLRenderTarget
40
+ export type VelocityBufferPluginPass = GBufferRenderPass<'velocityBuffer', VelocityBufferPluginTarget>
41
+ /**
42
+ * Velocity Buffer Plugin
43
+ *
44
+ * Adds a pre-render pass to render the normal buffer to a render target that can be used for postprocessing.
45
+ * @category Plugins
46
+ */
47
+ @uiFolderContainer('Velocity Buffer Plugin (for TAA)')
48
+ export class VelocityBufferPlugin
49
+ extends PipelinePassPlugin<VelocityBufferPluginPass, 'velocityBuffer'> {
50
+
51
+ readonly passId = 'velocityBuffer'
52
+ public static readonly PluginType = 'VelocityBuffer'
53
+ public static readonly OldPluginType = 'VelocityBufferPlugin' // todo swap
54
+
55
+ target?: VelocityBufferPluginTarget
56
+ @uiImage('Velocity Buffer', {readOnly: true}) texture?: Texture
57
+ readonly material: SSVelocityMaterial = new SSVelocityMaterial()
58
+
59
+ // @onChange2(VelocityBufferPlugin.prototype._createTarget)
60
+ // @uiDropdown('Buffer Type', threeConstMappings.TextureDataType.uiConfig)
61
+ readonly bufferType: TextureDataType // cannot be changed after creation (for now)
62
+
63
+ protected _createTarget(recreate = true) {
64
+ if (!this._viewer) return
65
+ if (recreate) this._disposeTarget()
66
+
67
+ if (!this.target) this.target = this._viewer.renderManager.createTarget<VelocityBufferPluginTarget>(
68
+ {
69
+ depthBuffer: true,
70
+ // samples: v.renderManager.composerTarget.samples || 0,
71
+ samples: 0,
72
+ type: this.bufferType,
73
+ // magFilter: NearestFilter,
74
+ // minFilter: NearestFilter,
75
+ generateMipmaps: false,
76
+ colorSpace: LinearSRGBColorSpace,
77
+ })
78
+ this.texture = this.target.texture
79
+ this.texture.name = 'velocityBuffer'
80
+
81
+ if (this._pass) this._pass.target = this.target
82
+ }
83
+ protected _disposeTarget() {
84
+ if (!this._viewer) return
85
+ if (this.target) {
86
+ this._viewer.renderManager.disposeTarget(this.target)
87
+ this.target = undefined
88
+ }
89
+ this.texture = undefined
90
+ }
91
+
92
+ protected _createPass() {
93
+ this._createTarget(true)
94
+ if (!this.target) throw new Error('VelocityBufferPlugin: target not created')
95
+ this.material.userData.isGBufferMaterial = true
96
+ const v = this._viewer!
97
+ const pass = new class extends GBufferRenderPass {
98
+ private _firstCall = true
99
+ render(renderer: IWebGLRenderer, writeBuffer?: WebGLRenderTarget | null, readBuffer?: WebGLRenderTarget, deltaTime?: number, maskActive?: boolean) {
100
+ if (v.renderManager.frameCount > 0) return
101
+ if (!this.enabled || !this.camera) return
102
+ const mat = this.overrideMaterial as ShaderMaterial
103
+ mat.uniforms.currentProjectionViewMatrix.value.copy(this.camera.projectionMatrix).multiply(this.camera.matrixWorldInverse)
104
+ if (this._firstCall) {
105
+ mat.uniforms.lastProjectionViewMatrix.value.copy(mat.uniforms.currentProjectionViewMatrix.value)
106
+ this._firstCall = false
107
+ }
108
+ super.render(renderer, writeBuffer, readBuffer, deltaTime, maskActive)
109
+ mat.uniforms.lastProjectionViewMatrix.value.copy(mat.uniforms.currentProjectionViewMatrix.value)
110
+ }
111
+ }(this.passId, this.target, this.material, new Color(0.5, 0.5, 0.5), 1) // clear color 0.5 means 0 velocity
112
+ const preprocessMaterial = pass.preprocessMaterial
113
+ // `disabled` is the opposite of renderToGBuffer; undefined keeps the material's own renderToGBuffer
114
+ pass.preprocessMaterial = (m) => {
115
+ const disabled = m.userData[VelocityBufferPlugin.PluginType]?.disabled
116
+ return preprocessMaterial(m, disabled === undefined ? undefined : !disabled)
117
+ }
118
+ pass.before = ['render']
119
+ pass.after = []
120
+ pass.required = ['render']
121
+ return pass as any
122
+ }
123
+
124
+ // automatically register the unpack extension with TAA plugin
125
+ protected readonly _attachToTaa: boolean
126
+
127
+ constructor(
128
+ bufferType: TextureDataType = UnsignedByteType,
129
+ enabled = true,
130
+ _attachToTaa = true
131
+ ) {
132
+ super()
133
+ this.enabled = enabled
134
+ this.bufferType = bufferType
135
+ this._attachToTaa = _attachToTaa
136
+ }
137
+
138
+ onAdded(viewer: ThreeViewer) {
139
+ super.onAdded(viewer)
140
+ viewer.forPlugin(TemporalAAPlugin, (taa) => {
141
+ this._attachToTaa && taa.pass?.material.registerMaterialExtensions([this.unpackExtension])
142
+ }, (taa)=>{
143
+ taa.pass?.material?.unregisterMaterialExtensions([this.unpackExtension])
144
+ }, this)
145
+ }
146
+
147
+ onRemove(viewer: ThreeViewer): void {
148
+ this._disposeTarget()
149
+ return super.onRemove(viewer)
150
+ }
151
+
152
+ unpackExtension: MaterialExtension = {
153
+ shaderExtender: (shader)=>{
154
+ if(this.isDisabled()) return
155
+ shader.fragmentShader = shaderReplaceString(shader.fragmentShader,
156
+ '#pragma <velocity_unpack>',
157
+ '\n' + VelocityBufferUnpack + '\n')
158
+ },
159
+ computeCacheKey: ()=>this.isDisabled() ? '' : 'vb',
160
+ extraUniforms: {
161
+ tVelocity: ()=>({value: !this.isDisabled() ? this.target?.texture:null}),
162
+ },
163
+ extraDefines: {
164
+ ['HAS_VELOCITY_BUFFER']: ()=>!this.isDisabled() && this.target?.texture ? 1 : undefined,
165
+ },
166
+ priority: 100,
167
+ isCompatible: () => true,
168
+ }
169
+
170
+ setDirty() {
171
+ super.setDirty();
172
+ this.unpackExtension.setDirty?.()
173
+ }
174
+
175
+ protected _beforeRender(scene: IScene, camera: ICamera, renderManager: IRenderManager): boolean {
176
+ if (!super._beforeRender(scene, camera, renderManager)) return false
177
+ const pass = this.pass
178
+ if (!pass) return false
179
+ if (renderManager.frameCount > 0) return false
180
+ pass.scene = scene
181
+ pass.camera = camera
182
+ camera.updateShaderProperties(pass.overrideMaterial as ShaderMaterial)
183
+ return true
184
+ }
185
+
186
+ }
187
+
188
+
189
+ declare module 'threepipe' {
190
+ interface IMaterialUserData {
191
+ [VelocityBufferPlugin.PluginType]?: {
192
+ /**
193
+ * Disables rendering to the velocity buffer.
194
+ */
195
+ disabled?: boolean
196
+ }
197
+ }
198
+ }
199
+ export class SSVelocityMaterial extends ShaderMaterial {
200
+
201
+ constructor() {
202
+ super({
203
+ vertexShader: ssVelocityVert,
204
+ fragmentShader: ssVelocityFrag,
205
+ uniforms: {
206
+ cameraNearFar: {value: new Vector2(0.1, 1000)},
207
+ alphaMap: {value: null},
208
+ alphaTest: {value: null},
209
+ alphaMapTransform: {value: /* @__PURE__*/ new Matrix3()},
210
+ currentProjectionViewMatrix: {value: new Matrix4()},
211
+ lastProjectionViewMatrix: {value: new Matrix4()},
212
+ },
213
+ blending: NoBlending, // todo?
214
+ })
215
+ }
216
+
217
+ extraUniformsToUpload: Record<string, IUniform> = {
218
+ modelMatrixPrevious: {value: new Matrix4().identity()},
219
+ }
220
+
221
+ private _previousWorldMatrices: Record<string, Matrix4> = {}
222
+
223
+ // this gets called for each object.
224
+ onBeforeRender(_r: WebGLRenderer, _s: Scene, _c: Camera, _geometry: BufferGeometry, object: IObject3D, group: Group) {
225
+ super.onBeforeRender(_r, _s, _c, _geometry, object, group)
226
+ const prevMatrix = this._previousWorldMatrices[object.uuid]
227
+ this.extraUniformsToUpload.modelMatrixPrevious.value.copy(prevMatrix ?? object.matrixWorld)
228
+
229
+ // todo: make sure all objects are only rendered once.
230
+ if (prevMatrix) {
231
+ prevMatrix.copy(object.matrixWorld)
232
+ } else {
233
+ this._previousWorldMatrices[object.uuid] = object.matrixWorld.clone()
234
+ }
235
+
236
+ // todo: add support for all this in the shaders.
237
+ let mat = object.material
238
+
239
+ if (Array.isArray(mat)) { // todo: add support for multi materials.
240
+ mat = mat[0]
241
+ }
242
+ this.uniforms.alphaMap.value = mat?.alphaMap ?? null
243
+ this.uniforms.alphaTest.value = !mat || !mat.alphaTest || mat.alphaTest < 0.0000001 ? 0.001 : mat.alphaTest
244
+
245
+ let x = this.uniforms.alphaMap.value ? 1 : undefined
246
+ if (x !== this.defines.USE_ALPHAMAP) {
247
+ if (x === undefined) {
248
+ delete this.defines.USE_ALPHAMAP
249
+ delete this.defines.ALPHAMAP_UV
250
+ } else {
251
+ this.defines.USE_ALPHAMAP = x
252
+ this.defines.ALPHAMAP_UV = 'uv' // uv_vertex needs the channel for vAlphaMapUv, as in GBufferMaterial
253
+ }
254
+ this.needsUpdate = true
255
+ }
256
+ const alphaMap = this.uniforms.alphaMap.value as Texture | null
257
+ if (alphaMap) {
258
+ if (alphaMap.matrixAutoUpdate) alphaMap.updateMatrix()
259
+ this.uniforms.alphaMapTransform.value.copy(alphaMap.matrix)
260
+ }
261
+ x = mat?.userData.ALPHA_I_RGBA_PACKING ? 1 : undefined
262
+ if (x !== this.defines.ALPHA_I_RGBA_PACKING) {
263
+ if (x === undefined) delete this.defines.ALPHA_I_RGBA_PACKING
264
+ else this.defines.ALPHA_I_RGBA_PACKING = x
265
+ this.needsUpdate = true
266
+ }
267
+
268
+ this.side = mat?.side ?? DoubleSide
269
+ }
270
+ }
@@ -0,0 +1,46 @@
1
+ #ifdef USE_ALPHAMAP
2
+ #define USE_UV
3
+ #include <packing>
4
+ uniform sampler2D alphaMap;
5
+ uniform float alphaTest;
6
+ #endif
7
+ #include <uv_pars_fragment>
8
+
9
+ varying vec3 vWorldPosition;
10
+ varying vec3 vWorldPositionPrevious;
11
+ uniform mat4 currentProjectionViewMatrix;
12
+ uniform mat4 lastProjectionViewMatrix;
13
+
14
+ vec2 computeScreenSpaceVelocity2() {
15
+ vec4 currentPositionClip = currentProjectionViewMatrix * vec4(vWorldPosition, 1.0);
16
+ vec4 prevPositionClip = lastProjectionViewMatrix * vec4(vWorldPositionPrevious, 1.0);
17
+
18
+ vec2 currentPositionNDC = currentPositionClip.xy / currentPositionClip.w;
19
+ vec2 prevPositionNDC = prevPositionClip.xy / prevPositionClip.w;
20
+
21
+ if(prevPositionNDC.x >= 1.0 || prevPositionNDC.x <= -1.0 || prevPositionNDC.x >= 1.0 || prevPositionNDC.y <= -1.0) {
22
+ return vec2(0.0);
23
+ }
24
+ return 0.5 * (currentPositionNDC - prevPositionNDC);
25
+ }
26
+
27
+ void main() {
28
+ // alpha test like the GBuffer material, so cut-out texels do not write the mesh's motion
29
+ #ifdef USE_ALPHAMAP
30
+ float alphaMapValue =
31
+ #ifdef ALPHA_I_RGBA_PACKING
32
+ 1. - unpackRGBAToDepth( texture2D( alphaMap, vAlphaMapUv ) );
33
+ #else
34
+ texture2D( alphaMap, vAlphaMapUv ).g;
35
+ #endif
36
+ if ( alphaMapValue < alphaTest ) discard;
37
+ #endif
38
+
39
+ vec2 velocity = clamp(computeScreenSpaceVelocity2(), -1.0, 1.0);
40
+ velocity = sign(velocity) * pow(abs(velocity), vec2(1./4.));
41
+ velocity = velocity * 0.5 + 0.5;
42
+ gl_FragColor = vec4(velocity.x, velocity.y, 1., 1.);
43
+
44
+ // float speed = length(computeScreenSpaceVelocity2());
45
+ // gl_FragColor = vec4(speed, speed, speed, 1.);
46
+ }
@@ -0,0 +1,51 @@
1
+ #ifdef USE_ALPHAMAP
2
+ #define USE_UV
3
+ #endif
4
+ #include <uv_pars_vertex>
5
+ #include <batching_pars_vertex>
6
+ #include <morphtarget_pars_vertex>
7
+ #include <skinning_pars_vertex>
8
+ #include <logdepthbuf_pars_vertex>
9
+ #include <clipping_planes_pars_vertex>
10
+
11
+ //varying vec3 vViewPosition;
12
+
13
+ varying vec3 vWorldPosition;
14
+ varying vec3 vWorldPositionPrevious;
15
+
16
+ uniform mat4 modelMatrixPrevious;
17
+
18
+ void main() {
19
+
20
+ #include <uv_vertex>
21
+ #include <batching_vertex>
22
+ #include <skinbase_vertex>
23
+
24
+ #include <begin_vertex>
25
+ #include <morphtarget_vertex>
26
+ #include <skinning_vertex>
27
+ #include <displacementmap_vertex>
28
+
29
+ // project_vertex
30
+
31
+ vec4 mvPosition = vec4( transformed, 1.0 );
32
+
33
+ #ifdef USE_INSTANCING
34
+
35
+ mvPosition = instanceMatrix * mvPosition;
36
+
37
+ #endif
38
+
39
+ vWorldPosition = (modelMatrix * mvPosition).xyz;
40
+ vWorldPositionPrevious = (modelMatrixPrevious * mvPosition).xyz;
41
+
42
+ mvPosition = modelViewMatrix * mvPosition;
43
+
44
+ gl_Position = projectionMatrix * mvPosition;
45
+
46
+ #include <logdepthbuf_vertex>
47
+ #include <clipping_planes_vertex>
48
+
49
+ // vViewPosition = - mvPosition.xyz;
50
+
51
+ }
@@ -0,0 +1,8 @@
1
+ #if defined(HAS_VELOCITY_BUFFER)
2
+ uniform sampler2D tVelocity;
3
+ vec2 getVelocity(const in vec2 uv) {
4
+ vec2 screenSpaceVelocity = texture2D(tVelocity, uv).xy * 2.0 - 1.0;
5
+ return sign(screenSpaceVelocity) * pow(abs(screenSpaceVelocity), vec2(4.));
6
+ }
7
+ #endif
8
+