AssimpLoader.js 53 KB

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