AssimpLoader.js 52 KB

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