Home Reference Source

src/passes/LuminancePass.js

  1. import { UnsignedByteType, WebGLRenderTarget } from "three";
  2. import { Resolution } from "../core/Resolution.js";
  3. import { LuminanceMaterial } from "../materials/LuminanceMaterial.js";
  4. import { Pass } from "./Pass.js";
  5.  
  6. /**
  7. * A pass that renders luminance.
  8. */
  9.  
  10. export class LuminancePass extends Pass {
  11.  
  12. /**
  13. * Constructs a new luminance pass.
  14. *
  15. * @param {Object} [options] - The options. See {@link LuminanceMaterial} for additional options.
  16. * @param {WebGLRenderTarget} [options.renderTarget] - A custom render target.
  17. * @param {Number} [options.resolutionScale=1.0] - The resolution scale.
  18. * @param {Number} [options.resolutionX=Resolution.AUTO_SIZE] - The horizontal resolution.
  19. * @param {Number} [options.resolutionY=Resolution.AUTO_SIZE] - The vertical resolution.
  20. * @param {Number} [options.width=Resolution.AUTO_SIZE] - Deprecated. Use resolutionX instead.
  21. * @param {Number} [options.height=Resolution.AUTO_SIZE] - Deprecated. Use resolutionY instead.
  22. */
  23.  
  24. constructor({
  25. renderTarget,
  26. luminanceRange,
  27. colorOutput,
  28. resolutionScale = 1.0,
  29. width = Resolution.AUTO_SIZE,
  30. height = Resolution.AUTO_SIZE,
  31. resolutionX = width,
  32. resolutionY = height
  33. } = {}) {
  34.  
  35. super("LuminancePass");
  36.  
  37. this.fullscreenMaterial = new LuminanceMaterial(colorOutput, luminanceRange);
  38. this.needsSwap = false;
  39.  
  40. /**
  41. * The luminance render target.
  42. *
  43. * @type {WebGLRenderTarget}
  44. * @readonly
  45. */
  46.  
  47. this.renderTarget = renderTarget;
  48.  
  49. if(this.renderTarget === undefined) {
  50.  
  51. this.renderTarget = new WebGLRenderTarget(1, 1, { depthBuffer: false });
  52. this.renderTarget.texture.name = "LuminancePass.Target";
  53.  
  54. }
  55.  
  56. /**
  57. * The resolution.
  58. *
  59. * @type {Resolution}
  60. * @readonly
  61. */
  62.  
  63. const resolution = this.resolution = new Resolution(this, resolutionX, resolutionY, resolutionScale);
  64. resolution.addEventListener("change", (e) => this.setSize(resolution.baseWidth, resolution.baseHeight));
  65.  
  66. }
  67.  
  68. /**
  69. * The luminance texture.
  70. *
  71. * @type {Texture}
  72. */
  73.  
  74. get texture() {
  75.  
  76. return this.renderTarget.texture;
  77.  
  78. }
  79.  
  80. /**
  81. * Returns the luminance texture.
  82. *
  83. * @deprecated Use texture instead.
  84. * @return {Texture} The texture.
  85. */
  86.  
  87. getTexture() {
  88.  
  89. return this.renderTarget.texture;
  90.  
  91. }
  92.  
  93. /**
  94. * Returns the resolution settings.
  95. *
  96. * @deprecated Use resolution instead.
  97. * @return {Resolution} The resolution.
  98. */
  99.  
  100. getResolution() {
  101.  
  102. return this.resolution;
  103.  
  104. }
  105.  
  106. /**
  107. * Renders the luminance.
  108. *
  109. * @param {WebGLRenderer} renderer - The renderer.
  110. * @param {WebGLRenderTarget} inputBuffer - A frame buffer that contains the result of the previous pass.
  111. * @param {WebGLRenderTarget} outputBuffer - A frame buffer that serves as the output render target unless this pass renders to screen.
  112. * @param {Number} [deltaTime] - The time between the last frame and the current one in seconds.
  113. * @param {Boolean} [stencilTest] - Indicates whether a stencil mask is active.
  114. */
  115.  
  116. render(renderer, inputBuffer, outputBuffer, deltaTime, stencilTest) {
  117.  
  118. const material = this.fullscreenMaterial;
  119. material.inputBuffer = inputBuffer.texture;
  120. renderer.setRenderTarget(this.renderToScreen ? null : this.renderTarget);
  121. renderer.render(this.scene, this.camera);
  122.  
  123. }
  124.  
  125. /**
  126. * Updates the size of this pass.
  127. *
  128. * @param {Number} width - The width.
  129. * @param {Number} height - The height.
  130. */
  131.  
  132. setSize(width, height) {
  133.  
  134. const resolution = this.resolution;
  135. resolution.setBaseSize(width, height);
  136. this.renderTarget.setSize(resolution.width, resolution.height);
  137.  
  138. }
  139.  
  140. /**
  141. * Performs initialization tasks.
  142. *
  143. * @param {WebGLRenderer} renderer - A renderer.
  144. * @param {Boolean} alpha - Whether the renderer uses the alpha channel.
  145. * @param {Number} frameBufferType - The type of the main frame buffers.
  146. */
  147.  
  148. initialize(renderer, alpha, frameBufferType) {
  149.  
  150. if(frameBufferType !== undefined && frameBufferType !== UnsignedByteType) {
  151.  
  152. this.renderTarget.texture.type = frameBufferType;
  153. this.fullscreenMaterial.defines.FRAMEBUFFER_PRECISION_HIGH = "1";
  154.  
  155. }
  156.  
  157. }
  158.  
  159. }