AssimpLoader.js 55 KB

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