2
0

OBJExporter.js 5.3 KB

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