Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodes: Cache linear depth and viewZ of depth textures in PassNode #28922

Merged
merged 6 commits into from
Jul 24, 2024
31 changes: 18 additions & 13 deletions src/nodes/display/PassNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ class PassNode extends TempNode {
depth: depthTexture
};

this._nodes = {};
this._textureNodes = {};
this._linearDepthNodes = {};
this._viewZNodes = {};

this._linearDepthNode = null;
this._viewZNode = null;
this._cameraNear = uniform( 0 );
this._cameraFar = uniform( 0 );

Expand Down Expand Up @@ -157,47 +157,52 @@ class PassNode extends TempNode {

getTextureNode( name = 'output' ) {

let textureNode = this._nodes[ name ];
let textureNode = this._textureNodes[ name ];

if ( textureNode === undefined ) {

this._nodes[ name ] = textureNode = nodeObject( new PassMultipleTextureNode( this, name ) );
this._textureNodes[ name ] = textureNode = nodeObject( new PassMultipleTextureNode( this, name ) );

}

return textureNode;

}

getViewZNode() {
getViewZNode( name = 'depth' ) {

if ( this._viewZNode === null ) {
let viewZNode = this._viewZNodes[ name ];

if ( viewZNode === undefined ) {

const cameraNear = this._cameraNear;
const cameraFar = this._cameraFar;

this._viewZNode = perspectiveDepthToViewZ( this.getTextureNode( 'depth' ), cameraNear, cameraFar );
this._viewZNodes[ name ] = viewZNode = perspectiveDepthToViewZ( this.getTextureNode( name ), cameraNear, cameraFar );

}

return this._viewZNode;
return viewZNode;

}

getLinearDepthNode() {
getLinearDepthNode( name = 'depth' ) {

let linearDepthNode = this._linearDepthNodes[ name ];

if ( this._linearDepthNode === null ) {
if ( linearDepthNode === undefined ) {

const cameraNear = this._cameraNear;
const cameraFar = this._cameraFar;
const viewZNode = this.getViewZNode( name );

// TODO: just if ( builder.camera.isPerspectiveCamera )
cmhhelgeson marked this conversation as resolved.
Show resolved Hide resolved

this._linearDepthNode = viewZToOrthographicDepth( this.getViewZNode(), cameraNear, cameraFar );
this._linearDepthNodes[ name ] = linearDepthNode = viewZToOrthographicDepth( viewZNode, cameraNear, cameraFar );

}

return this._linearDepthNode;
return linearDepthNode;

}

Expand Down