1
0

StereoCompositePassNode.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { RenderTarget, StereoCamera, HalfFloatType, LinearFilter, NearestFilter, Vector2, PassNode, QuadMesh, RendererUtils } from 'three/webgpu';
  2. import { texture } from 'three/tsl';
  3. const _size = /*@__PURE__*/ new Vector2();
  4. const _quadMesh = /*@__PURE__*/ new QuadMesh();
  5. let _rendererState;
  6. /**
  7. * A special (abstract) render pass node that renders the scene
  8. * as a stereoscopic image. Unlike {@link StereoPassNode}, this
  9. * node merges the image for the left and right eye
  10. * into a single one. That is required for effects like
  11. * anaglyph or parallax barrier.
  12. *
  13. * @abstract
  14. * @augments PassNode
  15. * @three_import import { StereoCompositePassNode } from 'three/addons/tsl/display/StereoCompositePassNode.js';
  16. */
  17. class StereoCompositePassNode extends PassNode {
  18. static get type() {
  19. return 'StereoCompositePassNode';
  20. }
  21. /**
  22. * Constructs a new stereo composite pass node.
  23. *
  24. * @param {Scene} scene - The scene to render.
  25. * @param {Camera} camera - The camera to render the scene with.
  26. */
  27. constructor( scene, camera ) {
  28. super( PassNode.COLOR, scene, camera );
  29. /**
  30. * This flag can be used for type testing.
  31. *
  32. * @type {boolean}
  33. * @readonly
  34. * @default true
  35. */
  36. this.isStereoCompositePassNode = true;
  37. /**
  38. * The internal stereo camera that is used to render the scene.
  39. *
  40. * @type {StereoCamera}
  41. */
  42. this.stereo = new StereoCamera();
  43. const _params = { minFilter: LinearFilter, magFilter: NearestFilter, type: HalfFloatType };
  44. /**
  45. * The render target for rendering the left eye's view.
  46. *
  47. * @private
  48. * @type {RenderTarget}
  49. */
  50. this._renderTargetL = new RenderTarget( 1, 1, _params );
  51. /**
  52. * The render target for rendering the right eye's view.
  53. *
  54. * @private
  55. * @type {RenderTarget}
  56. */
  57. this._renderTargetR = new RenderTarget( 1, 1, _params );
  58. /**
  59. * A texture node representing the left's eye view.
  60. *
  61. * @private
  62. * @type {TextureNode}
  63. */
  64. this._mapLeft = texture( this._renderTargetL.texture );
  65. /**
  66. * A texture node representing the right's eye view.
  67. *
  68. * @private
  69. * @type {TextureNode}
  70. */
  71. this._mapRight = texture( this._renderTargetR.texture );
  72. /**
  73. * The node material that implements the composite. All
  74. * derived effect passes must provide an instance for rendering.
  75. *
  76. * @private
  77. * @type {?NodeMaterial}
  78. * @default null
  79. */
  80. this._material = null;
  81. }
  82. /**
  83. * Updates the internal stereo camera.
  84. *
  85. * @param {number} coordinateSystem - The current coordinate system.
  86. */
  87. updateStereoCamera( coordinateSystem ) {
  88. this.stereo.cameraL.coordinateSystem = coordinateSystem;
  89. this.stereo.cameraR.coordinateSystem = coordinateSystem;
  90. this.stereo.update( this.camera );
  91. }
  92. /**
  93. * Sets the size of the pass.
  94. *
  95. * @param {number} width - The width of the pass.
  96. * @param {number} height - The height of the pass.
  97. */
  98. setSize( width, height ) {
  99. super.setSize( width, height );
  100. this._renderTargetL.setSize( this.renderTarget.width, this.renderTarget.height );
  101. this._renderTargetR.setSize( this.renderTarget.width, this.renderTarget.height );
  102. }
  103. /**
  104. * This method is used to render the effect once per frame.
  105. *
  106. * @param {NodeFrame} frame - The current node frame.
  107. */
  108. updateBefore( frame ) {
  109. const { renderer } = frame;
  110. const { scene, stereo, renderTarget } = this;
  111. _rendererState = RendererUtils.resetRendererState( renderer, _rendererState );
  112. //
  113. this._pixelRatio = renderer.getPixelRatio();
  114. this.updateStereoCamera( renderer.coordinateSystem );
  115. const size = renderer.getSize( _size );
  116. this.setSize( size.width, size.height );
  117. // left
  118. renderer.setRenderTarget( this._renderTargetL );
  119. renderer.render( scene, stereo.cameraL );
  120. // right
  121. renderer.setRenderTarget( this._renderTargetR );
  122. renderer.render( scene, stereo.cameraR );
  123. // composite
  124. renderer.setRenderTarget( renderTarget );
  125. _quadMesh.material = this._material;
  126. _quadMesh.render( renderer );
  127. // restore
  128. RendererUtils.restoreRendererState( renderer, _rendererState );
  129. }
  130. /**
  131. * Frees internal resources. This method should be called
  132. * when the pass is no longer required.
  133. */
  134. dispose() {
  135. super.dispose();
  136. this._renderTargetL.dispose();
  137. this._renderTargetR.dispose();
  138. if ( this._material !== null ) {
  139. this._material.dispose();
  140. }
  141. }
  142. }
  143. export default StereoCompositePassNode;
粤ICP备19079148号