PassNode.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { addNodeClass } from '../core/Node.js';
  2. import TempNode from '../core/TempNode.js';
  3. import { default as TextureNode/*, texture*/ } 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._linearDepthNode = 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. getLinearDepthNode() {
  68. if ( this._linearDepthNode === null ) {
  69. const cameraNear = this._cameraNear;
  70. const cameraFar = this._cameraFar;
  71. // TODO: just if ( builder.camera.isPerspectiveCamera )
  72. this._linearDepthNode = viewZToOrthographicDepth( this.getViewZNode(), cameraNear, cameraFar );
  73. }
  74. return this._linearDepthNode;
  75. }
  76. setup() {
  77. return this.scope === PassNode.COLOR ? this.getTextureNode() : this.getLinearDepthNode();
  78. }
  79. updateBefore( frame ) {
  80. const { renderer } = frame;
  81. const { scene, camera } = this;
  82. this._pixelRatio = renderer.getPixelRatio();
  83. const size = renderer.getSize( _size );
  84. this.setSize( size.width, size.height );
  85. const currentToneMapping = renderer.toneMapping;
  86. const currentToneMappingNode = renderer.toneMappingNode;
  87. const currentRenderTarget = renderer.getRenderTarget();
  88. this._cameraNear.value = camera.near;
  89. this._cameraFar.value = camera.far;
  90. renderer.toneMapping = NoToneMapping;
  91. renderer.toneMappingNode = null;
  92. renderer.setRenderTarget( this.renderTarget );
  93. renderer.render( scene, camera );
  94. renderer.toneMapping = currentToneMapping;
  95. renderer.toneMappingNode = currentToneMappingNode;
  96. renderer.setRenderTarget( currentRenderTarget );
  97. }
  98. setSize( width, height ) {
  99. this._width = width;
  100. this._height = height;
  101. const effectiveWidth = this._width * this._pixelRatio;
  102. const effectiveHeight = this._height * this._pixelRatio;
  103. this.renderTarget.setSize( effectiveWidth, effectiveHeight );
  104. }
  105. setPixelRatio( pixelRatio ) {
  106. this._pixelRatio = pixelRatio;
  107. this.setSize( this._width, this._height );
  108. }
  109. dispose() {
  110. this.renderTarget.dispose();
  111. }
  112. }
  113. PassNode.COLOR = 'color';
  114. PassNode.DEPTH = 'depth';
  115. export default PassNode;
  116. export const pass = ( scene, camera ) => nodeObject( new PassNode( PassNode.COLOR, scene, camera ) );
  117. export const texturePass = ( pass, texture ) => nodeObject( new PassTextureNode( pass, texture ) );
  118. export const depthPass = ( scene, camera ) => nodeObject( new PassNode( PassNode.DEPTH, scene, camera ) );
  119. addNodeClass( 'PassNode', PassNode );