CubeTexturePass.js 3.3 KB

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