PassNode.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { addNodeClass } from '../core/Node.js';
  2. import TempNode from '../core/TempNode.js';
  3. import TextureNode from '../accessors/TextureNode.js';
  4. import { NodeUpdateType } from '../core/constants.js';
  5. import { nodeObject } from '../shadernode/ShaderNode.js';
  6. import { uniform } from '../core/UniformNode.js';
  7. import { viewZToOrthographicDepth, perspectiveDepthToViewZ } from './ViewportDepthNode.js';
  8. import { RenderTarget, Vector2, HalfFloatType, DepthTexture, NoToneMapping/*, FloatType*/ } from 'three';
  9. const _size = new Vector2();
  10. class PassTextureNode extends TextureNode {
  11. constructor( passNode, texture ) {
  12. super( texture );
  13. this.passNode = passNode;
  14. this.setUpdateMatrix( false );
  15. }
  16. setup( builder ) {
  17. this.passNode.build( builder );
  18. return super.setup( builder );
  19. }
  20. clone() {
  21. return new this.constructor( this.passNode, this.value );
  22. }
  23. }
  24. class PassNode extends TempNode {
  25. constructor( scope, scene, camera ) {
  26. super( 'vec4' );
  27. this.scope = scope;
  28. this.scene = scene;
  29. this.camera = camera;
  30. this._pixelRatio = 1;
  31. this._width = 1;
  32. this._height = 1;
  33. const depthTexture = new DepthTexture();
  34. depthTexture.isRenderTargetTexture = true;
  35. //depthTexture.type = FloatType;
  36. depthTexture.name = 'PostProcessingDepth';
  37. const renderTarget = new RenderTarget( this._width * this._pixelRatio, this._height * this._pixelRatio, { type: HalfFloatType } );
  38. renderTarget.texture.name = 'PostProcessing';
  39. renderTarget.depthTexture = depthTexture;
  40. this.renderTarget = renderTarget;
  41. this.updateBeforeType = NodeUpdateType.FRAME;
  42. this._textureNode = nodeObject( new PassTextureNode( this, renderTarget.texture ) );
  43. this._depthTextureNode = nodeObject( new PassTextureNode( this, depthTexture ) );
  44. this._depthNode = null;
  45. this._viewZNode = null;
  46. this._cameraNear = uniform( 0 );
  47. this._cameraFar = uniform( 0 );
  48. this.isPassNode = true;
  49. }
  50. isGlobal() {
  51. return true;
  52. }
  53. getTextureNode() {
  54. return this._textureNode;
  55. }
  56. getTextureDepthNode() {
  57. return this._depthTextureNode;
  58. }
  59. getViewZNode() {
  60. if ( this._viewZNode === null ) {
  61. const cameraNear = this._cameraNear;
  62. const cameraFar = this._cameraFar;
  63. this._viewZNode = perspectiveDepthToViewZ( this._depthTextureNode, cameraNear, cameraFar );
  64. }
  65. return this._viewZNode;
  66. }
  67. getDepthNode() {
  68. if ( this._depthNode === null ) {
  69. const cameraNear = this._cameraNear;
  70. const cameraFar = this._cameraFar;
  71. this._depthNode = viewZToOrthographicDepth( this.getViewZNode(), cameraNear, cameraFar );
  72. }
  73. return this._depthNode;
  74. }
  75. setup() {
  76. return this.scope === PassNode.COLOR ? this.getTextureNode() : this.getDepthNode();
  77. }
  78. updateBefore( frame ) {
  79. const { renderer } = frame;
  80. const { scene, camera } = this;
  81. this._pixelRatio = renderer.getPixelRatio();
  82. const size = renderer.getSize( _size );
  83. this.setSize( size.width, size.height );
  84. const currentToneMapping = renderer.toneMapping;
  85. const currentToneMappingNode = renderer.toneMappingNode;
  86. const currentRenderTarget = renderer.getRenderTarget();
  87. this._cameraNear.value = camera.near;
  88. this._cameraFar.value = camera.far;
  89. renderer.toneMapping = NoToneMapping;
  90. renderer.toneMappingNode = null;
  91. renderer.setRenderTarget( this.renderTarget );
  92. renderer.render( scene, camera );
  93. renderer.toneMapping = currentToneMapping;
  94. renderer.toneMappingNode = currentToneMappingNode;
  95. renderer.setRenderTarget( currentRenderTarget );
  96. }
  97. setSize( width, height ) {
  98. this._width = width;
  99. this._height = height;
  100. const effectiveWidth = this._width * this._pixelRatio;
  101. const effectiveHeight = this._height * this._pixelRatio;
  102. this.renderTarget.setSize( effectiveWidth, effectiveHeight );
  103. }
  104. setPixelRatio( pixelRatio ) {
  105. this._pixelRatio = pixelRatio;
  106. this.setSize( this._width, this._height );
  107. }
  108. dispose() {
  109. this.renderTarget.dispose();
  110. }
  111. }
  112. PassNode.COLOR = 'color';
  113. PassNode.DEPTH = 'depth';
  114. export default PassNode;
  115. export const pass = ( scene, camera ) => nodeObject( new PassNode( PassNode.COLOR, scene, camera ) );
  116. export const texturePass = ( pass, texture ) => nodeObject( new PassTextureNode( pass, texture ) );
  117. export const depthPass = ( scene, camera ) => nodeObject( new PassNode( PassNode.DEPTH, scene, camera ) );
  118. addNodeClass( 'PassNode', PassNode );