GeometryExporter.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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; // geometry.faceUvs[ 0 ].length > 0;
  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. if ( hasFaceUv ) {
  57. var uv = geometry.faceUvs[ 0 ][ i ];
  58. uvs[ 0 ].push( uv.u, uv.v );
  59. }
  60. */
  61. if ( hasFaceVertexUv ) {
  62. var faceVertexUvs = geometry.faceVertexUvs[ 0 ][ i ];
  63. if ( isTriangle ) {
  64. faces.push(
  65. getUvIndex( faceVertexUvs[ 0 ] ),
  66. getUvIndex( faceVertexUvs[ 1 ] ),
  67. getUvIndex( faceVertexUvs[ 2 ] )
  68. );
  69. } else {
  70. faces.push(
  71. getUvIndex( faceVertexUvs[ 0 ] ),
  72. getUvIndex( faceVertexUvs[ 1 ] ),
  73. getUvIndex( faceVertexUvs[ 2 ] ),
  74. getUvIndex( faceVertexUvs[ 3 ] )
  75. );
  76. }
  77. }
  78. if ( hasFaceNormal ) {
  79. faces.push( getNormalIndex( face.normal ) );
  80. }
  81. if ( hasFaceVertexNormal ) {
  82. var vertexNormals = face.vertexNormals;
  83. if ( isTriangle ) {
  84. faces.push(
  85. getNormalIndex( vertexNormals[ 0 ] ),
  86. getNormalIndex( vertexNormals[ 1 ] ),
  87. getNormalIndex( vertexNormals[ 2 ] )
  88. );
  89. } else {
  90. faces.push(
  91. getNormalIndex( vertexNormals[ 0 ] ),
  92. getNormalIndex( vertexNormals[ 1 ] ),
  93. getNormalIndex( vertexNormals[ 2 ] ),
  94. getNormalIndex( vertexNormals[ 3 ] )
  95. );
  96. }
  97. }
  98. if ( hasFaceColor ) {
  99. faces.push( getColorIndex( face.color ) );
  100. }
  101. if ( hasFaceVertexColor ) {
  102. var vertexColors = face.vertexColors;
  103. if ( isTriangle ) {
  104. faces.push(
  105. getColorIndex( vertexColors[ 0 ] ),
  106. getColorIndex( vertexColors[ 1 ] ),
  107. getColorIndex( vertexColors[ 2 ] )
  108. );
  109. } else {
  110. faces.push(
  111. getColorIndex( vertexColors[ 0 ] ),
  112. getColorIndex( vertexColors[ 1 ] ),
  113. getColorIndex( vertexColors[ 2 ] ),
  114. getColorIndex( vertexColors[ 3 ] )
  115. );
  116. }
  117. }
  118. }
  119. function setBit( value, position, enabled ) {
  120. return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position) );
  121. }
  122. function getNormalIndex( normal ) {
  123. var hash = normal.x.toString() + normal.y.toString() + normal.z.toString();
  124. if ( normalsHash[ hash ] !== undefined ) {
  125. return normalsHash[ hash ];
  126. }
  127. normalsHash[ hash ] = normals.length / 3;
  128. normals.push( normal.x, normal.y, normal.z );
  129. return normalsHash[ hash ];
  130. }
  131. function getColorIndex( color ) {
  132. var hash = color.r.toString() + color.g.toString() + color.b.toString();
  133. if ( colorsHash[ hash ] !== undefined ) {
  134. return colorsHash[ hash ];
  135. }
  136. colorsHash[ hash ] = colors.length;
  137. colors.push( color.getHex() );
  138. return colorsHash[ hash ];
  139. }
  140. function getUvIndex( uv ) {
  141. var hash = uv.x.toString() + uv.y.toString();
  142. if ( uvsHash[ hash ] !== undefined ) {
  143. return uvsHash[ hash ];
  144. }
  145. uvsHash[ hash ] = uvs.length / 2;
  146. uvs.push( uv.x, uv.y );
  147. return uvsHash[ hash ];
  148. }
  149. output.vertices = vertices;
  150. output.normals = normals;
  151. if ( colors.length > 0 ) output.colors = colors;
  152. if ( uvs.length > 0 ) output.uvs = [ uvs ]; // temporal backward compatibility
  153. output.faces = faces;
  154. //
  155. return output;
  156. }
  157. };