OBJExporter.js 5.2 KB

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