AssimpLoader.js 55 KB

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