GeometryExporter.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.GeometryExporter = function () {};
  5. THREE.GeometryExporter.prototype = {
  6. constructor: THREE.GeometryExporter,
  7. parse: function ( geometry ) {
  8. var output = {
  9. metadata: {
  10. version: 4.0,
  11. type: 'geometry',
  12. generator: 'GeometryExporter'
  13. }
  14. };
  15. var vertices = [];
  16. for ( var i = 0; i < geometry.vertices.length; i ++ ) {
  17. var vertex = geometry.vertices[ i ];
  18. vertices.push( vertex.x, vertex.y, vertex.z );
  19. }
  20. var faces = [];
  21. var normals = [];
  22. var normalsHash = {};
  23. var colors = [];
  24. var colorsHash = {};
  25. var uvs = [];
  26. var uvsHash = {};
  27. for ( var i = 0; i < geometry.faces.length; i ++ ) {
  28. var face = geometry.faces[ i ];
  29. var hasMaterial = false; // face.materialIndex !== undefined;
  30. var hasFaceUv = false; // deprecated
  31. var hasFaceVertexUv = geometry.faceVertexUvs[ 0 ].length > 0;
  32. var hasFaceNormal = face.normal.length() > 0;
  33. var hasFaceVertexNormal = face.vertexNormals.length > 0;
  34. var hasFaceColor = face.color.r !== 1 || face.color.g !== 1 || face.color.b !== 1;
  35. var hasFaceVertexColor = face.vertexColors.length > 0;
  36. var faceType = 0;
  37. faceType = setBit( faceType, 0, 0 );
  38. faceType = setBit( faceType, 1, hasMaterial );
  39. faceType = setBit( faceType, 2, hasFaceUv );
  40. faceType = setBit( faceType, 3, hasFaceVertexUv );
  41. faceType = setBit( faceType, 4, hasFaceNormal );
  42. faceType = setBit( faceType, 5, hasFaceVertexNormal );
  43. faceType = setBit( faceType, 6, hasFaceColor );
  44. faceType = setBit( faceType, 7, hasFaceVertexColor );
  45. faces.push( faceType );
  46. faces.push( face.a, face.b, face.c );
  47. /*
  48. if ( hasMaterial ) {
  49. faces.push( face.materialIndex );
  50. }
  51. */
  52. if ( hasFaceVertexUv ) {
  53. var faceVertexUvs = geometry.faceVertexUvs[ 0 ][ i ];
  54. faces.push(
  55. getUvIndex( faceVertexUvs[ 0 ] ),
  56. getUvIndex( faceVertexUvs[ 1 ] ),
  57. getUvIndex( faceVertexUvs[ 2 ] )
  58. );
  59. }
  60. if ( hasFaceNormal ) {
  61. faces.push( getNormalIndex( face.normal ) );
  62. }
  63. if ( hasFaceVertexNormal ) {
  64. var vertexNormals = face.vertexNormals;
  65. faces.push(
  66. getNormalIndex( vertexNormals[ 0 ] ),
  67. getNormalIndex( vertexNormals[ 1 ] ),
  68. getNormalIndex( vertexNormals[ 2 ] )
  69. );
  70. }
  71. if ( hasFaceColor ) {
  72. faces.push( getColorIndex( face.color ) );
  73. }
  74. if ( hasFaceVertexColor ) {
  75. var vertexColors = face.vertexColors;
  76. faces.push(
  77. getColorIndex( vertexColors[ 0 ] ),
  78. getColorIndex( vertexColors[ 1 ] ),
  79. getColorIndex( vertexColors[ 2 ] )
  80. );
  81. }
  82. }
  83. function setBit( value, position, enabled ) {
  84. return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position) );
  85. }
  86. function getNormalIndex( normal ) {
  87. var hash = normal.x.toString() + normal.y.toString() + normal.z.toString();
  88. if ( normalsHash[ hash ] !== undefined ) {
  89. return normalsHash[ hash ];
  90. }
  91. normalsHash[ hash ] = normals.length / 3;
  92. normals.push( normal.x, normal.y, normal.z );
  93. return normalsHash[ hash ];
  94. }
  95. function getColorIndex( color ) {
  96. var hash = color.r.toString() + color.g.toString() + color.b.toString();
  97. if ( colorsHash[ hash ] !== undefined ) {
  98. return colorsHash[ hash ];
  99. }
  100. colorsHash[ hash ] = colors.length;
  101. colors.push( color.getHex() );
  102. return colorsHash[ hash ];
  103. }
  104. function getUvIndex( uv ) {
  105. var hash = uv.x.toString() + uv.y.toString();
  106. if ( uvsHash[ hash ] !== undefined ) {
  107. return uvsHash[ hash ];
  108. }
  109. uvsHash[ hash ] = uvs.length / 2;
  110. uvs.push( uv.x, uv.y );
  111. return uvsHash[ hash ];
  112. }
  113. output.vertices = vertices;
  114. output.normals = normals;
  115. if ( colors.length > 0 ) output.colors = colors;
  116. if ( uvs.length > 0 ) output.uvs = [ uvs ]; // temporal backward compatibility
  117. output.faces = faces;
  118. //
  119. return output;
  120. }
  121. };