OBJExporter.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. ( function () {
  2. class OBJExporter {
  3. parse( object ) {
  4. let output = '';
  5. let indexVertex = 0;
  6. let indexVertexUvs = 0;
  7. let indexNormals = 0;
  8. const vertex = new THREE.Vector3();
  9. const color = new THREE.Color();
  10. const normal = new THREE.Vector3();
  11. const uv = new THREE.Vector2();
  12. const face = [];
  13. function parseMesh( mesh ) {
  14. let nbVertex = 0;
  15. let nbNormals = 0;
  16. let nbVertexUvs = 0;
  17. const geometry = mesh.geometry;
  18. const normalMatrixWorld = new THREE.Matrix3();
  19. if ( geometry.isBufferGeometry !== true ) {
  20. throw new Error( 'THREE.OBJExporter: Geometry is not of type THREE.BufferGeometry.' );
  21. } // shortcuts
  22. const vertices = geometry.getAttribute( 'position' );
  23. const normals = geometry.getAttribute( 'normal' );
  24. const uvs = geometry.getAttribute( 'uv' );
  25. const indices = geometry.getIndex(); // name of the mesh object
  26. output += 'o ' + mesh.name + '\n'; // name of the mesh material
  27. if ( mesh.material && mesh.material.name ) {
  28. output += 'usemtl ' + mesh.material.name + '\n';
  29. } // vertices
  30. if ( vertices !== undefined ) {
  31. for ( let i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
  32. vertex.x = vertices.getX( i );
  33. vertex.y = vertices.getY( i );
  34. vertex.z = vertices.getZ( i ); // transform the vertex to world space
  35. vertex.applyMatrix4( mesh.matrixWorld ); // transform the vertex to export format
  36. output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
  37. }
  38. } // uvs
  39. if ( uvs !== undefined ) {
  40. for ( let i = 0, l = uvs.count; i < l; i ++, nbVertexUvs ++ ) {
  41. uv.x = uvs.getX( i );
  42. uv.y = uvs.getY( i ); // transform the uv to export format
  43. output += 'vt ' + uv.x + ' ' + uv.y + '\n';
  44. }
  45. } // normals
  46. if ( normals !== undefined ) {
  47. normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
  48. for ( let i = 0, l = normals.count; i < l; i ++, nbNormals ++ ) {
  49. normal.x = normals.getX( i );
  50. normal.y = normals.getY( i );
  51. normal.z = normals.getZ( i ); // transform the normal to world space
  52. normal.applyMatrix3( normalMatrixWorld ).normalize(); // transform the normal to export format
  53. output += 'vn ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\n';
  54. }
  55. } // faces
  56. if ( indices !== null ) {
  57. for ( let i = 0, l = indices.count; i < l; i += 3 ) {
  58. for ( let m = 0; m < 3; m ++ ) {
  59. const j = indices.getX( i + m ) + 1;
  60. face[ m ] = indexVertex + j + ( normals || uvs ? '/' + ( uvs ? indexVertexUvs + j : '' ) + ( normals ? '/' + ( indexNormals + j ) : '' ) : '' );
  61. } // transform the face to export format
  62. output += 'f ' + face.join( ' ' ) + '\n';
  63. }
  64. } else {
  65. for ( let i = 0, l = vertices.count; i < l; i += 3 ) {
  66. for ( let m = 0; m < 3; m ++ ) {
  67. const j = i + m + 1;
  68. face[ m ] = indexVertex + j + ( normals || uvs ? '/' + ( uvs ? indexVertexUvs + j : '' ) + ( normals ? '/' + ( indexNormals + j ) : '' ) : '' );
  69. } // transform the face to export format
  70. output += 'f ' + face.join( ' ' ) + '\n';
  71. }
  72. } // update index
  73. indexVertex += nbVertex;
  74. indexVertexUvs += nbVertexUvs;
  75. indexNormals += nbNormals;
  76. }
  77. function parseLine( line ) {
  78. let nbVertex = 0;
  79. const geometry = line.geometry;
  80. const type = line.type;
  81. if ( geometry.isBufferGeometry !== true ) {
  82. throw new Error( 'THREE.OBJExporter: Geometry is not of type THREE.BufferGeometry.' );
  83. } // shortcuts
  84. const vertices = geometry.getAttribute( 'position' ); // name of the line object
  85. output += 'o ' + line.name + '\n';
  86. if ( vertices !== undefined ) {
  87. for ( let i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
  88. vertex.x = vertices.getX( i );
  89. vertex.y = vertices.getY( i );
  90. vertex.z = vertices.getZ( i ); // transform the vertex to world space
  91. vertex.applyMatrix4( line.matrixWorld ); // transform the vertex to export format
  92. output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
  93. }
  94. }
  95. if ( type === 'Line' ) {
  96. output += 'l ';
  97. for ( let j = 1, l = vertices.count; j <= l; j ++ ) {
  98. output += indexVertex + j + ' ';
  99. }
  100. output += '\n';
  101. }
  102. if ( type === 'LineSegments' ) {
  103. for ( let j = 1, k = j + 1, l = vertices.count; j < l; j += 2, k = j + 1 ) {
  104. output += 'l ' + ( indexVertex + j ) + ' ' + ( indexVertex + k ) + '\n';
  105. }
  106. } // update index
  107. indexVertex += nbVertex;
  108. }
  109. function parsePoints( points ) {
  110. let nbVertex = 0;
  111. const geometry = points.geometry;
  112. if ( geometry.isBufferGeometry !== true ) {
  113. throw new Error( 'THREE.OBJExporter: Geometry is not of type THREE.BufferGeometry.' );
  114. }
  115. const vertices = geometry.getAttribute( 'position' );
  116. const colors = geometry.getAttribute( 'color' );
  117. output += 'o ' + points.name + '\n';
  118. if ( vertices !== undefined ) {
  119. for ( let i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
  120. vertex.fromBufferAttribute( vertices, i );
  121. vertex.applyMatrix4( points.matrixWorld );
  122. output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z;
  123. if ( colors !== undefined ) {
  124. color.fromBufferAttribute( colors, i );
  125. output += ' ' + color.r + ' ' + color.g + ' ' + color.b;
  126. }
  127. output += '\n';
  128. }
  129. }
  130. output += 'p ';
  131. for ( let j = 1, l = vertices.count; j <= l; j ++ ) {
  132. output += indexVertex + j + ' ';
  133. }
  134. output += '\n'; // update index
  135. indexVertex += nbVertex;
  136. }
  137. object.traverse( function ( child ) {
  138. if ( child.isMesh === true ) {
  139. parseMesh( child );
  140. }
  141. if ( child.isLine === true ) {
  142. parseLine( child );
  143. }
  144. if ( child.isPoints === true ) {
  145. parsePoints( child );
  146. }
  147. } );
  148. return output;
  149. }
  150. }
  151. THREE.OBJExporter = OBJExporter;
  152. } )();