PassNode.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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._cameraNear = uniform( 0 );
  45. this._cameraFar = uniform( 0 );
  46. this.isPassNode = true;
  47. }
  48. isGlobal() {
  49. return true;
  50. }
  51. getTextureNode() {
  52. return this._textureNode;
  53. }
  54. getTextureDepthNode() {
  55. return this._depthTextureNode;
  56. }
  57. getDepthNode() {
  58. if ( this._depthNode === null ) {
  59. const cameraNear = this._cameraNear;
  60. const cameraFar = this._cameraFar;
  61. this._depthNode = viewZToOrthographicDepth( perspectiveDepthToViewZ( this._depthTextureNode, cameraNear, cameraFar ), cameraNear, cameraFar );
  62. }
  63. return this._depthNode;
  64. }
  65. setup() {
  66. return this.scope === PassNode.COLOR ? this.getTextureNode() : this.getDepthNode();
  67. }
  68. updateBefore( frame ) {
  69. const { renderer } = frame;
  70. const { scene, camera } = this;
  71. this._pixelRatio = renderer.getPixelRatio();
  72. const size = renderer.getSize( new Vector2() );
  73. this.setSize( size.width, size.height );
  74. const currentToneMapping = renderer.toneMapping;
  75. const currentToneMappingNode = renderer.toneMappingNode;
  76. const currentRenderTarget = renderer.getRenderTarget();
  77. this._cameraNear.value = camera.near;
  78. this._cameraFar.value = camera.far;
  79. renderer.toneMapping = NoToneMapping;
  80. renderer.toneMappingNode = null;
  81. renderer.setRenderTarget( this.renderTarget );
  82. renderer.render( scene, camera );
  83. renderer.toneMapping = currentToneMapping;
  84. renderer.toneMappingNode = currentToneMappingNode;
  85. renderer.setRenderTarget( currentRenderTarget );
  86. }
  87. setSize( width, height ) {
  88. this._width = width;
  89. this._height = height;
  90. const effectiveWidth = this._width * this._pixelRatio;
  91. const effectiveHeight = this._height * this._pixelRatio;
  92. this.renderTarget.setSize( effectiveWidth, effectiveHeight );
  93. }
  94. setPixelRatio( pixelRatio ) {
  95. this._pixelRatio = pixelRatio;
  96. this.setSize( this._width, this._height );
  97. }
  98. dispose() {
  99. this.renderTarget.dispose();
  100. }
  101. }
  102. PassNode.COLOR = 'color';
  103. PassNode.DEPTH = 'depth';
  104. export default PassNode;
  105. export const pass = ( scene, camera ) => nodeObject( new PassNode( PassNode.COLOR, scene, camera ) );
  106. export const texturePass = ( pass, texture ) => nodeObject( new PassTextureNode( pass, texture ) );
  107. export const depthPass = ( scene, camera ) => nodeObject( new PassNode( PassNode.DEPTH, scene, camera ) );
  108. addNodeClass( 'PassNode', PassNode );