OBJExporter.js 3.1 KB

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