BufferGeometryUtils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. if ( geometry instanceof THREE.BufferGeometry ) {
  8. return geometry;
  9. }
  10. settings = settings || { 'vertexColors': THREE.NoColors };
  11. var vertices = geometry.vertices;
  12. var faces = geometry.faces;
  13. var faceVertexUvs = geometry.faceVertexUvs;
  14. var vertexColors = settings.vertexColors;
  15. var hasFaceVertexUv = faceVertexUvs[ 0 ].length > 0;
  16. var hasFaceVertexNormals = faces[ 0 ].vertexNormals.length == 3;
  17. var bufferGeometry = new THREE.BufferGeometry();
  18. bufferGeometry.attributes = {
  19. position: {
  20. itemSize: 3,
  21. array: new Float32Array( faces.length * 3 * 3 )
  22. },
  23. normal: {
  24. itemSize: 3,
  25. array: new Float32Array( faces.length * 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( faces.length * 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( faces.length * 3 * 2 )
  41. };
  42. var uvs = bufferGeometry.attributes.uv.array;
  43. }
  44. for ( var i = 0, i2 = 0, i3 = 0; i < faces.length; i ++, i2 += 6, i3 += 9 ) {
  45. var face = faces[ i ];
  46. var a = vertices[ face.a ];
  47. var b = vertices[ face.b ];
  48. var c = vertices[ face.c ];
  49. positions[ i3 ] = a.x;
  50. positions[ i3 + 1 ] = a.y;
  51. positions[ i3 + 2 ] = a.z;
  52. positions[ i3 + 3 ] = b.x;
  53. positions[ i3 + 4 ] = b.y;
  54. positions[ i3 + 5 ] = b.z;
  55. positions[ i3 + 6 ] = c.x;
  56. positions[ i3 + 7 ] = c.y;
  57. positions[ i3 + 8 ] = c.z;
  58. if ( hasFaceVertexNormals === true ) {
  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. } else {
  72. var n = face.normal;
  73. normals[ i3 ] = n.x;
  74. normals[ i3 + 1 ] = n.y;
  75. normals[ i3 + 2 ] = n.z;
  76. normals[ i3 + 3 ] = n.x;
  77. normals[ i3 + 4 ] = n.y;
  78. normals[ i3 + 5 ] = n.z;
  79. normals[ i3 + 6 ] = n.x;
  80. normals[ i3 + 7 ] = n.y;
  81. normals[ i3 + 8 ] = n.z;
  82. }
  83. if ( vertexColors === THREE.FaceColors ) {
  84. var fc = face.color;
  85. colors[ i3 ] = fc.r;
  86. colors[ i3 + 1 ] = fc.g;
  87. colors[ i3 + 2 ] = fc.b;
  88. colors[ i3 + 3 ] = fc.r;
  89. colors[ i3 + 4 ] = fc.g;
  90. colors[ i3 + 5 ] = fc.b;
  91. colors[ i3 + 6 ] = fc.r;
  92. colors[ i3 + 7 ] = fc.g;
  93. colors[ i3 + 8 ] = fc.b;
  94. } else if ( vertexColors === THREE.VertexColors ) {
  95. var vca = face.vertexColors[ 0 ];
  96. var vcb = face.vertexColors[ 1 ];
  97. var vcc = face.vertexColors[ 2 ];
  98. colors[ i3 ] = vca.r;
  99. colors[ i3 + 1 ] = vca.g;
  100. colors[ i3 + 2 ] = vca.b;
  101. colors[ i3 + 3 ] = vcb.r;
  102. colors[ i3 + 4 ] = vcb.g;
  103. colors[ i3 + 5 ] = vcb.b;
  104. colors[ i3 + 6 ] = vcc.r;
  105. colors[ i3 + 7 ] = vcc.g;
  106. colors[ i3 + 8 ] = vcc.b;
  107. }
  108. if ( hasFaceVertexUv === true ) {
  109. var uva = faceVertexUvs[ 0 ][ i ][ 0 ];
  110. var uvb = faceVertexUvs[ 0 ][ i ][ 1 ];
  111. var uvc = faceVertexUvs[ 0 ][ i ][ 2 ];
  112. uvs[ i2 ] = uva.x;
  113. uvs[ i2 + 1 ] = uva.y;
  114. uvs[ i2 + 2 ] = uvb.x;
  115. uvs[ i2 + 3 ] = uvb.y;
  116. uvs[ i2 + 4 ] = uvc.x;
  117. uvs[ i2 + 5 ] = uvc.y;
  118. }
  119. }
  120. bufferGeometry.computeBoundingSphere();
  121. return bufferGeometry;
  122. }
  123. }