OBJExporter.js 5.7 KB

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