OBJExporter.js 4.9 KB

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