OBJLoader.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.OBJLoader = function () {};
  5. THREE.OBJLoader.prototype = new THREE.Loader();
  6. THREE.OBJLoader.prototype.constructor = THREE.OBJLoader;
  7. THREE.OBJLoader.prototype.load = function ( url, callback ) {
  8. var that = this;
  9. var xhr = new XMLHttpRequest();
  10. xhr.onreadystatechange = function () {
  11. if ( xhr.readyState == 4 ) {
  12. if ( xhr.status == 200 || xhr.status == 0 ) {
  13. callback( that.parse( xhr.responseText ) );
  14. that.onLoadComplete();
  15. } else {
  16. console.error( 'THREE.OBJLoader: Couldn\'t load ' + url + ' (' + xhr.status + ')' );
  17. }
  18. }
  19. };
  20. xhr.open( "GET", url, true );
  21. xhr.send( null );
  22. that.onLoadStart();
  23. };
  24. THREE.OBJLoader.prototype.parse = function ( data ) {
  25. function vertex( a, b, c ) {
  26. return new THREE.Vector3( parseFloat( a ), parseFloat( b ), parseFloat( c ) );
  27. }
  28. function face3( a, b, c ) {
  29. return new THREE.Face3( parseInt( a ) - 1, parseInt( b ) - 1, parseInt( c ) - 1 );
  30. }
  31. function face4( a, b, c, d ) {
  32. return new THREE.Face4( parseInt( a ) - 1, parseInt( b ) - 1, parseInt( c ) - 1, parseInt( d ) - 1 );
  33. }
  34. var objects = [];
  35. var vertices = [];
  36. var pattern, result;
  37. // v float float float
  38. pattern = /v( [\d|\.|\+|\-|e]+)( [\d|\.|\+|\-|e]+)( [\d|\.|\+|\-|e]+)/g;
  39. while ( ( result = pattern.exec( data ) ) != null ) {
  40. // ["v 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
  41. vertices.push( vertex( result[ 1 ], result[ 2 ], result[ 3 ] ) );
  42. }
  43. var data = data.split( '\no ');
  44. for ( var i = 0, l = data.length; i < l; i ++ ) {
  45. var object = data[ i ];
  46. var geometry = new THREE.Geometry();
  47. geometry.vertices = vertices;
  48. // f vertex vertex vertex ...
  49. pattern = /f( [\d]+)( [\d]+)( [\d]+)( [\d]+)?/g;
  50. while ( ( result = pattern.exec( object ) ) != null ) {
  51. // ["f 1 2 3", "1", "2", "3", undefined]
  52. geometry.faces.push(
  53. result[ 4 ] === undefined ?
  54. face3( result[ 1 ], result[ 2 ], result[ 3 ] ) :
  55. face4( result[ 1 ], result[ 2 ], result[ 3 ], result[ 4 ] )
  56. );
  57. }
  58. // f vertex/uv vertex/uv vertex/uv ...
  59. pattern = /f( ([\d]+)\/([\d]+))( ([\d]+)\/([\d]+))( ([\d]+)\/([\d]+))( ([\d]+)\/([\d]+))?/g;
  60. while ( ( result = pattern.exec( object ) ) != null ) {
  61. // ["f 1/1 2/2 3/3", " 1/1", "1", "1", " 2/2", "2", "2", " 3/3", "3", "3", undefined, undefined, undefined]
  62. geometry.faces.push(
  63. result[ 10 ] === undefined ?
  64. face3( result[ 2 ], result[ 5 ], result[ 8 ] ) :
  65. face4( result[ 2 ], result[ 5 ], result[ 8 ], result[ 11 ] )
  66. );
  67. }
  68. // f vertex/uv/normal vertex/uv/normal vertex/uv/normal ...
  69. pattern = /f( ([\d]+)\/([\d]+)\/([\d]+))( ([\d]+)\/([\d]+)\/([\d]+))( ([\d]+)\/([\d]+)\/([\d]+))( ([\d]+)\/([\d]+)\/([\d]+))?/g;
  70. while ( ( result = pattern.exec( object ) ) != null ) {
  71. // ["f 1/1/1 2/2/2 3/3/3", " 1/1/1", "1", "1", "1", " 2/2/2", "2", "2", "2", " 3/3/3", "3", "3", "3", undefined, undefined, undefined, undefined]
  72. geometry.faces.push(
  73. result[ 13 ] === undefined ?
  74. face3( result[ 2 ], result[ 6 ], result[ 10 ] ) :
  75. face4( result[ 2 ], result[ 6 ], result[ 10 ], result[ 14 ] )
  76. );
  77. }
  78. // f vertex//normal vertex//normal vertex//normal ...
  79. pattern = /f( ([\d]+)\/\/([\d]+))( ([\d]+)\/\/([\d]+))( ([\d]+)\/\/([\d]+))( ([\d]+)\/\/([\d]+))?/g;
  80. while ( ( result = pattern.exec( object ) ) != null ) {
  81. // ["f 1//1 2//2 3//3", " 1//1", "1", "1", " 2//2", "2", "2", " 3//3", "3", "3", undefined, undefined, undefined]
  82. geometry.faces.push(
  83. result[ 10 ] === undefined ?
  84. face3( result[ 2 ], result[ 5 ], result[ 8 ] ) :
  85. face4( result[ 2 ], result[ 5 ], result[ 8 ], result[ 11 ] )
  86. );
  87. }
  88. geometry.computeCentroids();
  89. objects.push( new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } ) ) );
  90. }
  91. return objects;
  92. }