EquirectangularToCubeGenerator.js 2.8 KB
Newer Older
1 2 3 4
/**
* @author Richard M. / https://github.com/richardmonette
*/

5
THREE.EquirectangularToCubeGenerator = function ( sourceTexture, options ) {
6 7

	this.sourceTexture = sourceTexture;
R
Richard Monette 已提交
8
	this.resolution = options.resolution || 512;
9 10

 	this.views = [
11 12 13 14 15 16
		{ t: [ 1, 0, 0 ], u: [ 0, - 1, 0 ] },
		{ t: [ - 1, 0, 0 ], u: [ 0, - 1, 0 ] },
		{ t: [ 0, 1, 0 ], u: [ 0, 0, 1 ] },
		{ t: [ 0, - 1, 0 ], u: [ 0, 0, - 1 ] },
		{ t: [ 0, 0, 1 ], u: [ 0, - 1, 0 ] },
		{ t: [ 0, 0, - 1 ], u: [ 0, - 1, 0 ] },
17 18 19
	];

	this.camera = new THREE.PerspectiveCamera( 90, 1, 0.1, 10 );
20
	this.boxMesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 1, 1, 1 ), this.getShader() );
21
	this.boxMesh.material.side = THREE.BackSide;
22
	this.scene = new THREE.Scene();
23
	this.scene.add( this.boxMesh );
24

25
	var params = {
R
Richard Monette 已提交
26
		format: options.format || this.sourceTexture.format,
27 28
		magFilter: this.sourceTexture.magFilter,
		minFilter: this.sourceTexture.minFilter,
R
Richard Monette 已提交
29
		type: options.type || this.sourceTexture.type,
30 31 32 33
		generateMipmaps: this.sourceTexture.generateMipmaps,
		anisotropy: this.sourceTexture.anisotropy,
		encoding: this.sourceTexture.encoding
	};
34

35
	this.renderTarget = new THREE.WebGLRenderTargetCube( this.resolution, this.resolution, params );
36

37
};
38

W
WestLangley 已提交
39
THREE.EquirectangularToCubeGenerator.prototype = {
40

W
WestLangley 已提交
41
	constructor: THREE.EquirectangularToCubeGenerator,
42

43
	update: function ( renderer ) {
44

45
		for ( var i = 0; i < 6; i ++ ) {
46

47
			this.renderTarget.activeCubeFace = i;
48

49
			var v = this.views[ i ];
50

51 52 53
			this.camera.position.set( 0, 0, 0 );
			this.camera.up.set( v.u[ 0 ], v.u[ 1 ], v.u[ 2 ] );
			this.camera.lookAt( v.t[ 0 ], v.t[ 1 ], v.t[ 2 ] );
54

55
			renderer.render( this.scene, this.camera, this.renderTarget, true );
56 57 58

		}

59
		return this.renderTarget.texture;
60 61 62

	},

63
	getShader: function () {
64

65
		var shaderMaterial = new THREE.ShaderMaterial( {
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

			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\
W
WestLangley 已提交
84
				vec2 EquirectangularSampleUV(vec3 v) {\n\
85 86 87 88 89 90 91
			    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\
W
WestLangley 已提交
92
					vec2 uv = EquirectangularSampleUV(normalize(localPosition));\n\
W
WestLangley 已提交
93
					gl_FragColor = texture2D(equirectangularMap, uv);\n\
W
WestLangley 已提交
94 95
				}",

W
WestLangley 已提交
96
			blending: THREE.NoBlending
W
WestLangley 已提交
97

98 99
		} );

W
WestLangley 已提交
100
		shaderMaterial.type = 'EquirectangularToCubeGenerator';
101 102 103 104 105 106 107 108 109 110 111

		return shaderMaterial;

	},

	dispose: function () {

		this.boxMesh.geometry.dispose();
		this.boxMesh.material.dispose();
		this.renderTarget.dispose();

112 113 114
	}

};