AssimpLoader.js 52 KB

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