EquirectangularToCubeGenerator.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * @author Richard M. / https://github.com/richardmonette
  3. * @author WestLangley / http://github.com/WestLangley
  4. */
  5. THREE.CubemapGenerator = function ( renderer ) {
  6. this.renderer = renderer;
  7. };
  8. THREE.CubemapGenerator.prototype.fromEquirectangular = function ( texture, options ) {
  9. var scene = new THREE.Scene();
  10. var shader = {
  11. uniforms: {
  12. tEquirect: { value: null },
  13. },
  14. vertexShader:
  15. `
  16. varying vec3 vWorldDirection;
  17. //include <common>
  18. vec3 transformDirection( in vec3 dir, in mat4 matrix ) {
  19. return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );
  20. }
  21. void main() {
  22. vWorldDirection = transformDirection( position, modelMatrix );
  23. #include <begin_vertex>
  24. #include <project_vertex>
  25. }
  26. `,
  27. fragmentShader:
  28. `
  29. uniform sampler2D tEquirect;
  30. varying vec3 vWorldDirection;
  31. //include <common>
  32. #define RECIPROCAL_PI 0.31830988618
  33. #define RECIPROCAL_PI2 0.15915494
  34. void main() {
  35. vec3 direction = normalize( vWorldDirection );
  36. vec2 sampleUV;
  37. sampleUV.y = asin( clamp( direction.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;
  38. sampleUV.x = atan( direction.z, direction.x ) * RECIPROCAL_PI2 + 0.5;
  39. gl_FragColor = texture2D( tEquirect, sampleUV );
  40. }
  41. `
  42. };
  43. var material = new THREE.ShaderMaterial( {
  44. type: 'CubemapFromEquirect',
  45. uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
  46. vertexShader: shader.vertexShader,
  47. fragmentShader: shader.fragmentShader,
  48. side: THREE.BackSide,
  49. blending: THREE.NoBlending
  50. } );
  51. material.uniforms.tEquirect.value = texture;
  52. var mesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 5, 5, 5 ), material );
  53. scene.add( mesh );
  54. var resolution = options.resolution || 512;
  55. var params = {
  56. type: texture.type,
  57. format: texture.format,
  58. encoding: texture.encoding,
  59. generateMipmaps: ( options.generateMipmaps !== undefined ) ? options.generateMipmaps : texture.generateMipmaps,
  60. minFilter: ( options.minFilter !== undefined ) ? options.minFilter : texture.minFilter,
  61. magFilter: ( options.magFilter !== undefined ) ? options.magFilter : texture.magFilter
  62. };
  63. var camera = new THREE.CubeCamera( 1, 10, resolution, params );
  64. camera.update( this.renderer, scene );
  65. mesh.geometry.dispose();
  66. mesh.material.dispose();
  67. return camera.renderTarget;
  68. };
  69. //
  70. THREE.EquirectangularToCubeGenerator = ( function () {
  71. var camera = new THREE.PerspectiveCamera( 90, 1, 0.1, 10 );
  72. var scene = new THREE.Scene();
  73. var boxMesh = new THREE.Mesh( new THREE.BoxBufferGeometry( 1, 1, 1 ), getShader() );
  74. boxMesh.material.side = THREE.BackSide;
  75. scene.add( boxMesh );
  76. var EquirectangularToCubeGenerator = function ( sourceTexture, options ) {
  77. this.sourceTexture = sourceTexture;
  78. this.resolution = options.resolution || 512;
  79. this.views = [
  80. { t: [ 1, 0, 0 ], u: [ 0, - 1, 0 ] },
  81. { t: [ - 1, 0, 0 ], u: [ 0, - 1, 0 ] },
  82. { t: [ 0, 1, 0 ], u: [ 0, 0, 1 ] },
  83. { t: [ 0, - 1, 0 ], u: [ 0, 0, - 1 ] },
  84. { t: [ 0, 0, 1 ], u: [ 0, - 1, 0 ] },
  85. { t: [ 0, 0, - 1 ], u: [ 0, - 1, 0 ] },
  86. ];
  87. var params = {
  88. format: options.format || this.sourceTexture.format,
  89. magFilter: this.sourceTexture.magFilter,
  90. minFilter: this.sourceTexture.minFilter,
  91. type: options.type || this.sourceTexture.type,
  92. generateMipmaps: this.sourceTexture.generateMipmaps,
  93. anisotropy: this.sourceTexture.anisotropy,
  94. encoding: this.sourceTexture.encoding
  95. };
  96. this.renderTarget = new THREE.WebGLRenderTargetCube( this.resolution, this.resolution, params );
  97. };
  98. EquirectangularToCubeGenerator.prototype = {
  99. constructor: EquirectangularToCubeGenerator,
  100. update: function ( renderer ) {
  101. boxMesh.material.uniforms.equirectangularMap.value = this.sourceTexture;
  102. for ( var i = 0; i < 6; i ++ ) {
  103. this.renderTarget.activeCubeFace = i;
  104. var v = this.views[ i ];
  105. camera.position.set( 0, 0, 0 );
  106. camera.up.set( v.u[ 0 ], v.u[ 1 ], v.u[ 2 ] );
  107. camera.lookAt( v.t[ 0 ], v.t[ 1 ], v.t[ 2 ] );
  108. renderer.render( scene, camera, this.renderTarget, true );
  109. }
  110. return this.renderTarget.texture;
  111. },
  112. dispose: function () {
  113. this.renderTarget.dispose();
  114. }
  115. };
  116. function getShader() {
  117. var shaderMaterial = new THREE.ShaderMaterial( {
  118. uniforms: {
  119. "equirectangularMap": { value: null },
  120. },
  121. vertexShader:
  122. "varying vec3 localPosition;\n\
  123. \n\
  124. void main() {\n\
  125. localPosition = position;\n\
  126. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\
  127. }",
  128. fragmentShader:
  129. "#include <common>\n\
  130. varying vec3 localPosition;\n\
  131. uniform sampler2D equirectangularMap;\n\
  132. \n\
  133. vec2 EquirectangularSampleUV(vec3 v) {\n\
  134. vec2 uv = vec2(atan(v.z, v.x), asin(v.y));\n\
  135. uv *= vec2(0.1591, 0.3183); // inverse atan\n\
  136. uv += 0.5;\n\
  137. return uv;\n\
  138. }\n\
  139. \n\
  140. void main() {\n\
  141. vec2 uv = EquirectangularSampleUV(normalize(localPosition));\n\
  142. gl_FragColor = texture2D(equirectangularMap, uv);\n\
  143. }",
  144. blending: THREE.NoBlending
  145. } );
  146. shaderMaterial.type = 'EquirectangularToCubeGenerator';
  147. return shaderMaterial;
  148. }
  149. return EquirectangularToCubeGenerator;
  150. } )();