OBJLoader.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. var geometry = new THREE.Geometry();
  26. function vertex( a, b, c ) {
  27. return new THREE.Vector3( parseFloat( a ), parseFloat( b ), parseFloat( c ) );
  28. }
  29. function face3( a, b, c ) {
  30. return new THREE.Face3( parseInt( a ) - 1, parseInt( b ) - 1, parseInt( c ) - 1 );
  31. }
  32. function face4( a, b, c, d ) {
  33. return new THREE.Face4( parseInt( a ) - 1, parseInt( b ) - 1, parseInt( c ) - 1, parseInt( d ) - 1 );
  34. }
  35. var pattern, result;
  36. // v float float float
  37. // ["v 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
  38. pattern = /v( [\d|\.|\+|\-|e]+)( [\d|\.|\+|\-|e]+)( [\d|\.|\+|\-|e]+)/g;
  39. while ( ( result = pattern.exec( data ) ) != null ) {
  40. geometry.vertices.push( vertex( result[ 1 ], result[ 2 ], result[ 3 ] ) );
  41. }
  42. // f vertex vertex vertex ...
  43. // ["f 1 2 3", "1", "2", "3", undefined]
  44. pattern = /f( [\d]+)( [\d]+)( [\d]+)( [\d]+)?/g;
  45. while ( ( result = pattern.exec( data ) ) != null ) {
  46. geometry.faces.push(
  47. result[ 4 ] === undefined ?
  48. face3( result[ 1 ], result[ 2 ], result[ 3 ] ) :
  49. face4( result[ 1 ], result[ 2 ], result[ 3 ], result[ 4 ] )
  50. );
  51. }
  52. // f vertex/uv vertex/uv vertex/uv ...
  53. // ["f 1/1 2/2 3/3", " 1/1", "1", "1", " 2/2", "2", "2", " 3/3", "3", "3", undefined, undefined, undefined]
  54. pattern = /f( ([\d]+)\/([\d]+))( ([\d]+)\/([\d]+))( ([\d]+)\/([\d]+))( ([\d]+)\/([\d]+))?/g;
  55. while ( ( result = pattern.exec( data ) ) != null ) {
  56. geometry.faces.push(
  57. result[ 10 ] === undefined ?
  58. face3( result[ 2 ], result[ 5 ], result[ 8 ] ) :
  59. face4( result[ 2 ], result[ 5 ], result[ 8 ], result[ 11 ] )
  60. );
  61. }
  62. // f vertex/uv/normal vertex/uv/normal vertex/uv/normal ...
  63. // ["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]
  64. pattern = /f( ([\d]+)\/([\d]+)\/([\d]+))( ([\d]+)\/([\d]+)\/([\d]+))( ([\d]+)\/([\d]+)\/([\d]+))( ([\d]+)\/([\d]+)\/([\d]+))?/g;
  65. while ( ( result = pattern.exec( data ) ) != null ) {
  66. geometry.faces.push(
  67. result[ 13 ] === undefined ?
  68. face3( result[ 2 ], result[ 6 ], result[ 10 ] ) :
  69. face4( result[ 2 ], result[ 6 ], result[ 10 ], result[ 14 ] )
  70. );
  71. }
  72. // f vertex//normal vertex//normal vertex//normal ...
  73. // ["f 1//1 2//2 3//3", " 1//1", "1", "1", " 2//2", "2", "2", " 3//3", "3", "3", undefined, undefined, undefined]
  74. pattern = /f( ([\d]+)\/\/([\d]+))( ([\d]+)\/\/([\d]+))( ([\d]+)\/\/([\d]+))( ([\d]+)\/\/([\d]+))?/g;
  75. while ( ( result = pattern.exec( data ) ) != null ) {
  76. geometry.faces.push(
  77. result[ 10 ] === undefined ?
  78. face3( result[ 2 ], result[ 5 ], result[ 8 ] ) :
  79. face4( result[ 2 ], result[ 5 ], result[ 8 ], result[ 11 ] )
  80. );
  81. }
  82. geometry.computeCentroids();
  83. return geometry;
  84. }