OBJExporter.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import {
  2. Color,
  3. ColorManagement,
  4. Matrix3,
  5. SRGBColorSpace,
  6. Vector2,
  7. Vector3
  8. } from 'three';
  9. /**
  10. * An exporter for OBJ.
  11. *
  12. * `OBJExporter` is not able to export material data into MTL files so only geometry data are supported.
  13. *
  14. * ```js
  15. * const exporter = new OBJExporter();
  16. * const data = exporter.parse( scene );
  17. * ```
  18. *
  19. * @three_import import { OBJExporter } from 'three/addons/exporters/OBJExporter.js';
  20. */
  21. class OBJExporter {
  22. /**
  23. * Parses the given 3D object and generates the OBJ output.
  24. *
  25. * If the 3D object is composed of multiple children and geometry, they are merged into a single mesh in the file.
  26. *
  27. * @param {Object3D} object - The 3D object to export.
  28. * @return {string} The exported OBJ.
  29. */
  30. parse( object ) {
  31. let output = '';
  32. let indexVertex = 0;
  33. let indexVertexUvs = 0;
  34. let indexNormals = 0;
  35. const vertex = new Vector3();
  36. const color = new Color();
  37. const normal = new Vector3();
  38. const uv = new Vector2();
  39. const face = [];
  40. function parseMesh( mesh ) {
  41. let nbVertex = 0;
  42. let nbNormals = 0;
  43. let nbVertexUvs = 0;
  44. const geometry = mesh.geometry;
  45. const normalMatrixWorld = new Matrix3();
  46. // shortcuts
  47. const vertices = geometry.getAttribute( 'position' );
  48. const normals = geometry.getAttribute( 'normal' );
  49. const uvs = geometry.getAttribute( 'uv' );
  50. const indices = geometry.getIndex();
  51. // name of the mesh object
  52. output += 'o ' + mesh.name + '\n';
  53. // name of the mesh material
  54. if ( mesh.material && mesh.material.name ) {
  55. output += 'usemtl ' + mesh.material.name + '\n';
  56. }
  57. // vertices
  58. if ( vertices !== undefined ) {
  59. for ( let i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
  60. vertex.fromBufferAttribute( vertices, i );
  61. // transform the vertex to world space
  62. vertex.applyMatrix4( mesh.matrixWorld );
  63. // transform the vertex to export format
  64. output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
  65. }
  66. }
  67. // uvs
  68. if ( uvs !== undefined ) {
  69. for ( let i = 0, l = uvs.count; i < l; i ++, nbVertexUvs ++ ) {
  70. uv.fromBufferAttribute( uvs, i );
  71. // transform the uv to export format
  72. output += 'vt ' + uv.x + ' ' + uv.y + '\n';
  73. }
  74. }
  75. // normals
  76. if ( normals !== undefined ) {
  77. normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
  78. for ( let i = 0, l = normals.count; i < l; i ++, nbNormals ++ ) {
  79. normal.fromBufferAttribute( normals, i );
  80. // transform the normal to world space
  81. normal.applyMatrix3( normalMatrixWorld ).normalize();
  82. // transform the normal to export format
  83. output += 'vn ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\n';
  84. }
  85. }
  86. // faces
  87. if ( indices !== null ) {
  88. for ( let i = 0, l = indices.count; i < l; i += 3 ) {
  89. for ( let m = 0; m < 3; m ++ ) {
  90. const j = indices.getX( i + m ) + 1;
  91. face[ m ] = ( indexVertex + j ) + ( normals || uvs ? '/' + ( uvs ? ( indexVertexUvs + j ) : '' ) + ( normals ? '/' + ( indexNormals + j ) : '' ) : '' );
  92. }
  93. // transform the face to export format
  94. output += 'f ' + face.join( ' ' ) + '\n';
  95. }
  96. } else {
  97. for ( let i = 0, l = vertices.count; i < l; i += 3 ) {
  98. for ( let m = 0; m < 3; m ++ ) {
  99. const j = i + m + 1;
  100. face[ m ] = ( indexVertex + j ) + ( normals || uvs ? '/' + ( uvs ? ( indexVertexUvs + j ) : '' ) + ( normals ? '/' + ( indexNormals + j ) : '' ) : '' );
  101. }
  102. // transform the face to export format
  103. output += 'f ' + face.join( ' ' ) + '\n';
  104. }
  105. }
  106. // update index
  107. indexVertex += nbVertex;
  108. indexVertexUvs += nbVertexUvs;
  109. indexNormals += nbNormals;
  110. }
  111. function parseLine( line ) {
  112. let nbVertex = 0;
  113. const geometry = line.geometry;
  114. const type = line.type;
  115. // shortcuts
  116. const vertices = geometry.getAttribute( 'position' );
  117. // name of the line object
  118. output += 'o ' + line.name + '\n';
  119. if ( vertices !== undefined ) {
  120. for ( let i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
  121. vertex.fromBufferAttribute( vertices, i );
  122. // transform the vertex to world space
  123. vertex.applyMatrix4( line.matrixWorld );
  124. // transform the vertex to export format
  125. output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
  126. }
  127. }
  128. if ( type === 'Line' ) {
  129. output += 'l ';
  130. for ( let j = 1, l = vertices.count; j <= l; j ++ ) {
  131. output += ( indexVertex + j ) + ' ';
  132. }
  133. output += '\n';
  134. }
  135. if ( type === 'LineSegments' ) {
  136. for ( let j = 1, k = j + 1, l = vertices.count; j < l; j += 2, k = j + 1 ) {
  137. output += 'l ' + ( indexVertex + j ) + ' ' + ( indexVertex + k ) + '\n';
  138. }
  139. }
  140. // update index
  141. indexVertex += nbVertex;
  142. }
  143. function parsePoints( points ) {
  144. let nbVertex = 0;
  145. const geometry = points.geometry;
  146. const vertices = geometry.getAttribute( 'position' );
  147. const colors = geometry.getAttribute( 'color' );
  148. output += 'o ' + points.name + '\n';
  149. if ( vertices !== undefined ) {
  150. for ( let i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
  151. vertex.fromBufferAttribute( vertices, i );
  152. vertex.applyMatrix4( points.matrixWorld );
  153. output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z;
  154. if ( colors !== undefined ) {
  155. color.fromBufferAttribute( colors, i );
  156. ColorManagement.fromWorkingColorSpace( color, SRGBColorSpace );
  157. output += ' ' + color.r + ' ' + color.g + ' ' + color.b;
  158. }
  159. output += '\n';
  160. }
  161. output += 'p ';
  162. for ( let j = 1, l = vertices.count; j <= l; j ++ ) {
  163. output += ( indexVertex + j ) + ' ';
  164. }
  165. output += '\n';
  166. }
  167. // update index
  168. indexVertex += nbVertex;
  169. }
  170. object.traverse( function ( child ) {
  171. if ( child.isMesh === true ) {
  172. parseMesh( child );
  173. }
  174. if ( child.isLine === true ) {
  175. parseLine( child );
  176. }
  177. if ( child.isPoints === true ) {
  178. parsePoints( child );
  179. }
  180. } );
  181. return output;
  182. }
  183. }
  184. export { OBJExporter };