OBJExporter.js 5.8 KB

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