OBJExporter.js 5.7 KB

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