BinaryLoader.js 21 KB

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