CubeTexturePass.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import {
  2. BackSide,
  3. BoxGeometry,
  4. Mesh,
  5. PerspectiveCamera,
  6. Scene,
  7. ShaderLib,
  8. ShaderMaterial,
  9. UniformsUtils
  10. } from '../../../build/three.module.js';
  11. import { Pass } from './Pass.js';
  12. class CubeTexturePass extends Pass {
  13. constructor( camera, envMap, opacity = 1 ) {
  14. super();
  15. this.camera = camera;
  16. this.needsSwap = false;
  17. this.cubeShader = ShaderLib[ 'cube' ];
  18. this.cubeMesh = new Mesh(
  19. new BoxGeometry( 10, 10, 10 ),
  20. new ShaderMaterial( {
  21. uniforms: UniformsUtils.clone( this.cubeShader.uniforms ),
  22. vertexShader: this.cubeShader.vertexShader,
  23. fragmentShader: this.cubeShader.fragmentShader,
  24. depthTest: false,
  25. depthWrite: false,
  26. side: BackSide
  27. } )
  28. );
  29. Object.defineProperty( this.cubeMesh.material, 'envMap', {
  30. get: function () {
  31. return this.uniforms.envMap.value;
  32. }
  33. } );
  34. this.envMap = envMap;
  35. this.opacity = opacity;
  36. this.cubeScene = new Scene();
  37. this.cubeCamera = new PerspectiveCamera();
  38. this.cubeScene.add( this.cubeMesh );
  39. }
  40. render( renderer, writeBuffer, readBuffer/*, deltaTime, maskActive*/ ) {
  41. const oldAutoClear = renderer.autoClear;
  42. renderer.autoClear = false;
  43. this.cubeCamera.projectionMatrix.copy( this.camera.projectionMatrix );
  44. this.cubeCamera.quaternion.setFromRotationMatrix( this.camera.matrixWorld );
  45. this.cubeMesh.material.uniforms.envMap.value = this.envMap;
  46. this.cubeMesh.material.uniforms.flipEnvMap.value = ( this.envMap.isCubeTexture && this.envMap.isRenderTargetTexture === false ) ? - 1 : 1;
  47. this.cubeMesh.material.uniforms.opacity.value = this.opacity;
  48. this.cubeMesh.material.transparent = ( this.opacity < 1.0 );
  49. renderer.setRenderTarget( this.renderToScreen ? null : readBuffer );
  50. if ( this.clear ) renderer.clear();
  51. renderer.render( this.cubeScene, this.cubeCamera );
  52. renderer.autoClear = oldAutoClear;
  53. }
  54. }
  55. export { CubeTexturePass };