OBJLoader.js 7.1 KB

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