OBJLoader.js 3.2 KB

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