PassNode.js 4.2 KB

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