AssimpLoader.js 53 KB

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