123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- /**
- * @author Richard M. / https://github.com/richardmonette
- */
- THREE.EquiangularToCubeGenerator = function( sourceTexture, resolution ) {
- this.sourceTexture = sourceTexture;
- this.resolution = resolution;
- this.views = [
- { t: [1.0, 0.0, 0.0 ], u: [0.0, -1.0, 0.0] },
- { t: [-1.0, 0.0, 0.0], u: [0.0, -1.0, 0.0] },
- { t: [0.0, 1.0, 0.0], u: [0.0, 0.0, 1.0] },
- { t: [0.0, -1.0, 0.0], u: [0.0, 0.0, -1.0] },
- { t: [0.0, 0.0, 1.0], u: [0.0, -1.0, 0.0] },
- { t: [0.0, 0.0, -1.0], u: [0.0, -1.0, 0.0] },
- ];
- this.camera = new THREE.PerspectiveCamera( 90, 1, 0.1, 10 );
- this.planeMesh = new THREE.Mesh( new THREE.BoxGeometry( 1, 1, 1 ), this.getShader() );
- this.planeMesh.material.side = THREE.DoubleSide;
- this.scene = new THREE.Scene();
- this.scene.add( this.planeMesh );
- this.scene.add( this.camera );
- };
- THREE.EquiangularToCubeGenerator.prototype = {
- constructor : THREE.EquiangularToCubeGenerator,
- generate: function( renderer ) {
- var params = {
- format: this.sourceTexture.format,
- magFilter: this.sourceTexture.magFilter,
- minFilter: this.sourceTexture.minFilter,
- type: this.sourceTexture.type,
- generateMipmaps: this.sourceTexture.generateMipmaps,
- anisotropy: this.sourceTexture.anisotropy,
- encoding: this.sourceTexture.encoding
- };
- var renderTarget = new THREE.WebGLRenderTargetCube( this.resolution, this.resolution, params );
- for ( var i = 0; i < 6; i++ ) {
- renderTarget.activeCubeFace = i;
- var v = this.views[i];
- this.camera.position.set(0, 0, 0);
- this.camera.up.set(v.u[0], v.u[1], v.u[2]);
- this.camera.lookAt(new THREE.Vector3(v.t[0], v.t[1], v.t[2]));
- this.camera.updateProjectionMatrix();
- renderer.render( this.scene, this.camera, renderTarget, true );
- }
- return renderTarget.texture;
- },
- getShader: function() {
- return new THREE.ShaderMaterial( {
- uniforms: {
- "equirectangularMap": { value: this.sourceTexture },
- },
- vertexShader:
- "varying vec3 localPosition;\n\
- \n\
- void main() {\n\
- localPosition = position;\n\
- gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
- }",
- fragmentShader:
- "#include <common>\n\
- varying vec3 localPosition;\n\
- uniform sampler2D equirectangularMap;\n\
- \n\
- vec2 EquiangularSampleUV(vec3 v) {\n\
- vec2 uv = vec2(atan(v.z, v.x), asin(v.y));\n\
- uv *= vec2(0.1591, 0.3183); // inverse atan\n\
- uv += 0.5;\n\
- return uv;\n\
- }\n\
- \n\
- void main() {\n\
- vec2 uv = EquiangularSampleUV(normalize(localPosition));\n\
- vec3 color = texture2D(equirectangularMap, uv).rgb;\n\
- \n\
- gl_FragColor = vec4( color, 1.0 );\n\
- }",
- blending: THREE.CustomBlending,
- blendSrc: THREE.OneFactor,
- blendDst: THREE.ZeroFactor,
- blendSrcAlpha: THREE.OneFactor,
- blendDstAlpha: THREE.ZeroFactor,
- blendEquation: THREE.AddEquation
- } );
- }
- };
|