GeometryExporter.js 4.4 KB

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