BinaryLoader.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.BinaryLoader = function ( showStatus ) {
  5. THREE.Loader.call( this, showStatus );
  6. };
  7. THREE.BinaryLoader.prototype = new THREE.Loader();
  8. THREE.BinaryLoader.prototype.constructor = THREE.BinaryLoader;
  9. THREE.BinaryLoader.prototype.supr = THREE.Loader.prototype;
  10. // Load models generated by slim OBJ converter with BINARY option (converter_obj_three_slim.py -t binary)
  11. // - binary models consist of two files: JS and BIN
  12. // - parameters
  13. // - url (required)
  14. // - callback (required)
  15. // - texturePath (optional: if not specified, textures will be assumed to be in the same folder as JS model file)
  16. // - binaryPath (optional: if not specified, binary file will be assumed to be in the same folder as JS model file)
  17. THREE.BinaryLoader.prototype.load = function( url, callback, texturePath, binaryPath ) {
  18. texturePath = texturePath ? texturePath : this.extractUrlBase( url );
  19. binaryPath = binaryPath ? binaryPath : this.extractUrlBase( url );
  20. var callbackProgress = this.showProgress ? THREE.Loader.prototype.updateProgress : null;
  21. this.onLoadStart();
  22. // #1 load JS part via web worker
  23. this.loadAjaxJSON( this, url, callback, texturePath, binaryPath, callbackProgress );
  24. };
  25. THREE.BinaryLoader.prototype.loadAjaxJSON = function ( context, url, callback, texturePath, binaryPath, callbackProgress ) {
  26. var xhr = new XMLHttpRequest();
  27. xhr.onreadystatechange = function () {
  28. if ( xhr.readyState == 4 ) {
  29. if ( xhr.status == 200 || xhr.status == 0 ) {
  30. var json = JSON.parse( xhr.responseText );
  31. context.loadAjaxBuffers( json, callback, binaryPath, texturePath, callbackProgress );
  32. } else {
  33. console.error( "THREE.BinaryLoader: Couldn't load [" + url + "] [" + xhr.status + "]" );
  34. }
  35. }
  36. };
  37. xhr.open( "GET", url, true );
  38. if ( xhr.overrideMimeType ) xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
  39. xhr.setRequestHeader( "Content-Type", "text/plain" );
  40. xhr.send( null );
  41. };
  42. THREE.BinaryLoader.prototype.loadAjaxBuffers = function ( json, callback, binaryPath, texturePath, callbackProgress ) {
  43. var xhr = new XMLHttpRequest(),
  44. url = binaryPath + "/" + json.buffers;
  45. var length = 0;
  46. xhr.onreadystatechange = function () {
  47. if ( xhr.readyState == 4 ) {
  48. if ( xhr.status == 200 || xhr.status == 0 ) {
  49. THREE.BinaryLoader.prototype.createBinModel( xhr.response, callback, texturePath, json.materials );
  50. } else {
  51. console.error( "THREE.BinaryLoader: Couldn't load [" + url + "] [" + xhr.status + "]" );
  52. }
  53. } else if ( xhr.readyState == 3 ) {
  54. if ( callbackProgress ) {
  55. if ( length == 0 ) {
  56. length = xhr.getResponseHeader( "Content-Length" );
  57. }
  58. callbackProgress( { total: length, loaded: xhr.responseText.length } );
  59. }
  60. } else if ( xhr.readyState == 2 ) {
  61. length = xhr.getResponseHeader( "Content-Length" );
  62. }
  63. };
  64. xhr.open( "GET", url, true );
  65. xhr.responseType = "arraybuffer";
  66. xhr.send( null );
  67. };
  68. // Binary AJAX parser
  69. THREE.BinaryLoader.prototype.createBinModel = function ( data, callback, texturePath, materials ) {
  70. var Model = function ( texturePath ) {
  71. var scope = this,
  72. currentOffset = 0,
  73. md,
  74. normals = [],
  75. uvs = [],
  76. start_tri_flat, start_tri_smooth, start_tri_flat_uv, start_tri_smooth_uv,
  77. start_quad_flat, start_quad_smooth, start_quad_flat_uv, start_quad_smooth_uv,
  78. tri_size, quad_size,
  79. len_tri_flat, len_tri_smooth, len_tri_flat_uv, len_tri_smooth_uv,
  80. len_quad_flat, len_quad_smooth, len_quad_flat_uv, len_quad_smooth_uv;
  81. THREE.Geometry.call( this );
  82. THREE.Loader.prototype.initMaterials( scope, materials, texturePath );
  83. md = parseMetaData( data, currentOffset );
  84. currentOffset += md.header_bytes;
  85. /*
  86. md.vertex_index_bytes = Uint32Array.BYTES_PER_ELEMENT;
  87. md.material_index_bytes = Uint16Array.BYTES_PER_ELEMENT;
  88. md.normal_index_bytes = Uint32Array.BYTES_PER_ELEMENT;
  89. md.uv_index_bytes = Uint32Array.BYTES_PER_ELEMENT;
  90. */
  91. // buffers sizes
  92. tri_size = md.vertex_index_bytes * 3 + md.material_index_bytes;
  93. quad_size = md.vertex_index_bytes * 4 + md.material_index_bytes;
  94. len_tri_flat = md.ntri_flat * ( tri_size );
  95. len_tri_smooth = md.ntri_smooth * ( tri_size + md.normal_index_bytes * 3 );
  96. len_tri_flat_uv = md.ntri_flat_uv * ( tri_size + md.uv_index_bytes * 3 );
  97. len_tri_smooth_uv = md.ntri_smooth_uv * ( tri_size + md.normal_index_bytes * 3 + md.uv_index_bytes * 3 );
  98. len_quad_flat = md.nquad_flat * ( quad_size );
  99. len_quad_smooth = md.nquad_smooth * ( quad_size + md.normal_index_bytes * 4 );
  100. len_quad_flat_uv = md.nquad_flat_uv * ( quad_size + md.uv_index_bytes * 4 );
  101. len_quad_smooth_uv = md.nquad_smooth_uv * ( quad_size + md.normal_index_bytes * 4 + md.uv_index_bytes * 4 );
  102. // read buffers
  103. currentOffset += init_vertices( currentOffset );
  104. currentOffset += init_normals( currentOffset );
  105. currentOffset += handlePadding( md.nnormals * 3 );
  106. currentOffset += init_uvs( currentOffset );
  107. start_tri_flat = currentOffset;
  108. start_tri_smooth = start_tri_flat + len_tri_flat + handlePadding( md.ntri_flat * 2 );
  109. start_tri_flat_uv = start_tri_smooth + len_tri_smooth + handlePadding( md.ntri_smooth * 2 );
  110. start_tri_smooth_uv = start_tri_flat_uv + len_tri_flat_uv + handlePadding( md.ntri_flat_uv * 2 );
  111. start_quad_flat = start_tri_smooth_uv + len_tri_smooth_uv + handlePadding( md.ntri_smooth_uv * 2 );
  112. start_quad_smooth = start_quad_flat + len_quad_flat + handlePadding( md.nquad_flat * 2 );
  113. start_quad_flat_uv = start_quad_smooth + len_quad_smooth + handlePadding( md.nquad_smooth * 2 );
  114. start_quad_smooth_uv= start_quad_flat_uv + len_quad_flat_uv + handlePadding( md.nquad_flat_uv * 2 );
  115. // have to first process faces with uvs
  116. // so that face and uv indices match
  117. init_triangles_flat_uv( start_tri_flat_uv );
  118. init_triangles_smooth_uv( start_tri_smooth_uv );
  119. init_quads_flat_uv( start_quad_flat_uv );
  120. init_quads_smooth_uv( start_quad_smooth_uv );
  121. // now we can process untextured faces
  122. init_triangles_flat( start_tri_flat );
  123. init_triangles_smooth( start_tri_smooth );
  124. init_quads_flat( start_quad_flat );
  125. init_quads_smooth( start_quad_smooth );
  126. this.computeCentroids();
  127. this.computeFaceNormals();
  128. if ( THREE.Loader.prototype.hasNormals( this ) ) this.computeTangents();
  129. function handlePadding( n ) {
  130. return ( n % 4 ) ? ( 4 - n % 4 ) : 0;
  131. };
  132. function parseMetaData( data, offset ) {
  133. var metaData = {
  134. 'signature' :parseString( data, offset, 12 ),
  135. 'header_bytes' :parseUChar8( data, offset + 12 ),
  136. 'vertex_coordinate_bytes' :parseUChar8( data, offset + 13 ),
  137. 'normal_coordinate_bytes' :parseUChar8( data, offset + 14 ),
  138. 'uv_coordinate_bytes' :parseUChar8( data, offset + 15 ),
  139. 'vertex_index_bytes' :parseUChar8( data, offset + 16 ),
  140. 'normal_index_bytes' :parseUChar8( data, offset + 17 ),
  141. 'uv_index_bytes' :parseUChar8( data, offset + 18 ),
  142. 'material_index_bytes' :parseUChar8( data, offset + 19 ),
  143. 'nvertices' :parseUInt32( data, offset + 20 ),
  144. 'nnormals' :parseUInt32( data, offset + 20 + 4*1 ),
  145. 'nuvs' :parseUInt32( data, offset + 20 + 4*2 ),
  146. 'ntri_flat' :parseUInt32( data, offset + 20 + 4*3 ),
  147. 'ntri_smooth' :parseUInt32( data, offset + 20 + 4*4 ),
  148. 'ntri_flat_uv' :parseUInt32( data, offset + 20 + 4*5 ),
  149. 'ntri_smooth_uv' :parseUInt32( data, offset + 20 + 4*6 ),
  150. 'nquad_flat' :parseUInt32( data, offset + 20 + 4*7 ),
  151. 'nquad_smooth' :parseUInt32( data, offset + 20 + 4*8 ),
  152. 'nquad_flat_uv' :parseUInt32( data, offset + 20 + 4*9 ),
  153. 'nquad_smooth_uv' :parseUInt32( data, offset + 20 + 4*10 )
  154. };
  155. /*
  156. console.log( "signature: " + metaData.signature );
  157. console.log( "header_bytes: " + metaData.header_bytes );
  158. console.log( "vertex_coordinate_bytes: " + metaData.vertex_coordinate_bytes );
  159. console.log( "normal_coordinate_bytes: " + metaData.normal_coordinate_bytes );
  160. console.log( "uv_coordinate_bytes: " + metaData.uv_coordinate_bytes );
  161. console.log( "vertex_index_bytes: " + metaData.vertex_index_bytes );
  162. console.log( "normal_index_bytes: " + metaData.normal_index_bytes );
  163. console.log( "uv_index_bytes: " + metaData.uv_index_bytes );
  164. console.log( "material_index_bytes: " + metaData.material_index_bytes );
  165. console.log( "nvertices: " + metaData.nvertices );
  166. console.log( "nnormals: " + metaData.nnormals );
  167. console.log( "nuvs: " + metaData.nuvs );
  168. console.log( "ntri_flat: " + metaData.ntri_flat );
  169. console.log( "ntri_smooth: " + metaData.ntri_smooth );
  170. console.log( "ntri_flat_uv: " + metaData.ntri_flat_uv );
  171. console.log( "ntri_smooth_uv: " + metaData.ntri_smooth_uv );
  172. console.log( "nquad_flat: " + metaData.nquad_flat );
  173. console.log( "nquad_smooth: " + metaData.nquad_smooth );
  174. console.log( "nquad_flat_uv: " + metaData.nquad_flat_uv );
  175. console.log( "nquad_smooth_uv: " + metaData.nquad_smooth_uv );
  176. var total = metaData.header_bytes
  177. + metaData.nvertices * metaData.vertex_coordinate_bytes * 3
  178. + metaData.nnormals * metaData.normal_coordinate_bytes * 3
  179. + metaData.nuvs * metaData.uv_coordinate_bytes * 2
  180. + metaData.ntri_flat * ( metaData.vertex_index_bytes*3 + metaData.material_index_bytes )
  181. + metaData.ntri_smooth * ( metaData.vertex_index_bytes*3 + metaData.material_index_bytes + metaData.normal_index_bytes*3 )
  182. + metaData.ntri_flat_uv * ( metaData.vertex_index_bytes*3 + metaData.material_index_bytes + metaData.uv_index_bytes*3 )
  183. + metaData.ntri_smooth_uv * ( metaData.vertex_index_bytes*3 + metaData.material_index_bytes + metaData.normal_index_bytes*3 + metaData.uv_index_bytes*3 )
  184. + metaData.nquad_flat * ( metaData.vertex_index_bytes*4 + metaData.material_index_bytes )
  185. + metaData.nquad_smooth * ( metaData.vertex_index_bytes*4 + metaData.material_index_bytes + metaData.normal_index_bytes*4 )
  186. + metaData.nquad_flat_uv * ( metaData.vertex_index_bytes*4 + metaData.material_index_bytes + metaData.uv_index_bytes*4 )
  187. + metaData.nquad_smooth_uv * ( metaData.vertex_index_bytes*4 + metaData.material_index_bytes + metaData.normal_index_bytes*4 + metaData.uv_index_bytes*4 );
  188. console.log( "total bytes: " + total );
  189. */
  190. return metaData;
  191. };
  192. function parseString( data, offset, length ) {
  193. var charArray = new Uint8Array( data, offset, length );
  194. var text = "";
  195. for ( var i = 0; i < length; i ++ ) {
  196. text += String.fromCharCode( charArray[ offset + i ] );
  197. }
  198. return text;
  199. };
  200. function parseUChar8( data, offset ) {
  201. var charArray = new Uint8Array( data, offset, 1 );
  202. return charArray[ 0 ];
  203. };
  204. function parseUInt32( data, offset ) {
  205. var intArray = new Uint32Array( data, offset, 1 );
  206. return intArray[ 0 ];
  207. };
  208. function init_vertices( start ) {
  209. var nElements = md.nvertices;
  210. var coordArray = new Float32Array( data, start, nElements * 3 );
  211. var i, x, y, z;
  212. for( i = 0; i < nElements; i ++ ) {
  213. x = coordArray[ i * 3 ];
  214. y = coordArray[ i * 3 + 1 ];
  215. z = coordArray[ i * 3 + 2 ];
  216. vertex( scope, x, y, z );
  217. }
  218. return nElements * 3 * Float32Array.BYTES_PER_ELEMENT;
  219. };
  220. function init_normals( start ) {
  221. var nElements = md.nnormals;
  222. if ( nElements ) {
  223. var normalArray = new Int8Array( data, start, nElements * 3 );
  224. var i, x, y, z;
  225. for( i = 0; i < nElements; i ++ ) {
  226. x = normalArray[ i * 3 ];
  227. y = normalArray[ i * 3 + 1 ];
  228. z = normalArray[ i * 3 + 2 ];
  229. normals.push( x/127, y/127, z/127 );
  230. }
  231. }
  232. return nElements * 3 * Int8Array.BYTES_PER_ELEMENT;
  233. };
  234. function init_uvs( start ) {
  235. var nElements = md.nuvs;
  236. if ( nElements ) {
  237. var uvArray = new Float32Array( data, start, nElements * 2 );
  238. var i, u, v;
  239. for( i = 0; i < nElements; i ++ ) {
  240. u = uvArray[ i * 2 ];
  241. v = uvArray[ i * 2 + 1 ];
  242. uvs.push( u, v );
  243. }
  244. }
  245. return nElements * 2 * Float32Array.BYTES_PER_ELEMENT;
  246. };
  247. function init_uvs3( nElements, offset ) {
  248. var i, uva, uvb, uvc, u1, u2, u3, v1, v2, v3;
  249. var uvIndexBuffer = new Uint32Array( data, offset, 3 * nElements );
  250. for( i = 0; i < nElements; i ++ ) {
  251. uva = uvIndexBuffer[ i * 3 ];
  252. uvb = uvIndexBuffer[ i * 3 + 1 ];
  253. uvc = uvIndexBuffer[ i * 3 + 2 ];
  254. u1 = uvs[ uva*2 ];
  255. v1 = uvs[ uva*2 + 1 ];
  256. u2 = uvs[ uvb*2 ];
  257. v2 = uvs[ uvb*2 + 1 ];
  258. u3 = uvs[ uvc*2 ];
  259. v3 = uvs[ uvc*2 + 1 ];
  260. uv3( scope.faceVertexUvs[ 0 ], u1, v1, u2, v2, u3, v3 );
  261. }
  262. };
  263. function init_uvs4( nElements, offset ) {
  264. var i, uva, uvb, uvc, uvd, u1, u2, u3, u4, v1, v2, v3, v4;
  265. var uvIndexBuffer = new Uint32Array( data, offset, 4 * nElements );
  266. for( i = 0; i < nElements; i ++ ) {
  267. uva = uvIndexBuffer[ i * 4 ];
  268. uvb = uvIndexBuffer[ i * 4 + 1 ];
  269. uvc = uvIndexBuffer[ i * 4 + 2 ];
  270. uvd = uvIndexBuffer[ i * 4 + 3 ];
  271. u1 = uvs[ uva*2 ];
  272. v1 = uvs[ uva*2 + 1 ];
  273. u2 = uvs[ uvb*2 ];
  274. v2 = uvs[ uvb*2 + 1 ];
  275. u3 = uvs[ uvc*2 ];
  276. v3 = uvs[ uvc*2 + 1 ];
  277. u4 = uvs[ uvd*2 ];
  278. v4 = uvs[ uvd*2 + 1 ];
  279. uv4( scope.faceVertexUvs[ 0 ], u1, v1, u2, v2, u3, v3, u4, v4 );
  280. }
  281. };
  282. function init_faces3_flat( nElements, offsetVertices, offsetMaterials ) {
  283. var i, a, b, c, m;
  284. var vertexIndexBuffer = new Uint32Array( data, offsetVertices, 3 * nElements );
  285. var materialIndexBuffer = new Uint16Array( data, offsetMaterials, nElements );
  286. for( i = 0; i < nElements; i ++ ) {
  287. a = vertexIndexBuffer[ i * 3 ];
  288. b = vertexIndexBuffer[ i * 3 + 1 ];
  289. c = vertexIndexBuffer[ i * 3 + 2 ];
  290. m = materialIndexBuffer[ i ];
  291. f3( scope, a, b, c, m );
  292. }
  293. };
  294. function init_faces4_flat( nElements, offsetVertices, offsetMaterials ) {
  295. var i, a, b, c, d, m;
  296. var vertexIndexBuffer = new Uint32Array( data, offsetVertices, 4 * nElements );
  297. var materialIndexBuffer = new Uint16Array( data, offsetMaterials, nElements );
  298. for( i = 0; i < nElements; i ++ ) {
  299. a = vertexIndexBuffer[ i * 4 ];
  300. b = vertexIndexBuffer[ i * 4 + 1 ];
  301. c = vertexIndexBuffer[ i * 4 + 2 ];
  302. d = vertexIndexBuffer[ i * 4 + 3 ];
  303. m = materialIndexBuffer[ i ];
  304. f4( scope, a, b, c, d, m );
  305. }
  306. };
  307. function init_faces3_smooth( nElements, offsetVertices, offsetNormals, offsetMaterials ) {
  308. var i, a, b, c, m;
  309. var na, nb, nc;
  310. var vertexIndexBuffer = new Uint32Array( data, offsetVertices, 3 * nElements );
  311. var normalIndexBuffer = new Uint32Array( data, offsetNormals, 3 * nElements );
  312. var materialIndexBuffer = new Uint16Array( data, offsetMaterials, nElements );
  313. for( i = 0; i < nElements; i ++ ) {
  314. a = vertexIndexBuffer[ i * 3 ];
  315. b = vertexIndexBuffer[ i * 3 + 1 ];
  316. c = vertexIndexBuffer[ i * 3 + 2 ];
  317. na = normalIndexBuffer[ i * 3 ];
  318. nb = normalIndexBuffer[ i * 3 + 1 ];
  319. nc = normalIndexBuffer[ i * 3 + 2 ];
  320. m = materialIndexBuffer[ i ];
  321. f3n( scope, normals, a, b, c, m, na, nb, nc );
  322. }
  323. };
  324. function init_faces4_smooth( nElements, offsetVertices, offsetNormals, offsetMaterials ) {
  325. var i, a, b, c, d, m;
  326. var na, nb, nc, nd;
  327. var vertexIndexBuffer = new Uint32Array( data, offsetVertices, 4 * nElements );
  328. var normalIndexBuffer = new Uint32Array( data, offsetNormals, 4 * nElements );
  329. var materialIndexBuffer = new Uint16Array( data, offsetMaterials, nElements );
  330. for( i = 0; i < nElements; i ++ ) {
  331. a = vertexIndexBuffer[ i * 4 ];
  332. b = vertexIndexBuffer[ i * 4 + 1 ];
  333. c = vertexIndexBuffer[ i * 4 + 2 ];
  334. d = vertexIndexBuffer[ i * 4 + 3 ];
  335. na = normalIndexBuffer[ i * 4 ];
  336. nb = normalIndexBuffer[ i * 4 + 1 ];
  337. nc = normalIndexBuffer[ i * 4 + 2 ];
  338. nd = normalIndexBuffer[ i * 4 + 3 ];
  339. m = materialIndexBuffer[ i ];
  340. f4n( scope, normals, a, b, c, d, m, na, nb, nc, nd );
  341. }
  342. };
  343. function init_triangles_flat( start ) {
  344. var nElements = md.ntri_flat;
  345. if ( nElements ) {
  346. var offsetMaterials = start + nElements * Uint32Array.BYTES_PER_ELEMENT * 3;
  347. init_faces3_flat( nElements, start, offsetMaterials );
  348. }
  349. };
  350. function init_triangles_flat_uv( start ) {
  351. var nElements = md.ntri_flat_uv;
  352. if ( nElements ) {
  353. var offsetUvs = start + nElements * Uint32Array.BYTES_PER_ELEMENT * 3;
  354. var offsetMaterials = offsetUvs + nElements * Uint32Array.BYTES_PER_ELEMENT * 3;
  355. init_faces3_flat( nElements, start, offsetMaterials );
  356. init_uvs3( nElements, offsetUvs );
  357. }
  358. };
  359. function init_triangles_smooth( start ) {
  360. var nElements = md.ntri_smooth;
  361. if ( nElements ) {
  362. var offsetNormals = start + nElements * Uint32Array.BYTES_PER_ELEMENT * 3;
  363. var offsetMaterials = offsetNormals + nElements * Uint32Array.BYTES_PER_ELEMENT * 3;
  364. init_faces3_smooth( nElements, start, offsetNormals, offsetMaterials );
  365. }
  366. };
  367. function init_triangles_smooth_uv( start ) {
  368. var nElements = md.ntri_smooth_uv;
  369. if ( nElements ) {
  370. var offsetNormals = start + nElements * Uint32Array.BYTES_PER_ELEMENT * 3;
  371. var offsetUvs = offsetNormals + nElements * Uint32Array.BYTES_PER_ELEMENT * 3;
  372. var offsetMaterials = offsetUvs + nElements * Uint32Array.BYTES_PER_ELEMENT * 3;
  373. init_faces3_smooth( nElements, start, offsetNormals, offsetMaterials );
  374. init_uvs3( nElements, offsetUvs );
  375. }
  376. };
  377. function init_quads_flat( start ) {
  378. var nElements = md.nquad_flat;
  379. if ( nElements ) {
  380. var offsetMaterials = start + nElements * Uint32Array.BYTES_PER_ELEMENT * 4;
  381. init_faces4_flat( nElements, start, offsetMaterials );
  382. }
  383. };
  384. function init_quads_flat_uv( start ) {
  385. var nElements = md.nquad_flat_uv;
  386. if ( nElements ) {
  387. var offsetUvs = start + nElements * Uint32Array.BYTES_PER_ELEMENT * 4;
  388. var offsetMaterials = offsetUvs + nElements * Uint32Array.BYTES_PER_ELEMENT * 4;
  389. init_faces4_flat( nElements, start, offsetMaterials );
  390. init_uvs4( nElements, offsetUvs );
  391. }
  392. };
  393. function init_quads_smooth( start ) {
  394. var nElements = md.nquad_smooth;
  395. if ( nElements ) {
  396. var offsetNormals = start + nElements * Uint32Array.BYTES_PER_ELEMENT * 4;
  397. var offsetMaterials = offsetNormals + nElements * Uint32Array.BYTES_PER_ELEMENT * 4;
  398. init_faces4_smooth( nElements, start, offsetNormals, offsetMaterials );
  399. }
  400. };
  401. function init_quads_smooth_uv( start ) {
  402. var nElements = md.nquad_smooth_uv;
  403. if ( nElements ) {
  404. var offsetNormals = start + nElements * Uint32Array.BYTES_PER_ELEMENT * 4;
  405. var offsetUvs = offsetNormals + nElements * Uint32Array.BYTES_PER_ELEMENT * 4;
  406. var offsetMaterials = offsetUvs + nElements * Uint32Array.BYTES_PER_ELEMENT * 4;
  407. init_faces4_smooth( nElements, start, offsetNormals, offsetMaterials );
  408. init_uvs4( nElements, offsetUvs );
  409. }
  410. };
  411. };
  412. function vertex ( scope, x, y, z ) {
  413. scope.vertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z ) ) );
  414. };
  415. function f3 ( scope, a, b, c, mi ) {
  416. scope.faces.push( new THREE.Face3( a, b, c, null, null, mi ) );
  417. };
  418. function f4 ( scope, a, b, c, d, mi ) {
  419. scope.faces.push( new THREE.Face4( a, b, c, d, null, null, mi ) );
  420. };
  421. function f3n ( scope, normals, a, b, c, mi, na, nb, nc ) {
  422. var nax = normals[ na*3 ],
  423. nay = normals[ na*3 + 1 ],
  424. naz = normals[ na*3 + 2 ],
  425. nbx = normals[ nb*3 ],
  426. nby = normals[ nb*3 + 1 ],
  427. nbz = normals[ nb*3 + 2 ],
  428. ncx = normals[ nc*3 ],
  429. ncy = normals[ nc*3 + 1 ],
  430. ncz = normals[ nc*3 + 2 ];
  431. scope.faces.push( new THREE.Face3( a, b, c,
  432. [new THREE.Vector3( nax, nay, naz ),
  433. new THREE.Vector3( nbx, nby, nbz ),
  434. new THREE.Vector3( ncx, ncy, ncz )],
  435. null,
  436. mi ) );
  437. };
  438. function f4n ( scope, normals, a, b, c, d, mi, na, nb, nc, nd ) {
  439. var nax = normals[ na*3 ],
  440. nay = normals[ na*3 + 1 ],
  441. naz = normals[ na*3 + 2 ],
  442. nbx = normals[ nb*3 ],
  443. nby = normals[ nb*3 + 1 ],
  444. nbz = normals[ nb*3 + 2 ],
  445. ncx = normals[ nc*3 ],
  446. ncy = normals[ nc*3 + 1 ],
  447. ncz = normals[ nc*3 + 2 ],
  448. ndx = normals[ nd*3 ],
  449. ndy = normals[ nd*3 + 1 ],
  450. ndz = normals[ nd*3 + 2 ];
  451. scope.faces.push( new THREE.Face4( a, b, c, d,
  452. [new THREE.Vector3( nax, nay, naz ),
  453. new THREE.Vector3( nbx, nby, nbz ),
  454. new THREE.Vector3( ncx, ncy, ncz ),
  455. new THREE.Vector3( ndx, ndy, ndz )],
  456. null,
  457. mi ) );
  458. };
  459. function uv3 ( where, u1, v1, u2, v2, u3, v3 ) {
  460. var uv = [];
  461. uv.push( new THREE.UV( u1, v1 ) );
  462. uv.push( new THREE.UV( u2, v2 ) );
  463. uv.push( new THREE.UV( u3, v3 ) );
  464. where.push( uv );
  465. };
  466. function uv4 ( where, u1, v1, u2, v2, u3, v3, u4, v4 ) {
  467. var uv = [];
  468. uv.push( new THREE.UV( u1, v1 ) );
  469. uv.push( new THREE.UV( u2, v2 ) );
  470. uv.push( new THREE.UV( u3, v3 ) );
  471. uv.push( new THREE.UV( u4, v4 ) );
  472. where.push( uv );
  473. };
  474. Model.prototype = new THREE.Geometry();
  475. Model.prototype.constructor = Model;
  476. callback( new Model( texturePath ) );
  477. };