AssimpLoader.js 51 KB

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