Skip to content

Commit ca69e6c

Browse files
querieloCopilotsunag
authored
TSL: Introduce builtinAOContext and Enhance AO Post-Processing Example (#32378)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: sunag <sunagbrasil@gmail.com>
1 parent 83c5e36 commit ca69e6c

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

examples/webgpu_postprocessing_ao.html

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<script type="module">
3737

3838
import * as THREE from 'three/webgpu';
39-
import { sample, pass, mrt, context, screenUV, normalView, velocity, vec3, vec4, directionToColor, colorToDirection, colorSpaceToWorking } from 'three/tsl';
39+
import { sample, pass, mrt, screenUV, normalView, velocity, vec3, vec4, directionToColor, colorToDirection, colorSpaceToWorking, builtinAOContext } from 'three/tsl';
4040
import { ao } from 'three/addons/tsl/display/GTAONode.js';
4141
import { traa } from 'three/addons/tsl/display/TRAANode.js';
4242

@@ -49,7 +49,7 @@
4949

5050
let camera, scene, renderer, postProcessing, controls;
5151

52-
let aoPass, traaPass;
52+
let aoPass, traaPass, transparentMesh;
5353

5454
const params = {
5555
samples: 16,
@@ -58,7 +58,8 @@
5858
radius: 0.25,
5959
scale: 1,
6060
thickness: 1,
61-
aoOnly: false
61+
aoOnly: false,
62+
transparentOpacity: 0.3
6263
};
6364

6465
init();
@@ -142,9 +143,7 @@
142143

143144
// scene context
144145

145-
scenePass.contextNode = context( {
146-
ao: aoPassOutput.sample( screenUV ).r
147-
} );
146+
scenePass.contextNode = builtinAOContext( aoPassOutput.sample( screenUV ).r );
148147

149148
// final output + traa
150149

@@ -170,8 +169,7 @@
170169

171170
//
172171

173-
const transparentMesh = new THREE.Mesh( new THREE.PlaneGeometry( 1.8, 2 ), new THREE.MeshStandardNodeMaterial( { transparent: true, opacity: .1 } ) );
174-
transparentMesh.material.transparent = true;
172+
transparentMesh = new THREE.Mesh( new THREE.PlaneGeometry( 1.8, 2 ), new THREE.MeshStandardNodeMaterial( { transparent: true, opacity: params.transparentOpacity } ) );
175173
transparentMesh.position.z = 0;
176174
transparentMesh.position.y = 0.5;
177175
transparentMesh.visible = false;
@@ -192,6 +190,7 @@
192190
gui.add( params, 'thickness', 0.01, 2 ).onChange( updateParameters );
193191
gui.add( aoPass, 'useTemporalFiltering' ).name( 'temporal filtering' );
194192
gui.add( transparentMesh, 'visible' ).name( 'show transparent mesh' );
193+
gui.add( params, 'transparentOpacity', 0, 1, 0.01 ).name( 'transparent opacity' ).onChange( updateParameters );
195194
gui.add( params, 'aoOnly' ).onChange( ( value ) => {
196195

197196
if ( value === true ) {
@@ -220,6 +219,8 @@
220219
aoPass.scale.value = params.scale;
221220
aoPass.thickness.value = params.thickness;
222221

222+
transparentMesh.material.opacity = params.transparentOpacity;
223+
223224
}
224225

225226
function onWindowResize() {

src/Three.TSL.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export const bufferAttribute = TSL.bufferAttribute;
100100
export const bumpMap = TSL.bumpMap;
101101
export const burn = TSL.burn;
102102
export const builtin = TSL.builtin;
103+
export const builtinAOContext = TSL.builtinAOContext;
103104
export const builtinShadowContext = TSL.builtinShadowContext;
104105
export const bvec2 = TSL.bvec2;
105106
export const bvec3 = TSL.bvec3;

src/materials/nodes/NodeMaterial.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,17 +1060,21 @@ class NodeMaterial extends Material {
10601060

10611061
}
10621062

1063-
let aoNode = builder.context.ao || null;
1063+
let aoNode = this.aoNode;
10641064

1065-
if ( this.aoNode !== null || builder.material.aoMap ) {
1065+
if ( aoNode === null && builder.material.aoMap ) {
10661066

1067-
const mtlAO = this.aoNode !== null ? this.aoNode : materialAO;
1067+
aoNode = materialAO;
10681068

1069-
aoNode = aoNode !== null ? aoNode.mul( mtlAO ) : mtlAO;
1069+
}
1070+
1071+
if ( builder.context.getAO ) {
1072+
1073+
aoNode = builder.context.getAO( aoNode, builder );
10701074

10711075
}
10721076

1073-
if ( aoNode !== null ) {
1077+
if ( aoNode ) {
10741078

10751079
materialLightsNode.push( new AONode( aoNode ) );
10761080

src/materials/nodes/manager/NodeMaterialObserver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ class NodeMaterialObserver {
259259

260260
}
261261

262-
if ( builder.context.modelViewMatrix || builder.context.modelNormalViewMatrix || builder.context.ao || builder.context.getShadow )
262+
if ( builder.context.modelViewMatrix || builder.context.modelNormalViewMatrix || builder.context.getAO || builder.context.getShadow )
263263
return true;
264264

265265
return false;

src/nodes/core/ContextNode.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,31 @@ export function builtinShadowContext( shadowNode, light, node = null ) {
232232

233233
}
234234

235+
/**
236+
* TSL function for defining a built-in ambient occlusion context for a given node.
237+
*
238+
* @tsl
239+
* @function
240+
* @param {Node} aoNode - The ambient occlusion value node to apply.
241+
* @param {Node} [node=null] - The node whose context should be modified.
242+
* @returns {ContextNode}
243+
*/
244+
export function builtinAOContext( aoNode, node = null ) {
245+
246+
return context( node, {
247+
248+
getAO: ( inputNode, { material } ) => {
249+
250+
if ( material.transparent === true ) return inputNode;
251+
252+
return inputNode !== null ? inputNode.mul( aoNode ) : aoNode;
253+
254+
}
255+
256+
} );
257+
258+
}
259+
235260
/**
236261
* TSL function for defining a label context value for a given node.
237262
*
@@ -255,3 +280,4 @@ addMethodChaining( 'label', label );
255280
addMethodChaining( 'uniformFlow', uniformFlow );
256281
addMethodChaining( 'setName', setName );
257282
addMethodChaining( 'builtinShadowContext', ( node, shadowNode, light ) => builtinShadowContext( shadowNode, light, node ) );
283+
addMethodChaining( 'builtinAOContext', ( node, aoValue ) => builtinAOContext( aoValue, node ) );

0 commit comments

Comments
 (0)