OBJLoader.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. } else {
  15. console.error( 'THREE.OBJLoader: Couldn\'t load ' + url + ' (' + xhr.status + ')' );
  16. }
  17. }
  18. };
  19. xhr.open( "GET", url, true );
  20. xhr.send( null );
  21. };
  22. THREE.OBJLoader.prototype.parse = function ( data ) {
  23. function vector( x, y, z ) {
  24. return new THREE.Vector3( x, y, z );
  25. }
  26. function face3( a, b, c, normals ) {
  27. return new THREE.Face3( a, b, c, normals );
  28. }
  29. function face4( a, b, c, d, normals ) {
  30. return new THREE.Face4( a, b, c, d, normals );
  31. }
  32. var objects = [];
  33. var vertices = [];
  34. var normals = [];
  35. var pattern, result;
  36. // v float float float
  37. pattern = /v( [\d|\.|\+|\-|e]+)( [\d|\.|\+|\-|e]+)( [\d|\.|\+|\-|e]+)/g;
  38. while ( ( result = pattern.exec( data ) ) != null ) {
  39. // ["v 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
  40. vertices.push( vector(
  41. parseFloat( result[ 1 ] ),
  42. parseFloat( result[ 2 ] ),
  43. parseFloat( result[ 3 ] )
  44. ) );
  45. }
  46. // vn float float float
  47. pattern = /vn( [\d|\.|\+|\-|e]+)( [\d|\.|\+|\-|e]+)( [\d|\.|\+|\-|e]+)/g;
  48. while ( ( result = pattern.exec( data ) ) != null ) {
  49. // ["v 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
  50. normals.push( vector(
  51. parseFloat( result[ 1 ] ),
  52. parseFloat( result[ 2 ] ),
  53. parseFloat( result[ 3 ] )
  54. ) );
  55. }
  56. var data = data.split( '\no ');
  57. for ( var i = 0, l = data.length; i < l; i ++ ) {
  58. var object = data[ i ];
  59. var geometry = new THREE.Geometry();
  60. geometry.vertices = vertices;
  61. // f vertex vertex vertex ...
  62. pattern = /f( [\d]+)( [\d]+)( [\d]+)( [\d]+)?/g;
  63. while ( ( result = pattern.exec( object ) ) != null ) {
  64. // ["f 1 2 3", "1", "2", "3", undefined]
  65. if ( result[ 4 ] === undefined ) {
  66. geometry.faces.push( face3(
  67. parseInt( result[ 1 ] ) - 1,
  68. parseInt( result[ 2 ] ) - 1,
  69. parseInt( result[ 3 ] ) - 1
  70. ) );
  71. } else {
  72. geometry.faces.push( face4(
  73. parseInt( result[ 1 ] ) - 1,
  74. parseInt( result[ 2 ] ) - 1,
  75. parseInt( result[ 3 ] ) - 1,
  76. parseInt( result[ 4 ] ) - 1
  77. ) );
  78. }
  79. }
  80. // f vertex/uv vertex/uv vertex/uv ...
  81. pattern = /f( ([\d]+)\/([\d]+))( ([\d]+)\/([\d]+))( ([\d]+)\/([\d]+))( ([\d]+)\/([\d]+))?/g;
  82. while ( ( result = pattern.exec( object ) ) != null ) {
  83. // ["f 1/1 2/2 3/3", " 1/1", "1", "1", " 2/2", "2", "2", " 3/3", "3", "3", undefined, undefined, undefined]
  84. if ( result[ 10 ] === undefined ) {
  85. geometry.faces.push( face3(
  86. parseInt( result[ 2 ] ) - 1,
  87. parseInt( result[ 5 ] ) - 1,
  88. parseInt( result[ 8 ] ) - 1
  89. ) );
  90. } else {
  91. geometry.faces.push( face4(
  92. parseInt( result[ 2 ] ) - 1,
  93. parseInt( result[ 5 ] ) - 1,
  94. parseInt( result[ 8 ] ) - 1,
  95. parseInt( result[ 11 ] ) - 1
  96. ) );
  97. }
  98. }
  99. // f vertex/uv/normal vertex/uv/normal vertex/uv/normal ...
  100. pattern = /f( ([\d]+)\/([\d]+)\/([\d]+))( ([\d]+)\/([\d]+)\/([\d]+))( ([\d]+)\/([\d]+)\/([\d]+))( ([\d]+)\/([\d]+)\/([\d]+))?/g;
  101. while ( ( result = pattern.exec( object ) ) != null ) {
  102. // ["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]
  103. if ( result[ 13 ] === undefined ) {
  104. geometry.faces.push( face3(
  105. parseInt( result[ 2 ] ) - 1,
  106. parseInt( result[ 6 ] ) - 1,
  107. parseInt( result[ 10 ] ) - 1,
  108. [
  109. normals[ parseInt( result[ 4 ] ) - 1 ],
  110. normals[ parseInt( result[ 8 ] ) - 1 ],
  111. normals[ parseInt( result[ 12 ] ) - 1 ]
  112. ]
  113. ) );
  114. } else {
  115. geometry.faces.push( face4(
  116. parseInt( result[ 2 ] ) - 1,
  117. parseInt( result[ 6 ] ) - 1,
  118. parseInt( result[ 10 ] ) - 1,
  119. parseInt( result[ 14 ] ) - 1,
  120. [
  121. normals[ parseInt( result[ 4 ] ) - 1 ],
  122. normals[ parseInt( result[ 8 ] ) - 1 ],
  123. normals[ parseInt( result[ 12 ] ) - 1 ],
  124. normals[ parseInt( result[ 16 ] ) - 1 ]
  125. ]
  126. ) );
  127. }
  128. }
  129. // f vertex//normal vertex//normal vertex//normal ...
  130. pattern = /f( ([\d]+)\/\/([\d]+))( ([\d]+)\/\/([\d]+))( ([\d]+)\/\/([\d]+))( ([\d]+)\/\/([\d]+))?/g;
  131. while ( ( result = pattern.exec( object ) ) != null ) {
  132. // ["f 1//1 2//2 3//3", " 1//1", "1", "1", " 2//2", "2", "2", " 3//3", "3", "3", undefined, undefined, undefined]
  133. if ( result[ 10 ] === undefined ) {
  134. geometry.faces.push( face3(
  135. parseInt( result[ 2 ] ) - 1,
  136. parseInt( result[ 5 ] ) - 1,
  137. parseInt( result[ 8 ] ) - 1,
  138. [
  139. normals[ parseInt( result[ 3 ] ) - 1 ],
  140. normals[ parseInt( result[ 6 ] ) - 1 ],
  141. normals[ parseInt( result[ 9 ] ) - 1 ]
  142. ]
  143. ) );
  144. } else {
  145. geometry.faces.push( face4(
  146. parseInt( result[ 2 ] ) - 1,
  147. parseInt( result[ 5 ] ) - 1,
  148. parseInt( result[ 8 ] ) - 1,
  149. parseInt( result[ 11 ] ) - 1,
  150. [
  151. normals[ parseInt( result[ 3 ] ) - 1 ],
  152. normals[ parseInt( result[ 6 ] ) - 1 ],
  153. normals[ parseInt( result[ 9 ] ) - 1 ],
  154. normals[ parseInt( result[ 12 ] ) - 1 ]
  155. ]
  156. ) );
  157. }
  158. }
  159. geometry.computeCentroids();
  160. objects.push( new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) ) );
  161. }
  162. return objects;
  163. }