OBJLoader.js 6.5 KB

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