AssimpLoader.js 51 KB

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