AssimpLoader.js 52 KB

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