11import { Vector2 , Vector3 } from 'three' ;
2- import { getViewPosition , convertToTexture , TempNode , nodeObject , Fn , float , NodeUpdateType , uv , uniform , Loop , luminance , vec2 , vec3 , vec4 , uniformArray , int , dot , max , pow , abs , If , textureSize , sin , cos , mat2 , PI } from 'three/tsl' ;
2+ import { getNormalFromDepth , getViewPosition , convertToTexture , TempNode , nodeObject , Fn , float , NodeUpdateType , uv , uniform , Loop , luminance , vec2 , vec3 , vec4 , uniformArray , int , dot , max , pow , abs , If , textureSize , sin , cos , mat2 , PI } from 'three/tsl' ;
33
44class DenoiseNode extends TempNode {
55
@@ -46,16 +46,16 @@ class DenoiseNode extends TempNode {
4646
4747 const sampleTexture = ( uv ) => this . textureNode . uv ( uv ) ;
4848 const sampleDepth = ( uv ) => this . depthNode . uv ( uv ) . x ;
49- const sampleNormal = ( uv ) => this . normalNode . uv ( uv ) ;
49+ const sampleNormal = ( uv ) => ( this . normalNode !== null ) ? this . normalNode . uv ( uv ) . rgb . normalize ( ) : getNormalFromDepth ( uv , this . depthNode . value , this . cameraProjectionMatrixInverse ) ;
5050 const sampleNoise = ( uv ) => this . noiseNode . uv ( uv ) ;
5151
5252 const denoiseSample = Fn ( ( [ center , viewNormal , viewPosition , sampleUv ] ) => {
5353
54- const texel = sampleTexture ( sampleUv ) ;
55- const depth = sampleDepth ( sampleUv ) ;
56- const normal = sampleNormal ( sampleUv ) . rgb . normalize ( ) ;
54+ const texel = sampleTexture ( sampleUv ) . toVar ( ) ;
55+ const depth = sampleDepth ( sampleUv ) . toVar ( ) ;
56+ const normal = sampleNormal ( sampleUv ) . toVar ( ) ;
5757 const neighborColor = texel . rgb ;
58- const viewPos = getViewPosition ( sampleUv , depth , this . cameraProjectionMatrixInverse ) ;
58+ const viewPos = getViewPosition ( sampleUv , depth , this . cameraProjectionMatrixInverse ) . toVar ( ) ;
5959
6060 const normalDiff = dot ( viewNormal , normal ) . toVar ( ) ;
6161 const normalSimilarity = pow ( max ( normalDiff , 0 ) , this . normalPhi ) . toVar ( ) ;
@@ -71,31 +71,31 @@ class DenoiseNode extends TempNode {
7171
7272 const denoise = Fn ( ( [ uvNode ] ) => {
7373
74- const depth = sampleDepth ( uvNode ) ;
75- const viewNormal = sampleNormal ( uvNode ) . rgb . normalize ( ) ;
74+ const depth = sampleDepth ( uvNode ) . toVar ( ) ;
75+ const viewNormal = sampleNormal ( uvNode ) . toVar ( ) ;
7676
77- const texel = sampleTexture ( uvNode ) ;
77+ const texel = sampleTexture ( uvNode ) . toVar ( ) ;
7878
7979 If ( depth . greaterThanEqual ( 1.0 ) . or ( dot ( viewNormal , viewNormal ) . equal ( 0.0 ) ) , ( ) => {
8080
8181 return texel ;
8282
8383 } ) ;
8484
85- const center = vec3 ( texel . rgb ) ;
85+ const center = vec3 ( texel . rgb ) . toVar ( ) ;
8686
87- const viewPosition = getViewPosition ( uvNode , depth , this . cameraProjectionMatrixInverse ) ;
87+ const viewPosition = getViewPosition ( uvNode , depth , this . cameraProjectionMatrixInverse ) . toVar ( ) ;
8888
8989 const noiseResolution = textureSize ( this . noiseNode , 0 ) ;
9090 let noiseUv = vec2 ( uvNode . x , uvNode . y . oneMinus ( ) ) ;
9191 noiseUv = noiseUv . mul ( this . _resolution . div ( noiseResolution ) ) ;
92- const noiseTexel = sampleNoise ( noiseUv ) ;
92+ const noiseTexel = sampleNoise ( noiseUv ) . toVar ( ) ;
9393
94- const x = sin ( noiseTexel . element ( this . index . mod ( 4 ) . mul ( 2 ) . mul ( PI ) ) ) ;
95- const y = cos ( noiseTexel . element ( this . index . mod ( 4 ) . mul ( 2 ) . mul ( PI ) ) ) ;
94+ const x = sin ( noiseTexel . element ( this . index . mod ( 4 ) . mul ( 2 ) . mul ( PI ) ) ) . toVar ( ) ;
95+ const y = cos ( noiseTexel . element ( this . index . mod ( 4 ) . mul ( 2 ) . mul ( PI ) ) ) . toVar ( ) ;
9696
97- const noiseVec = vec2 ( x , y ) ;
98- const rotationMatrix = mat2 ( noiseVec . x , noiseVec . y . negate ( ) , noiseVec . x , noiseVec . y ) ;
97+ const noiseVec = vec2 ( x , y ) . toVar ( ) ;
98+ const rotationMatrix = mat2 ( noiseVec . x , noiseVec . y . negate ( ) , noiseVec . x , noiseVec . y ) . toVar ( ) ;
9999
100100 const totalWeight = float ( 1.0 ) . toVar ( ) ;
101101 const denoised = vec3 ( texel . rgb ) . toVar ( ) ;
0 commit comments