PMREM_CubeUVPacker.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @author Prashant Sharma / spidersharma03
  3. */
  4. THREE.PMREM_CubeUVPacker = function( cubeTextureLods, numLods ) {
  5. this.cubeLods = cubeTextureLods;
  6. this.numLods = numLods;
  7. var size = cubeTextureLods[ 0 ].width * 4;
  8. this.CubeUVRenderTarget = new THREE.WebGLRenderTarget( size, size,
  9. { format: THREE.RGBAFormat, magFilter: THREE.LinearFilter, minFilter: THREE.LinearFilter } );
  10. this.CubeUVRenderTarget.texture.generateMipmaps = false;
  11. this.CubeUVRenderTarget.mapping = THREE.CubeUVReflectionMapping;
  12. this.camera = new THREE.OrthographicCamera( - size * 0.5, size * 0.5, - size * 0.5, size * 0.5, 0.0, 1000 );
  13. this.CubeUVRenderTarget.encoding = this.cubeLods[0].encoding;
  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. /*
  40. var testColor = [];
  41. testColor.push( new THREE.Vector3( 1, 0, 0 ) );
  42. testColor.push( new THREE.Vector3( 0, 1, 0 ) );
  43. testColor.push( new THREE.Vector3( 0, 0, 1 ) );
  44. testColor.push( new THREE.Vector3( 1, 1, 0 ) );
  45. testColor.push( new THREE.Vector3( 0, 1, 1 ) );
  46. testColor.push( new THREE.Vector3( 1, 0, 1 ) );
  47. testColor.push( new THREE.Vector3( 1, 1, 1 ) );
  48. testColor.push( new THREE.Vector3( 0.5, 1, 0.5 ) );*/
  49. for ( var j = 0; j < nMips; j ++ ) {
  50. // Mip Maps
  51. for ( var k = 0; k < 6; k ++ ) {
  52. // 6 Cube Faces
  53. var material = this.getShader();
  54. material.uniforms[ "cubeTexture" ].value = this.cubeLods[ i ];
  55. material.uniforms[ "faceIndex" ].value = k;
  56. material.uniforms[ "mapSize" ].value = mipSize;
  57. var color = material.uniforms[ "testColor" ].value;
  58. //color.copy(testColor[j]);
  59. var planeMesh = new THREE.Mesh(
  60. new THREE.PlaneGeometry( mipSize, mipSize, 0 ),
  61. material );
  62. planeMesh.position.x = faceOffsets[ k ].x * mipSize - offset1 + mipOffsetX;
  63. planeMesh.position.y = faceOffsets[ k ].y * mipSize - offset1 + offset2 + mipOffsetY;
  64. planeMesh.material.side = THREE.DoubleSide;
  65. this.scene.add( planeMesh );
  66. this.objects.push( planeMesh );
  67. }
  68. mipOffsetY += 1.75 * mipSize;
  69. mipOffsetX += 1.25 * mipSize;
  70. mipSize /= 2;
  71. }
  72. offset2 += 2 * size;
  73. if ( size > 16 )
  74. size /= 2;
  75. }
  76. };
  77. THREE.PMREM_CubeUVPacker.prototype = {
  78. constructor : THREE.PMREM_CubeUVPacker,
  79. update: function( renderer ) {
  80. renderer.render( this.scene, this.camera, this.CubeUVRenderTarget, true );
  81. },
  82. getShader: function() {
  83. return new THREE.ShaderMaterial( {
  84. uniforms: {
  85. "faceIndex": { type: 'i', value: 0 },
  86. "mapSize": { type: 'f', value: 0 },
  87. "cubeTexture": { type: 't', value: null },
  88. "testColor": { type: 'v3', value: new THREE.Vector3( 1, 1, 1 ) }
  89. },
  90. vertexShader:
  91. "precision highp float;\
  92. varying vec2 vUv;\
  93. void main() {\
  94. vUv = uv;\
  95. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\
  96. }",
  97. fragmentShader:
  98. "precision highp float;\
  99. varying vec2 vUv;\
  100. uniform samplerCube cubeTexture;\
  101. uniform float mapSize;\
  102. uniform vec3 testColor;\
  103. uniform int faceIndex;\
  104. \
  105. const float PI = 3.14159265358979;\
  106. vec4 sampleCubeMap(float phi, float theta) {\
  107. float sinTheta = sin(theta);\
  108. float cosTheta = cos(theta);\
  109. vec3 sampleDir = vec3(cos(phi) * sinTheta, cosTheta, sin(phi) * sinTheta);\
  110. vec4 color = textureCube(cubeTexture, sampleDir);\
  111. return color * vec4(testColor, 1.0);\
  112. }\
  113. void main() {\
  114. vec3 sampleDirection;\
  115. vec2 uv = vUv;\
  116. uv = uv * 2.0 - 1.0;\
  117. uv.y *= -1.0;\
  118. if(faceIndex == 0) {\
  119. sampleDirection = normalize(vec3(1.0, uv.y, -uv.x));\
  120. }\
  121. else if(faceIndex == 1) {\
  122. sampleDirection = normalize(vec3(uv.x, 1.0, uv.y));\
  123. }\
  124. else if(faceIndex == 2) {\
  125. sampleDirection = normalize(vec3(uv.x, uv.y, 1.0));\
  126. }\
  127. else if(faceIndex == 3) {\
  128. sampleDirection = normalize(vec3(-1.0, uv.y, uv.x));\
  129. }\
  130. else if(faceIndex == 4) {\
  131. sampleDirection = normalize(vec3(uv.x, -1.0, -uv.y));\
  132. }\
  133. else {\
  134. sampleDirection = normalize(vec3(-uv.x, uv.y, -1.0));\
  135. }\
  136. vec4 color = textureCube(cubeTexture, (sampleDirection));\
  137. gl_FragColor = color * vec4(testColor, 1.0);\
  138. }"
  139. });
  140. }
  141. };