OBJExporter.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import {
  2. BufferGeometry,
  3. Color,
  4. Geometry,
  5. Line,
  6. Matrix3,
  7. Mesh,
  8. Points,
  9. Vector2,
  10. Vector3
  11. } from "../../../build/three.module.js";
  12. var OBJExporter = function () {};
  13. OBJExporter.prototype = {
  14. constructor: OBJExporter,
  15. parse: function ( object ) {
  16. var output = '';
  17. var indexVertex = 0;
  18. var indexVertexUvs = 0;
  19. var indexNormals = 0;
  20. var vertex = new Vector3();
  21. var color = new Color();
  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. // transform 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. // transform 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. // transform 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. var parsePoints = function ( points ) {
  150. var nbVertex = 0;
  151. var geometry = points.geometry;
  152. if ( geometry instanceof Geometry ) {
  153. geometry = new BufferGeometry().setFromObject( points );
  154. }
  155. if ( geometry instanceof BufferGeometry ) {
  156. var vertices = geometry.getAttribute( 'position' );
  157. var colors = geometry.getAttribute( 'color' );
  158. output += 'o ' + points.name + '\n';
  159. if ( vertices !== undefined ) {
  160. for ( i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
  161. vertex.fromBufferAttribute( vertices, i );
  162. vertex.applyMatrix4( points.matrixWorld );
  163. output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z;
  164. if ( colors !== undefined ) {
  165. color.fromBufferAttribute( colors, i );
  166. output += ' ' + color.r + ' ' + color.g + ' ' + color.b;
  167. }
  168. output += '\n';
  169. }
  170. }
  171. output += 'p ';
  172. for ( j = 1, l = vertices.count; j <= l; j ++ ) {
  173. output += ( indexVertex + j ) + ' ';
  174. }
  175. output += '\n';
  176. } else {
  177. console.warn( 'THREE.OBJExporter.parsePoints(): geometry type unsupported', geometry );
  178. }
  179. // update index
  180. indexVertex += nbVertex;
  181. };
  182. object.traverse( function ( child ) {
  183. if ( child instanceof Mesh ) {
  184. parseMesh( child );
  185. }
  186. if ( child instanceof Line ) {
  187. parseLine( child );
  188. }
  189. if ( child instanceof Points ) {
  190. parsePoints( child );
  191. }
  192. } );
  193. return output;
  194. }
  195. };
  196. export { OBJExporter };