WebGLRenderTargetCube.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { BackSide, NoBlending } from '../constants.js';
  2. import { Scene } from '../scenes/Scene.js';
  3. import { Mesh } from '../objects/Mesh.js';
  4. import { BoxBufferGeometry } from '../geometries/BoxGeometry.js';
  5. import { ShaderMaterial } from '../materials/ShaderMaterial.js';
  6. import { cloneUniforms } from './shaders/UniformsUtils.js';
  7. import { WebGLRenderTarget } from './WebGLRenderTarget.js';
  8. import { CubeCamera } from '../cameras/CubeCamera.js';
  9. /**
  10. * @author alteredq / http://alteredqualia.com
  11. * @author WestLangley / http://github.com/WestLangley
  12. */
  13. function WebGLRenderTargetCube( width, height, options ) {
  14. WebGLRenderTarget.call( this, width, height, options );
  15. }
  16. WebGLRenderTargetCube.prototype = Object.create( WebGLRenderTarget.prototype );
  17. WebGLRenderTargetCube.prototype.constructor = WebGLRenderTargetCube;
  18. WebGLRenderTargetCube.prototype.isWebGLRenderTargetCube = true;
  19. WebGLRenderTargetCube.prototype.fromEquirectangularTexture = function ( renderer, texture ) {
  20. this.texture.type = texture.type;
  21. this.texture.format = texture.format;
  22. this.texture.encoding = texture.encoding;
  23. var scene = new Scene();
  24. var shader = {
  25. uniforms: {
  26. tEquirect: { value: null },
  27. },
  28. vertexShader: [
  29. "varying vec3 vWorldDirection;",
  30. "vec3 transformDirection( in vec3 dir, in mat4 matrix ) {",
  31. " return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );",
  32. "}",
  33. "void main() {",
  34. " vWorldDirection = transformDirection( position, modelMatrix );",
  35. " #include <begin_vertex>",
  36. " #include <project_vertex>",
  37. "}"
  38. ].join( '\n' ),
  39. fragmentShader: [
  40. "uniform sampler2D tEquirect;",
  41. "varying vec3 vWorldDirection;",
  42. "#define RECIPROCAL_PI 0.31830988618",
  43. "#define RECIPROCAL_PI2 0.15915494",
  44. "void main() {",
  45. " vec3 direction = normalize( vWorldDirection );",
  46. " vec2 sampleUV;",
  47. " sampleUV.y = asin( clamp( direction.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;",
  48. " sampleUV.x = atan( direction.z, direction.x ) * RECIPROCAL_PI2 + 0.5;",
  49. " gl_FragColor = texture2D( tEquirect, sampleUV );",
  50. "}"
  51. ].join( '\n' ),
  52. };
  53. var material = new ShaderMaterial( {
  54. type: 'CubemapFromEquirect',
  55. uniforms: cloneUniforms( shader.uniforms ),
  56. vertexShader: shader.vertexShader,
  57. fragmentShader: shader.fragmentShader,
  58. side: BackSide,
  59. blending: NoBlending
  60. } );
  61. material.uniforms.tEquirect.value = texture;
  62. var mesh = new Mesh( new BoxBufferGeometry( 5, 5, 5 ), material );
  63. scene.add( mesh );
  64. var camera = new CubeCamera( 1, 10, 1 );
  65. camera.renderTarget = this;
  66. camera.renderTarget.texture.name = 'CubeCameraTexture';
  67. camera.update( renderer, scene );
  68. mesh.geometry.dispose();
  69. mesh.material.dispose();
  70. return this;
  71. };
  72. export { WebGLRenderTargetCube };