AssimpLoader.js 55 KB

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