BufferGeometryUtils.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * @author spite / http://www.clicktorelease.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. THREE.BufferGeometryUtils = {
  6. fromGeometry: function geometryToBufferGeometry( geometry ) {
  7. var vertices = geometry.vertices;
  8. var faces = geometry.faces;
  9. var faceVertexUvs = geometry.faceVertexUvs;
  10. var hasFaceVertexUv = faceVertexUvs[ 0 ].length > 0;
  11. var triangles = 0;
  12. for ( var i = 0; i < faces.length; i ++ ) {
  13. triangles += faces[ i ] instanceof THREE.Face3 ? 1 : 2;
  14. }
  15. console.log( faces.length, triangles );
  16. var bufferGeometry = new THREE.BufferGeometry();
  17. bufferGeometry.attributes = {
  18. position: {
  19. itemSize: 3,
  20. array: new Float32Array( triangles * 3 * 3 )
  21. },
  22. normal: {
  23. itemSize: 3,
  24. array: new Float32Array( triangles * 3 * 3 )
  25. }
  26. /*
  27. color: {
  28. itemSize: 3,
  29. array: new Float32Array( triangles * 3 * 3 )
  30. }*/
  31. }
  32. if ( hasFaceVertexUv === true ) {
  33. bufferGeometry.attributes.uv = {
  34. itemSize: 2,
  35. array: new Float32Array( triangles * 3 * 2 )
  36. };
  37. var uvs = bufferGeometry.attributes.uv.array;
  38. }
  39. var positions = bufferGeometry.attributes.position.array;
  40. var normals = bufferGeometry.attributes.normal.array;
  41. // var colors = bufferGeometry.attributes.color.array;
  42. var i2 = 0, i3 = 0;
  43. for ( var i = 0; i < faces.length; i ++ ) {
  44. var face = faces[ i ];
  45. var a = vertices[ face.a ];
  46. var b = vertices[ face.b ];
  47. var c = vertices[ face.c ];
  48. positions[ i3 ] = a.x;
  49. positions[ i3 + 1 ] = a.y;
  50. positions[ i3 + 2 ] = a.z;
  51. positions[ i3 + 3 ] = b.x;
  52. positions[ i3 + 4 ] = b.y;
  53. positions[ i3 + 5 ] = b.z;
  54. positions[ i3 + 6 ] = c.x;
  55. positions[ i3 + 7 ] = c.y;
  56. positions[ i3 + 8 ] = c.z;
  57. var na = face.vertexNormals[ 0 ];
  58. var nb = face.vertexNormals[ 1 ];
  59. var nc = face.vertexNormals[ 2 ];
  60. normals[ i3 ] = na.x;
  61. normals[ i3 + 1 ] = na.y;
  62. normals[ i3 + 2 ] = na.z;
  63. normals[ i3 + 3 ] = nb.x;
  64. normals[ i3 + 4 ] = nb.y;
  65. normals[ i3 + 5 ] = nb.z;
  66. normals[ i3 + 6 ] = nc.x;
  67. normals[ i3 + 7 ] = nc.y;
  68. normals[ i3 + 8 ] = nc.z;
  69. if ( hasFaceVertexUv === true ) {
  70. var uva = faceVertexUvs[ 0 ][ i ][ 0 ];
  71. var uvb = faceVertexUvs[ 0 ][ i ][ 1 ];
  72. var uvc = faceVertexUvs[ 0 ][ i ][ 2 ];
  73. uvs[ i2 ] = uva.x;
  74. uvs[ i2 + 1 ] = uva.y;
  75. uvs[ i2 + 2 ] = uvb.x;
  76. uvs[ i2 + 3 ] = uvb.y;
  77. uvs[ i2 + 4 ] = uvc.x;
  78. uvs[ i2 + 5 ] = uvc.y;
  79. }
  80. i3 += 9;
  81. i2 += 6;
  82. if ( face instanceof THREE.Face4 ) {
  83. var d = vertices[ face.d ];
  84. var nd = face.vertexNormals[ 3 ];
  85. positions[ i3 ] = a.x;
  86. positions[ i3 + 1 ] = a.y;
  87. positions[ i3 + 2 ] = a.z;
  88. positions[ i3 + 3 ] = c.x;
  89. positions[ i3 + 4 ] = c.y;
  90. positions[ i3 + 5 ] = c.z;
  91. positions[ i3 + 6 ] = d.x;
  92. positions[ i3 + 7 ] = d.y;
  93. positions[ i3 + 8 ] = d.z;
  94. normals[ i3 ] = na.x;
  95. normals[ i3 + 1 ] = na.y;
  96. normals[ i3 + 2 ] = na.z;
  97. normals[ i3 + 3 ] = nc.x;
  98. normals[ i3 + 4 ] = nc.y;
  99. normals[ i3 + 5 ] = nc.z;
  100. normals[ i3 + 6 ] = nd.x;
  101. normals[ i3 + 7 ] = nd.y;
  102. normals[ i3 + 8 ] = nd.z;
  103. if ( hasFaceVertexUv === true ) {
  104. var uvd = faceVertexUvs[ 0 ][ i ][ 3 ];
  105. uvs[ i2 ] = uva.x;
  106. uvs[ i2 + 1 ] = uva.y;
  107. uvs[ i2 + 2 ] = uvc.x;
  108. uvs[ i2 + 3 ] = uvc.y;
  109. uvs[ i2 + 4 ] = uvd.x;
  110. uvs[ i2 + 5 ] = uvd.y;
  111. }
  112. i3 += 9;
  113. i2 += 6;
  114. }
  115. }
  116. bufferGeometry.computeBoundingSphere();
  117. return bufferGeometry;
  118. }
  119. }