@@ -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