[b86468]: / v3 / js / libs / three / loaders / EquiangularToCubeGenerator.js

Download this file

123 lines (90 with data), 3.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* @author Richard M. / https://github.com/richardmonette
*/
THREE.EquiangularToCubeGenerator = function ( sourceTexture, resolution ) {
this.sourceTexture = sourceTexture;
this.resolution = resolution;
this.views = [
{ 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 ] },
];
this.camera = new THREE.PerspectiveCamera( 90, 1, 0.1, 10 );
this.boxMesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 1, 1, 1 ), this.getShader() );
this.boxMesh.material.side = THREE.BackSide;
this.scene = new THREE.Scene();
this.scene.add( this.boxMesh );
var params = {
format: THREE.RGBAFormat,
magFilter: this.sourceTexture.magFilter,
minFilter: this.sourceTexture.minFilter,
type: this.sourceTexture.type,
generateMipmaps: this.sourceTexture.generateMipmaps,
anisotropy: this.sourceTexture.anisotropy,
encoding: this.sourceTexture.encoding
};
this.renderTarget = new THREE.WebGLRenderTargetCube( this.resolution, this.resolution, params );
};
THREE.EquiangularToCubeGenerator.prototype = {
constructor: THREE.EquiangularToCubeGenerator,
update: function ( renderer ) {
for ( var i = 0; i < 6; i ++ ) {
this.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( v.t[ 0 ], v.t[ 1 ], v.t[ 2 ] );
renderer.render( this.scene, this.camera, this.renderTarget, true );
}
return this.renderTarget.texture;
},
getShader: function () {
var shaderMaterial = 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,
premultipliedAlpha: false,
blendSrc: THREE.OneFactor,
blendDst: THREE.ZeroFactor,
blendSrcAlpha: THREE.OneFactor,
blendDstAlpha: THREE.ZeroFactor,
blendEquation: THREE.AddEquation
} );
shaderMaterial.type = 'EquiangularToCubeGenerator';
return shaderMaterial;
},
dispose: function () {
this.boxMesh.geometry.dispose();
this.boxMesh.material.dispose();
this.renderTarget.dispose();
}
};