AssimpLoader.js 53 KB

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