WebGLCubeRenderTarget.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 WebGLCubeRenderTarget( size, options, dummy ) {
  14. if ( Number.isInteger( options ) ) {
  15. console.warn( 'THREE.WebGLCubeRenderTarget: constructor signature is now WebGLCubeRenderTarget( size, options )' );
  16. options = dummy;
  17. }
  18. WebGLRenderTarget.call( this, size, size, options );
  19. }
  20. WebGLCubeRenderTarget.prototype = Object.create( WebGLRenderTarget.prototype );
  21. WebGLCubeRenderTarget.prototype.constructor = WebGLCubeRenderTarget;
  22. WebGLCubeRenderTarget.prototype.isWebGLCubeRenderTarget = true;
  23. WebGLCubeRenderTarget.prototype.fromEquirectangularTexture = function ( renderer, texture ) {
  24. this.texture.type = texture.type;
  25. this.texture.format = texture.format;
  26. this.texture.encoding = texture.encoding;
  27. var scene = new Scene();
  28. var shader = {
  29. uniforms: {
  30. tEquirect: { value: null },
  31. },
  32. vertexShader: [
  33. "varying vec3 vWorldDirection;",
  34. "vec3 transformDirection( in vec3 dir, in mat4 matrix ) {",
  35. " return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );",
  36. "}",
  37. "void main() {",
  38. " vWorldDirection = transformDirection( position, modelMatrix );",
  39. " #include <begin_vertex>",
  40. " #include <project_vertex>",
  41. "}"
  42. ].join( '\n' ),
  43. fragmentShader: [
  44. "uniform sampler2D tEquirect;",
  45. "varying vec3 vWorldDirection;",
  46. "#define RECIPROCAL_PI 0.31830988618",
  47. "#define RECIPROCAL_PI2 0.15915494",
  48. "void main() {",
  49. " vec3 direction = normalize( vWorldDirection );",
  50. " vec2 sampleUV;",
  51. " sampleUV.y = asin( clamp( direction.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;",
  52. " sampleUV.x = atan( direction.z, direction.x ) * RECIPROCAL_PI2 + 0.5;",
  53. " gl_FragColor = texture2D( tEquirect, sampleUV );",
  54. "}"
  55. ].join( '\n' ),
  56. };
  57. var material = new ShaderMaterial( {
  58. type: 'CubemapFromEquirect',
  59. uniforms: cloneUniforms( shader.uniforms ),
  60. vertexShader: shader.vertexShader,
  61. fragmentShader: shader.fragmentShader,
  62. side: BackSide,
  63. blending: NoBlending
  64. } );
  65. material.uniforms.tEquirect.value = texture;
  66. var mesh = new Mesh( new BoxBufferGeometry( 5, 5, 5 ), material );
  67. scene.add( mesh );
  68. var camera = new CubeCamera( 1, 10, 1 );
  69. camera.renderTarget = this;
  70. camera.renderTarget.texture.name = 'CubeCameraTexture';
  71. camera.update( renderer, scene );
  72. mesh.geometry.dispose();
  73. mesh.material.dispose();
  74. return this;
  75. };
  76. export { WebGLCubeRenderTarget };
粤ICP备19079148号