OBJLoader.js 6.8 KB

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