CubeTexturePass.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import {
  2. BackSide,
  3. BoxGeometry,
  4. Mesh,
  5. PerspectiveCamera,
  6. Scene,
  7. ShaderLib,
  8. ShaderMaterial,
  9. UniformsUtils
  10. } from 'three';
  11. import { Pass } from './Pass.js';
  12. /**
  13. * This pass can be used to render a cube texture over the entire screen.
  14. *
  15. * ```js
  16. * const cubeMap = new THREE.CubeTextureLoader().load( urls );
  17. *
  18. * const cubeTexturePass = new CubeTexturePass( camera, cubemap );
  19. * composer.addPass( cubeTexturePass );
  20. * ```
  21. *
  22. * @augments Pass
  23. * @three_import import { CubeTexturePass } from 'three/addons/postprocessing/CubeTexturePass.js';
  24. */
  25. class CubeTexturePass extends Pass {
  26. /**
  27. * Constructs a new cube texture pass.
  28. *
  29. * @param {PerspectiveCamera} camera - The camera.
  30. * @param {CubeTexture} tCube - The cube texture to render.
  31. * @param {number} [opacity=1] - The opacity.
  32. */
  33. constructor( camera, tCube, opacity = 1 ) {
  34. super();
  35. /**
  36. * The camera.
  37. *
  38. * @type {PerspectiveCamera}
  39. */
  40. this.camera = camera;
  41. /**
  42. * The cube texture to render.
  43. *
  44. * @type {CubeTexture}
  45. */
  46. this.tCube = tCube;
  47. /**
  48. * The opacity.
  49. *
  50. * @type {number}
  51. * @default 1
  52. */
  53. this.opacity = opacity;
  54. /**
  55. * Overwritten to disable the swap.
  56. *
  57. * @type {boolean}
  58. * @default false
  59. */
  60. this.needsSwap = false;
  61. // internals
  62. const cubeShader = ShaderLib[ 'cube' ];
  63. this._cubeMesh = new Mesh(
  64. new BoxGeometry( 10, 10, 10 ),
  65. new ShaderMaterial( {
  66. uniforms: UniformsUtils.clone( cubeShader.uniforms ),
  67. vertexShader: cubeShader.vertexShader,
  68. fragmentShader: cubeShader.fragmentShader,
  69. depthTest: false,
  70. depthWrite: false,
  71. side: BackSide
  72. } )
  73. );
  74. Object.defineProperty( this._cubeMesh.material, 'envMap', {
  75. get: function () {
  76. return this.uniforms.tCube.value;
  77. }
  78. } );
  79. this._cubeScene = new Scene();
  80. this._cubeCamera = new PerspectiveCamera();
  81. this._cubeScene.add( this._cubeMesh );
  82. }
  83. /**
  84. * Performs the cube texture pass.
  85. *
  86. * @param {WebGLRenderer} renderer - The renderer.
  87. * @param {WebGLRenderTarget} writeBuffer - The write buffer. This buffer is intended as the rendering
  88. * destination for the pass.
  89. * @param {WebGLRenderTarget} readBuffer - The read buffer. The pass can access the result from the
  90. * previous pass from this buffer.
  91. * @param {number} deltaTime - The delta time in seconds.
  92. * @param {boolean} maskActive - Whether masking is active or not.
  93. */
  94. render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive*/ ) {
  95. const oldAutoClear = renderer.autoClear;
  96. renderer.autoClear = false;
  97. this._cubeCamera.projectionMatrix.copy( this.camera.projectionMatrix );
  98. this._cubeCamera.quaternion.setFromRotationMatrix( this.camera.matrixWorld );
  99. this._cubeMesh.material.uniforms.tCube.value = this.tCube;
  100. this._cubeMesh.material.uniforms.tFlip.value = ( this.tCube.isCubeTexture && this.tCube.isRenderTargetTexture === false ) ? - 1 : 1;
  101. this._cubeMesh.material.uniforms.opacity.value = this.opacity;
  102. this._cubeMesh.material.transparent = ( this.opacity < 1.0 );
  103. renderer.setRenderTarget( this.renderToScreen ? null : readBuffer );
  104. if ( this.clear ) renderer.clear();
  105. renderer.render( this._cubeScene, this._cubeCamera );
  106. renderer.autoClear = oldAutoClear;
  107. }
  108. /**
  109. * Frees the GPU-related resources allocated by this instance. Call this
  110. * method whenever the pass is no longer used in your app.
  111. */
  112. dispose() {
  113. this._cubeMesh.geometry.dispose();
  114. this._cubeMesh.material.dispose();
  115. }
  116. }
  117. export { CubeTexturePass };