OBJExporter.js 5.9 KB

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