AssimpLoader.js 53 KB

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