OBJExporter.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 faceVertexKeys = [ "a", "b", "c" ];
  13. var parseMesh = function ( mesh ) {
  14. var nbVertex = 0;
  15. var nbVertexUvs = 0;
  16. var nbNormals = 0;
  17. var geometry = mesh.geometry;
  18. if ( geometry instanceof THREE.BufferGeometry ) {
  19. geometry = new THREE.Geometry().fromBufferGeometry( geometry );
  20. }
  21. if ( geometry instanceof THREE.Geometry ) {
  22. output += 'o ' + mesh.name + '\n';
  23. var vertices = geometry.vertices;
  24. for ( var i = 0, l = vertices.length; i < l; i ++ ) {
  25. var vertex = vertices[ i ].clone();
  26. vertex.applyMatrix4( mesh.matrixWorld );
  27. output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
  28. nbVertex ++;
  29. }
  30. // uvs
  31. var faces = geometry.faces;
  32. var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
  33. var hasVertexUvs = faces.length === faceVertexUvs.length;
  34. if ( hasVertexUvs ) {
  35. for ( var i = 0, l = faceVertexUvs.length; i < l; i ++ ) {
  36. var vertexUvs = faceVertexUvs[ i ];
  37. for ( var j = 0, jl = vertexUvs.length; j < jl; j ++ ) {
  38. var uv = vertexUvs[ j ];
  39. output += 'vt ' + uv.x + ' ' + uv.y + '\n';
  40. nbVertexUvs ++;
  41. }
  42. }
  43. }
  44. // normals
  45. var normalMatrixWorld = new THREE.Matrix3();
  46. normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
  47. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  48. var face = faces[ i ];
  49. var vertexNormals = face.vertexNormals;
  50. if ( vertexNormals.length === 3 ) {
  51. for ( var j = 0, jl = vertexNormals.length; j < jl; j ++ ) {
  52. var normal = vertexNormals[ j ].clone();
  53. normal.applyMatrix3( normalMatrixWorld );
  54. output += 'vn ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\n';
  55. nbNormals ++;
  56. }
  57. } else {
  58. var normal = face.normal.clone();
  59. normal.applyMatrix3( normalMatrixWorld );
  60. for ( var j = 0; j < 3; j ++ ) {
  61. output += 'vn ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\n';
  62. nbNormals ++;
  63. }
  64. }
  65. }
  66. // faces
  67. var indices = [];
  68. for ( var i = 0, j = 1, l = faces.length; i < l; i ++, j += 3 ) {
  69. var face = faces[ i ];
  70. for ( var m = 0; m < 3; m ++ ) {
  71. indices[ m ] = ( indexVertex + face[ faceVertexKeys[ m ] ] + 1 ) + '/' + ( hasVertexUvs ? ( indexVertexUvs + j + m + 1 ) : '' ) + '/' + ( indexNormals + j + m + 1 );
  72. }
  73. output += 'f ' + indices.join( ' ' ) + "\n";
  74. }
  75. } else {
  76. console.warn( 'THREE.OBJExporter.parseMesh(): geometry type unsupported', mesh );
  77. }
  78. // update index
  79. indexVertex += nbVertex;
  80. indexVertexUvs += nbVertexUvs;
  81. indexNormals += nbNormals;
  82. };
  83. var parseLine = function( line ) {
  84. var geometry = line.geometry;
  85. var type = line.type;
  86. if ( geometry instanceof THREE.BufferGeometry ) {
  87. geometry = new THREE.Geometry().fromBufferGeometry( geometry );
  88. }
  89. if ( geometry instanceof THREE.Geometry ) {
  90. output += 'o ' + line.name + '\n';
  91. var vertices = geometry.vertices;
  92. for ( var i = 0, l = vertices.length; i < l; i++ ) {
  93. var vertex = vertices[ i ].clone();
  94. vertex.applyMatrix4( line.matrixWorld );
  95. output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
  96. }
  97. if ( type === 'Line' ) {
  98. output += 'l ';
  99. for ( var j = 1, m = vertices.length; j <= m; j++ ) {
  100. output += j + ' ';
  101. }
  102. output += '\n';
  103. }
  104. if ( type === 'LineSegments' ) {
  105. for ( var j = 1, k = j + 1, m = vertices.length; j < m; j += 2, k = j + 1 ) {
  106. output += 'l ' + j + ' ' + k + '\n';
  107. }
  108. }
  109. } else {
  110. console.warn('THREE.OBJExporter.parseLine(): geometry type unsupported', line);
  111. }
  112. };
  113. object.traverse( function ( child ) {
  114. if ( child instanceof THREE.Mesh ) {
  115. parseMesh( child );
  116. }
  117. if ( child instanceof THREE.Line ) {
  118. parseLine( child );
  119. }
  120. } );
  121. return output;
  122. }
  123. };