OBJLoader2.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.OBJLoader = function ( manager ) {
  5. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  6. };
  7. THREE.OBJLoader.prototype = {
  8. constructor: THREE.OBJLoader,
  9. load: function ( url, onLoad, onProgress, onError ) {
  10. var scope = this;
  11. var loader = new THREE.XHRLoader( scope.manager );
  12. loader.setCrossOrigin( this.crossOrigin );
  13. loader.load( url, function ( text ) {
  14. onLoad( scope.parse( text ) );
  15. } );
  16. },
  17. parse: function ( text ) {
  18. /*
  19. function vector( x, y, z ) {
  20. return new THREE.Vector3( parseFloat( x ), parseFloat( y ), parseFloat( z ) );
  21. }
  22. function uv( u, v ) {
  23. return new THREE.Vector2( parseFloat( u ), parseFloat( v ) );
  24. }
  25. function face3( a, b, c, normals ) {
  26. return new THREE.Face3( a, b, c, normals );
  27. }
  28. */
  29. var object = new THREE.Object3D();
  30. var geometry, material, mesh;
  31. function parseVertexIndex( value ) {
  32. var index = parseInt( value );
  33. return ( index >= 0 ? index - 1 : index + vertices.length / 3 ) * 3;
  34. }
  35. function parseNormalIndex( value ) {
  36. var index = parseInt( value );
  37. return ( index >= 0 ? index - 1 : index + normals.length / 3 ) * 3;
  38. }
  39. function parseUVIndex( value ) {
  40. var index = parseInt( value );
  41. return ( index >= 0 ? index - 1 : index + uvs.length / 2 ) * 2;
  42. }
  43. function addVertex( a, b, c ) {
  44. geometry.vertices.push(
  45. vertices[ a ], vertices[ a + 1 ], vertices[ a + 2 ],
  46. vertices[ b ], vertices[ b + 1 ], vertices[ b + 2 ],
  47. vertices[ c ], vertices[ c + 1 ], vertices[ c + 2 ]
  48. );
  49. }
  50. function addNormal( a, b, c ) {
  51. geometry.normals.push(
  52. normals[ a ], normals[ a + 1 ], normals[ a + 2 ],
  53. normals[ b ], normals[ b + 1 ], normals[ b + 2 ],
  54. normals[ c ], normals[ c + 1 ], normals[ c + 2 ]
  55. );
  56. }
  57. function addUV( a, b, c ) {
  58. geometry.uvs.push(
  59. uvs[ a ], uvs[ a + 1 ],
  60. uvs[ b ], uvs[ b + 1 ],
  61. uvs[ c ], uvs[ c + 1 ]
  62. );
  63. }
  64. function handleFace( a, b, c, d ) {
  65. var ia = parseVertexIndex( a );
  66. var ib = parseVertexIndex( b );
  67. var ic = parseVertexIndex( c );
  68. if ( d === undefined ) {
  69. addVertex( ia, ib, ic );
  70. } else {
  71. var id = parseVertexIndex( d );
  72. addVertex( ia, ib, id );
  73. addVertex( ib, ic, id );
  74. }
  75. }
  76. function handleUV( a, b, c, d ) {
  77. var ia = parseUVIndex( a );
  78. var ib = parseUVIndex( b );
  79. var ic = parseUVIndex( c );
  80. if ( d === undefined ) {
  81. addUV( ia, ib, ic );
  82. } else {
  83. var id = parseUVIndex( d );
  84. addUV( ia, ib, id );
  85. addUV( ib, ic, id );
  86. }
  87. }
  88. function handleNormal( a, b, c, d ) {
  89. var ia = parseNormalIndex( a );
  90. var ib = parseNormalIndex( b );
  91. var ic = parseNormalIndex( c );
  92. if ( d === undefined ) {
  93. addNormal( ia, ib, ic );
  94. } else {
  95. var id = parseNormalIndex( d );
  96. addNormal( ia, ib, id );
  97. addNormal( ib, ic, id );
  98. }
  99. }
  100. // create mesh if no objects in text
  101. if ( /^o /gm.test( text ) === false ) {
  102. geometry = new THREE.Geometry2();
  103. material = new THREE.MeshLambertMaterial();
  104. mesh = new THREE.Mesh( geometry, material );
  105. object.add( mesh );
  106. }
  107. var vertices = [];
  108. var normals = [];
  109. var uvs = [];
  110. // v float float float
  111. var vertex_pattern = /v( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)/;
  112. // vn float float float
  113. var normal_pattern = /vn( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)/;
  114. // vt float float
  115. var uv_pattern = /vt( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)/;
  116. // f vertex vertex vertex ...
  117. var face_pattern1 = /f( +-?\d+)( +-?\d+)( +-?\d+)( +-?\d+)?/;
  118. // f vertex/uv vertex/uv vertex/uv ...
  119. var face_pattern2 = /f( +(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+))?/;
  120. // f vertex/uv/normal vertex/uv/normal vertex/uv/normal ...
  121. var face_pattern3 = /f( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))?/;
  122. // f vertex//normal vertex//normal vertex//normal ...
  123. var face_pattern4 = /f( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))?/
  124. //
  125. var lines = text.split( '\n' );
  126. for ( var i = 0; i < lines.length; i ++ ) {
  127. var line = lines[ i ];
  128. line = line.trim();
  129. var result;
  130. if ( line.length === 0 || line.charAt( 0 ) === '#' ) {
  131. continue;
  132. } else if ( ( result = vertex_pattern.exec( line ) ) !== null ) {
  133. // ["v 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
  134. vertices.push(
  135. parseFloat( result[ 1 ] ),
  136. parseFloat( result[ 2 ] ),
  137. parseFloat( result[ 3 ] )
  138. );
  139. } else if ( ( result = normal_pattern.exec( line ) ) !== null ) {
  140. // ["vn 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
  141. normals.push(
  142. parseFloat( result[ 1 ] ),
  143. parseFloat( result[ 2 ] ),
  144. parseFloat( result[ 3 ] )
  145. );
  146. } else if ( ( result = uv_pattern.exec( line ) ) !== null ) {
  147. // ["vt 0.1 0.2", "0.1", "0.2"]
  148. uvs.push(
  149. parseFloat( result[ 1 ] ),
  150. parseFloat( result[ 2 ] )
  151. );
  152. } else if ( ( result = face_pattern1.exec( line ) ) !== null ) {
  153. // ["f 1 2 3", "1", "2", "3", undefined]
  154. handleFace( result[ 1 ], result[ 2 ], result[ 3 ], result[ 4 ] );
  155. } else if ( ( result = face_pattern2.exec( line ) ) !== null ) {
  156. // ["f 1/1 2/2 3/3", " 1/1", "1", "1", " 2/2", "2", "2", " 3/3", "3", "3", undefined, undefined, undefined]
  157. handleFace( result[ 2 ], result[ 5 ], result[ 8 ], result[ 11 ] );
  158. handleUV( result[ 3 ], result[ 6 ], result[ 9 ], result[ 12 ] );
  159. } else if ( ( result = face_pattern3.exec( line ) ) !== null ) {
  160. // ["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]
  161. handleFace( result[ 2 ], result[ 6 ], result[ 10 ], result[ 14 ] );
  162. handleUV( result[ 3 ], result[ 7 ], result[ 11 ], result[ 15 ] );
  163. handleNormal( result[ 4 ], result[ 8 ], result[ 12 ], result[ 16 ] );
  164. } else if ( ( result = face_pattern4.exec( line ) ) !== null ) {
  165. // ["f 1//1 2//2 3//3", " 1//1", "1", "1", " 2//2", "2", "2", " 3//3", "3", "3", undefined, undefined, undefined]
  166. handleFace( result[ 2 ], result[ 5 ], result[ 8 ], result[ 11 ] );
  167. handleNormal( result[ 3 ], result[ 6 ], result[ 9 ], result[ 12 ] );
  168. } else if ( /^o /.test( line ) ) {
  169. geometry = new THREE.Geometry2();
  170. material = new THREE.MeshLambertMaterial();
  171. mesh = new THREE.Mesh( geometry, material );
  172. mesh.name = line.substring( 2 ).trim();
  173. object.add( mesh );
  174. } else if ( /^g /.test( line ) ) {
  175. // group
  176. } else if ( /^usemtl /.test( line ) ) {
  177. // material
  178. material.name = line.substring( 7 ).trim();
  179. } else if ( /^mtllib /.test( line ) ) {
  180. // mtl file
  181. } else if ( /^s /.test( line ) ) {
  182. // smooth shading
  183. } else {
  184. // console.log( "THREE.OBJLoader: Unhandled line " + line );
  185. }
  186. }
  187. var children = object.children;
  188. for ( var i = 0, l = children.length; i < l; i ++ ) {
  189. var geometry = children[ i ].geometry;
  190. geometry.vertices = new Float32Array( geometry.vertices );
  191. geometry.normals = new Float32Array( geometry.normals );
  192. geometry.uvs = new Float32Array( geometry.uvs );
  193. }
  194. return object;
  195. }
  196. };