AssimpLoader.js 51 KB

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