AssimpLoader.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307
  1. import {
  2. Bone,
  3. BufferAttribute,
  4. BufferGeometry,
  5. Color,
  6. FileLoader,
  7. Loader,
  8. LoaderUtils,
  9. Matrix4,
  10. Mesh,
  11. MeshLambertMaterial,
  12. MeshPhongMaterial,
  13. Object3D,
  14. Quaternion,
  15. Skeleton,
  16. SkinnedMesh,
  17. TextureLoader,
  18. Vector3
  19. } from '../../../build/three.module.js';
  20. var AssimpLoader = function ( manager ) {
  21. Loader.call( this, manager );
  22. };
  23. AssimpLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
  24. constructor: AssimpLoader,
  25. load: function ( url, onLoad, onProgress, onError ) {
  26. var scope = this;
  27. var path = ( scope.path === '' ) ? LoaderUtils.extractUrlBase( url ) : scope.path;
  28. var loader = new FileLoader( scope.manager );
  29. loader.setPath( scope.path );
  30. loader.setResponseType( 'arraybuffer' );
  31. loader.setRequestHeader( scope.requestHeader );
  32. loader.setWithCredentials( scope.withCredentials );
  33. loader.load( url, function ( buffer ) {
  34. try {
  35. onLoad( scope.parse( buffer, path ) );
  36. } catch ( e ) {
  37. if ( onError ) {
  38. onError( e );
  39. } else {
  40. console.error( e );
  41. }
  42. scope.manager.itemError( url );
  43. }
  44. }, onProgress, onError );
  45. },
  46. parse: function ( buffer, path ) {
  47. var textureLoader = new TextureLoader( this.manager );
  48. textureLoader.setPath( this.resourcePath || path ).setCrossOrigin( this.crossOrigin );
  49. var Virtulous = {};
  50. Virtulous.KeyFrame = function ( time, matrix ) {
  51. this.time = time;
  52. this.matrix = matrix.clone();
  53. this.position = new Vector3();
  54. this.quaternion = new Quaternion();
  55. this.scale = new Vector3( 1, 1, 1 );
  56. this.matrix.decompose( this.position, this.quaternion, this.scale );
  57. this.clone = function () {
  58. var n = new Virtulous.KeyFrame( this.time, this.matrix );
  59. return n;
  60. };
  61. this.lerp = function ( nextKey, time ) {
  62. time -= this.time;
  63. var dist = ( nextKey.time - this.time );
  64. var l = time / dist;
  65. var l2 = 1 - l;
  66. var keypos = this.position;
  67. var keyrot = this.quaternion;
  68. // var keyscl = key.parentspaceScl || key.scl;
  69. var key2pos = nextKey.position;
  70. var key2rot = nextKey.quaternion;
  71. // var key2scl = key2.parentspaceScl || key2.scl;
  72. Virtulous.KeyFrame.tempAniPos.x = keypos.x * l2 + key2pos.x * l;
  73. Virtulous.KeyFrame.tempAniPos.y = keypos.y * l2 + key2pos.y * l;
  74. Virtulous.KeyFrame.tempAniPos.z = keypos.z * l2 + key2pos.z * l;
  75. // tempAniScale.x = keyscl[0] * l2 + key2scl[0] * l;
  76. // tempAniScale.y = keyscl[1] * l2 + key2scl[1] * l;
  77. // tempAniScale.z = keyscl[2] * l2 + key2scl[2] * l;
  78. Virtulous.KeyFrame.tempAniQuat.set( keyrot.x, keyrot.y, keyrot.z, keyrot.w );
  79. Virtulous.KeyFrame.tempAniQuat.slerp( key2rot, l );
  80. return Virtulous.KeyFrame.tempAniMatrix.compose( Virtulous.KeyFrame.tempAniPos, Virtulous.KeyFrame.tempAniQuat, Virtulous.KeyFrame.tempAniScale );
  81. };
  82. };
  83. Virtulous.KeyFrame.tempAniPos = new Vector3();
  84. Virtulous.KeyFrame.tempAniQuat = new Quaternion();
  85. Virtulous.KeyFrame.tempAniScale = new Vector3( 1, 1, 1 );
  86. Virtulous.KeyFrame.tempAniMatrix = new Matrix4();
  87. Virtulous.KeyFrameTrack = function () {
  88. this.keys = [];
  89. this.target = null;
  90. this.time = 0;
  91. this.length = 0;
  92. this._accelTable = {};
  93. this.fps = 20;
  94. this.addKey = function ( key ) {
  95. this.keys.push( key );
  96. };
  97. this.init = function () {
  98. this.sortKeys();
  99. if ( this.keys.length > 0 )
  100. this.length = this.keys[ this.keys.length - 1 ].time;
  101. else
  102. this.length = 0;
  103. if ( ! this.fps ) return;
  104. for ( var j = 0; j < this.length * this.fps; j ++ ) {
  105. for ( var i = 0; i < this.keys.length; i ++ ) {
  106. if ( this.keys[ i ].time == j ) {
  107. this._accelTable[ j ] = i;
  108. break;
  109. } else if ( this.keys[ i ].time < j / this.fps && this.keys[ i + 1 ] && this.keys[ i + 1 ].time >= j / this.fps ) {
  110. this._accelTable[ j ] = i;
  111. break;
  112. }
  113. }
  114. }
  115. };
  116. this.parseFromThree = function ( data ) {
  117. var fps = data.fps;
  118. this.target = data.node;
  119. var track = data.hierarchy[ 0 ].keys;
  120. for ( var i = 0; i < track.length; i ++ ) {
  121. this.addKey( new Virtulous.KeyFrame( i / fps || track[ i ].time, track[ i ].targets[ 0 ].data ) );
  122. }
  123. this.init();
  124. };
  125. this.parseFromCollada = function ( data ) {
  126. var track = data.keys;
  127. var fps = this.fps;
  128. for ( var i = 0; i < track.length; i ++ ) {
  129. this.addKey( new Virtulous.KeyFrame( i / fps || track[ i ].time, track[ i ].matrix ) );
  130. }
  131. this.init();
  132. };
  133. this.sortKeys = function () {
  134. this.keys.sort( this.keySortFunc );
  135. };
  136. this.keySortFunc = function ( a, b ) {
  137. return a.time - b.time;
  138. };
  139. this.clone = function () {
  140. var t = new Virtulous.KeyFrameTrack();
  141. t.target = this.target;
  142. t.time = this.time;
  143. t.length = this.length;
  144. for ( var i = 0; i < this.keys.length; i ++ ) {
  145. t.addKey( this.keys[ i ].clone() );
  146. }
  147. t.init();
  148. return t;
  149. };
  150. this.reTarget = function ( root, compareitor ) {
  151. if ( ! compareitor ) compareitor = Virtulous.TrackTargetNodeNameCompare;
  152. this.target = compareitor( root, this.target );
  153. };
  154. this.keySearchAccel = function ( time ) {
  155. time *= this.fps;
  156. time = Math.floor( time );
  157. return this._accelTable[ time ] || 0;
  158. };
  159. this.setTime = function ( time ) {
  160. time = Math.abs( time );
  161. if ( this.length )
  162. time = time % this.length + .05;
  163. var key0 = null;
  164. var key1 = null;
  165. for ( var i = this.keySearchAccel( time ); i < this.keys.length; i ++ ) {
  166. if ( this.keys[ i ].time == time ) {
  167. key0 = this.keys[ i ];
  168. key1 = this.keys[ i ];
  169. break;
  170. } else if ( this.keys[ i ].time < time && this.keys[ i + 1 ] && this.keys[ i + 1 ].time > time ) {
  171. key0 = this.keys[ i ];
  172. key1 = this.keys[ i + 1 ];
  173. break;
  174. } else if ( this.keys[ i ].time < time && i == this.keys.length - 1 ) {
  175. key0 = this.keys[ i ];
  176. key1 = this.keys[ 0 ].clone();
  177. key1.time += this.length + .05;
  178. break;
  179. }
  180. }
  181. if ( key0 && key1 && key0 !== key1 ) {
  182. this.target.matrixAutoUpdate = false;
  183. this.target.matrix.copy( key0.lerp( key1, time ) );
  184. this.target.matrixWorldNeedsUpdate = true;
  185. return;
  186. }
  187. if ( key0 && key1 && key0 == key1 ) {
  188. this.target.matrixAutoUpdate = false;
  189. this.target.matrix.copy( key0.matrix );
  190. this.target.matrixWorldNeedsUpdate = true;
  191. return;
  192. }
  193. };
  194. };
  195. Virtulous.TrackTargetNodeNameCompare = function ( root, target ) {
  196. function find( node, name ) {
  197. if ( node.name == name )
  198. return node;
  199. for ( var i = 0; i < node.children.length; i ++ ) {
  200. var r = find( node.children[ i ], name );
  201. if ( r ) return r;
  202. }
  203. return null;
  204. }
  205. return find( root, target.name );
  206. };
  207. Virtulous.Animation = function () {
  208. this.tracks = [];
  209. this.length = 0;
  210. this.addTrack = function ( track ) {
  211. this.tracks.push( track );
  212. this.length = Math.max( track.length, this.length );
  213. };
  214. this.setTime = function ( time ) {
  215. this.time = time;
  216. for ( var i = 0; i < this.tracks.length; i ++ )
  217. this.tracks[ i ].setTime( time );
  218. };
  219. this.clone = function ( target, compareitor ) {
  220. if ( ! compareitor ) compareitor = Virtulous.TrackTargetNodeNameCompare;
  221. var n = new Virtulous.Animation();
  222. n.target = target;
  223. for ( var i = 0; i < this.tracks.length; i ++ ) {
  224. var track = this.tracks[ i ].clone();
  225. track.reTarget( target, compareitor );
  226. n.addTrack( track );
  227. }
  228. return n;
  229. };
  230. };
  231. var ASSBIN_CHUNK_AICAMERA = 0x1234;
  232. var ASSBIN_CHUNK_AILIGHT = 0x1235;
  233. var ASSBIN_CHUNK_AITEXTURE = 0x1236;
  234. var ASSBIN_CHUNK_AIMESH = 0x1237;
  235. var ASSBIN_CHUNK_AINODEANIM = 0x1238;
  236. var ASSBIN_CHUNK_AISCENE = 0x1239;
  237. var ASSBIN_CHUNK_AIBONE = 0x123a;
  238. var ASSBIN_CHUNK_AIANIMATION = 0x123b;
  239. var ASSBIN_CHUNK_AINODE = 0x123c;
  240. var ASSBIN_CHUNK_AIMATERIAL = 0x123d;
  241. var ASSBIN_CHUNK_AIMATERIALPROPERTY = 0x123e;
  242. var ASSBIN_MESH_HAS_POSITIONS = 0x1;
  243. var ASSBIN_MESH_HAS_NORMALS = 0x2;
  244. var ASSBIN_MESH_HAS_TANGENTS_AND_BITANGENTS = 0x4;
  245. var ASSBIN_MESH_HAS_TEXCOORD_BASE = 0x100;
  246. var ASSBIN_MESH_HAS_COLOR_BASE = 0x10000;
  247. var AI_MAX_NUMBER_OF_COLOR_SETS = 1;
  248. var AI_MAX_NUMBER_OF_TEXTURECOORDS = 4;
  249. //var aiLightSource_UNDEFINED = 0x0;
  250. //! A directional light source has a well-defined direction
  251. //! but is infinitely far away. That's quite a good
  252. //! approximation for sun light.
  253. var aiLightSource_DIRECTIONAL = 0x1;
  254. //! A point light source has a well-defined position
  255. //! in space but no direction - it emits light in all
  256. //! directions. A normal bulb is a point light.
  257. //var aiLightSource_POINT = 0x2;
  258. //! A spot light source emits light in a specific
  259. //! angle. It has a position and a direction it is pointing to.
  260. //! A good example for a spot light is a light spot in
  261. //! sport arenas.
  262. var aiLightSource_SPOT = 0x3;
  263. //! The generic light level of the world, including the bounces
  264. //! of all other lightsources.
  265. //! Typically, there's at most one ambient light in a scene.
  266. //! This light type doesn't have a valid position, direction, or
  267. //! other properties, just a color.
  268. //var aiLightSource_AMBIENT = 0x4;
  269. /** Flat shading. Shading is done on per-face base,
  270. * diffuse only. Also known as 'faceted shading'.
  271. */
  272. //var aiShadingMode_Flat = 0x1;
  273. /** Simple Gouraud shading.
  274. */
  275. //var aiShadingMode_Gouraud = 0x2;
  276. /** Phong-Shading -
  277. */
  278. //var aiShadingMode_Phong = 0x3;
  279. /** Phong-Blinn-Shading
  280. */
  281. //var aiShadingMode_Blinn = 0x4;
  282. /** Toon-Shading per pixel
  283. *
  284. * Also known as 'comic' shader.
  285. */
  286. //var aiShadingMode_Toon = 0x5;
  287. /** OrenNayar-Shading per pixel
  288. *
  289. * Extension to standard Lambertian shading, taking the
  290. * roughness of the material into account
  291. */
  292. //var aiShadingMode_OrenNayar = 0x6;
  293. /** Minnaert-Shading per pixel
  294. *
  295. * Extension to standard Lambertian shading, taking the
  296. * "darkness" of the material into account
  297. */
  298. //var aiShadingMode_Minnaert = 0x7;
  299. /** CookTorrance-Shading per pixel
  300. *
  301. * Special shader for metallic surfaces.
  302. */
  303. //var aiShadingMode_CookTorrance = 0x8;
  304. /** No shading at all. Constant light influence of 1.0.
  305. */
  306. //var aiShadingMode_NoShading = 0x9;
  307. /** Fresnel shading
  308. */
  309. //var aiShadingMode_Fresnel = 0xa;
  310. //var aiTextureType_NONE = 0x0;
  311. /** The texture is combined with the result of the diffuse
  312. * lighting equation.
  313. */
  314. var aiTextureType_DIFFUSE = 0x1;
  315. /** The texture is combined with the result of the specular
  316. * lighting equation.
  317. */
  318. //var aiTextureType_SPECULAR = 0x2;
  319. /** The texture is combined with the result of the ambient
  320. * lighting equation.
  321. */
  322. //var aiTextureType_AMBIENT = 0x3;
  323. /** The texture is added to the result of the lighting
  324. * calculation. It isn't influenced by incoming light.
  325. */
  326. //var aiTextureType_EMISSIVE = 0x4;
  327. /** The texture is a height map.
  328. *
  329. * By convention, higher gray-scale values stand for
  330. * higher elevations from the base height.
  331. */
  332. //var aiTextureType_HEIGHT = 0x5;
  333. /** The texture is a (tangent space) normal-map.
  334. *
  335. * Again, there are several conventions for tangent-space
  336. * normal maps. Assimp does (intentionally) not
  337. * distinguish here.
  338. */
  339. var aiTextureType_NORMALS = 0x6;
  340. /** The texture defines the glossiness of the material.
  341. *
  342. * The glossiness is in fact the exponent of the specular
  343. * (phong) lighting equation. Usually there is a conversion
  344. * function defined to map the linear color values in the
  345. * texture to a suitable exponent. Have fun.
  346. */
  347. //var aiTextureType_SHININESS = 0x7;
  348. /** The texture defines per-pixel opacity.
  349. *
  350. * Usually 'white' means opaque and 'black' means
  351. * 'transparency'. Or quite the opposite. Have fun.
  352. */
  353. var aiTextureType_OPACITY = 0x8;
  354. /** Displacement texture
  355. *
  356. * The exact purpose and format is application-dependent.
  357. * Higher color values stand for higher vertex displacements.
  358. */
  359. //var aiTextureType_DISPLACEMENT = 0x9;
  360. /** Lightmap texture (aka Ambient Occlusion)
  361. *
  362. * Both 'Lightmaps' and dedicated 'ambient occlusion maps' are
  363. * covered by this material property. The texture contains a
  364. * scaling value for the final color value of a pixel. Its
  365. * intensity is not affected by incoming light.
  366. */
  367. var aiTextureType_LIGHTMAP = 0xA;
  368. /** Reflection texture
  369. *
  370. * Contains the color of a perfect mirror reflection.
  371. * Rarely used, almost never for real-time applications.
  372. */
  373. //var aiTextureType_REFLECTION = 0xB;
  374. /** Unknown texture
  375. *
  376. * A texture reference that does not match any of the definitions
  377. * above is considered to be 'unknown'. It is still imported,
  378. * but is excluded from any further postprocessing.
  379. */
  380. //var aiTextureType_UNKNOWN = 0xC;
  381. var BONESPERVERT = 4;
  382. function ASSBIN_MESH_HAS_TEXCOORD( n ) {
  383. return ASSBIN_MESH_HAS_TEXCOORD_BASE << n;
  384. }
  385. function ASSBIN_MESH_HAS_COLOR( n ) {
  386. return ASSBIN_MESH_HAS_COLOR_BASE << n;
  387. }
  388. function markBones( scene ) {
  389. for ( var i in scene.mMeshes ) {
  390. var mesh = scene.mMeshes[ i ];
  391. for ( var k in mesh.mBones ) {
  392. var boneNode = scene.findNode( mesh.mBones[ k ].mName );
  393. if ( boneNode )
  394. boneNode.isBone = true;
  395. }
  396. }
  397. }
  398. function cloneTreeToBones( root, scene ) {
  399. var rootBone = new Bone();
  400. rootBone.matrix.copy( root.matrix );
  401. rootBone.matrixWorld.copy( root.matrixWorld );
  402. rootBone.position.copy( root.position );
  403. rootBone.quaternion.copy( root.quaternion );
  404. rootBone.scale.copy( root.scale );
  405. scene.nodeCount ++;
  406. rootBone.name = 'bone_' + root.name + scene.nodeCount.toString();
  407. if ( ! scene.nodeToBoneMap[ root.name ] )
  408. scene.nodeToBoneMap[ root.name ] = [];
  409. scene.nodeToBoneMap[ root.name ].push( rootBone );
  410. for ( var i in root.children ) {
  411. var child = cloneTreeToBones( root.children[ i ], scene );
  412. rootBone.add( child );
  413. }
  414. return rootBone;
  415. }
  416. function sortWeights( indexes, weights ) {
  417. var pairs = [];
  418. for ( var i = 0; i < indexes.length; i ++ ) {
  419. pairs.push( {
  420. i: indexes[ i ],
  421. w: weights[ i ]
  422. } );
  423. }
  424. pairs.sort( function ( a, b ) {
  425. return b.w - a.w;
  426. } );
  427. while ( pairs.length < 4 ) {
  428. pairs.push( {
  429. i: 0,
  430. w: 0
  431. } );
  432. }
  433. if ( pairs.length > 4 )
  434. pairs.length = 4;
  435. var sum = 0;
  436. for ( var i = 0; i < 4; i ++ ) {
  437. sum += pairs[ i ].w * pairs[ i ].w;
  438. }
  439. sum = Math.sqrt( sum );
  440. for ( var i = 0; i < 4; i ++ ) {
  441. pairs[ i ].w = pairs[ i ].w / sum;
  442. indexes[ i ] = pairs[ i ].i;
  443. weights[ i ] = pairs[ i ].w;
  444. }
  445. }
  446. function findMatchingBone( root, name ) {
  447. if ( root.name.indexOf( 'bone_' + name ) == 0 )
  448. return root;
  449. for ( var i in root.children ) {
  450. var ret = findMatchingBone( root.children[ i ], name );
  451. if ( ret )
  452. return ret;
  453. }
  454. return undefined;
  455. }
  456. function aiMesh() {
  457. this.mPrimitiveTypes = 0;
  458. this.mNumVertices = 0;
  459. this.mNumFaces = 0;
  460. this.mNumBones = 0;
  461. this.mMaterialIndex = 0;
  462. this.mVertices = [];
  463. this.mNormals = [];
  464. this.mTangents = [];
  465. this.mBitangents = [];
  466. this.mColors = [
  467. []
  468. ];
  469. this.mTextureCoords = [
  470. []
  471. ];
  472. this.mFaces = [];
  473. this.mBones = [];
  474. this.hookupSkeletons = function ( scene ) {
  475. if ( this.mBones.length == 0 ) return;
  476. var allBones = [];
  477. var offsetMatrix = [];
  478. var skeletonRoot = scene.findNode( this.mBones[ 0 ].mName );
  479. while ( skeletonRoot.mParent && skeletonRoot.mParent.isBone ) {
  480. skeletonRoot = skeletonRoot.mParent;
  481. }
  482. var threeSkeletonRoot = skeletonRoot.toTHREE( scene );
  483. var threeSkeletonRootBone = cloneTreeToBones( threeSkeletonRoot, scene );
  484. this.threeNode.add( threeSkeletonRootBone );
  485. for ( var i = 0; i < this.mBones.length; i ++ ) {
  486. var bone = findMatchingBone( threeSkeletonRootBone, this.mBones[ i ].mName );
  487. if ( bone ) {
  488. var tbone = bone;
  489. allBones.push( tbone );
  490. //tbone.matrixAutoUpdate = false;
  491. offsetMatrix.push( this.mBones[ i ].mOffsetMatrix.toTHREE() );
  492. } else {
  493. var skeletonRoot = scene.findNode( this.mBones[ i ].mName );
  494. if ( ! skeletonRoot ) return;
  495. var threeSkeletonRoot = skeletonRoot.toTHREE( scene );
  496. var threeSkeletonRootBone = cloneTreeToBones( threeSkeletonRoot, scene );
  497. this.threeNode.add( threeSkeletonRootBone );
  498. var bone = findMatchingBone( threeSkeletonRootBone, this.mBones[ i ].mName );
  499. var tbone = bone;
  500. allBones.push( tbone );
  501. //tbone.matrixAutoUpdate = false;
  502. offsetMatrix.push( this.mBones[ i ].mOffsetMatrix.toTHREE() );
  503. }
  504. }
  505. var skeleton = new Skeleton( allBones, offsetMatrix );
  506. this.threeNode.bind( skeleton, new Matrix4() );
  507. this.threeNode.material.skinning = true;
  508. };
  509. this.toTHREE = function ( scene ) {
  510. if ( this.threeNode ) return this.threeNode;
  511. var geometry = new BufferGeometry();
  512. var mat;
  513. if ( scene.mMaterials[ this.mMaterialIndex ] )
  514. mat = scene.mMaterials[ this.mMaterialIndex ].toTHREE( scene );
  515. else
  516. mat = new MeshLambertMaterial();
  517. geometry.setIndex( new BufferAttribute( new Uint32Array( this.mIndexArray ), 1 ) );
  518. geometry.setAttribute( 'position', new BufferAttribute( this.mVertexBuffer, 3 ) );
  519. if ( this.mNormalBuffer && this.mNormalBuffer.length > 0 )
  520. geometry.setAttribute( 'normal', new BufferAttribute( this.mNormalBuffer, 3 ) );
  521. if ( this.mColorBuffer && this.mColorBuffer.length > 0 )
  522. geometry.setAttribute( 'color', new BufferAttribute( this.mColorBuffer, 4 ) );
  523. if ( this.mTexCoordsBuffers[ 0 ] && this.mTexCoordsBuffers[ 0 ].length > 0 )
  524. geometry.setAttribute( 'uv', new BufferAttribute( new Float32Array( this.mTexCoordsBuffers[ 0 ] ), 2 ) );
  525. if ( this.mTexCoordsBuffers[ 1 ] && this.mTexCoordsBuffers[ 1 ].length > 0 )
  526. geometry.setAttribute( 'uv1', new BufferAttribute( new Float32Array( this.mTexCoordsBuffers[ 1 ] ), 2 ) );
  527. if ( this.mTangentBuffer && this.mTangentBuffer.length > 0 )
  528. geometry.setAttribute( 'tangents', new BufferAttribute( this.mTangentBuffer, 3 ) );
  529. if ( this.mBitangentBuffer && this.mBitangentBuffer.length > 0 )
  530. geometry.setAttribute( 'bitangents', new BufferAttribute( this.mBitangentBuffer, 3 ) );
  531. if ( this.mBones.length > 0 ) {
  532. var weights = [];
  533. var bones = [];
  534. for ( var i = 0; i < this.mBones.length; i ++ ) {
  535. for ( var j = 0; j < this.mBones[ i ].mWeights.length; j ++ ) {
  536. var weight = this.mBones[ i ].mWeights[ j ];
  537. if ( weight ) {
  538. if ( ! weights[ weight.mVertexId ] ) weights[ weight.mVertexId ] = [];
  539. if ( ! bones[ weight.mVertexId ] ) bones[ weight.mVertexId ] = [];
  540. weights[ weight.mVertexId ].push( weight.mWeight );
  541. bones[ weight.mVertexId ].push( parseInt( i ) );
  542. }
  543. }
  544. }
  545. for ( var i in bones ) {
  546. sortWeights( bones[ i ], weights[ i ] );
  547. }
  548. var _weights = [];
  549. var _bones = [];
  550. for ( var i = 0; i < weights.length; i ++ ) {
  551. for ( var j = 0; j < 4; j ++ ) {
  552. if ( weights[ i ] && bones[ i ] ) {
  553. _weights.push( weights[ i ][ j ] );
  554. _bones.push( bones[ i ][ j ] );
  555. } else {
  556. _weights.push( 0 );
  557. _bones.push( 0 );
  558. }
  559. }
  560. }
  561. geometry.setAttribute( 'skinWeight', new BufferAttribute( new Float32Array( _weights ), BONESPERVERT ) );
  562. geometry.setAttribute( 'skinIndex', new BufferAttribute( new Float32Array( _bones ), BONESPERVERT ) );
  563. }
  564. var mesh;
  565. if ( this.mBones.length == 0 )
  566. mesh = new Mesh( geometry, mat );
  567. if ( this.mBones.length > 0 ) {
  568. mesh = new SkinnedMesh( geometry, mat );
  569. mesh.normalizeSkinWeights();
  570. }
  571. this.threeNode = mesh;
  572. //mesh.matrixAutoUpdate = false;
  573. return mesh;
  574. };
  575. }
  576. function aiFace() {
  577. this.mNumIndices = 0;
  578. this.mIndices = [];
  579. }
  580. function aiVector3D() {
  581. this.x = 0;
  582. this.y = 0;
  583. this.z = 0;
  584. this.toTHREE = function () {
  585. return new Vector3( this.x, this.y, this.z );
  586. };
  587. }
  588. function aiColor3D() {
  589. this.r = 0;
  590. this.g = 0;
  591. this.b = 0;
  592. this.a = 0;
  593. this.toTHREE = function () {
  594. return new Color( this.r, this.g, this.b );
  595. };
  596. }
  597. function aiQuaternion() {
  598. this.x = 0;
  599. this.y = 0;
  600. this.z = 0;
  601. this.w = 0;
  602. this.toTHREE = function () {
  603. return new Quaternion( this.x, this.y, this.z, this.w );
  604. };
  605. }
  606. function aiVertexWeight() {
  607. this.mVertexId = 0;
  608. this.mWeight = 0;
  609. }
  610. function aiString() {
  611. this.data = [];
  612. this.toString = function () {
  613. var str = '';
  614. this.data.forEach( function ( i ) {
  615. str += ( String.fromCharCode( i ) );
  616. } );
  617. return str.replace( /[^\x20-\x7E]+/g, '' );
  618. };
  619. }
  620. function aiVectorKey() {
  621. this.mTime = 0;
  622. this.mValue = null;
  623. }
  624. function aiQuatKey() {
  625. this.mTime = 0;
  626. this.mValue = null;
  627. }
  628. function aiNode() {
  629. this.mName = '';
  630. this.mTransformation = [];
  631. this.mNumChildren = 0;
  632. this.mNumMeshes = 0;
  633. this.mMeshes = [];
  634. this.mChildren = [];
  635. this.toTHREE = function ( scene ) {
  636. if ( this.threeNode ) return this.threeNode;
  637. var o = new Object3D();
  638. o.name = this.mName;
  639. o.matrix = this.mTransformation.toTHREE();
  640. for ( var i = 0; i < this.mChildren.length; i ++ ) {
  641. o.add( this.mChildren[ i ].toTHREE( scene ) );
  642. }
  643. for ( var i = 0; i < this.mMeshes.length; i ++ ) {
  644. o.add( scene.mMeshes[ this.mMeshes[ i ] ].toTHREE( scene ) );
  645. }
  646. this.threeNode = o;
  647. //o.matrixAutoUpdate = false;
  648. o.matrix.decompose( o.position, o.quaternion, o.scale );
  649. return o;
  650. };
  651. }
  652. function aiBone() {
  653. this.mName = '';
  654. this.mNumWeights = 0;
  655. this.mOffsetMatrix = 0;
  656. }
  657. function aiMaterialProperty() {
  658. this.mKey = '';
  659. this.mSemantic = 0;
  660. this.mIndex = 0;
  661. this.mData = [];
  662. this.mDataLength = 0;
  663. this.mType = 0;
  664. this.dataAsColor = function () {
  665. var array = ( new Uint8Array( this.mData ) ).buffer;
  666. var reader = new DataView( array );
  667. var r = reader.getFloat32( 0, true );
  668. var g = reader.getFloat32( 4, true );
  669. var b = reader.getFloat32( 8, true );
  670. //var a = reader.getFloat32(12, true);
  671. return new Color( r, g, b );
  672. };
  673. this.dataAsFloat = function () {
  674. var array = ( new Uint8Array( this.mData ) ).buffer;
  675. var reader = new DataView( array );
  676. var r = reader.getFloat32( 0, true );
  677. return r;
  678. };
  679. this.dataAsBool = function () {
  680. var array = ( new Uint8Array( this.mData ) ).buffer;
  681. var reader = new DataView( array );
  682. var r = reader.getFloat32( 0, true );
  683. return !! r;
  684. };
  685. this.dataAsString = function () {
  686. var s = new aiString();
  687. s.data = this.mData;
  688. return s.toString();
  689. };
  690. this.dataAsMap = function () {
  691. var s = new aiString();
  692. s.data = this.mData;
  693. var path = s.toString();
  694. path = path.replace( /\\/g, '/' );
  695. if ( path.indexOf( '/' ) != - 1 ) {
  696. path = path.substr( path.lastIndexOf( '/' ) + 1 );
  697. }
  698. return textureLoader.load( path );
  699. };
  700. }
  701. var namePropMapping = {
  702. '?mat.name': 'name',
  703. '$mat.shadingm': 'shading',
  704. '$mat.twosided': 'twoSided',
  705. '$mat.wireframe': 'wireframe',
  706. '$clr.ambient': 'ambient',
  707. '$clr.diffuse': 'color',
  708. '$clr.specular': 'specular',
  709. '$clr.emissive': 'emissive',
  710. '$clr.transparent': 'transparent',
  711. '$clr.reflective': 'reflect',
  712. '$mat.shininess': 'shininess',
  713. '$mat.reflectivity': 'reflectivity',
  714. '$mat.refracti': 'refraction',
  715. '$tex.file': 'map'
  716. };
  717. var nameTypeMapping = {
  718. '?mat.name': 'string',
  719. '$mat.shadingm': 'bool',
  720. '$mat.twosided': 'bool',
  721. '$mat.wireframe': 'bool',
  722. '$clr.ambient': 'color',
  723. '$clr.diffuse': 'color',
  724. '$clr.specular': 'color',
  725. '$clr.emissive': 'color',
  726. '$clr.transparent': 'color',
  727. '$clr.reflective': 'color',
  728. '$mat.shininess': 'float',
  729. '$mat.reflectivity': 'float',
  730. '$mat.refracti': 'float',
  731. '$tex.file': 'map'
  732. };
  733. function aiMaterial() {
  734. this.mNumAllocated = 0;
  735. this.mNumProperties = 0;
  736. this.mProperties = [];
  737. this.toTHREE = function () {
  738. var mat = new MeshPhongMaterial();
  739. for ( var i = 0; i < this.mProperties.length; i ++ ) {
  740. if ( nameTypeMapping[ this.mProperties[ i ].mKey ] == 'float' )
  741. mat[ namePropMapping[ this.mProperties[ i ].mKey ] ] = this.mProperties[ i ].dataAsFloat();
  742. if ( nameTypeMapping[ this.mProperties[ i ].mKey ] == 'color' )
  743. mat[ namePropMapping[ this.mProperties[ i ].mKey ] ] = this.mProperties[ i ].dataAsColor();
  744. if ( nameTypeMapping[ this.mProperties[ i ].mKey ] == 'bool' )
  745. mat[ namePropMapping[ this.mProperties[ i ].mKey ] ] = this.mProperties[ i ].dataAsBool();
  746. if ( nameTypeMapping[ this.mProperties[ i ].mKey ] == 'string' )
  747. mat[ namePropMapping[ this.mProperties[ i ].mKey ] ] = this.mProperties[ i ].dataAsString();
  748. if ( nameTypeMapping[ this.mProperties[ i ].mKey ] == 'map' ) {
  749. var prop = this.mProperties[ i ];
  750. if ( prop.mSemantic == aiTextureType_DIFFUSE )
  751. mat.map = this.mProperties[ i ].dataAsMap();
  752. if ( prop.mSemantic == aiTextureType_NORMALS )
  753. mat.normalMap = this.mProperties[ i ].dataAsMap();
  754. if ( prop.mSemantic == aiTextureType_LIGHTMAP )
  755. mat.lightMap = this.mProperties[ i ].dataAsMap();
  756. if ( prop.mSemantic == aiTextureType_OPACITY )
  757. mat.alphaMap = this.mProperties[ i ].dataAsMap();
  758. }
  759. }
  760. mat.ambient.r = .53;
  761. mat.ambient.g = .53;
  762. mat.ambient.b = .53;
  763. mat.color.r = 1;
  764. mat.color.g = 1;
  765. mat.color.b = 1;
  766. return mat;
  767. };
  768. }
  769. function veclerp( v1, v2, l ) {
  770. var v = new Vector3();
  771. var lm1 = 1 - l;
  772. v.x = v1.x * l + v2.x * lm1;
  773. v.y = v1.y * l + v2.y * lm1;
  774. v.z = v1.z * l + v2.z * lm1;
  775. return v;
  776. }
  777. function quatlerp( q1, q2, l ) {
  778. return q1.clone().slerp( q2, 1 - l );
  779. }
  780. function sampleTrack( keys, time, lne, lerp ) {
  781. if ( keys.length == 1 ) return keys[ 0 ].mValue.toTHREE();
  782. var dist = Infinity;
  783. var key = null;
  784. var nextKey = null;
  785. for ( var i = 0; i < keys.length; i ++ ) {
  786. var timeDist = Math.abs( keys[ i ].mTime - time );
  787. if ( timeDist < dist && keys[ i ].mTime <= time ) {
  788. dist = timeDist;
  789. key = keys[ i ];
  790. nextKey = keys[ i + 1 ];
  791. }
  792. }
  793. if ( ! key ) {
  794. return null;
  795. } else if ( nextKey ) {
  796. var dT = nextKey.mTime - key.mTime;
  797. var T = key.mTime - time;
  798. var l = T / dT;
  799. return lerp( key.mValue.toTHREE(), nextKey.mValue.toTHREE(), l );
  800. } else {
  801. nextKey = keys[ 0 ].clone();
  802. nextKey.mTime += lne;
  803. var dT = nextKey.mTime - key.mTime;
  804. var T = key.mTime - time;
  805. var l = T / dT;
  806. return lerp( key.mValue.toTHREE(), nextKey.mValue.toTHREE(), l );
  807. }
  808. }
  809. function aiNodeAnim() {
  810. this.mNodeName = '';
  811. this.mNumPositionKeys = 0;
  812. this.mNumRotationKeys = 0;
  813. this.mNumScalingKeys = 0;
  814. this.mPositionKeys = [];
  815. this.mRotationKeys = [];
  816. this.mScalingKeys = [];
  817. this.mPreState = '';
  818. this.mPostState = '';
  819. this.init = function ( tps ) {
  820. if ( ! tps ) tps = 1;
  821. function t( t ) {
  822. t.mTime /= tps;
  823. }
  824. this.mPositionKeys.forEach( t );
  825. this.mRotationKeys.forEach( t );
  826. this.mScalingKeys.forEach( t );
  827. };
  828. this.sortKeys = function () {
  829. function comp( a, b ) {
  830. return a.mTime - b.mTime;
  831. }
  832. this.mPositionKeys.sort( comp );
  833. this.mRotationKeys.sort( comp );
  834. this.mScalingKeys.sort( comp );
  835. };
  836. this.getLength = function () {
  837. return Math.max(
  838. Math.max.apply( null, this.mPositionKeys.map( function ( a ) {
  839. return a.mTime;
  840. } ) ),
  841. Math.max.apply( null, this.mRotationKeys.map( function ( a ) {
  842. return a.mTime;
  843. } ) ),
  844. Math.max.apply( null, this.mScalingKeys.map( function ( a ) {
  845. return a.mTime;
  846. } ) )
  847. );
  848. };
  849. this.toTHREE = function ( o ) {
  850. this.sortKeys();
  851. var length = this.getLength();
  852. var track = new Virtulous.KeyFrameTrack();
  853. for ( var i = 0; i < length; i += .05 ) {
  854. var matrix = new Matrix4();
  855. var time = i;
  856. var pos = sampleTrack( this.mPositionKeys, time, length, veclerp );
  857. var scale = sampleTrack( this.mScalingKeys, time, length, veclerp );
  858. var rotation = sampleTrack( this.mRotationKeys, time, length, quatlerp );
  859. matrix.compose( pos, rotation, scale );
  860. var key = new Virtulous.KeyFrame( time, matrix );
  861. track.addKey( key );
  862. }
  863. track.target = o.findNode( this.mNodeName ).toTHREE();
  864. var tracks = [ track ];
  865. if ( o.nodeToBoneMap[ this.mNodeName ] ) {
  866. for ( var i = 0; i < o.nodeToBoneMap[ this.mNodeName ].length; i ++ ) {
  867. var t2 = track.clone();
  868. t2.target = o.nodeToBoneMap[ this.mNodeName ][ i ];
  869. tracks.push( t2 );
  870. }
  871. }
  872. return tracks;
  873. };
  874. }
  875. function aiAnimation() {
  876. this.mName = '';
  877. this.mDuration = 0;
  878. this.mTicksPerSecond = 0;
  879. this.mNumChannels = 0;
  880. this.mChannels = [];
  881. this.toTHREE = function ( root ) {
  882. var animationHandle = new Virtulous.Animation();
  883. for ( var i in this.mChannels ) {
  884. this.mChannels[ i ].init( this.mTicksPerSecond );
  885. var tracks = this.mChannels[ i ].toTHREE( root );
  886. for ( var j in tracks ) {
  887. tracks[ j ].init();
  888. animationHandle.addTrack( tracks[ j ] );
  889. }
  890. }
  891. animationHandle.length = Math.max.apply( null, animationHandle.tracks.map( function ( e ) {
  892. return e.length;
  893. } ) );
  894. return animationHandle;
  895. };
  896. }
  897. function aiTexture() {
  898. this.mWidth = 0;
  899. this.mHeight = 0;
  900. this.texAchFormatHint = [];
  901. this.pcData = [];
  902. }
  903. function aiLight() {
  904. this.mName = '';
  905. this.mType = 0;
  906. this.mAttenuationConstant = 0;
  907. this.mAttenuationLinear = 0;
  908. this.mAttenuationQuadratic = 0;
  909. this.mAngleInnerCone = 0;
  910. this.mAngleOuterCone = 0;
  911. this.mColorDiffuse = null;
  912. this.mColorSpecular = null;
  913. this.mColorAmbient = null;
  914. }
  915. function aiCamera() {
  916. this.mName = '';
  917. this.mPosition = null;
  918. this.mLookAt = null;
  919. this.mUp = null;
  920. this.mHorizontalFOV = 0;
  921. this.mClipPlaneNear = 0;
  922. this.mClipPlaneFar = 0;
  923. this.mAspect = 0;
  924. }
  925. function aiScene() {
  926. this.versionMajor = 0;
  927. this.versionMinor = 0;
  928. this.versionRevision = 0;
  929. this.compileFlags = 0;
  930. this.mFlags = 0;
  931. this.mNumMeshes = 0;
  932. this.mNumMaterials = 0;
  933. this.mNumAnimations = 0;
  934. this.mNumTextures = 0;
  935. this.mNumLights = 0;
  936. this.mNumCameras = 0;
  937. this.mRootNode = null;
  938. this.mMeshes = [];
  939. this.mMaterials = [];
  940. this.mAnimations = [];
  941. this.mLights = [];
  942. this.mCameras = [];
  943. this.nodeToBoneMap = {};
  944. this.findNode = function ( name, root ) {
  945. if ( ! root ) {
  946. root = this.mRootNode;
  947. }
  948. if ( root.mName == name ) {
  949. return root;
  950. }
  951. for ( var i = 0; i < root.mChildren.length; i ++ ) {
  952. var ret = this.findNode( name, root.mChildren[ i ] );
  953. if ( ret ) return ret;
  954. }
  955. return null;
  956. };
  957. this.toTHREE = function () {
  958. this.nodeCount = 0;
  959. markBones( this );
  960. var o = this.mRootNode.toTHREE( this );
  961. for ( var i in this.mMeshes )
  962. this.mMeshes[ i ].hookupSkeletons( this );
  963. if ( this.mAnimations.length > 0 ) {
  964. var a = this.mAnimations[ 0 ].toTHREE( this );
  965. }
  966. return { object: o, animation: a };
  967. };
  968. }
  969. function aiMatrix4() {
  970. this.elements = [
  971. [],
  972. [],
  973. [],
  974. []
  975. ];
  976. this.toTHREE = function () {
  977. var m = new Matrix4();
  978. for ( var i = 0; i < 4; ++ i ) {
  979. for ( var i2 = 0; i2 < 4; ++ i2 ) {
  980. m.elements[ i * 4 + i2 ] = this.elements[ i2 ][ i ];
  981. }
  982. }
  983. return m;
  984. };
  985. }
  986. var littleEndian = true;
  987. function readFloat( dataview ) {
  988. var val = dataview.getFloat32( dataview.readOffset, littleEndian );
  989. dataview.readOffset += 4;
  990. return val;
  991. }
  992. function Read_double( dataview ) {
  993. var val = dataview.getFloat64( dataview.readOffset, littleEndian );
  994. dataview.readOffset += 8;
  995. return val;
  996. }
  997. function Read_uint8_t( dataview ) {
  998. var val = dataview.getUint8( dataview.readOffset );
  999. dataview.readOffset += 1;
  1000. return val;
  1001. }
  1002. function Read_uint16_t( dataview ) {
  1003. var val = dataview.getUint16( dataview.readOffset, littleEndian );
  1004. dataview.readOffset += 2;
  1005. return val;
  1006. }
  1007. function Read_unsigned_int( dataview ) {
  1008. var val = dataview.getUint32( dataview.readOffset, littleEndian );
  1009. dataview.readOffset += 4;
  1010. return val;
  1011. }
  1012. function Read_uint32_t( dataview ) {
  1013. var val = dataview.getUint32( dataview.readOffset, littleEndian );
  1014. dataview.readOffset += 4;
  1015. return val;
  1016. }
  1017. function Read_aiVector3D( stream ) {
  1018. var v = new aiVector3D();
  1019. v.x = readFloat( stream );
  1020. v.y = readFloat( stream );
  1021. v.z = readFloat( stream );
  1022. return v;
  1023. }
  1024. function Read_aiColor3D( stream ) {
  1025. var c = new aiColor3D();
  1026. c.r = readFloat( stream );
  1027. c.g = readFloat( stream );
  1028. c.b = readFloat( stream );
  1029. return c;
  1030. }
  1031. function Read_aiQuaternion( stream ) {
  1032. var v = new aiQuaternion();
  1033. v.w = readFloat( stream );
  1034. v.x = readFloat( stream );
  1035. v.y = readFloat( stream );
  1036. v.z = readFloat( stream );
  1037. return v;
  1038. }
  1039. function Read_aiString( stream ) {
  1040. var s = new aiString();
  1041. var stringlengthbytes = Read_unsigned_int( stream );
  1042. stream.ReadBytes( s.data, 1, stringlengthbytes );
  1043. return s.toString();
  1044. }
  1045. function Read_aiVertexWeight( stream ) {
  1046. var w = new aiVertexWeight();
  1047. w.mVertexId = Read_unsigned_int( stream );
  1048. w.mWeight = readFloat( stream );
  1049. return w;
  1050. }
  1051. function Read_aiMatrix4x4( stream ) {
  1052. var m = new aiMatrix4();
  1053. for ( var i = 0; i < 4; ++ i ) {
  1054. for ( var i2 = 0; i2 < 4; ++ i2 ) {
  1055. m.elements[ i ][ i2 ] = readFloat( stream );
  1056. }
  1057. }
  1058. return m;
  1059. }
  1060. function Read_aiVectorKey( stream ) {
  1061. var v = new aiVectorKey();
  1062. v.mTime = Read_double( stream );
  1063. v.mValue = Read_aiVector3D( stream );
  1064. return v;
  1065. }
  1066. function Read_aiQuatKey( stream ) {
  1067. var v = new aiQuatKey();
  1068. v.mTime = Read_double( stream );
  1069. v.mValue = Read_aiQuaternion( stream );
  1070. return v;
  1071. }
  1072. function ReadArray_aiVertexWeight( stream, data, size ) {
  1073. for ( var i = 0; i < size; i ++ ) data[ i ] = Read_aiVertexWeight( stream );
  1074. }
  1075. function ReadArray_aiVectorKey( stream, data, size ) {
  1076. for ( var i = 0; i < size; i ++ ) data[ i ] = Read_aiVectorKey( stream );
  1077. }
  1078. function ReadArray_aiQuatKey( stream, data, size ) {
  1079. for ( var i = 0; i < size; i ++ ) data[ i ] = Read_aiQuatKey( stream );
  1080. }
  1081. function ReadBounds( stream, T /*p*/, n ) {
  1082. // not sure what to do here, the data isn't really useful.
  1083. return stream.Seek( sizeof( T ) * n, aiOrigin_CUR ); // eslint-disable-line no-undef
  1084. }
  1085. function ai_assert( bool ) {
  1086. if ( ! bool )
  1087. throw ( 'asset failed' );
  1088. }
  1089. function ReadBinaryNode( stream, parent, depth ) {
  1090. var chunkID = Read_uint32_t( stream );
  1091. ai_assert( chunkID == ASSBIN_CHUNK_AINODE );
  1092. /*uint32_t size =*/
  1093. Read_uint32_t( stream );
  1094. var node = new aiNode();
  1095. node.mParent = parent;
  1096. node.mDepth = depth;
  1097. node.mName = Read_aiString( stream );
  1098. node.mTransformation = Read_aiMatrix4x4( stream );
  1099. node.mNumChildren = Read_unsigned_int( stream );
  1100. node.mNumMeshes = Read_unsigned_int( stream );
  1101. if ( node.mNumMeshes ) {
  1102. node.mMeshes = [];
  1103. for ( var i = 0; i < node.mNumMeshes; ++ i ) {
  1104. node.mMeshes[ i ] = Read_unsigned_int( stream );
  1105. }
  1106. }
  1107. if ( node.mNumChildren ) {
  1108. node.mChildren = [];
  1109. for ( var i = 0; i < node.mNumChildren; ++ i ) {
  1110. var node2 = ReadBinaryNode( stream, node, depth ++ );
  1111. node.mChildren[ i ] = node2;
  1112. }
  1113. }
  1114. return node;
  1115. }
  1116. // -----------------------------------------------------------------------------------
  1117. function ReadBinaryBone( stream, b ) {
  1118. var chunkID = Read_uint32_t( stream );
  1119. ai_assert( chunkID == ASSBIN_CHUNK_AIBONE );
  1120. /*uint32_t size =*/
  1121. Read_uint32_t( stream );
  1122. b.mName = Read_aiString( stream );
  1123. b.mNumWeights = Read_unsigned_int( stream );
  1124. b.mOffsetMatrix = Read_aiMatrix4x4( stream );
  1125. // for the moment we write dumb min/max values for the bones, too.
  1126. // maybe I'll add a better, hash-like solution later
  1127. if ( shortened ) {
  1128. ReadBounds( stream, b.mWeights, b.mNumWeights );
  1129. } else {
  1130. // else write as usual
  1131. b.mWeights = [];
  1132. ReadArray_aiVertexWeight( stream, b.mWeights, b.mNumWeights );
  1133. }
  1134. return b;
  1135. }
  1136. function ReadBinaryMesh( stream, mesh ) {
  1137. var chunkID = Read_uint32_t( stream );
  1138. ai_assert( chunkID == ASSBIN_CHUNK_AIMESH );
  1139. /*uint32_t size =*/
  1140. Read_uint32_t( stream );
  1141. mesh.mPrimitiveTypes = Read_unsigned_int( stream );
  1142. mesh.mNumVertices = Read_unsigned_int( stream );
  1143. mesh.mNumFaces = Read_unsigned_int( stream );
  1144. mesh.mNumBones = Read_unsigned_int( stream );
  1145. mesh.mMaterialIndex = Read_unsigned_int( stream );
  1146. mesh.mNumUVComponents = [];
  1147. // first of all, write bits for all existent vertex components
  1148. var c = Read_unsigned_int( stream );
  1149. if ( c & ASSBIN_MESH_HAS_POSITIONS ) {
  1150. if ( shortened ) {
  1151. ReadBounds( stream, mesh.mVertices, mesh.mNumVertices );
  1152. } else {
  1153. // else write as usual
  1154. mesh.mVertices = [];
  1155. mesh.mVertexBuffer = stream.subArray32( stream.readOffset, stream.readOffset + mesh.mNumVertices * 3 * 4 );
  1156. stream.Seek( mesh.mNumVertices * 3 * 4, aiOrigin_CUR );
  1157. }
  1158. }
  1159. if ( c & ASSBIN_MESH_HAS_NORMALS ) {
  1160. if ( shortened ) {
  1161. ReadBounds( stream, mesh.mNormals, mesh.mNumVertices );
  1162. } else {
  1163. // else write as usual
  1164. mesh.mNormals = [];
  1165. mesh.mNormalBuffer = stream.subArray32( stream.readOffset, stream.readOffset + mesh.mNumVertices * 3 * 4 );
  1166. stream.Seek( mesh.mNumVertices * 3 * 4, aiOrigin_CUR );
  1167. }
  1168. }
  1169. if ( c & ASSBIN_MESH_HAS_TANGENTS_AND_BITANGENTS ) {
  1170. if ( shortened ) {
  1171. ReadBounds( stream, mesh.mTangents, mesh.mNumVertices );
  1172. ReadBounds( stream, mesh.mBitangents, mesh.mNumVertices );
  1173. } else {
  1174. // else write as usual
  1175. mesh.mTangents = [];
  1176. mesh.mTangentBuffer = stream.subArray32( stream.readOffset, stream.readOffset + mesh.mNumVertices * 3 * 4 );
  1177. stream.Seek( mesh.mNumVertices * 3 * 4, aiOrigin_CUR );
  1178. mesh.mBitangents = [];
  1179. mesh.mBitangentBuffer = stream.subArray32( stream.readOffset, stream.readOffset + mesh.mNumVertices * 3 * 4 );
  1180. stream.Seek( mesh.mNumVertices * 3 * 4, aiOrigin_CUR );
  1181. }
  1182. }
  1183. for ( var n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS; ++ n ) {
  1184. if ( ! ( c & ASSBIN_MESH_HAS_COLOR( n ) ) ) break;
  1185. if ( shortened ) {
  1186. ReadBounds( stream, mesh.mColors[ n ], mesh.mNumVertices );
  1187. } else {
  1188. // else write as usual
  1189. mesh.mColors[ n ] = [];
  1190. mesh.mColorBuffer = stream.subArray32( stream.readOffset, stream.readOffset + mesh.mNumVertices * 4 * 4 );
  1191. stream.Seek( mesh.mNumVertices * 4 * 4, aiOrigin_CUR );
  1192. }
  1193. }
  1194. mesh.mTexCoordsBuffers = [];
  1195. for ( var n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++ n ) {
  1196. if ( ! ( c & ASSBIN_MESH_HAS_TEXCOORD( n ) ) ) break;
  1197. // write number of UV components
  1198. mesh.mNumUVComponents[ n ] = Read_unsigned_int( stream );
  1199. if ( shortened ) {
  1200. ReadBounds( stream, mesh.mTextureCoords[ n ], mesh.mNumVertices );
  1201. } else {
  1202. // else write as usual
  1203. mesh.mTextureCoords[ n ] = [];
  1204. //note that assbin always writes 3d texcoords
  1205. mesh.mTexCoordsBuffers[ n ] = [];
  1206. for ( var uv = 0; uv < mesh.mNumVertices; uv ++ ) {
  1207. mesh.mTexCoordsBuffers[ n ].push( readFloat( stream ) );
  1208. mesh.mTexCoordsBuffers[ n ].push( readFloat( stream ) );
  1209. readFloat( stream );
  1210. }
  1211. }
  1212. }
  1213. // write faces. There are no floating-point calculations involved
  1214. // in these, so we can write a simple hash over the face data
  1215. // to the dump file. We generate a single 32 Bit hash for 512 faces
  1216. // using Assimp's standard hashing function.
  1217. if ( shortened ) {
  1218. Read_unsigned_int( stream );
  1219. } else {
  1220. // else write as usual
  1221. // if there are less than 2^16 vertices, we can simply use 16 bit integers ...
  1222. mesh.mFaces = [];
  1223. mesh.mIndexArray = [];
  1224. for ( var i = 0; i < mesh.mNumFaces; ++ i ) {
  1225. var f = mesh.mFaces[ i ] = new aiFace();
  1226. // BOOST_STATIC_ASSERT(AI_MAX_FACE_INDICES <= 0xffff);
  1227. f.mNumIndices = Read_uint16_t( stream );
  1228. f.mIndices = [];
  1229. for ( var a = 0; a < f.mNumIndices; ++ a ) {
  1230. if ( mesh.mNumVertices < ( 1 << 16 ) ) {
  1231. f.mIndices[ a ] = Read_uint16_t( stream );
  1232. } else {
  1233. f.mIndices[ a ] = Read_unsigned_int( stream );
  1234. }
  1235. }
  1236. if ( f.mNumIndices === 3 ) {
  1237. mesh.mIndexArray.push( f.mIndices[ 0 ] );
  1238. mesh.mIndexArray.push( f.mIndices[ 1 ] );
  1239. mesh.mIndexArray.push( f.mIndices[ 2 ] );
  1240. } else if ( f.mNumIndices === 4 ) {
  1241. mesh.mIndexArray.push( f.mIndices[ 0 ] );
  1242. mesh.mIndexArray.push( f.mIndices[ 1 ] );
  1243. mesh.mIndexArray.push( f.mIndices[ 2 ] );
  1244. mesh.mIndexArray.push( f.mIndices[ 2 ] );
  1245. mesh.mIndexArray.push( f.mIndices[ 3 ] );
  1246. mesh.mIndexArray.push( f.mIndices[ 0 ] );
  1247. } else {
  1248. throw ( new Error( 'Sorry, can\'t currently triangulate polys. Use the triangulate preprocessor in Assimp.' ) );
  1249. }
  1250. }
  1251. }
  1252. // write bones
  1253. if ( mesh.mNumBones ) {
  1254. mesh.mBones = [];
  1255. for ( var a = 0; a < mesh.mNumBones; ++ a ) {
  1256. mesh.mBones[ a ] = new aiBone();
  1257. ReadBinaryBone( stream, mesh.mBones[ a ] );
  1258. }
  1259. }
  1260. }
  1261. function ReadBinaryMaterialProperty( stream, prop ) {
  1262. var chunkID = Read_uint32_t( stream );
  1263. ai_assert( chunkID == ASSBIN_CHUNK_AIMATERIALPROPERTY );
  1264. /*uint32_t size =*/
  1265. Read_uint32_t( stream );
  1266. prop.mKey = Read_aiString( stream );
  1267. prop.mSemantic = Read_unsigned_int( stream );
  1268. prop.mIndex = Read_unsigned_int( stream );
  1269. prop.mDataLength = Read_unsigned_int( stream );
  1270. prop.mType = Read_unsigned_int( stream );
  1271. prop.mData = [];
  1272. stream.ReadBytes( prop.mData, 1, prop.mDataLength );
  1273. }
  1274. // -----------------------------------------------------------------------------------
  1275. function ReadBinaryMaterial( stream, mat ) {
  1276. var chunkID = Read_uint32_t( stream );
  1277. ai_assert( chunkID == ASSBIN_CHUNK_AIMATERIAL );
  1278. /*uint32_t size =*/
  1279. Read_uint32_t( stream );
  1280. mat.mNumAllocated = mat.mNumProperties = Read_unsigned_int( stream );
  1281. if ( mat.mNumProperties ) {
  1282. if ( mat.mProperties ) {
  1283. delete mat.mProperties;
  1284. }
  1285. mat.mProperties = [];
  1286. for ( var i = 0; i < mat.mNumProperties; ++ i ) {
  1287. mat.mProperties[ i ] = new aiMaterialProperty();
  1288. ReadBinaryMaterialProperty( stream, mat.mProperties[ i ] );
  1289. }
  1290. }
  1291. }
  1292. function ReadBinaryNodeAnim( stream, nd ) {
  1293. var chunkID = Read_uint32_t( stream );
  1294. ai_assert( chunkID == ASSBIN_CHUNK_AINODEANIM );
  1295. /*uint32_t size =*/
  1296. Read_uint32_t( stream );
  1297. nd.mNodeName = Read_aiString( stream );
  1298. nd.mNumPositionKeys = Read_unsigned_int( stream );
  1299. nd.mNumRotationKeys = Read_unsigned_int( stream );
  1300. nd.mNumScalingKeys = Read_unsigned_int( stream );
  1301. nd.mPreState = Read_unsigned_int( stream );
  1302. nd.mPostState = Read_unsigned_int( stream );
  1303. if ( nd.mNumPositionKeys ) {
  1304. if ( shortened ) {
  1305. ReadBounds( stream, nd.mPositionKeys, nd.mNumPositionKeys );
  1306. } else {
  1307. // else write as usual
  1308. nd.mPositionKeys = [];
  1309. ReadArray_aiVectorKey( stream, nd.mPositionKeys, nd.mNumPositionKeys );
  1310. }
  1311. }
  1312. if ( nd.mNumRotationKeys ) {
  1313. if ( shortened ) {
  1314. ReadBounds( stream, nd.mRotationKeys, nd.mNumRotationKeys );
  1315. } else {
  1316. // else write as usual
  1317. nd.mRotationKeys = [];
  1318. ReadArray_aiQuatKey( stream, nd.mRotationKeys, nd.mNumRotationKeys );
  1319. }
  1320. }
  1321. if ( nd.mNumScalingKeys ) {
  1322. if ( shortened ) {
  1323. ReadBounds( stream, nd.mScalingKeys, nd.mNumScalingKeys );
  1324. } else {
  1325. // else write as usual
  1326. nd.mScalingKeys = [];
  1327. ReadArray_aiVectorKey( stream, nd.mScalingKeys, nd.mNumScalingKeys );
  1328. }
  1329. }
  1330. }
  1331. function ReadBinaryAnim( stream, anim ) {
  1332. var chunkID = Read_uint32_t( stream );
  1333. ai_assert( chunkID == ASSBIN_CHUNK_AIANIMATION );
  1334. /*uint32_t size =*/
  1335. Read_uint32_t( stream );
  1336. anim.mName = Read_aiString( stream );
  1337. anim.mDuration = Read_double( stream );
  1338. anim.mTicksPerSecond = Read_double( stream );
  1339. anim.mNumChannels = Read_unsigned_int( stream );
  1340. if ( anim.mNumChannels ) {
  1341. anim.mChannels = [];
  1342. for ( var a = 0; a < anim.mNumChannels; ++ a ) {
  1343. anim.mChannels[ a ] = new aiNodeAnim();
  1344. ReadBinaryNodeAnim( stream, anim.mChannels[ a ] );
  1345. }
  1346. }
  1347. }
  1348. function ReadBinaryTexture( stream, tex ) {
  1349. var chunkID = Read_uint32_t( stream );
  1350. ai_assert( chunkID == ASSBIN_CHUNK_AITEXTURE );
  1351. /*uint32_t size =*/
  1352. Read_uint32_t( stream );
  1353. tex.mWidth = Read_unsigned_int( stream );
  1354. tex.mHeight = Read_unsigned_int( stream );
  1355. stream.ReadBytes( tex.achFormatHint, 1, 4 );
  1356. if ( ! shortened ) {
  1357. if ( ! tex.mHeight ) {
  1358. tex.pcData = [];
  1359. stream.ReadBytes( tex.pcData, 1, tex.mWidth );
  1360. } else {
  1361. tex.pcData = [];
  1362. stream.ReadBytes( tex.pcData, 1, tex.mWidth * tex.mHeight * 4 );
  1363. }
  1364. }
  1365. }
  1366. function ReadBinaryLight( stream, l ) {
  1367. var chunkID = Read_uint32_t( stream );
  1368. ai_assert( chunkID == ASSBIN_CHUNK_AILIGHT );
  1369. /*uint32_t size =*/
  1370. Read_uint32_t( stream );
  1371. l.mName = Read_aiString( stream );
  1372. l.mType = Read_unsigned_int( stream );
  1373. if ( l.mType != aiLightSource_DIRECTIONAL ) {
  1374. l.mAttenuationConstant = readFloat( stream );
  1375. l.mAttenuationLinear = readFloat( stream );
  1376. l.mAttenuationQuadratic = readFloat( stream );
  1377. }
  1378. l.mColorDiffuse = Read_aiColor3D( stream );
  1379. l.mColorSpecular = Read_aiColor3D( stream );
  1380. l.mColorAmbient = Read_aiColor3D( stream );
  1381. if ( l.mType == aiLightSource_SPOT ) {
  1382. l.mAngleInnerCone = readFloat( stream );
  1383. l.mAngleOuterCone = readFloat( stream );
  1384. }
  1385. }
  1386. function ReadBinaryCamera( stream, cam ) {
  1387. var chunkID = Read_uint32_t( stream );
  1388. ai_assert( chunkID == ASSBIN_CHUNK_AICAMERA );
  1389. /*uint32_t size =*/
  1390. Read_uint32_t( stream );
  1391. cam.mName = Read_aiString( stream );
  1392. cam.mPosition = Read_aiVector3D( stream );
  1393. cam.mLookAt = Read_aiVector3D( stream );
  1394. cam.mUp = Read_aiVector3D( stream );
  1395. cam.mHorizontalFOV = readFloat( stream );
  1396. cam.mClipPlaneNear = readFloat( stream );
  1397. cam.mClipPlaneFar = readFloat( stream );
  1398. cam.mAspect = readFloat( stream );
  1399. }
  1400. function ReadBinaryScene( stream, scene ) {
  1401. var chunkID = Read_uint32_t( stream );
  1402. ai_assert( chunkID == ASSBIN_CHUNK_AISCENE );
  1403. /*uint32_t size =*/
  1404. Read_uint32_t( stream );
  1405. scene.mFlags = Read_unsigned_int( stream );
  1406. scene.mNumMeshes = Read_unsigned_int( stream );
  1407. scene.mNumMaterials = Read_unsigned_int( stream );
  1408. scene.mNumAnimations = Read_unsigned_int( stream );
  1409. scene.mNumTextures = Read_unsigned_int( stream );
  1410. scene.mNumLights = Read_unsigned_int( stream );
  1411. scene.mNumCameras = Read_unsigned_int( stream );
  1412. // Read node graph
  1413. scene.mRootNode = new aiNode();
  1414. scene.mRootNode = ReadBinaryNode( stream, null, 0 );
  1415. // Read all meshes
  1416. if ( scene.mNumMeshes ) {
  1417. scene.mMeshes = [];
  1418. for ( var i = 0; i < scene.mNumMeshes; ++ i ) {
  1419. scene.mMeshes[ i ] = new aiMesh();
  1420. ReadBinaryMesh( stream, scene.mMeshes[ i ] );
  1421. }
  1422. }
  1423. // Read materials
  1424. if ( scene.mNumMaterials ) {
  1425. scene.mMaterials = [];
  1426. for ( var i = 0; i < scene.mNumMaterials; ++ i ) {
  1427. scene.mMaterials[ i ] = new aiMaterial();
  1428. ReadBinaryMaterial( stream, scene.mMaterials[ i ] );
  1429. }
  1430. }
  1431. // Read all animations
  1432. if ( scene.mNumAnimations ) {
  1433. scene.mAnimations = [];
  1434. for ( var i = 0; i < scene.mNumAnimations; ++ i ) {
  1435. scene.mAnimations[ i ] = new aiAnimation();
  1436. ReadBinaryAnim( stream, scene.mAnimations[ i ] );
  1437. }
  1438. }
  1439. // Read all textures
  1440. if ( scene.mNumTextures ) {
  1441. scene.mTextures = [];
  1442. for ( var i = 0; i < scene.mNumTextures; ++ i ) {
  1443. scene.mTextures[ i ] = new aiTexture();
  1444. ReadBinaryTexture( stream, scene.mTextures[ i ] );
  1445. }
  1446. }
  1447. // Read lights
  1448. if ( scene.mNumLights ) {
  1449. scene.mLights = [];
  1450. for ( var i = 0; i < scene.mNumLights; ++ i ) {
  1451. scene.mLights[ i ] = new aiLight();
  1452. ReadBinaryLight( stream, scene.mLights[ i ] );
  1453. }
  1454. }
  1455. // Read cameras
  1456. if ( scene.mNumCameras ) {
  1457. scene.mCameras = [];
  1458. for ( var i = 0; i < scene.mNumCameras; ++ i ) {
  1459. scene.mCameras[ i ] = new aiCamera();
  1460. ReadBinaryCamera( stream, scene.mCameras[ i ] );
  1461. }
  1462. }
  1463. }
  1464. var aiOrigin_CUR = 0;
  1465. var aiOrigin_BEG = 1;
  1466. function extendStream( stream ) {
  1467. stream.readOffset = 0;
  1468. stream.Seek = function ( off, ori ) {
  1469. if ( ori == aiOrigin_CUR ) {
  1470. stream.readOffset += off;
  1471. }
  1472. if ( ori == aiOrigin_BEG ) {
  1473. stream.readOffset = off;
  1474. }
  1475. };
  1476. stream.ReadBytes = function ( buff, size, n ) {
  1477. var bytes = size * n;
  1478. for ( var i = 0; i < bytes; i ++ )
  1479. buff[ i ] = Read_uint8_t( this );
  1480. };
  1481. stream.subArray32 = function ( start, end ) {
  1482. var buff = this.buffer;
  1483. var newbuff = buff.slice( start, end );
  1484. return new Float32Array( newbuff );
  1485. };
  1486. stream.subArrayUint16 = function ( start, end ) {
  1487. var buff = this.buffer;
  1488. var newbuff = buff.slice( start, end );
  1489. return new Uint16Array( newbuff );
  1490. };
  1491. stream.subArrayUint8 = function ( start, end ) {
  1492. var buff = this.buffer;
  1493. var newbuff = buff.slice( start, end );
  1494. return new Uint8Array( newbuff );
  1495. };
  1496. stream.subArrayUint32 = function ( start, end ) {
  1497. var buff = this.buffer;
  1498. var newbuff = buff.slice( start, end );
  1499. return new Uint32Array( newbuff );
  1500. };
  1501. }
  1502. var shortened, compressed;
  1503. function InternReadFile( pFiledata ) {
  1504. var pScene = new aiScene();
  1505. var stream = new DataView( pFiledata );
  1506. extendStream( stream );
  1507. stream.Seek( 44, aiOrigin_CUR ); // signature
  1508. /*unsigned int versionMajor =*/
  1509. pScene.versionMajor = Read_unsigned_int( stream );
  1510. /*unsigned int versionMinor =*/
  1511. pScene.versionMinor = Read_unsigned_int( stream );
  1512. /*unsigned int versionRevision =*/
  1513. pScene.versionRevision = Read_unsigned_int( stream );
  1514. /*unsigned int compileFlags =*/
  1515. pScene.compileFlags = Read_unsigned_int( stream );
  1516. shortened = Read_uint16_t( stream ) > 0;
  1517. compressed = Read_uint16_t( stream ) > 0;
  1518. if ( shortened )
  1519. throw 'Shortened binaries are not supported!';
  1520. stream.Seek( 256, aiOrigin_CUR ); // original filename
  1521. stream.Seek( 128, aiOrigin_CUR ); // options
  1522. stream.Seek( 64, aiOrigin_CUR ); // padding
  1523. if ( compressed ) {
  1524. var uncompressedSize = Read_uint32_t( stream );
  1525. var compressedSize = stream.FileSize() - stream.Tell();
  1526. var compressedData = [];
  1527. stream.Read( compressedData, 1, compressedSize );
  1528. var uncompressedData = [];
  1529. uncompress( uncompressedData, uncompressedSize, compressedData, compressedSize ); // eslint-disable-line no-undef
  1530. var buff = new ArrayBuffer( uncompressedData );
  1531. ReadBinaryScene( buff, pScene );
  1532. } else {
  1533. ReadBinaryScene( stream, pScene );
  1534. }
  1535. return pScene.toTHREE();
  1536. }
  1537. return InternReadFile( buffer );
  1538. }
  1539. } );
  1540. export { AssimpLoader };