OBJExporter.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 parseMesh = function ( mesh ) {
  13. var nbVertex = 0;
  14. var nbVertexUvs = 0;
  15. var nbNormals = 0;
  16. var geometry = mesh.geometry;
  17. if ( geometry instanceof THREE.Geometry ) {
  18. output += 'o ' + mesh.name + '\n';
  19. var vertices = geometry.vertices;
  20. for ( var i = 0, l = vertices.length; i < l; i ++ ) {
  21. var vertex = vertices[ i ].clone();
  22. vertex.applyMatrix4( mesh.matrixWorld );
  23. output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
  24. nbVertex ++;
  25. }
  26. // uvs
  27. var faces = geometry.faces;
  28. var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
  29. var hasVertexUvs = faces.length === faceVertexUvs.length;
  30. if ( hasVertexUvs ) {
  31. for ( var i = 0, l = faceVertexUvs.length; i < l; i ++ ) {
  32. var vertexUvs = faceVertexUvs[ i ];
  33. for ( var j = 0, jl = vertexUvs.length; j < jl; j ++ ) {
  34. var uv = vertexUvs[ j ];
  35. output += 'vt ' + uv.x + ' ' + uv.y + '\n';
  36. nbVertexUvs ++;
  37. }
  38. }
  39. }
  40. // normals
  41. var normalMatrixWorld = new THREE.Matrix3();
  42. normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
  43. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  44. var face = faces[ i ];
  45. var vertexNormals = face.vertexNormals;
  46. if ( vertexNormals.length === 3 ) {
  47. for ( var j = 0, jl = vertexNormals.length; j < jl; j ++ ) {
  48. var normal = vertexNormals[ j ].clone();
  49. normal.applyMatrix3( normalMatrixWorld );
  50. output += 'vn ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\n';
  51. nbNormals ++;
  52. }
  53. } else {
  54. var normal = face.normal.clone();
  55. normal.applyMatrix3( normalMatrixWorld );
  56. for ( var j = 0; j < 3; j ++ ) {
  57. output += 'vn ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\n';
  58. nbNormals ++;
  59. }
  60. }
  61. }
  62. // faces
  63. for ( var i = 0, j = 1, l = faces.length; i < l; i ++, j += 3 ) {
  64. var face = faces[ i ];
  65. output += 'f ';
  66. output += ( indexVertex + face.a + 1 ) + '/' + ( hasVertexUvs ? ( indexVertexUvs + j ) : '' ) + '/' + ( indexNormals + j ) + ' ';
  67. output += ( indexVertex + face.b + 1 ) + '/' + ( hasVertexUvs ? ( indexVertexUvs + j + 1 ) : '' ) + '/' + ( indexNormals + j + 1 ) + ' ';
  68. output += ( indexVertex + face.c + 1 ) + '/' + ( hasVertexUvs ? ( indexVertexUvs + j + 2 ) : '' ) + '/' + ( indexNormals + j + 2 ) + '\n';
  69. }
  70. } else {
  71. console.warn( 'THREE.OBJExporter.parseMesh(): geometry type unsupported', mesh );
  72. // TODO: Support only BufferGeometry and use use setFromObject()
  73. }
  74. // update index
  75. indexVertex += nbVertex;
  76. indexVertexUvs += nbVertexUvs;
  77. indexNormals += nbNormals;
  78. };
  79. object.traverse( function ( child ) {
  80. if ( child instanceof THREE.Mesh ) parseMesh( child );
  81. } );
  82. return output;
  83. }
  84. };