AssimpLoader.js 53 KB

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