11import { TempNode , MeshBasicNodeMaterial , RenderTarget , RGBAFormat , NearestFilter , QuadMesh , Vector2 } from 'three/webgpu' ;
22import { nodeObject , vec4 , vec3 , float , modelPosition , modelWorldMatrix , passTexture , hash , Fn , wgslFn , NodeUpdateType , texture , screenUV } from 'three/tsl' ;
33
4+ //Source: https://www.jacktollenaar.top/mesh-seam-smoothing-blending#h.50wag6hqg9gh
5+
46const _size = /*@__PURE__ */ new Vector2 ( ) ;
57
68class MeshBlendNode extends TempNode {
@@ -9,24 +11,18 @@ class MeshBlendNode extends TempNode {
911 this . sceneOutputNode = sceneOutputNode ;
1012 this . sceneDepthNode = sceneDepthNode ;
1113 this . updateBeforeType = NodeUpdateType . FRAME ;
12- this . renderTarget = new RenderTarget (
13- window . innerWidth * window . devicePixelRatio ,
14- window . innerHeight * window . devicePixelRatio ,
15- { format : RGBAFormat , count : 1 , minFilter : NearestFilter , magFilter : NearestFilter }
16- ) ;
14+ this . renderTarget = new RenderTarget ( 1 , 1 ) ;
1715 this . mainCamera = camera ;
1816 this . mainScene = scene ;
19- this . factor = float ( .1 ) ;
17+ this . blendFactor = float ( 1.2 ) ;
2018 this . kernelSize = float ( 5 ) ;
21- this . kernelRadius = float ( 0.3 ) ;
22- this . depthFalloff = float ( 0.00001 ) ;
23- this . depthThreshold = float ( 0.5 ) ;
19+ this . kernelRadius = float ( 0.01 * this . blendFactor . value ) ;
20+ this . depthFalloff = float ( 0.0001 * this . blendFactor . value ) ;
2421 this . debugMaterial = new MeshBasicNodeMaterial ( ) ;
2522 this . _quadMesh = new QuadMesh ( this . debugMaterial ) ;
2623 }
2724
2825 setup ( ) {
29- console . log ( "setup mesh blend" )
3026 const CustomHash = wgslFn ( `
3127 fn Hash(p: vec3f) -> f32 {
3228
@@ -35,12 +31,8 @@ class MeshBlendNode extends TempNode {
3531 return fract(lp.x * lp.y * lp.z * (lp.x + lp.y + lp.z));
3632 }
3733 ` )
38- this . hashShader = Fn ( ( { material, geometry, object } ) => {
39- // const objectPosition = new THREE.Vector3();
40- // object.getWorldPosition(objectPosition);
41- // const pos = vec3().mul(modelPosition);
34+ this . hashShader = Fn ( ( ) => {
4235 let p = vec3 ( modelWorldMatrix . mul ( vec3 ( modelPosition ) ) ) . toVar ( ) ;
43-
4436 return vec4 ( CustomHash ( p ) , 0. , 0. , 1. ) ;
4537 } ) ;
4638 this . hashMaterial = new MeshBasicNodeMaterial ( ) ;
@@ -49,8 +41,7 @@ class MeshBlendNode extends TempNode {
4941 const uv = screenUV ;
5042 const FinalOutputNode = Fn ( ( ) => {
5143 const outputPassFunc1 = wgslFn ( `
52- fn OutputPassFunc1(sceneColor: vec4<f32>, sceneDepth: vec4<f32>, tex: texture_2d<f32>, sampler: sampler, uv: vec2f, kernelSize: f32, kernelRadius: f32, depthFalloff: f32, depthThreshold: f32) -> vec4<f32> {
53- var result = sceneColor;
44+ fn OutputPassFunc1(sceneDepth: vec4<f32>, tex: texture_2d<f32>, sampler: sampler, uv: vec2f, kernelSize: f32, kernelRadius: f32, depthFalloff: f32) -> vec4<f32> {
5445 var seamLocation = vec2<f32>(0., 0.);
5546 var minDist = f32(9999999.);
5647
@@ -76,7 +67,7 @@ class MeshBlendNode extends TempNode {
7667 ` )
7768
7869 const finalPass = wgslFn ( `
79- fn FinalPass(sceneColor: vec3f , mirroredColor: vec3f , seamLocation: vec2f, kernelRadius: f32, sceneDepth: vec4f, otherDepth: vec4f, depthFalloff: f32, minDist: f32, depthThreshold: f32 ) -> vec3f {
70+ fn FinalPass(sceneColor: vec4f , mirroredColor: vec4f , seamLocation: vec2f, kernelRadius: f32, sceneDepth: vec4f, otherDepth: vec4f, depthFalloff: f32, minDist: f32) -> vec4f {
8071
8172 let depthDiff = abs(otherDepth.r - sceneDepth.r);
8273
@@ -85,26 +76,22 @@ class MeshBlendNode extends TempNode {
8576 let depthWeight = saturate(1. -depthDiff / depthFalloff * kernelRadius);
8677 var finalWeight = weight * depthWeight;
8778
88- if(sceneDepth.r > sceneDepth.r + depthThreshold){
89- finalWeight = 0.;
90- }
9179 return mix(sceneColor, mirroredColor, finalWeight);
9280 }
9381 ` ) ;
9482
9583 const pass1 = outputPassFunc1 (
96- texture ( this . sceneOutputNode , uv ) ,
9784 texture ( this . sceneDepthNode , uv ) ,
9885 texture ( this . renderTarget . textures [ 0 ] , uv ) ,
9986 texture ( this . sceneOutputNode , uv ) ,
100- uv , this . kernelSize , this . kernelRadius , this . depthFalloff , this . depthThreshold ) ;
87+ uv , this . kernelSize , this . kernelRadius , this . depthFalloff ) ;
10188
10289 const mirroredColor = texture ( this . sceneOutputNode , uv . add ( pass1 . xy . mul ( 2. ) ) ) ;
10390 const otherDepth = texture ( this . sceneDepthNode , uv . add ( pass1 . xy . mul ( 2. ) ) ) ;
10491
10592 const sceneColor = texture ( this . sceneOutputNode , uv ) ;
10693 const sceneDepth = texture ( this . sceneDepthNode , uv ) ;
107- return finalPass ( sceneColor , mirroredColor , pass1 . xy , this . kernelRadius , sceneDepth , otherDepth , this . depthFalloff , pass1 . z , this . depthThreshold ) ;
94+ return finalPass ( sceneColor , mirroredColor , pass1 . xy , this . kernelRadius , sceneDepth , otherDepth , this . depthFalloff , pass1 . z ) ;
10895 } ) ( ) ;
10996 return FinalOutputNode ;
11097 }
0 commit comments