OBJExporter.js 5.4 KB

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