Skip to content

Commit ae88e87

Browse files
sunagRuthySheffi
authored andcommitted
TSL: Fix denoise() sampler texture (mrdoob#30975)
* remove layout * rename
1 parent 48da7b7 commit ae88e87

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

examples/jsm/tsl/display/DenoiseNode.js

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DataTexture, RepeatWrapping, Vector2, Vector3, TempNode } from 'three/webgpu';
2-
import { texture, getNormalFromDepth, getViewPosition, convertToTexture, 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 { texture, getNormalFromDepth, getViewPosition, convertToTexture, nodeObject, Fn, float, NodeUpdateType, uv, uniform, Loop, luminance, vec2, vec3, vec4, uniformArray, int, dot, max, pow, abs, If, textureSize, sin, cos, mat2, PI, property } from 'three/tsl';
33
import { SimplexNoise } from '../../math/SimplexNoise.js';
44

55
/**
@@ -187,59 +187,58 @@ class DenoiseNode extends TempNode {
187187
const viewNormal = sampleNormal( uvNode ).toVar();
188188

189189
const texel = sampleTexture( uvNode ).toVar();
190+
const result = property( 'vec4' );
190191

191192
If( depth.greaterThanEqual( 1.0 ).or( dot( viewNormal, viewNormal ).equal( 0.0 ) ), () => {
192193

193-
return texel;
194+
result.assign( texel );
194195

195-
} );
196+
} ).Else( () => {
196197

197-
const center = vec3( texel.rgb ).toVar();
198+
const center = vec3( texel.rgb );
198199

199-
const viewPosition = getViewPosition( uvNode, depth, this._cameraProjectionMatrixInverse ).toVar();
200+
const viewPosition = getViewPosition( uvNode, depth, this._cameraProjectionMatrixInverse ).toConst();
200201

201-
const noiseResolution = textureSize( this.noiseNode, 0 );
202-
let noiseUv = vec2( uvNode.x, uvNode.y.oneMinus() );
203-
noiseUv = noiseUv.mul( this._resolution.div( noiseResolution ) );
204-
const noiseTexel = sampleNoise( noiseUv ).toVar();
202+
const noiseResolution = textureSize( this.noiseNode, 0 );
203+
let noiseUv = vec2( uvNode.x, uvNode.y.oneMinus() );
204+
noiseUv = noiseUv.mul( this._resolution.div( noiseResolution ) );
205+
const noiseTexel = sampleNoise( noiseUv ).toVar();
205206

206-
const x = sin( noiseTexel.element( this.index.mod( 4 ).mul( 2 ).mul( PI ) ) ).toVar();
207-
const y = cos( noiseTexel.element( this.index.mod( 4 ).mul( 2 ).mul( PI ) ) ).toVar();
207+
const x = sin( noiseTexel.element( this.index.mod( 4 ).mul( 2 ).mul( PI ) ) );
208+
const y = cos( noiseTexel.element( this.index.mod( 4 ).mul( 2 ).mul( PI ) ) );
208209

209-
const noiseVec = vec2( x, y ).toVar();
210-
const rotationMatrix = mat2( noiseVec.x, noiseVec.y.negate(), noiseVec.x, noiseVec.y ).toVar();
210+
const noiseVec = vec2( x, y );
211+
const rotationMatrix = mat2( noiseVec.x, noiseVec.y.negate(), noiseVec.x, noiseVec.y );
211212

212-
const totalWeight = float( 1.0 ).toVar();
213-
const denoised = vec3( texel.rgb ).toVar();
213+
const totalWeight = float( 1.0 ).toVar();
214+
const denoised = vec3( texel.rgb ).toVar();
214215

215-
Loop( { start: int( 0 ), end: int( 16 ), type: 'int', condition: '<' }, ( { i } ) => {
216+
Loop( { start: int( 0 ), end: int( 16 ), type: 'int', condition: '<' }, ( { i } ) => {
216217

217-
const sampleDir = this._sampleVectors.element( i ).toVar();
218-
const offset = rotationMatrix.mul( sampleDir.xy.mul( float( 1.0 ).add( sampleDir.z.mul( this.radius.sub( 1 ) ) ) ) ).div( this._resolution ).toVar();
219-
const sampleUv = uvNode.add( offset ).toVar();
218+
const sampleDir = this._sampleVectors.element( i );
219+
const offset = rotationMatrix.mul( sampleDir.xy.mul( float( 1.0 ).add( sampleDir.z.mul( this.radius.sub( 1 ) ) ) ) ).div( this._resolution );
220+
const sampleUv = uvNode.add( offset );
220221

221-
const result = denoiseSample( center, viewNormal, viewPosition, sampleUv );
222+
const sampleResult = denoiseSample( center, viewNormal, viewPosition, sampleUv );
222223

223-
denoised.addAssign( result.xyz );
224-
totalWeight.addAssign( result.w );
224+
denoised.addAssign( sampleResult.xyz );
225+
totalWeight.addAssign( sampleResult.w );
225226

226-
} );
227+
} );
228+
229+
If( totalWeight.greaterThan( float( 0 ) ), () => {
227230

228-
If( totalWeight.greaterThan( float( 0 ) ), () => {
231+
denoised.divAssign( totalWeight );
229232

230-
denoised.divAssign( totalWeight );
233+
} );
234+
235+
result.assign( vec4( denoised, texel.a ) );
231236

232237
} );
233238

234-
return vec4( denoised, texel.a );
239+
return result;
235240

236-
} ).setLayout( {
237-
name: 'denoise',
238-
type: 'vec4',
239-
inputs: [
240-
{ name: 'uv', type: 'vec2' }
241-
]
242-
} );
241+
}/*, { uv: 'vec2', return: 'vec4' }*/ );
243242

244243
const output = Fn( () => {
245244

0 commit comments

Comments
 (0)