Browse Source

ViewportDepthNode: Support reversed depth in viewZ functions. (#33001)

Michael Herzog 3 months ago
parent
commit
8efd540f39
2 changed files with 42 additions and 4 deletions
  1. 2 0
      src/Three.TSL.js
  2. 40 4
      src/nodes/display/ViewportDepthNode.js

+ 2 - 0
src/Three.TSL.js

@@ -604,6 +604,8 @@ export const vibrance = TSL.vibrance;
 export const viewZToLogarithmicDepth = TSL.viewZToLogarithmicDepth;
 export const viewZToLogarithmicDepth = TSL.viewZToLogarithmicDepth;
 export const viewZToOrthographicDepth = TSL.viewZToOrthographicDepth;
 export const viewZToOrthographicDepth = TSL.viewZToOrthographicDepth;
 export const viewZToPerspectiveDepth = TSL.viewZToPerspectiveDepth;
 export const viewZToPerspectiveDepth = TSL.viewZToPerspectiveDepth;
+export const viewZToReversedOrthographicDepth = TSL.viewZToReversedOrthographicDepth;
+export const viewZToReversedPerspectiveDepth = TSL.viewZToReversedPerspectiveDepth;
 export const viewport = TSL.viewport;
 export const viewport = TSL.viewport;
 export const viewportCoordinate = TSL.viewportCoordinate;
 export const viewportCoordinate = TSL.viewportCoordinate;
 export const viewportDepthTexture = TSL.viewportDepthTexture;
 export const viewportDepthTexture = TSL.viewportDepthTexture;

+ 40 - 4
src/nodes/display/ViewportDepthNode.js

@@ -1,5 +1,5 @@
 import Node from '../core/Node.js';
 import Node from '../core/Node.js';
-import { float, log, log2, nodeImmutable, nodeProxy } from '../tsl/TSLBase.js';
+import { float, Fn, log, log2, nodeImmutable, nodeProxy } from '../tsl/TSLBase.js';
 import { cameraNear, cameraFar } from '../accessors/Camera.js';
 import { cameraNear, cameraFar } from '../accessors/Camera.js';
 import { positionView } from '../accessors/Position.js';
 import { positionView } from '../accessors/Position.js';
 import { viewportDepthTexture } from './ViewportDepthTextureNode.js';
 import { viewportDepthTexture } from './ViewportDepthTextureNode.js';
@@ -86,7 +86,7 @@ class ViewportDepthNode extends Node {
 
 
 			if ( value !== null ) {
 			if ( value !== null ) {
 
 
- 				node = depthBase().assign( value );
+				node = depthBase().assign( value );
 
 
 			}
 			}
 
 
@@ -152,6 +152,18 @@ export default ViewportDepthNode;
  */
  */
 export const viewZToOrthographicDepth = ( viewZ, near, far ) => viewZ.add( near ).div( near.sub( far ) );
 export const viewZToOrthographicDepth = ( viewZ, near, far ) => viewZ.add( near ).div( near.sub( far ) );
 
 
+/**
+ * TSL function for converting a viewZ value to a reversed orthographic depth value.
+ *
+ * @tsl
+ * @function
+ * @param {Node<float>} viewZ - The viewZ node.
+ * @param {Node<float>} near - The camera's near value.
+ * @param {Node<float>} far - The camera's far value.
+ * @returns {Node<float>}
+ */
+export const viewZToReversedOrthographicDepth = ( viewZ, near, far ) => viewZ.add( far ).div( far.sub( near ) );
+
 /**
 /**
  * TSL function for converting an orthographic depth value to a viewZ value.
  * TSL function for converting an orthographic depth value to a viewZ value.
  *
  *
@@ -162,7 +174,19 @@ export const viewZToOrthographicDepth = ( viewZ, near, far ) => viewZ.add( near
  * @param {Node<float>} far - The camera's far value.
  * @param {Node<float>} far - The camera's far value.
  * @returns {Node<float>}
  * @returns {Node<float>}
  */
  */
-export const orthographicDepthToViewZ = ( depth, near, far ) => near.sub( far ).mul( depth ).sub( near );
+export const orthographicDepthToViewZ = /*@__PURE__*/ Fn( ( [ depth, near, far ], builder ) => {
+
+	if ( builder.renderer.reversedDepthBuffer === true ) {
+
+		return far.sub( near ).mul( depth ).sub( far );
+
+	} else {
+
+		return near.sub( far ).mul( depth ).sub( near );
+
+	}
+
+} );
 
 
 /**
 /**
  * TSL function for converting a viewZ value to a perspective depth value.
  * TSL function for converting a viewZ value to a perspective depth value.
@@ -200,7 +224,19 @@ export const viewZToReversedPerspectiveDepth = ( viewZ, near, far ) => near.mul(
  * @param {Node<float>} far - The camera's far value.
  * @param {Node<float>} far - The camera's far value.
  * @returns {Node<float>}
  * @returns {Node<float>}
  */
  */
-export const perspectiveDepthToViewZ = ( depth, near, far ) => near.mul( far ).div( far.sub( near ).mul( depth ).sub( far ) );
+export const perspectiveDepthToViewZ = /*@__PURE__*/ Fn( ( [ depth, near, far ], builder ) => {
+
+	if ( builder.renderer.reversedDepthBuffer === true ) {
+
+		return near.mul( far ).div( near.sub( far ).mul( depth ).sub( near ) );
+
+	} else {
+
+		return near.mul( far ).div( far.sub( near ).mul( depth ).sub( far ) );
+
+	}
+
+} );
 
 
 /**
 /**
  * TSL function for converting a viewZ value to a logarithmic depth value.
  * TSL function for converting a viewZ value to a logarithmic depth value.

粤ICP备19079148号