Преглед изворни кода

TRAANode: Add support for reversed/logarithmic depth buffers and orthograhic cameras (#33364)

Shota Matsuda пре 5 дана
родитељ
комит
61371fac8c
1 измењених фајлова са 25 додато и 5 уклоњено
  1. 25 5
      examples/jsm/tsl/display/TRAANode.js

+ 25 - 5
examples/jsm/tsl/display/TRAANode.js

@@ -1,5 +1,5 @@
-import { HalfFloatType, Vector2, RenderTarget, RendererUtils, QuadMesh, NodeMaterial, TempNode, NodeUpdateType, Matrix4, DepthTexture } from 'three/webgpu';
-import { add, float, If, Fn, max, texture, uniform, uv, vec2, vec4, luminance, convertToTexture, passTexture, velocity, getViewPosition, viewZToPerspectiveDepth, struct, ivec2, mix } from 'three/tsl';
+import { HalfFloatType, Vector2, RenderTarget, RendererUtils, QuadMesh, NodeMaterial, TempNode, NodeUpdateType, Matrix4, DepthTexture, FloatType } from 'three/webgpu';
+import { add, float, If, Fn, max, texture, uniform, uv, vec2, vec4, luminance, convertToTexture, passTexture, velocity, getViewPosition, viewZToPerspectiveDepth, struct, ivec2, mix, logarithmicDepthToViewZ, viewZToOrthographicDepth } from 'three/tsl';
 
 const _quadMesh = /*@__PURE__*/ new QuadMesh();
 const _size = /*@__PURE__*/ new Vector2();
@@ -463,6 +463,12 @@ class TRAANode extends TempNode {
 
 		}
 
+		if ( builder.renderer.reversedDepthBuffer === true ) {
+
+			this._historyRenderTarget.depthTexture.type = FloatType;
+
+		}
+
 		if ( builder.context.velocity !== undefined ) {
 
 			this._velocityNode = builder.context.velocity;
@@ -473,6 +479,14 @@ class TRAANode extends TempNode {
 
 		}
 
+		const logarithmicToPerspectiveDepth = ( depth ) => {
+
+			const { x: near, y: far } = this._cameraNearFar;
+			const viewZ = logarithmicDepthToViewZ( depth, near, far );
+			return viewZToPerspectiveDepth( viewZ, near, far );
+
+		};
+
 		const currentDepthStruct = struct( {
 
 			closestDepth: 'float',
@@ -493,7 +507,10 @@ class TRAANode extends TempNode {
 				for ( let y = - 1; y <= 1; ++ y ) {
 
 					const neighbor = positionTexel.add( vec2( x, y ) ).toVar();
-					const depth = this.depthNode.load( neighbor ).r.toVar();
+					let depth = this.depthNode.load( neighbor ).r;
+					if ( builder.renderer.reversedDepthBuffer ) depth = depth.oneMinus();
+					if ( builder.renderer.logarithmicDepthBuffer ) depth = logarithmicToPerspectiveDepth( depth );
+					depth = depth.toVar();
 
 					If( depth.lessThan( closestDepth ), () => {
 
@@ -519,11 +536,14 @@ class TRAANode extends TempNode {
 		// Samples a previous depth and reproject it using the current camera matrices.
 		const samplePreviousDepth = ( uv ) => {
 
-			const depth = this._previousDepthNode.sample( uv ).r;
+			let depth = this._previousDepthNode.sample( uv ).r;
+			if ( builder.renderer.logarithmicDepthBuffer ) depth = logarithmicToPerspectiveDepth( depth );
 			const positionView = getViewPosition( uv, depth, this._previousCameraProjectionMatrixInverse );
 			const positionWorld = this._previousCameraWorldMatrix.mul( vec4( positionView, 1 ) ).xyz;
 			const viewZ = this._cameraWorldMatrixInverse.mul( vec4( positionWorld, 1 ) ).z;
-			return viewZToPerspectiveDepth( viewZ, this._cameraNearFar.x, this._cameraNearFar.y );
+			return this.camera.isOrthographicCamera
+				? viewZToOrthographicDepth( viewZ, this._cameraNearFar.x, this._cameraNearFar.y )
+				: viewZToPerspectiveDepth( viewZ, this._cameraNearFar.x, this._cameraNearFar.y );
 
 		};
 

粤ICP备19079148号