CubeRenderTarget.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { WebGLCubeRenderTarget, Scene, CubeCamera, BoxGeometry, Mesh, BackSide, NoBlending, LinearFilter, LinearMipmapLinearFilter } from 'three';
  2. import { equirectUV } from '../../nodes/utils/EquirectUVNode.js';
  3. import { texture as TSL_Texture } from '../../nodes/accessors/TextureNode.js';
  4. import { positionWorldDirection } from '../../nodes/accessors/PositionNode.js';
  5. import { createNodeMaterialFromType } from '../../nodes/materials/NodeMaterial.js';
  6. // @TODO: Consider rename WebGLCubeRenderTarget to just CubeRenderTarget
  7. class CubeRenderTarget extends WebGLCubeRenderTarget {
  8. constructor( size = 1, options = {} ) {
  9. super( size, options );
  10. this.isCubeRenderTarget = true;
  11. }
  12. fromEquirectangularTexture( renderer, texture ) {
  13. const currentMinFilter = texture.minFilter;
  14. const currentGenerateMipmaps = texture.generateMipmaps;
  15. texture.generateMipmaps = true;
  16. this.texture.type = texture.type;
  17. this.texture.colorSpace = texture.colorSpace;
  18. this.texture.generateMipmaps = texture.generateMipmaps;
  19. this.texture.minFilter = texture.minFilter;
  20. this.texture.magFilter = texture.magFilter;
  21. const geometry = new BoxGeometry( 5, 5, 5 );
  22. const uvNode = equirectUV( positionWorldDirection );
  23. const material = createNodeMaterialFromType( 'MeshBasicNodeMaterial' );
  24. material.colorNode = TSL_Texture( texture, uvNode, 0 );
  25. material.side = BackSide;
  26. material.blending = NoBlending;
  27. const mesh = new Mesh( geometry, material );
  28. const scene = new Scene();
  29. scene.add( mesh );
  30. // Avoid blurred poles
  31. if ( texture.minFilter === LinearMipmapLinearFilter ) texture.minFilter = LinearFilter;
  32. const camera = new CubeCamera( 1, 10, this );
  33. camera.update( renderer, scene );
  34. texture.minFilter = currentMinFilter;
  35. texture.currentGenerateMipmaps = currentGenerateMipmaps;
  36. mesh.geometry.dispose();
  37. mesh.material.dispose();
  38. return this;
  39. }
  40. }
  41. export default CubeRenderTarget;