BufferGeometryUtils.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * @author spite / http://www.clicktorelease.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. THREE.BufferGeometryUtils = {
  6. fromGeometry: function geometryToBufferGeometry( geometry, settings ) {
  7. settings = settings || { 'vertexColors': THREE.NoColors };
  8. var vertices = geometry.vertices;
  9. var faces = geometry.faces;
  10. var faceVertexUvs = geometry.faceVertexUvs;
  11. var vertexColors = settings.vertexColors;
  12. var hasFaceVertexUv = faceVertexUvs[ 0 ].length > 0;
  13. var triangles = 0;
  14. for ( var i = 0; i < faces.length; i ++ ) {
  15. triangles += faces[ i ] instanceof THREE.Face3 ? 1 : 2;
  16. }
  17. var bufferGeometry = new THREE.BufferGeometry();
  18. bufferGeometry.attributes = {
  19. position: {
  20. itemSize: 3,
  21. array: new Float32Array( triangles * 3 * 3 )
  22. },
  23. normal: {
  24. itemSize: 3,
  25. array: new Float32Array( triangles * 3 * 3 )
  26. }
  27. }
  28. var positions = bufferGeometry.attributes.position.array;
  29. var normals = bufferGeometry.attributes.normal.array;
  30. if ( vertexColors !== THREE.NoColors ) {
  31. bufferGeometry.attributes.color = {
  32. itemSize: 3,
  33. array: new Float32Array( triangles * 3 * 3 )
  34. };
  35. var colors = bufferGeometry.attributes.color.array;
  36. }
  37. if ( hasFaceVertexUv === true ) {
  38. bufferGeometry.attributes.uv = {
  39. itemSize: 2,
  40. array: new Float32Array( triangles * 3 * 2 )
  41. };
  42. var uvs = bufferGeometry.attributes.uv.array;
  43. }
  44. var i2 = 0, i3 = 0;
  45. for ( var i = 0; i < faces.length; i ++ ) {
  46. var face = faces[ i ];
  47. var a = vertices[ face.a ];
  48. var b = vertices[ face.b ];
  49. var c = vertices[ face.c ];
  50. positions[ i3 ] = a.x;
  51. positions[ i3 + 1 ] = a.y;
  52. positions[ i3 + 2 ] = a.z;
  53. positions[ i3 + 3 ] = b.x;
  54. positions[ i3 + 4 ] = b.y;
  55. positions[ i3 + 5 ] = b.z;
  56. positions[ i3 + 6 ] = c.x;
  57. positions[ i3 + 7 ] = c.y;
  58. positions[ i3 + 8 ] = c.z;
  59. var na = face.vertexNormals[ 0 ];
  60. var nb = face.vertexNormals[ 1 ];
  61. var nc = face.vertexNormals[ 2 ];
  62. normals[ i3 ] = na.x;
  63. normals[ i3 + 1 ] = na.y;
  64. normals[ i3 + 2 ] = na.z;
  65. normals[ i3 + 3 ] = nb.x;
  66. normals[ i3 + 4 ] = nb.y;
  67. normals[ i3 + 5 ] = nb.z;
  68. normals[ i3 + 6 ] = nc.x;
  69. normals[ i3 + 7 ] = nc.y;
  70. normals[ i3 + 8 ] = nc.z;
  71. if ( vertexColors === THREE.FaceColors ) {
  72. var fc = face.color;
  73. colors[ i3 ] = fc.r;
  74. colors[ i3 + 1 ] = fc.g;
  75. colors[ i3 + 2 ] = fc.b;
  76. colors[ i3 + 3 ] = fc.r;
  77. colors[ i3 + 4 ] = fc.g;
  78. colors[ i3 + 5 ] = fc.b;
  79. colors[ i3 + 6 ] = fc.r;
  80. colors[ i3 + 7 ] = fc.g;
  81. colors[ i3 + 8 ] = fc.b;
  82. } else if ( vertexColors === THREE.VertexColors ) {
  83. var vca = face.vertexColors[ 0 ];
  84. var vcb = face.vertexColors[ 1 ];
  85. var vcc = face.vertexColors[ 2 ];
  86. colors[ i3 ] = vca.r;
  87. colors[ i3 + 1 ] = vca.g;
  88. colors[ i3 + 2 ] = vca.b;
  89. colors[ i3 + 3 ] = vcb.r;
  90. colors[ i3 + 4 ] = vcb.g;
  91. colors[ i3 + 5 ] = vcb.b;
  92. colors[ i3 + 6 ] = vcc.r;
  93. colors[ i3 + 7 ] = vcc.g;
  94. colors[ i3 + 8 ] = vcc.b;
  95. }
  96. if ( hasFaceVertexUv === true ) {
  97. var uva = faceVertexUvs[ 0 ][ i ][ 0 ];
  98. var uvb = faceVertexUvs[ 0 ][ i ][ 1 ];
  99. var uvc = faceVertexUvs[ 0 ][ i ][ 2 ];
  100. uvs[ i2 ] = uva.x;
  101. uvs[ i2 + 1 ] = uva.y;
  102. uvs[ i2 + 2 ] = uvb.x;
  103. uvs[ i2 + 3 ] = uvb.y;
  104. uvs[ i2 + 4 ] = uvc.x;
  105. uvs[ i2 + 5 ] = uvc.y;
  106. }
  107. i3 += 9;
  108. i2 += 6;
  109. if ( face instanceof THREE.Face4 ) {
  110. var d = vertices[ face.d ];
  111. positions[ i3 ] = a.x;
  112. positions[ i3 + 1 ] = a.y;
  113. positions[ i3 + 2 ] = a.z;
  114. positions[ i3 + 3 ] = c.x;
  115. positions[ i3 + 4 ] = c.y;
  116. positions[ i3 + 5 ] = c.z;
  117. positions[ i3 + 6 ] = d.x;
  118. positions[ i3 + 7 ] = d.y;
  119. positions[ i3 + 8 ] = d.z;
  120. var nd = face.vertexNormals[ 3 ];
  121. normals[ i3 ] = na.x;
  122. normals[ i3 + 1 ] = na.y;
  123. normals[ i3 + 2 ] = na.z;
  124. normals[ i3 + 3 ] = nc.x;
  125. normals[ i3 + 4 ] = nc.y;
  126. normals[ i3 + 5 ] = nc.z;
  127. normals[ i3 + 6 ] = nd.x;
  128. normals[ i3 + 7 ] = nd.y;
  129. normals[ i3 + 8 ] = nd.z;
  130. if ( vertexColors === THREE.FaceColors ) {
  131. colors[ i3 ] = fc.r;
  132. colors[ i3 + 1 ] = fc.g;
  133. colors[ i3 + 2 ] = fc.b;
  134. colors[ i3 + 3 ] = fc.r;
  135. colors[ i3 + 4 ] = fc.g;
  136. colors[ i3 + 5 ] = fc.b;
  137. colors[ i3 + 6 ] = fc.r;
  138. colors[ i3 + 7 ] = fc.g;
  139. colors[ i3 + 8 ] = fc.b;
  140. } else if ( vertexColors === THREE.VertexColors ) {
  141. var vcd = face.vertexColors[ 3 ];
  142. colors[ i3 ] = vca.r;
  143. colors[ i3 + 1 ] = vca.g;
  144. colors[ i3 + 2 ] = vca.b;
  145. colors[ i3 + 3 ] = vcc.r;
  146. colors[ i3 + 4 ] = vcc.g;
  147. colors[ i3 + 5 ] = vcc.b;
  148. colors[ i3 + 6 ] = vcd.r;
  149. colors[ i3 + 7 ] = vcd.g;
  150. colors[ i3 + 8 ] = vcd.b;
  151. }
  152. if ( hasFaceVertexUv === true ) {
  153. var uvd = faceVertexUvs[ 0 ][ i ][ 3 ];
  154. uvs[ i2 ] = uva.x;
  155. uvs[ i2 + 1 ] = uva.y;
  156. uvs[ i2 + 2 ] = uvc.x;
  157. uvs[ i2 + 3 ] = uvc.y;
  158. uvs[ i2 + 4 ] = uvd.x;
  159. uvs[ i2 + 5 ] = uvd.y;
  160. }
  161. i3 += 9;
  162. i2 += 6;
  163. }
  164. }
  165. bufferGeometry.computeBoundingSphere();
  166. return bufferGeometry;
  167. }
  168. }