AssimpLoader.js 53 KB

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