AssimpLoader.js 51 KB

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