BufferGeometryUtils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 bufferGeometry = new THREE.BufferGeometry();
  14. bufferGeometry.attributes = {
  15. position: {
  16. itemSize: 3,
  17. array: new Float32Array( faces.length * 3 * 3 )
  18. },
  19. normal: {
  20. itemSize: 3,
  21. array: new Float32Array( faces.length * 3 * 3 )
  22. }
  23. }
  24. var positions = bufferGeometry.attributes.position.array;
  25. var normals = bufferGeometry.attributes.normal.array;
  26. if ( vertexColors !== THREE.NoColors ) {
  27. bufferGeometry.attributes.color = {
  28. itemSize: 3,
  29. array: new Float32Array( faces.length * 3 * 3 )
  30. };
  31. var colors = bufferGeometry.attributes.color.array;
  32. }
  33. if ( hasFaceVertexUv === true ) {
  34. bufferGeometry.attributes.uv = {
  35. itemSize: 2,
  36. array: new Float32Array( faces.length * 3 * 2 )
  37. };
  38. var uvs = bufferGeometry.attributes.uv.array;
  39. }
  40. var i2 = 0, i3 = 0;
  41. for ( var i = 0; i < faces.length; i ++ ) {
  42. var face = faces[ i ];
  43. var a = vertices[ face.a ];
  44. var b = vertices[ face.b ];
  45. var c = vertices[ face.c ];
  46. positions[ i3 ] = a.x;
  47. positions[ i3 + 1 ] = a.y;
  48. positions[ i3 + 2 ] = a.z;
  49. positions[ i3 + 3 ] = b.x;
  50. positions[ i3 + 4 ] = b.y;
  51. positions[ i3 + 5 ] = b.z;
  52. positions[ i3 + 6 ] = c.x;
  53. positions[ i3 + 7 ] = c.y;
  54. positions[ i3 + 8 ] = c.z;
  55. var na = face.vertexNormals[ 0 ];
  56. var nb = face.vertexNormals[ 1 ];
  57. var nc = face.vertexNormals[ 2 ];
  58. normals[ i3 ] = na.x;
  59. normals[ i3 + 1 ] = na.y;
  60. normals[ i3 + 2 ] = na.z;
  61. normals[ i3 + 3 ] = nb.x;
  62. normals[ i3 + 4 ] = nb.y;
  63. normals[ i3 + 5 ] = nb.z;
  64. normals[ i3 + 6 ] = nc.x;
  65. normals[ i3 + 7 ] = nc.y;
  66. normals[ i3 + 8 ] = nc.z;
  67. if ( vertexColors === THREE.FaceColors ) {
  68. var fc = face.color;
  69. colors[ i3 ] = fc.r;
  70. colors[ i3 + 1 ] = fc.g;
  71. colors[ i3 + 2 ] = fc.b;
  72. colors[ i3 + 3 ] = fc.r;
  73. colors[ i3 + 4 ] = fc.g;
  74. colors[ i3 + 5 ] = fc.b;
  75. colors[ i3 + 6 ] = fc.r;
  76. colors[ i3 + 7 ] = fc.g;
  77. colors[ i3 + 8 ] = fc.b;
  78. } else if ( vertexColors === THREE.VertexColors ) {
  79. var vca = face.vertexColors[ 0 ];
  80. var vcb = face.vertexColors[ 1 ];
  81. var vcc = face.vertexColors[ 2 ];
  82. colors[ i3 ] = vca.r;
  83. colors[ i3 + 1 ] = vca.g;
  84. colors[ i3 + 2 ] = vca.b;
  85. colors[ i3 + 3 ] = vcb.r;
  86. colors[ i3 + 4 ] = vcb.g;
  87. colors[ i3 + 5 ] = vcb.b;
  88. colors[ i3 + 6 ] = vcc.r;
  89. colors[ i3 + 7 ] = vcc.g;
  90. colors[ i3 + 8 ] = vcc.b;
  91. }
  92. if ( hasFaceVertexUv === true ) {
  93. var uva = faceVertexUvs[ 0 ][ i ][ 0 ];
  94. var uvb = faceVertexUvs[ 0 ][ i ][ 1 ];
  95. var uvc = faceVertexUvs[ 0 ][ i ][ 2 ];
  96. uvs[ i2 ] = uva.x;
  97. uvs[ i2 + 1 ] = uva.y;
  98. uvs[ i2 + 2 ] = uvb.x;
  99. uvs[ i2 + 3 ] = uvb.y;
  100. uvs[ i2 + 4 ] = uvc.x;
  101. uvs[ i2 + 5 ] = uvc.y;
  102. }
  103. i3 += 9;
  104. i2 += 6;
  105. }
  106. bufferGeometry.computeBoundingSphere();
  107. return bufferGeometry;
  108. }
  109. }