WebGLCubeRenderTarget.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { BackSide, LinearFilter, LinearMipmapLinearFilter, NoBlending, RGBAFormat } from '../constants.js';
  2. import { Mesh } from '../objects/Mesh.js';
  3. import { BoxBufferGeometry } from '../geometries/BoxGeometry.js';
  4. import { ShaderMaterial } from '../materials/ShaderMaterial.js';
  5. import { cloneUniforms } from './shaders/UniformsUtils.js';
  6. import { WebGLRenderTarget } from './WebGLRenderTarget.js';
  7. import { CubeCamera } from '../cameras/CubeCamera.js';
  8. function WebGLCubeRenderTarget( size, options, dummy ) {
  9. if ( Number.isInteger( options ) ) {
  10. console.warn( 'THREE.WebGLCubeRenderTarget: constructor signature is now WebGLCubeRenderTarget( size, options )' );
  11. options = dummy;
  12. }
  13. WebGLRenderTarget.call( this, size, size, options );
  14. this.texture.isWebGLCubeRenderTargetTexture = true; // HACK Why is texture not a CubeTexture?
  15. }
  16. WebGLCubeRenderTarget.prototype = Object.create( WebGLRenderTarget.prototype );
  17. WebGLCubeRenderTarget.prototype.constructor = WebGLCubeRenderTarget;
  18. WebGLCubeRenderTarget.prototype.isWebGLCubeRenderTarget = true;
  19. WebGLCubeRenderTarget.prototype.fromEquirectangularTexture = function ( renderer, texture ) {
  20. this.texture.type = texture.type;
  21. this.texture.format = RGBAFormat; // see #18859
  22. this.texture.encoding = texture.encoding;
  23. this.texture.generateMipmaps = texture.generateMipmaps;
  24. this.texture.minFilter = texture.minFilter;
  25. this.texture.magFilter = texture.magFilter;
  26. const shader = {
  27. uniforms: {
  28. tEquirect: { value: null },
  29. },
  30. vertexShader: /* glsl */`
  31. varying vec3 vWorldDirection;
  32. vec3 transformDirection( in vec3 dir, in mat4 matrix ) {
  33. return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );
  34. }
  35. void main() {
  36. vWorldDirection = transformDirection( position, modelMatrix );
  37. #include <begin_vertex>
  38. #include <project_vertex>
  39. }
  40. `,
  41. fragmentShader: /* glsl */`
  42. uniform sampler2D tEquirect;
  43. varying vec3 vWorldDirection;
  44. #include <common>
  45. void main() {
  46. vec3 direction = normalize( vWorldDirection );
  47. vec2 sampleUV = equirectUv( direction );
  48. gl_FragColor = texture2D( tEquirect, sampleUV );
  49. }
  50. `
  51. };
  52. const geometry = new BoxBufferGeometry( 5, 5, 5 );
  53. const material = new ShaderMaterial( {
  54. name: 'CubemapFromEquirect',
  55. uniforms: cloneUniforms( shader.uniforms ),
  56. vertexShader: shader.vertexShader,
  57. fragmentShader: shader.fragmentShader,
  58. side: BackSide,
  59. blending: NoBlending
  60. } );
  61. material.uniforms.tEquirect.value = texture;
  62. const mesh = new Mesh( geometry, material );
  63. const currentMinFilter = texture.minFilter;
  64. // Avoid blurred poles
  65. if ( texture.minFilter === LinearMipmapLinearFilter ) texture.minFilter = LinearFilter;
  66. const camera = new CubeCamera( 1, 10, this );
  67. camera.update( renderer, mesh );
  68. texture.minFilter = currentMinFilter;
  69. mesh.geometry.dispose();
  70. mesh.material.dispose();
  71. return this;
  72. };
  73. export { WebGLCubeRenderTarget };