PMREMCubeUVPacker.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * @author Prashant Sharma / spidersharma03
  3. * @author Ben Houston / bhouston, https://clara.io
  4. */
  5. THREE.PMREMCubeUVPacker = function( cubeTextureLods, numLods ) {
  6. this.cubeLods = cubeTextureLods;
  7. this.numLods = numLods;
  8. var size = cubeTextureLods[ 0 ].width * 4;
  9. this.CubeUVRenderTarget = new THREE.WebGLRenderTarget( size, size,
  10. { format: THREE.RGBAFormat, magFilter: THREE.LinearFilter, minFilter: THREE.LinearFilter, type: cubeTextureLods[ 0 ].texture.type } );
  11. this.CubeUVRenderTarget.texture.generateMipmaps = false;
  12. this.CubeUVRenderTarget.mapping = THREE.CubeUVReflectionMapping;
  13. this.camera = new THREE.OrthographicCamera( - size * 0.5, size * 0.5, - size * 0.5, size * 0.5, 0.0, 1000 );
  14. this.scene = new THREE.Scene();
  15. this.scene.add( this.camera );
  16. this.objects = [];
  17. var xOffset = 0;
  18. var faceOffsets = [];
  19. faceOffsets.push( new THREE.Vector2( 0, 0 ) );
  20. faceOffsets.push( new THREE.Vector2( 1, 0 ) );
  21. faceOffsets.push( new THREE.Vector2( 2, 0 ) );
  22. faceOffsets.push( new THREE.Vector2( 0, 1 ) );
  23. faceOffsets.push( new THREE.Vector2( 1, 1 ) );
  24. faceOffsets.push( new THREE.Vector2( 2, 1 ) );
  25. var yOffset = 0;
  26. var textureResolution = size;
  27. size = cubeTextureLods[ 0 ].width;
  28. var offset2 = 0;
  29. var c = 4.0;
  30. this.numLods = Math.log2( cubeTextureLods[ 0 ].width ) - 2;
  31. for ( var i = 0; i < this.numLods; i ++ ) {
  32. var offset1 = ( textureResolution - textureResolution / c ) * 0.5;
  33. if ( size > 16 )
  34. c *= 2;
  35. var nMips = size > 16 ? 6 : 1;
  36. var mipOffsetX = 0;
  37. var mipOffsetY = 0;
  38. var mipSize = size;
  39. for ( var j = 0; j < nMips; j ++ ) {
  40. // Mip Maps
  41. for ( var k = 0; k < 6; k ++ ) {
  42. // 6 Cube Faces
  43. var material = this.getShader();
  44. material.uniforms[ "envMap" ].value = this.cubeLods[ i ];
  45. material.envMap = this.cubeLods[ i ]
  46. material.uniforms[ "faceIndex" ].value = k;
  47. material.uniforms[ "mapSize" ].value = mipSize;
  48. var color = material.uniforms[ "testColor" ].value;
  49. //color.copy(testColor[j]);
  50. var planeMesh = new THREE.Mesh(
  51. new THREE.PlaneGeometry( mipSize, mipSize, 0 ),
  52. material );
  53. planeMesh.position.x = faceOffsets[ k ].x * mipSize - offset1 + mipOffsetX;
  54. planeMesh.position.y = faceOffsets[ k ].y * mipSize - offset1 + offset2 + mipOffsetY;
  55. planeMesh.material.side = THREE.DoubleSide;
  56. this.scene.add( planeMesh );
  57. this.objects.push( planeMesh );
  58. }
  59. mipOffsetY += 1.75 * mipSize;
  60. mipOffsetX += 1.25 * mipSize;
  61. mipSize /= 2;
  62. }
  63. offset2 += 2 * size;
  64. if ( size > 16 )
  65. size /= 2;
  66. }
  67. };
  68. THREE.PMREMCubeUVPacker.prototype = {
  69. constructor : THREE.PMREMCubeUVPacker,
  70. update: function( renderer ) {
  71. var gammaInput = renderer.gammaInput;
  72. var gammaOutput = renderer.gammaOutput;
  73. renderer.gammaInput = false;
  74. renderer.gammaOutput = false;
  75. renderer.render( this.scene, this.camera, this.CubeUVRenderTarget, true );
  76. renderer.gammaInput = renderer.gammaInput;
  77. renderer.gammaOutput = renderer.gammaOutput;
  78. },
  79. getShader: function() {
  80. var shaderMaterial = new THREE.ShaderMaterial( {
  81. uniforms: {
  82. "faceIndex": { type: 'i', value: 0 },
  83. "mapSize": { type: 'f', value: 0 },
  84. "envMap": { type: 't', value: null },
  85. "testColor": { type: 'v3', value: new THREE.Vector3( 1, 1, 1 ) }
  86. },
  87. vertexShader:
  88. "precision highp float;\
  89. varying vec2 vUv;\
  90. void main() {\
  91. vUv = uv;\
  92. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\
  93. }",
  94. fragmentShader:
  95. "precision highp float;\
  96. varying vec2 vUv;\
  97. uniform samplerCube envMap;\
  98. uniform float mapSize;\
  99. uniform vec3 testColor;\
  100. uniform int faceIndex;\
  101. \
  102. void main() {\
  103. vec3 sampleDirection;\
  104. vec2 uv = vUv;\
  105. uv = uv * 2.0 - 1.0;\
  106. uv.y *= -1.0;\
  107. if(faceIndex == 0) {\
  108. sampleDirection = normalize(vec3(1.0, uv.y, -uv.x));\
  109. }\
  110. else if(faceIndex == 1) {\
  111. sampleDirection = normalize(vec3(uv.x, 1.0, uv.y));\
  112. }\
  113. else if(faceIndex == 2) {\
  114. sampleDirection = normalize(vec3(uv.x, uv.y, 1.0));\
  115. }\
  116. else if(faceIndex == 3) {\
  117. sampleDirection = normalize(vec3(-1.0, uv.y, uv.x));\
  118. }\
  119. else if(faceIndex == 4) {\
  120. sampleDirection = normalize(vec3(uv.x, -1.0, -uv.y));\
  121. }\
  122. else {\
  123. sampleDirection = normalize(vec3(-uv.x, uv.y, -1.0));\
  124. }\
  125. vec4 color = envMapTexelToLinear( textureCube( envMap, sampleDirection ) );\
  126. gl_FragColor = linearToOutputTexel( color * vec4(testColor, 1.0) );\
  127. }",
  128. blending: THREE.CustomBlending,
  129. blendSrc: THREE.OneFactor,
  130. blendDst: THREE.ZeroFactor,
  131. blendSrcAlpha: THREE.OneFactor,
  132. blendDstAlpha: THREE.ZeroFactor,
  133. blendEquation: THREE.AddEquation
  134. });
  135. return shaderMaterial;
  136. }
  137. };