2
0

OBJLoader.js 5.5 KB

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