Pass.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import {
  2. BufferGeometry,
  3. Float32BufferAttribute,
  4. OrthographicCamera,
  5. Mesh
  6. } from 'three';
  7. /**
  8. * Abstract base class for all post processing passes.
  9. *
  10. * This module is only relevant for post processing with {@link WebGLRenderer}.
  11. *
  12. * @abstract
  13. */
  14. class Pass {
  15. /**
  16. * Constructs a new pass.
  17. */
  18. constructor() {
  19. /**
  20. * This flag can be used for type testing.
  21. *
  22. * @type {boolean}
  23. * @readonly
  24. * @default true
  25. */
  26. this.isPass = true;
  27. /**
  28. * If set to `true`, the pass is processed by the composer.
  29. *
  30. * @type {boolean}
  31. * @default true
  32. */
  33. this.enabled = true;
  34. /**
  35. * If set to `true`, the pass indicates to swap read and write buffer after rendering.
  36. *
  37. * @type {boolean}
  38. * @default true
  39. */
  40. this.needsSwap = true;
  41. /**
  42. * If set to `true`, the pass clears its buffer before rendering
  43. *
  44. * @type {boolean}
  45. * @default false
  46. */
  47. this.clear = false;
  48. /**
  49. * If set to `true`, the result of the pass is rendered to screen. The last pass in the composers
  50. * pass chain gets automatically rendered to screen, no matter how this property is configured.
  51. *
  52. * @type {boolean}
  53. * @default false
  54. */
  55. this.renderToScreen = false;
  56. }
  57. /**
  58. * Sets the size of the pass.
  59. *
  60. * @abstract
  61. * @param {number} width - The width to set.
  62. * @param {number} height - The width to set.
  63. */
  64. setSize( /* width, height */ ) {}
  65. /**
  66. * This method holds the render logic of a pass. It must be implemented in all derived classes.
  67. *
  68. * @abstract
  69. * @param {WebGLRenderer} renderer - The renderer.
  70. * @param {WebGLRenderTarget} writeBuffer - The write buffer. This buffer is intended as the rendering
  71. * destination for the pass.
  72. * @param {WebGLRenderTarget} readBuffer - The read buffer. The pass can access the result from the
  73. * previous pass from this buffer.
  74. * @param {number} deltaTime - The delta time in seconds.
  75. * @param {boolean} maskActive - Whether masking is active or not.
  76. */
  77. render( /* renderer, writeBuffer, readBuffer, deltaTime, maskActive */ ) {
  78. console.error( 'THREE.Pass: .render() must be implemented in derived pass.' );
  79. }
  80. /**
  81. * Frees the GPU-related resources allocated by this instance. Call this
  82. * method whenever the pass is no longer used in your app.
  83. *
  84. * @abstract
  85. */
  86. dispose() {}
  87. }
  88. // Helper for passes that need to fill the viewport with a single quad.
  89. const _camera = new OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  90. // https://github.com/mrdoob/three.js/pull/21358
  91. class FullscreenTriangleGeometry extends BufferGeometry {
  92. constructor() {
  93. super();
  94. this.setAttribute( 'position', new Float32BufferAttribute( [ - 1, 3, 0, - 1, - 1, 0, 3, - 1, 0 ], 3 ) );
  95. this.setAttribute( 'uv', new Float32BufferAttribute( [ 0, 2, 0, 0, 2, 0 ], 2 ) );
  96. }
  97. }
  98. const _geometry = new FullscreenTriangleGeometry();
  99. /**
  100. * This module is a helper for passes which need to render a full
  101. * screen effect which is quite common in context of post processing.
  102. *
  103. * The intended usage is to reuse a single full screen quad for rendering
  104. * subsequent passes by just reassigning the `material` reference.
  105. *
  106. * This module can only be used with {@link WebGLRenderer}.
  107. *
  108. * @augments Mesh
  109. */
  110. class FullScreenQuad {
  111. /**
  112. * Constructs a new full screen quad.
  113. *
  114. * @param {?Material} material - The material to render te full screen quad with.
  115. */
  116. constructor( material ) {
  117. this._mesh = new Mesh( _geometry, material );
  118. }
  119. /**
  120. * Frees the GPU-related resources allocated by this instance. Call this
  121. * method whenever the instance is no longer used in your app.
  122. */
  123. dispose() {
  124. this._mesh.geometry.dispose();
  125. }
  126. /**
  127. * Renders the full screen quad.
  128. *
  129. * @param {WebGLRenderer} renderer - The renderer.
  130. */
  131. render( renderer ) {
  132. renderer.render( this._mesh, _camera );
  133. }
  134. /**
  135. * The quad's material.
  136. *
  137. * @type {?Material}
  138. */
  139. get material() {
  140. return this._mesh.material;
  141. }
  142. set material( value ) {
  143. this._mesh.material = value;
  144. }
  145. }
  146. export { Pass, FullScreenQuad };
粤ICP备19079148号