| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583 |
- import { HalfFloatType, Vector2, RenderTarget, RendererUtils, QuadMesh, NodeMaterial, TempNode, NodeUpdateType, Matrix4, DepthTexture } from 'three/webgpu';
- import { add, float, If, Loop, int, Fn, min, max, clamp, nodeObject, texture, uniform, uv, vec2, vec4, luminance, convertToTexture, passTexture, velocity, getViewPosition, length } from 'three/tsl';
- const _quadMesh = /*@__PURE__*/ new QuadMesh();
- const _size = /*@__PURE__*/ new Vector2();
- let _rendererState;
- /**
- * A special node that applies TRAA (Temporal Reprojection Anti-Aliasing).
- *
- * References:
- * - {@link https://alextardif.com/TAA.html}
- * - {@link https://www.elopezr.com/temporal-aa-and-the-quest-for-the-holy-trail/}
- *
- * @augments TempNode
- * @three_import import { traa } from 'three/addons/tsl/display/TRAANode.js';
- */
- class TRAANode extends TempNode {
- static get type() {
- return 'TRAANode';
- }
- /**
- * Constructs a new TRAA node.
- *
- * @param {TextureNode} beautyNode - The texture node that represents the input of the effect.
- * @param {TextureNode} depthNode - A node that represents the scene's depth.
- * @param {TextureNode} velocityNode - A node that represents the scene's velocity.
- * @param {Camera} camera - The camera the scene is rendered with.
- */
- constructor( beautyNode, depthNode, velocityNode, camera ) {
- super( 'vec4' );
- /**
- * This flag can be used for type testing.
- *
- * @type {boolean}
- * @readonly
- * @default true
- */
- this.isTRAANode = true;
- /**
- * The `updateBeforeType` is set to `NodeUpdateType.FRAME` since the node renders
- * its effect once per frame in `updateBefore()`.
- *
- * @type {string}
- * @default 'frame'
- */
- this.updateBeforeType = NodeUpdateType.FRAME;
- /**
- * The texture node that represents the input of the effect.
- *
- * @type {TextureNode}
- */
- this.beautyNode = beautyNode;
- /**
- * A node that represents the scene's velocity.
- *
- * @type {TextureNode}
- */
- this.depthNode = depthNode;
- /**
- * A node that represents the scene's velocity.
- *
- * @type {TextureNode}
- */
- this.velocityNode = velocityNode;
- /**
- * The camera the scene is rendered with.
- *
- * @type {Camera}
- */
- this.camera = camera;
- /**
- * The jitter index selects the current camera offset value.
- *
- * @private
- * @type {number}
- * @default 0
- */
- this._jitterIndex = 0;
- /**
- * A uniform node holding the inverse resolution value.
- *
- * @private
- * @type {UniformNode<vec2>}
- */
- this._invSize = uniform( new Vector2() );
- /**
- * A uniform node holding the camera world matrix.
- *
- * @private
- * @type {UniformNode<mat4>}
- */
- this._cameraWorldMatrix = uniform( new Matrix4() );
- /**
- * A uniform node holding the camera projection matrix inverse.
- *
- * @private
- * @type {UniformNode<mat4>}
- */
- this._cameraProjectionMatrixInverse = uniform( new Matrix4() );
- /**
- * A uniform node holding the previous frame's view matrix.
- *
- * @private
- * @type {UniformNode<mat4>}
- */
- this._previousCameraWorldMatrix = uniform( new Matrix4() );
- /**
- * A uniform node holding the previous frame's projection matrix inverse.
- *
- * @private
- * @type {UniformNode<mat4>}
- */
- this._previousCameraProjectionMatrixInverse = uniform( new Matrix4() );
- /**
- * The render target that represents the history of frame data.
- *
- * @private
- * @type {?RenderTarget}
- */
- this._historyRenderTarget = new RenderTarget( 1, 1, { depthBuffer: false, type: HalfFloatType, depthTexture: new DepthTexture() } );
- this._historyRenderTarget.texture.name = 'TRAANode.history';
- /**
- * The render target for the resolve.
- *
- * @private
- * @type {?RenderTarget}
- */
- this._resolveRenderTarget = new RenderTarget( 1, 1, { depthBuffer: false, type: HalfFloatType } );
- this._resolveRenderTarget.texture.name = 'TRAANode.resolve';
- /**
- * Material used for the resolve step.
- *
- * @private
- * @type {NodeMaterial}
- */
- this._resolveMaterial = new NodeMaterial();
- this._resolveMaterial.name = 'TRAA.resolve';
- /**
- * The result of the effect is represented as a separate texture node.
- *
- * @private
- * @type {PassTextureNode}
- */
- this._textureNode = passTexture( this, this._resolveRenderTarget.texture );
- /**
- * Used to save the original/unjittered projection matrix.
- *
- * @private
- * @type {Matrix4}
- */
- this._originalProjectionMatrix = new Matrix4();
- /**
- * A texture node for the previous depth buffer.
- *
- * @private
- * @type {TextureNode}
- */
- this._previousDepthNode = texture( new DepthTexture( 1, 1 ) );
- /**
- * Sync the post processing stack with the TRAA node.
- * @private
- * @type {boolean}
- */
- this._needsPostProcessingSync = false;
- }
- /**
- * Returns the result of the effect as a texture node.
- *
- * @return {PassTextureNode} A texture node that represents the result of the effect.
- */
- getTextureNode() {
- return this._textureNode;
- }
- /**
- * Sets the size of the effect.
- *
- * @param {number} width - The width of the effect.
- * @param {number} height - The height of the effect.
- */
- setSize( width, height ) {
- this._historyRenderTarget.setSize( width, height );
- this._resolveRenderTarget.setSize( width, height );
- this._invSize.value.set( 1 / width, 1 / height );
- }
- /**
- * Defines the TRAA's current jitter as a view offset
- * to the scene's camera.
- *
- * @param {number} width - The width of the effect.
- * @param {number} height - The height of the effect.
- */
- setViewOffset( width, height ) {
- // save original/unjittered projection matrix for velocity pass
- this.camera.updateProjectionMatrix();
- this._originalProjectionMatrix.copy( this.camera.projectionMatrix );
- velocity.setProjectionMatrix( this._originalProjectionMatrix );
- //
- const viewOffset = {
- fullWidth: width,
- fullHeight: height,
- offsetX: 0,
- offsetY: 0,
- width: width,
- height: height
- };
- const jitterOffset = _haltonOffsets[ this._jitterIndex ];
- this.camera.setViewOffset(
- viewOffset.fullWidth, viewOffset.fullHeight,
- viewOffset.offsetX + jitterOffset[ 0 ] - 0.5, viewOffset.offsetY + jitterOffset[ 1 ] - 0.5,
- viewOffset.width, viewOffset.height
- );
- }
- /**
- * Clears the view offset from the scene's camera.
- */
- clearViewOffset() {
- this.camera.clearViewOffset();
- velocity.setProjectionMatrix( null );
- // update jitter index
- this._jitterIndex ++;
- this._jitterIndex = this._jitterIndex % ( _haltonOffsets.length - 1 );
- }
- /**
- * This method is used to render the effect once per frame.
- *
- * @param {NodeFrame} frame - The current node frame.
- */
- updateBefore( frame ) {
- const { renderer } = frame;
- // store previous frame matrices before updating current ones
- this._previousCameraWorldMatrix.value.copy( this._cameraWorldMatrix.value );
- this._previousCameraProjectionMatrixInverse.value.copy( this._cameraProjectionMatrixInverse.value );
- // update camera matrices uniforms
- this._cameraWorldMatrix.value.copy( this.camera.matrixWorld );
- this._cameraProjectionMatrixInverse.value.copy( this.camera.projectionMatrixInverse );
- // keep the TRAA in sync with the dimensions of the beauty node
- const beautyRenderTarget = ( this.beautyNode.isRTTNode ) ? this.beautyNode.renderTarget : this.beautyNode.passNode.renderTarget;
- const width = beautyRenderTarget.texture.width;
- const height = beautyRenderTarget.texture.height;
- //
- if ( this._needsPostProcessingSync === true ) {
- this.setViewOffset( width, height );
- this._needsPostProcessingSync = false;
- }
- _rendererState = RendererUtils.resetRendererState( renderer, _rendererState );
- //
- const needsRestart = this._historyRenderTarget.width !== width || this._historyRenderTarget.height !== height;
- this.setSize( width, height );
- // every time when the dimensions change we need fresh history data
- if ( needsRestart === true ) {
- // bind and clear render target to make sure they are initialized after the resize which triggers a dispose()
- renderer.setRenderTarget( this._historyRenderTarget );
- renderer.clear();
- renderer.setRenderTarget( this._resolveRenderTarget );
- renderer.clear();
- // make sure to reset the history with the contents of the beauty buffer otherwise subsequent frames after the
- // resize will fade from a darker color to the correct one because the history was cleared with black.
- renderer.copyTextureToTexture( beautyRenderTarget.texture, this._historyRenderTarget.texture );
- }
- // resolve
- renderer.setRenderTarget( this._resolveRenderTarget );
- _quadMesh.material = this._resolveMaterial;
- _quadMesh.name = 'TRAA';
- _quadMesh.render( renderer );
- renderer.setRenderTarget( null );
- // update history
- renderer.copyTextureToTexture( this._resolveRenderTarget.texture, this._historyRenderTarget.texture );
- // Copy current depth to previous depth buffer
- const size = renderer.getDrawingBufferSize( _size );
- // only allow the depth copy if the dimensions of the history render target match with the drawing
- // render buffer and thus the depth texture of the scene. For some reasons, there are timing issues
- // with WebGPU resulting in different size of the drawing buffer and the beauty render target when
- // resizing the browser window. This does not happen with the WebGL backend
- if ( this._historyRenderTarget.height === size.height && this._historyRenderTarget.width === size.width ) {
- const currentDepth = this.depthNode.value;
- renderer.copyTextureToTexture( currentDepth, this._historyRenderTarget.depthTexture );
- this._previousDepthNode.value = this._historyRenderTarget.depthTexture;
- }
- // restore
- RendererUtils.restoreRendererState( renderer, _rendererState );
- }
- /**
- * This method is used to setup the effect's render targets and TSL code.
- *
- * @param {NodeBuilder} builder - The current node builder.
- * @return {PassTextureNode}
- */
- setup( builder ) {
- const postProcessing = builder.context.postProcessing;
- if ( postProcessing ) {
- this._needsPostProcessingSync = true;
- postProcessing.context.onBeforePostProcessing = () => {
- const size = builder.renderer.getDrawingBufferSize( _size );
- this.setViewOffset( size.width, size.height );
- };
- postProcessing.context.onAfterPostProcessing = () => {
- this.clearViewOffset();
- };
- }
- const historyTexture = texture( this._historyRenderTarget.texture );
- const sampleTexture = this.beautyNode;
- const depthTexture = this.depthNode;
- const velocityTexture = this.velocityNode;
- const resolve = Fn( () => {
- const uvNode = uv();
- const minColor = vec4( 10000 ).toVar();
- const maxColor = vec4( - 10000 ).toVar();
- const closestDepth = float( 1 ).toVar();
- const farthestDepth = float( 0 ).toVar();
- const closestDepthPixelPosition = vec2( 0 ).toVar();
- // sample a 3x3 neighborhood to create a box in color space
- // clamping the history color with the resulting min/max colors mitigates ghosting
- Loop( { start: int( - 1 ), end: int( 1 ), type: 'int', condition: '<=', name: 'x' }, ( { x } ) => {
- Loop( { start: int( - 1 ), end: int( 1 ), type: 'int', condition: '<=', name: 'y' }, ( { y } ) => {
- const uvNeighbor = uvNode.add( vec2( float( x ), float( y ) ).mul( this._invSize ) ).toVar();
- const colorNeighbor = max( vec4( 0 ), sampleTexture.sample( uvNeighbor ) ).toVar(); // use max() to avoid propagate garbage values
- minColor.assign( min( minColor, colorNeighbor ) );
- maxColor.assign( max( maxColor, colorNeighbor ) );
- const currentDepth = depthTexture.sample( uvNeighbor ).r.toVar();
- // find the sample position of the closest depth in the neighborhood (used for velocity)
- If( currentDepth.lessThan( closestDepth ), () => {
- closestDepth.assign( currentDepth );
- closestDepthPixelPosition.assign( uvNeighbor );
- } );
- // find the farthest depth in the neighborhood (used to preserve edge anti-aliasing)
- If( currentDepth.greaterThan( farthestDepth ), () => {
- farthestDepth.assign( currentDepth );
- } );
- } );
- } );
- // sampling/reprojection
- const offset = velocityTexture.sample( closestDepthPixelPosition ).xy.mul( vec2( 0.5, - 0.5 ) ); // NDC to uv offset
- const currentColor = sampleTexture.sample( uvNode );
- const historyColor = historyTexture.sample( uvNode.sub( offset ) );
- // clamping
- const clampedHistoryColor = clamp( historyColor, minColor, maxColor );
- // calculate current frame world position
- const currentDepth = depthTexture.sample( uvNode ).r;
- const currentViewPosition = getViewPosition( uvNode, currentDepth, this._cameraProjectionMatrixInverse );
- const currentWorldPosition = this._cameraWorldMatrix.mul( vec4( currentViewPosition, 1.0 ) ).xyz;
- // calculate previous frame world position from history UV and previous depth
- const historyUV = uvNode.sub( offset );
- const previousDepth = this._previousDepthNode.sample( historyUV ).r;
- const previousViewPosition = getViewPosition( historyUV, previousDepth, this._previousCameraProjectionMatrixInverse );
- const previousWorldPosition = this._previousCameraWorldMatrix.mul( vec4( previousViewPosition, 1.0 ) ).xyz;
- // calculate difference in world positions
- const worldPositionDifference = length( currentWorldPosition.sub( previousWorldPosition ) ).toVar();
- worldPositionDifference.assign( min( max( worldPositionDifference.sub( 1.0 ), 0.0 ), 1.0 ) );
- // Adaptive blend weights based on velocity magnitude suggested by CLAUDE in #32133
- // Higher velocity or position difference = more weight on current frame to reduce ghosting
- const velocityMagnitude = length( offset ).toConst();
- const motionFactor = max( worldPositionDifference.mul( 0.5 ), velocityMagnitude.mul( 10.0 ) ).toVar();
- motionFactor.assign( min( motionFactor, 1.0 ) );
- const currentWeight = float( 0.05 ).add( motionFactor.mul( 0.25 ) ).toVar();
- const historyWeight = currentWeight.oneMinus().toVar();
- // zero out history weight if world positions are different (indicating motion) except on edges.
- // note that the constants 0.00001 and 0.5 were suggested by CLAUDE in #32133
- const isEdge = farthestDepth.sub( closestDepth ).greaterThan( 0.00001 );
- const strongDisocclusion = worldPositionDifference.greaterThan( 0.5 ).and( isEdge.not() );
- If( strongDisocclusion, () => {
- currentWeight.assign( 1.0 );
- historyWeight.assign( 0.0 );
- } );
- // flicker reduction based on luminance weighing
- const compressedCurrent = currentColor.mul( float( 1 ).div( ( max( currentColor.r, currentColor.g, currentColor.b ).add( 1.0 ) ) ) );
- const compressedHistory = clampedHistoryColor.mul( float( 1 ).div( ( max( clampedHistoryColor.r, clampedHistoryColor.g, clampedHistoryColor.b ).add( 1.0 ) ) ) );
- const luminanceCurrent = luminance( compressedCurrent.rgb );
- const luminanceHistory = luminance( compressedHistory.rgb );
- currentWeight.mulAssign( float( 1.0 ).div( luminanceCurrent.add( 1 ) ) );
- historyWeight.mulAssign( float( 1.0 ).div( luminanceHistory.add( 1 ) ) );
- const smoothedOutput = add( currentColor.mul( currentWeight ), clampedHistoryColor.mul( historyWeight ) ).div( max( currentWeight.add( historyWeight ), 0.00001 ) ).toVar();
- return smoothedOutput;
- } );
- // materials
- this._resolveMaterial.colorNode = resolve();
- return this._textureNode;
- }
- /**
- * Frees internal resources. This method should be called
- * when the effect is no longer required.
- */
- dispose() {
- this._historyRenderTarget.dispose();
- this._resolveRenderTarget.dispose();
- this._resolveMaterial.dispose();
- }
- }
- export default TRAANode;
- function _halton( index, base ) {
- let fraction = 1;
- let result = 0;
- while ( index > 0 ) {
- fraction /= base;
- result += fraction * ( index % base );
- index = Math.floor( index / base );
- }
- return result;
- }
- const _haltonOffsets = /*@__PURE__*/ Array.from(
- { length: 32 },
- ( _, index ) => [ _halton( index + 1, 2 ), _halton( index + 1, 3 ) ]
- );
- /**
- * TSL function for creating a TRAA node for Temporal Reprojection Anti-Aliasing.
- *
- * @tsl
- * @function
- * @param {TextureNode} beautyNode - The texture node that represents the input of the effect.
- * @param {TextureNode} depthNode - A node that represents the scene's depth.
- * @param {TextureNode} velocityNode - A node that represents the scene's velocity.
- * @param {Camera} camera - The camera the scene is rendered with.
- * @returns {TRAANode}
- */
- export const traa = ( beautyNode, depthNode, velocityNode, camera ) => nodeObject( new TRAANode( convertToTexture( beautyNode ), depthNode, velocityNode, camera ) );
|