GeometryExporter.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 isTriangle = face instanceof THREE.Face3;
  30. var hasMaterial = false; // face.materialIndex !== undefined;
  31. var hasFaceUv = false; // deprecated
  32. var hasFaceVertexUv = geometry.faceVertexUvs[ 0 ].length > 0;
  33. var hasFaceNormal = face.normal.length() > 0;
  34. var hasFaceVertexNormal = face.vertexNormals.length > 0;
  35. var hasFaceColor = face.color.r !== 1 || face.color.g !== 1 || face.color.b !== 1;
  36. var hasFaceVertexColor = face.vertexColors.length > 0;
  37. var faceType = 0;
  38. faceType = setBit( faceType, 0, ! isTriangle );
  39. faceType = setBit( faceType, 1, hasMaterial );
  40. faceType = setBit( faceType, 2, hasFaceUv );
  41. faceType = setBit( faceType, 3, hasFaceVertexUv );
  42. faceType = setBit( faceType, 4, hasFaceNormal );
  43. faceType = setBit( faceType, 5, hasFaceVertexNormal );
  44. faceType = setBit( faceType, 6, hasFaceColor );
  45. faceType = setBit( faceType, 7, hasFaceVertexColor );
  46. faces.push( faceType );
  47. if ( isTriangle ) {
  48. faces.push( face.a, face.b, face.c );
  49. } else {
  50. faces.push( face.a, face.b, face.c, face.d );
  51. }
  52. /*
  53. if ( hasMaterial ) {
  54. faces.push( face.materialIndex );
  55. }
  56. */
  57. if ( hasFaceVertexUv ) {
  58. var faceVertexUvs = geometry.faceVertexUvs[ 0 ][ i ];
  59. if ( isTriangle ) {
  60. faces.push(
  61. getUvIndex( faceVertexUvs[ 0 ] ),
  62. getUvIndex( faceVertexUvs[ 1 ] ),
  63. getUvIndex( faceVertexUvs[ 2 ] )
  64. );
  65. } else {
  66. faces.push(
  67. getUvIndex( faceVertexUvs[ 0 ] ),
  68. getUvIndex( faceVertexUvs[ 1 ] ),
  69. getUvIndex( faceVertexUvs[ 2 ] ),
  70. getUvIndex( faceVertexUvs[ 3 ] )
  71. );
  72. }
  73. }
  74. if ( hasFaceNormal ) {
  75. faces.push( getNormalIndex( face.normal ) );
  76. }
  77. if ( hasFaceVertexNormal ) {
  78. var vertexNormals = face.vertexNormals;
  79. if ( isTriangle ) {
  80. faces.push(
  81. getNormalIndex( vertexNormals[ 0 ] ),
  82. getNormalIndex( vertexNormals[ 1 ] ),
  83. getNormalIndex( vertexNormals[ 2 ] )
  84. );
  85. } else {
  86. faces.push(
  87. getNormalIndex( vertexNormals[ 0 ] ),
  88. getNormalIndex( vertexNormals[ 1 ] ),
  89. getNormalIndex( vertexNormals[ 2 ] ),
  90. getNormalIndex( vertexNormals[ 3 ] )
  91. );
  92. }
  93. }
  94. if ( hasFaceColor ) {
  95. faces.push( getColorIndex( face.color ) );
  96. }
  97. if ( hasFaceVertexColor ) {
  98. var vertexColors = face.vertexColors;
  99. if ( isTriangle ) {
  100. faces.push(
  101. getColorIndex( vertexColors[ 0 ] ),
  102. getColorIndex( vertexColors[ 1 ] ),
  103. getColorIndex( vertexColors[ 2 ] )
  104. );
  105. } else {
  106. faces.push(
  107. getColorIndex( vertexColors[ 0 ] ),
  108. getColorIndex( vertexColors[ 1 ] ),
  109. getColorIndex( vertexColors[ 2 ] ),
  110. getColorIndex( vertexColors[ 3 ] )
  111. );
  112. }
  113. }
  114. }
  115. function setBit( value, position, enabled ) {
  116. return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position) );
  117. }
  118. function getNormalIndex( normal ) {
  119. var hash = normal.x.toString() + normal.y.toString() + normal.z.toString();
  120. if ( normalsHash[ hash ] !== undefined ) {
  121. return normalsHash[ hash ];
  122. }
  123. normalsHash[ hash ] = normals.length / 3;
  124. normals.push( normal.x, normal.y, normal.z );
  125. return normalsHash[ hash ];
  126. }
  127. function getColorIndex( color ) {
  128. var hash = color.r.toString() + color.g.toString() + color.b.toString();
  129. if ( colorsHash[ hash ] !== undefined ) {
  130. return colorsHash[ hash ];
  131. }
  132. colorsHash[ hash ] = colors.length;
  133. colors.push( color.getHex() );
  134. return colorsHash[ hash ];
  135. }
  136. function getUvIndex( uv ) {
  137. var hash = uv.x.toString() + uv.y.toString();
  138. if ( uvsHash[ hash ] !== undefined ) {
  139. return uvsHash[ hash ];
  140. }
  141. uvsHash[ hash ] = uvs.length / 2;
  142. uvs.push( uv.x, uv.y );
  143. return uvsHash[ hash ];
  144. }
  145. output.vertices = vertices;
  146. output.normals = normals;
  147. if ( colors.length > 0 ) output.colors = colors;
  148. if ( uvs.length > 0 ) output.uvs = [ uvs ]; // temporal backward compatibility
  149. output.faces = faces;
  150. //
  151. return output;
  152. }
  153. };