OBJExporter.js 5.2 KB

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