Skip to content

Commit 7905c96

Browse files
authored
GTAONode: Add resolutionScale. (#29826)
1 parent 8bc792b commit 7905c96

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

examples/jsm/tsl/display/DenoiseNode.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ class DenoiseNode extends TempNode {
1818
this.normalNode = normalNode;
1919
this.noiseNode = noiseNode;
2020

21-
this.cameraProjectionMatrixInverse = uniform( camera.projectionMatrixInverse );
2221
this.lumaPhi = uniform( 5 );
2322
this.depthPhi = uniform( 5 );
2423
this.normalPhi = uniform( 5 );
2524
this.radius = uniform( 5 );
2625
this.index = uniform( 0 );
2726

27+
this.updateBeforeType = NodeUpdateType.FRAME;
28+
29+
// uniforms
30+
2831
this._resolution = uniform( new Vector2() );
2932
this._sampleVectors = uniformArray( generatePdSamplePointInitializer( 16, 2, 1 ) );
30-
31-
this.updateBeforeType = NodeUpdateType.FRAME;
33+
this._cameraProjectionMatrixInverse = uniform( camera.projectionMatrixInverse );
3234

3335
}
3436

@@ -46,7 +48,7 @@ class DenoiseNode extends TempNode {
4648

4749
const sampleTexture = ( uv ) => this.textureNode.uv( uv );
4850
const sampleDepth = ( uv ) => this.depthNode.uv( uv ).x;
49-
const sampleNormal = ( uv ) => ( this.normalNode !== null ) ? this.normalNode.uv( uv ).rgb.normalize() : getNormalFromDepth( uv, this.depthNode.value, this.cameraProjectionMatrixInverse );
51+
const sampleNormal = ( uv ) => ( this.normalNode !== null ) ? this.normalNode.uv( uv ).rgb.normalize() : getNormalFromDepth( uv, this.depthNode.value, this._cameraProjectionMatrixInverse );
5052
const sampleNoise = ( uv ) => this.noiseNode.uv( uv );
5153

5254
const denoiseSample = Fn( ( [ center, viewNormal, viewPosition, sampleUv ] ) => {
@@ -55,7 +57,7 @@ class DenoiseNode extends TempNode {
5557
const depth = sampleDepth( sampleUv ).toVar();
5658
const normal = sampleNormal( sampleUv ).toVar();
5759
const neighborColor = texel.rgb;
58-
const viewPos = getViewPosition( sampleUv, depth, this.cameraProjectionMatrixInverse ).toVar();
60+
const viewPos = getViewPosition( sampleUv, depth, this._cameraProjectionMatrixInverse ).toVar();
5961

6062
const normalDiff = dot( viewNormal, normal ).toVar();
6163
const normalSimilarity = pow( max( normalDiff, 0 ), this.normalPhi ).toVar();
@@ -84,7 +86,7 @@ class DenoiseNode extends TempNode {
8486

8587
const center = vec3( texel.rgb ).toVar();
8688

87-
const viewPosition = getViewPosition( uvNode, depth, this.cameraProjectionMatrixInverse ).toVar();
89+
const viewPosition = getViewPosition( uvNode, depth, this._cameraProjectionMatrixInverse ).toVar();
8890

8991
const noiseResolution = textureSize( this.noiseNode, 0 );
9092
let noiseUv = vec2( uvNode.x, uvNode.y.oneMinus() );

examples/jsm/tsl/display/GTAONode.js

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,37 @@ class GTAONode extends TempNode {
2525
this.depthNode = depthNode;
2626
this.normalNode = normalNode;
2727

28+
this.resolutionScale = 1;
29+
2830
this.radius = uniform( 0.25 );
2931
this.resolution = uniform( new Vector2() );
3032
this.thickness = uniform( 1 );
3133
this.distanceExponent = uniform( 1 );
3234
this.distanceFallOff = uniform( 1 );
3335
this.scale = uniform( 1 );
34-
this.noiseNode = texture( generateMagicSquareNoise() );
36+
this.samples = uniform( 16 );
3537

36-
this.cameraProjectionMatrix = uniform( camera.projectionMatrix );
37-
this.cameraProjectionMatrixInverse = uniform( camera.projectionMatrixInverse );
38+
this.updateBeforeType = NodeUpdateType.FRAME;
3839

39-
this.SAMPLES = uniform( 16 );
40+
// render targets
4041

4142
this._aoRenderTarget = new RenderTarget( 1, 1, { depthBuffer: false } );
4243
this._aoRenderTarget.texture.name = 'GTAONode.AO';
4344

44-
this._material = null;
45-
this._textureNode = passTexture( this, this._aoRenderTarget.texture );
45+
// uniforms
4646

47-
this.updateBeforeType = NodeUpdateType.FRAME;
47+
this._noiseNode = texture( generateMagicSquareNoise() );
48+
this._cameraProjectionMatrix = uniform( camera.projectionMatrix );
49+
this._cameraProjectionMatrixInverse = uniform( camera.projectionMatrixInverse );
50+
51+
// materials
52+
53+
this._material = new NodeMaterial();
54+
this._material.name = 'GTAO';
55+
56+
//
57+
58+
this._textureNode = passTexture( this, this._aoRenderTarget.texture );
4859

4960
}
5061

@@ -56,6 +67,9 @@ class GTAONode extends TempNode {
5667

5768
setSize( width, height ) {
5869

70+
width = Math.round( this.resolutionScale * width );
71+
height = Math.round( this.resolutionScale * height );
72+
5973
this.resolution.value.set( width, height );
6074
this._aoRenderTarget.setSize( width, height );
6175

@@ -94,21 +108,21 @@ class GTAONode extends TempNode {
94108
const uvNode = uv();
95109

96110
const sampleDepth = ( uv ) => this.depthNode.uv( uv ).x;
97-
const sampleNoise = ( uv ) => this.noiseNode.uv( uv );
98-
const sampleNormal = ( uv ) => ( this.normalNode !== null ) ? this.normalNode.uv( uv ).rgb.normalize() : getNormalFromDepth( uv, this.depthNode.value, this.cameraProjectionMatrixInverse );
111+
const sampleNoise = ( uv ) => this._noiseNode.uv( uv );
112+
const sampleNormal = ( uv ) => ( this.normalNode !== null ) ? this.normalNode.uv( uv ).rgb.normalize() : getNormalFromDepth( uv, this.depthNode.value, this._cameraProjectionMatrixInverse );
99113

100114
const ao = Fn( () => {
101115

102116
const depth = sampleDepth( uvNode ).toVar();
103117

104118
depth.greaterThanEqual( 1.0 ).discard();
105119

106-
const viewPosition = getViewPosition( uvNode, depth, this.cameraProjectionMatrixInverse ).toVar();
120+
const viewPosition = getViewPosition( uvNode, depth, this._cameraProjectionMatrixInverse ).toVar();
107121
const viewNormal = sampleNormal( uvNode ).toVar();
108122

109123
const radiusToUse = this.radius;
110124

111-
const noiseResolution = textureSize( this.noiseNode, 0 );
125+
const noiseResolution = textureSize( this._noiseNode, 0 );
112126
let noiseUv = vec2( uvNode.x, uvNode.y.oneMinus() );
113127
noiseUv = noiseUv.mul( this.resolution.div( noiseResolution ) );
114128
const noiseTexel = sampleNoise( noiseUv );
@@ -117,8 +131,8 @@ class GTAONode extends TempNode {
117131
const bitangent = vec3( tangent.y.mul( - 1.0 ), tangent.x, 0.0 );
118132
const kernelMatrix = mat3( tangent, bitangent, vec3( 0.0, 0.0, 1.0 ) );
119133

120-
const DIRECTIONS = this.SAMPLES.lessThan( 30 ).select( 3, 5 ).toVar();
121-
const STEPS = add( this.SAMPLES, DIRECTIONS.sub( 1 ) ).div( DIRECTIONS ).toVar();
134+
const DIRECTIONS = this.samples.lessThan( 30 ).select( 3, 5 ).toVar();
135+
const STEPS = add( this.samples, DIRECTIONS.sub( 1 ) ).div( DIRECTIONS ).toVar();
122136

123137
const ao = float( 0 ).toVar();
124138

@@ -142,9 +156,9 @@ class GTAONode extends TempNode {
142156

143157
// x
144158

145-
const sampleScreenPositionX = getScreenPosition( viewPosition.add( sampleViewOffset ), this.cameraProjectionMatrix ).toVar();
159+
const sampleScreenPositionX = getScreenPosition( viewPosition.add( sampleViewOffset ), this._cameraProjectionMatrix ).toVar();
146160
const sampleDepthX = sampleDepth( sampleScreenPositionX ).toVar();
147-
const sampleSceneViewPositionX = getViewPosition( sampleScreenPositionX, sampleDepthX, this.cameraProjectionMatrixInverse ).toVar();
161+
const sampleSceneViewPositionX = getViewPosition( sampleScreenPositionX, sampleDepthX, this._cameraProjectionMatrixInverse ).toVar();
148162
const viewDeltaX = sampleSceneViewPositionX.sub( viewPosition ).toVar();
149163

150164
If( abs( viewDeltaX.z ).lessThan( this.thickness ), () => {
@@ -156,9 +170,9 @@ class GTAONode extends TempNode {
156170

157171
// y
158172

159-
const sampleScreenPositionY = getScreenPosition( viewPosition.sub( sampleViewOffset ), this.cameraProjectionMatrix ).toVar();
173+
const sampleScreenPositionY = getScreenPosition( viewPosition.sub( sampleViewOffset ), this._cameraProjectionMatrix ).toVar();
160174
const sampleDepthY = sampleDepth( sampleScreenPositionY ).toVar();
161-
const sampleSceneViewPositionY = getViewPosition( sampleScreenPositionY, sampleDepthY, this.cameraProjectionMatrixInverse ).toVar();
175+
const sampleSceneViewPositionY = getViewPosition( sampleScreenPositionY, sampleDepthY, this._cameraProjectionMatrixInverse ).toVar();
162176
const viewDeltaY = sampleSceneViewPositionY.sub( viewPosition ).toVar();
163177

164178
If( abs( viewDeltaY.z ).lessThan( this.thickness ), () => {
@@ -187,10 +201,8 @@ class GTAONode extends TempNode {
187201

188202
} );
189203

190-
const material = this._material || ( this._material = new NodeMaterial() );
191-
material.fragmentNode = ao().context( builder.getSharedContext() );
192-
material.name = 'GTAO';
193-
material.needsUpdate = true;
204+
this._material.fragmentNode = ao().context( builder.getSharedContext() );
205+
this._material.needsUpdate = true;
194206

195207
//
196208

@@ -202,6 +214,8 @@ class GTAONode extends TempNode {
202214

203215
this._aoRenderTarget.dispose();
204216

217+
this._material.dispose();
218+
205219
}
206220

207221
}

0 commit comments

Comments
 (0)