Spaces:
Running
Running
File size: 5,442 Bytes
6cd9596 |
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
/**
* @author Richard M. / https://github.com/richardmonette
* @author WestLangley / http://github.com/WestLangley
*/
THREE.CubemapGenerator = function ( renderer ) {
this.renderer = renderer;
};
THREE.CubemapGenerator.prototype.fromEquirectangular = function ( texture, options ) {
options = options || {};
var scene = new THREE.Scene();
var shader = {
uniforms: {
tEquirect: { value: null },
},
vertexShader:
`
varying vec3 vWorldDirection;
//include <common>
vec3 transformDirection( in vec3 dir, in mat4 matrix ) {
return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );
}
void main() {
vWorldDirection = transformDirection( position, modelMatrix );
#include <begin_vertex>
#include <project_vertex>
}
`,
fragmentShader:
`
uniform sampler2D tEquirect;
varying vec3 vWorldDirection;
//include <common>
#define RECIPROCAL_PI 0.31830988618
#define RECIPROCAL_PI2 0.15915494
void main() {
vec3 direction = normalize( vWorldDirection );
vec2 sampleUV;
sampleUV.y = asin( clamp( direction.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;
sampleUV.x = atan( direction.z, direction.x ) * RECIPROCAL_PI2 + 0.5;
gl_FragColor = texture2D( tEquirect, sampleUV );
}
`
};
var material = new THREE.ShaderMaterial( {
type: 'CubemapFromEquirect',
uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
vertexShader: shader.vertexShader,
fragmentShader: shader.fragmentShader,
side: THREE.BackSide,
blending: THREE.NoBlending
} );
material.uniforms.tEquirect.value = texture;
var mesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 5, 5, 5 ), material );
scene.add( mesh );
var resolution = options.resolution || 512;
var params = {
type: texture.type,
format: texture.format,
encoding: texture.encoding,
generateMipmaps: ( options.generateMipmaps !== undefined ) ? options.generateMipmaps : texture.generateMipmaps,
minFilter: ( options.minFilter !== undefined ) ? options.minFilter : texture.minFilter,
magFilter: ( options.magFilter !== undefined ) ? options.magFilter : texture.magFilter
};
var camera = new THREE.CubeCamera( 1, 10, resolution, params );
camera.update( this.renderer, scene );
mesh.geometry.dispose();
mesh.material.dispose();
return camera.renderTarget;
};
//
THREE.EquirectangularToCubeGenerator = ( function () {
var camera = new THREE.PerspectiveCamera( 90, 1, 0.1, 10 );
var scene = new THREE.Scene();
var boxMesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 1, 1, 1 ), getShader() );
boxMesh.material.side = THREE.BackSide;
scene.add( boxMesh );
var EquirectangularToCubeGenerator = function ( sourceTexture, options ) {
options = options || {};
this.sourceTexture = sourceTexture;
this.resolution = options.resolution || 512;
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 ] },
];
var params = {
format: options.format || this.sourceTexture.format,
magFilter: this.sourceTexture.magFilter,
minFilter: this.sourceTexture.minFilter,
type: options.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 );
};
EquirectangularToCubeGenerator.prototype = {
constructor: EquirectangularToCubeGenerator,
update: function ( renderer ) {
var currentRenderTarget = renderer.getRenderTarget();
boxMesh.material.uniforms.equirectangularMap.value = this.sourceTexture;
for ( var i = 0; i < 6; i ++ ) {
var v = this.views[ i ];
camera.position.set( 0, 0, 0 );
camera.up.set( v.u[ 0 ], v.u[ 1 ], v.u[ 2 ] );
camera.lookAt( v.t[ 0 ], v.t[ 1 ], v.t[ 2 ] );
renderer.setRenderTarget( this.renderTarget, i );
renderer.clear();
renderer.render( scene, camera );
}
renderer.setRenderTarget( currentRenderTarget );
return this.renderTarget.texture;
},
dispose: function () {
this.renderTarget.dispose();
}
};
function getShader() {
var shaderMaterial = new THREE.ShaderMaterial( {
uniforms: {
"equirectangularMap": { value: null },
},
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 EquirectangularSampleUV(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 = EquirectangularSampleUV(normalize(localPosition));\n\
gl_FragColor = texture2D(equirectangularMap, uv);\n\
}",
blending: THREE.NoBlending
} );
shaderMaterial.type = 'EquirectangularToCubeGenerator';
return shaderMaterial;
}
return EquirectangularToCubeGenerator;
} )();
|