OBJLoader.js 7.0 KB

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