AssimpLoader.js 53 KB

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