ViewportDepthNode.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import Node, { addNodeClass } from '../core/Node.js';
  2. import { nodeImmutable, nodeProxy } from '../shadernode/ShaderNode.js';
  3. import { cameraNear, cameraFar } from '../accessors/CameraNode.js';
  4. import { positionView } from '../accessors/PositionNode.js';
  5. import { viewportDepthTexture } from './ViewportDepthTextureNode.js';
  6. class ViewportDepthNode extends Node {
  7. constructor( scope, valueNode = null ) {
  8. super( 'float' );
  9. this.scope = scope;
  10. this.valueNode = valueNode;
  11. this.isViewportDepthNode = true;
  12. }
  13. generate( builder ) {
  14. const { scope } = this;
  15. if ( scope === ViewportDepthNode.DEPTH_PIXEL ) {
  16. return builder.getFragDepth();
  17. }
  18. return super.generate( builder );
  19. }
  20. setup( /*builder*/ ) {
  21. const { scope } = this;
  22. let node = null;
  23. if ( scope === ViewportDepthNode.DEPTH ) {
  24. node = viewZToOrthographicDepth( positionView.z, cameraNear, cameraFar );
  25. } else if ( scope === ViewportDepthNode.DEPTH_TEXTURE ) {
  26. const texture = this.valueNode || viewportDepthTexture();
  27. const viewZ = perspectiveDepthToViewZ( texture, cameraNear, cameraFar );
  28. node = viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );
  29. } else if ( scope === ViewportDepthNode.DEPTH_PIXEL ) {
  30. if ( this.valueNode !== null ) {
  31. node = depthPixelBase().assign( this.valueNode );
  32. }
  33. }
  34. return node;
  35. }
  36. }
  37. // NOTE: viewZ, the z-coordinate in camera space, is negative for points in front of the camera
  38. // -near maps to 0; -far maps to 1
  39. export const viewZToOrthographicDepth = ( viewZ, near, far ) => viewZ.add( near ).div( near.sub( far ) );
  40. // maps orthographic depth in [ 0, 1 ] to viewZ
  41. export const orthographicDepthToViewZ = ( depth, near, far ) => near.sub( far ).mul( depth ).sub( near );
  42. // NOTE: https://twitter.com/gonnavis/status/1377183786949959682
  43. // -near maps to 0; -far maps to 1
  44. export const viewZToPerspectiveDepth = ( viewZ, near, far ) => near.add( viewZ ).mul( far ).div( near.sub( far ).mul( viewZ ) );
  45. // maps perspective depth in [ 0, 1 ] to viewZ
  46. export const perspectiveDepthToViewZ = ( depth, near, far ) => near.mul( far ).div( far.sub( near ).mul( depth ).sub( far ) );
  47. ViewportDepthNode.DEPTH = 'depth';
  48. ViewportDepthNode.DEPTH_TEXTURE = 'depthTexture';
  49. ViewportDepthNode.DEPTH_PIXEL = 'depthPixel';
  50. export default ViewportDepthNode;
  51. const depthPixelBase = nodeProxy( ViewportDepthNode, ViewportDepthNode.DEPTH_PIXEL );
  52. export const depth = nodeImmutable( ViewportDepthNode, ViewportDepthNode.DEPTH );
  53. export const depthTexture = nodeProxy( ViewportDepthNode, ViewportDepthNode.DEPTH_TEXTURE );
  54. export const depthPixel = nodeImmutable( ViewportDepthNode, ViewportDepthNode.DEPTH_PIXEL );
  55. depthPixel.assign = ( value ) => depthPixelBase( value );
  56. addNodeClass( 'ViewportDepthNode', ViewportDepthNode );