OBJExporter.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. object.traverse( function ( child ) {
  84. if ( child instanceof THREE.Mesh ) parseMesh( child );
  85. } );
  86. return output;
  87. }
  88. };