AWDLoader.js 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. /**
  2. * Author: Pierre Lepers
  3. * Date: 09/12/2013 17:21
  4. */
  5. import {
  6. Bone,
  7. BufferAttribute,
  8. BufferGeometry,
  9. FileLoader,
  10. ImageLoader,
  11. Loader,
  12. Matrix4,
  13. Mesh,
  14. MeshPhongMaterial,
  15. Object3D,
  16. Texture
  17. } from "../../../build/three.module.js";
  18. var AWDLoader = ( function () {
  19. var //UNCOMPRESSED = 0,
  20. //DEFLATE = 1,
  21. //LZMA = 2,
  22. AWD_FIELD_INT8 = 1,
  23. AWD_FIELD_INT16 = 2,
  24. AWD_FIELD_INT32 = 3,
  25. AWD_FIELD_UINT8 = 4,
  26. AWD_FIELD_UINT16 = 5,
  27. AWD_FIELD_UINT32 = 6,
  28. AWD_FIELD_FLOAT32 = 7,
  29. AWD_FIELD_FLOAT64 = 8,
  30. AWD_FIELD_BOOL = 21,
  31. //AWD_FIELD_COLOR = 22,
  32. AWD_FIELD_BADDR = 23,
  33. //AWD_FIELD_STRING = 31,
  34. //AWD_FIELD_BYTEARRAY = 32,
  35. AWD_FIELD_VECTOR2x1 = 41,
  36. AWD_FIELD_VECTOR3x1 = 42,
  37. AWD_FIELD_VECTOR4x1 = 43,
  38. AWD_FIELD_MTX3x2 = 44,
  39. AWD_FIELD_MTX3x3 = 45,
  40. AWD_FIELD_MTX4x3 = 46,
  41. AWD_FIELD_MTX4x4 = 47,
  42. BOOL = 21,
  43. //COLOR = 22,
  44. BADDR = 23,
  45. //INT8 = 1,
  46. //INT16 = 2,
  47. //INT32 = 3,
  48. UINT8 = 4,
  49. UINT16 = 5,
  50. //UINT32 = 6,
  51. FLOAT32 = 7,
  52. FLOAT64 = 8;
  53. var littleEndian = true;
  54. function Block() {
  55. this.id = 0;
  56. this.data = null;
  57. this.namespace = 0;
  58. this.flags = 0;
  59. }
  60. function AWDProperties() {}
  61. AWDProperties.prototype = {
  62. set: function ( key, value ) {
  63. this[ key ] = value;
  64. },
  65. get: function ( key, fallback ) {
  66. if ( this.hasOwnProperty( key ) ) {
  67. return this[ key ];
  68. } else {
  69. return fallback;
  70. }
  71. }
  72. };
  73. var AWDLoader = function ( manager ) {
  74. Loader.call( this, manager );
  75. this.trunk = new Object3D();
  76. this.materialFactory = undefined;
  77. this._url = '';
  78. this._baseDir = '';
  79. this._data = undefined;
  80. this._ptr = 0;
  81. this._version = [];
  82. this._streaming = false;
  83. this._optimized_for_accuracy = false;
  84. this._compression = 0;
  85. this._bodylen = 0xFFFFFFFF;
  86. this._blocks = [ new Block() ];
  87. this._accuracyMatrix = false;
  88. this._accuracyGeo = false;
  89. this._accuracyProps = false;
  90. };
  91. AWDLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
  92. constructor: AWDLoader,
  93. load: function ( url, onLoad, onProgress, onError ) {
  94. var scope = this;
  95. this._url = url;
  96. this._baseDir = url.substr( 0, url.lastIndexOf( '/' ) + 1 );
  97. var loader = new FileLoader( this.manager );
  98. loader.setPath( this.path );
  99. loader.setResponseType( 'arraybuffer' );
  100. loader.load( url, function ( text ) {
  101. onLoad( scope.parse( text ) );
  102. }, onProgress, onError );
  103. },
  104. parse: function ( data ) {
  105. var blen = data.byteLength;
  106. this._ptr = 0;
  107. this._data = new DataView( data );
  108. this._parseHeader( );
  109. if ( this._compression != 0 ) {
  110. console.error( 'compressed AWD not supported' );
  111. }
  112. if ( ! this._streaming && this._bodylen != data.byteLength - this._ptr ) {
  113. console.error( 'AWDLoader: body len does not match file length', this._bodylen, blen - this._ptr );
  114. }
  115. while ( this._ptr < blen ) {
  116. this.parseNextBlock();
  117. }
  118. return this.trunk;
  119. },
  120. parseNextBlock: function () {
  121. var assetData,
  122. block,
  123. blockId = this.readU32(),
  124. ns = this.readU8(),
  125. type = this.readU8(),
  126. flags = this.readU8(),
  127. len = this.readU32();
  128. switch ( type ) {
  129. case 1:
  130. assetData = this.parseMeshData();
  131. break;
  132. case 22:
  133. assetData = this.parseContainer();
  134. break;
  135. case 23:
  136. assetData = this.parseMeshInstance();
  137. break;
  138. case 81:
  139. assetData = this.parseMaterial();
  140. break;
  141. case 82:
  142. assetData = this.parseTexture();
  143. break;
  144. case 101:
  145. assetData = this.parseSkeleton();
  146. break;
  147. case 112:
  148. assetData = this.parseMeshPoseAnimation( false );
  149. break;
  150. case 113:
  151. assetData = this.parseVertexAnimationSet();
  152. break;
  153. case 102:
  154. assetData = this.parseSkeletonPose();
  155. break;
  156. case 103:
  157. assetData = this.parseSkeletonAnimation();
  158. break;
  159. case 122:
  160. assetData = this.parseAnimatorSet();
  161. break;
  162. default:
  163. //debug('Ignoring block!',type, len);
  164. this._ptr += len;
  165. break;
  166. }
  167. // Store block reference for later use
  168. this._blocks[ blockId ] = block = new Block();
  169. block.data = assetData;
  170. block.id = blockId;
  171. block.namespace = ns;
  172. block.flags = flags;
  173. },
  174. _parseHeader: function () {
  175. var version = this._version,
  176. awdmagic = ( this.readU8() << 16 ) | ( this.readU8() << 8 ) | this.readU8();
  177. if ( awdmagic != 4282180 )
  178. throw new Error( "AWDLoader - bad magic" );
  179. version[ 0 ] = this.readU8();
  180. version[ 1 ] = this.readU8();
  181. var flags = this.readU16();
  182. this._streaming = ( flags & 0x1 ) == 0x1;
  183. if ( ( version[ 0 ] === 2 ) && ( version[ 1 ] === 1 ) ) {
  184. this._accuracyMatrix = ( flags & 0x2 ) === 0x2;
  185. this._accuracyGeo = ( flags & 0x4 ) === 0x4;
  186. this._accuracyProps = ( flags & 0x8 ) === 0x8;
  187. }
  188. this._geoNrType = this._accuracyGeo ? FLOAT64 : FLOAT32;
  189. this._matrixNrType = this._accuracyMatrix ? FLOAT64 : FLOAT32;
  190. this._propsNrType = this._accuracyProps ? FLOAT64 : FLOAT32;
  191. this._optimized_for_accuracy = ( flags & 0x2 ) === 0x2;
  192. this._compression = this.readU8();
  193. this._bodylen = this.readU32();
  194. },
  195. parseContainer: function () {
  196. var parent,
  197. ctr = new Object3D(),
  198. par_id = this.readU32(),
  199. mtx = this.parseMatrix4();
  200. ctr.name = this.readUTF();
  201. ctr.applyMatrix( mtx );
  202. parent = this._blocks[ par_id ].data || this.trunk;
  203. parent.add( ctr );
  204. this.parseProperties( {
  205. 1: this._matrixNrType,
  206. 2: this._matrixNrType,
  207. 3: this._matrixNrType,
  208. 4: UINT8
  209. } );
  210. ctr.extra = this.parseUserAttributes();
  211. return ctr;
  212. },
  213. parseMeshInstance: function () {
  214. var name,
  215. mesh, geometries, meshLen, meshes,
  216. par_id, data_id,
  217. mtx,
  218. materials, mat, mat_id,
  219. num_materials,
  220. parent,
  221. i;
  222. par_id = this.readU32();
  223. mtx = this.parseMatrix4();
  224. name = this.readUTF();
  225. data_id = this.readU32();
  226. num_materials = this.readU16();
  227. geometries = this.getBlock( data_id );
  228. materials = [];
  229. for ( i = 0; i < num_materials; i ++ ) {
  230. mat_id = this.readU32();
  231. mat = this.getBlock( mat_id );
  232. materials.push( mat );
  233. }
  234. meshLen = geometries.length;
  235. meshes = [];
  236. // TODO : BufferGeometry don't support "geometryGroups" for now.
  237. // so we create sub meshes for each groups
  238. if ( meshLen > 1 ) {
  239. mesh = new Object3D();
  240. for ( i = 0; i < meshLen; i ++ ) {
  241. var sm = new Mesh( geometries[ i ] );
  242. meshes.push( sm );
  243. mesh.add( sm );
  244. }
  245. } else {
  246. mesh = new Mesh( geometries[ 0 ] );
  247. meshes.push( mesh );
  248. }
  249. mesh.applyMatrix( mtx );
  250. mesh.name = name;
  251. parent = this.getBlock( par_id ) || this.trunk;
  252. parent.add( mesh );
  253. var matLen = materials.length;
  254. var maxLen = Math.max( meshLen, matLen );
  255. for ( i = 0; i < maxLen; i ++ )
  256. meshes[ i % meshLen ].material = materials[ i % matLen ];
  257. // Ignore for now
  258. this.parseProperties( null );
  259. mesh.extra = this.parseUserAttributes();
  260. return mesh;
  261. },
  262. parseMaterial: function () {
  263. var name,
  264. type,
  265. props,
  266. mat,
  267. attributes,
  268. num_methods,
  269. methods_parsed;
  270. name = this.readUTF();
  271. type = this.readU8();
  272. num_methods = this.readU8();
  273. //log( "AWDLoader parseMaterial ",name )
  274. // Read material numerical properties
  275. // (1=color, 2=bitmap url, 11=alpha_blending, 12=alpha_threshold, 13=repeat)
  276. props = this.parseProperties( {
  277. 1: AWD_FIELD_INT32,
  278. 2: AWD_FIELD_BADDR,
  279. 11: AWD_FIELD_BOOL,
  280. 12: AWD_FIELD_FLOAT32,
  281. 13: AWD_FIELD_BOOL
  282. } );
  283. methods_parsed = 0;
  284. while ( methods_parsed < num_methods ) {
  285. // read method_type before
  286. this.readU16();
  287. this.parseProperties( null );
  288. this.parseUserAttributes();
  289. }
  290. attributes = this.parseUserAttributes();
  291. if ( this.materialFactory !== undefined ) {
  292. mat = this.materialFactory( name );
  293. if ( mat ) return mat;
  294. }
  295. mat = new MeshPhongMaterial();
  296. if ( type === 1 ) {
  297. // Color material
  298. mat.color.setHex( props.get( 1, 0xcccccc ) );
  299. } else if ( type === 2 ) {
  300. // Bitmap material
  301. var tex_addr = props.get( 2, 0 );
  302. mat.map = this.getBlock( tex_addr );
  303. }
  304. mat.extra = attributes;
  305. mat.alphaThreshold = props.get( 12, 0.0 );
  306. mat.repeat = props.get( 13, false );
  307. return mat;
  308. },
  309. parseTexture: function () {
  310. var name = this.readUTF(),
  311. type = this.readU8(),
  312. asset,
  313. data_len;
  314. // External
  315. if ( type === 0 ) {
  316. data_len = this.readU32();
  317. var url = this.readUTFBytes( data_len );
  318. console.log( url );
  319. asset = this.loadTexture( url );
  320. asset.userData = {};
  321. asset.userData.name = name;
  322. } else {
  323. // embed texture not supported
  324. }
  325. // Ignore for now
  326. this.parseProperties( null );
  327. this.parseUserAttributes();
  328. return asset;
  329. },
  330. loadTexture: function ( url ) {
  331. var tex = new Texture();
  332. var loader = new ImageLoader( this.manager );
  333. loader.load( this._baseDir + url, function ( image ) {
  334. tex.image = image;
  335. tex.needsUpdate = true;
  336. } );
  337. return tex;
  338. },
  339. parseSkeleton: function () {
  340. // Array<Bone>
  341. //
  342. this.readUTF();
  343. var num_joints = this.readU16(),
  344. skeleton = [],
  345. joints_parsed = 0;
  346. this.parseProperties( null );
  347. while ( joints_parsed < num_joints ) {
  348. var joint, ibp;
  349. // Ignore joint id
  350. this.readU16();
  351. joint = new Bone();
  352. joint.parent = this.readU16() - 1; // 0=null in AWD
  353. joint.name = this.readUTF();
  354. ibp = this.parseMatrix4();
  355. joint.skinMatrix = ibp;
  356. // Ignore joint props/attributes for now
  357. this.parseProperties( null );
  358. this.parseUserAttributes();
  359. skeleton.push( joint );
  360. joints_parsed ++;
  361. }
  362. // Discard attributes for now
  363. this.parseUserAttributes();
  364. return skeleton;
  365. },
  366. parseSkeletonPose: function () {
  367. var name = this.readUTF();
  368. var num_joints = this.readU16();
  369. this.parseProperties( null );
  370. // debug( 'parse Skeleton Pose. joints : ' + num_joints);
  371. var pose = [];
  372. var joints_parsed = 0;
  373. while ( joints_parsed < num_joints ) {
  374. var has_transform; //:uint;
  375. var mtx_data;
  376. has_transform = this.readU8();
  377. if ( has_transform === 1 ) {
  378. mtx_data = this.parseMatrix4();
  379. } else {
  380. mtx_data = new Matrix4();
  381. }
  382. pose[ joints_parsed ] = mtx_data;
  383. joints_parsed ++;
  384. }
  385. // Skip attributes for now
  386. this.parseUserAttributes();
  387. return pose;
  388. },
  389. parseSkeletonAnimation: function () {
  390. var frame_dur;
  391. var pose_addr;
  392. var pose;
  393. var name = this.readUTF();
  394. var clip = [];
  395. var num_frames = this.readU16();
  396. this.parseProperties( null );
  397. var frames_parsed = 0;
  398. // debug( 'parse Skeleton Animation. frames : ' + num_frames);
  399. while ( frames_parsed < num_frames ) {
  400. pose_addr = this.readU32();
  401. frame_dur = this.readU16();
  402. pose = this._blocks[ pose_addr ].data;
  403. // debug( 'pose address ',pose[2].elements[12],pose[2].elements[13],pose[2].elements[14] );
  404. clip.push( {
  405. pose: pose,
  406. duration: frame_dur
  407. } );
  408. frames_parsed ++;
  409. }
  410. if ( clip.length === 0 ) {
  411. // debug("Could not this SkeletonClipNode, because no Frames where set.");
  412. return;
  413. }
  414. // Ignore attributes for now
  415. this.parseUserAttributes();
  416. return clip;
  417. },
  418. parseVertexAnimationSet: function () {
  419. var poseBlockAdress,
  420. name = this.readUTF(),
  421. num_frames = this.readU16(),
  422. props = this.parseProperties( { 1: UINT16 } ),
  423. frames_parsed = 0,
  424. skeletonFrames = [];
  425. while ( frames_parsed < num_frames ) {
  426. poseBlockAdress = this.readU32();
  427. skeletonFrames.push( this._blocks[ poseBlockAdress ].data );
  428. frames_parsed ++;
  429. }
  430. this.parseUserAttributes();
  431. return skeletonFrames;
  432. },
  433. parseAnimatorSet: function () {
  434. var animSetBlockAdress; //:int
  435. var targetAnimationSet; //:AnimationSetBase;
  436. var name = this.readUTF();
  437. var type = this.readU16();
  438. var props = this.parseProperties( { 1: BADDR } );
  439. animSetBlockAdress = this.readU32();
  440. var targetMeshLength = this.readU16();
  441. var meshAdresses = []; //:Vector.<uint> = new Vector.<uint>;
  442. for ( var i = 0; i < targetMeshLength; i ++ )
  443. meshAdresses.push( this.readU32() );
  444. var activeState = this.readU16();
  445. var autoplay = Boolean( this.readU8() );
  446. this.parseUserAttributes();
  447. this.parseUserAttributes();
  448. var targetMeshes = []; //:Vector.<Mesh> = new Vector.<Mesh>;
  449. for ( i = 0; i < meshAdresses.length; i ++ ) {
  450. // returnedArray = getAssetByID(meshAdresses[i], [AssetType.MESH]);
  451. // if (returnedArray[0])
  452. targetMeshes.push( this._blocks[ meshAdresses[ i ] ].data );
  453. }
  454. targetAnimationSet = this._blocks[ animSetBlockAdress ].data;
  455. var thisAnimator;
  456. if ( type == 1 ) {
  457. thisAnimator = {
  458. animationSet: targetAnimationSet,
  459. skeleton: this._blocks[ props.get( 1, 0 ) ].data
  460. };
  461. } else if ( type == 2 ) {
  462. // debug( "vertex Anim???");
  463. }
  464. for ( i = 0; i < targetMeshes.length; i ++ ) {
  465. targetMeshes[ i ].animator = thisAnimator;
  466. }
  467. // debug("Parsed a Animator: Name = " + name);
  468. return thisAnimator;
  469. },
  470. parseMeshData: function () {
  471. var name = this.readUTF(),
  472. num_subs = this.readU16(),
  473. geom,
  474. subs_parsed = 0,
  475. buffer,
  476. geometries = [];
  477. // Ignore for now
  478. this.parseProperties( { 1: this._geoNrType, 2: this._geoNrType } );
  479. // Loop through sub meshes
  480. while ( subs_parsed < num_subs ) {
  481. var sm_len, sm_end, attrib;
  482. geom = new BufferGeometry();
  483. geom.name = name;
  484. geometries.push( geom );
  485. sm_len = this.readU32();
  486. sm_end = this._ptr + sm_len;
  487. // Ignore for now
  488. this.parseProperties( { 1: this._geoNrType, 2: this._geoNrType } );
  489. // Loop through data streams
  490. while ( this._ptr < sm_end ) {
  491. var idx = 0,
  492. str_type = this.readU8(),
  493. str_ftype = this.readU8(),
  494. str_len = this.readU32(),
  495. str_end = str_len + this._ptr;
  496. if ( str_type === 1 ) {
  497. // VERTICES
  498. buffer = new Float32Array( ( str_len / 12 ) * 3 );
  499. attrib = new BufferAttribute( buffer, 3 );
  500. geom.setAttribute( 'position', attrib );
  501. idx = 0;
  502. while ( this._ptr < str_end ) {
  503. buffer[ idx ] = - this.readF32();
  504. buffer[ idx + 1 ] = this.readF32();
  505. buffer[ idx + 2 ] = this.readF32();
  506. idx += 3;
  507. }
  508. } else if ( str_type === 2 ) {
  509. // INDICES
  510. buffer = new Uint16Array( str_len / 2 );
  511. attrib = new BufferAttribute( buffer, 1 );
  512. geom.setIndex( attrib );
  513. idx = 0;
  514. while ( this._ptr < str_end ) {
  515. buffer[ idx + 1 ] = this.readU16();
  516. buffer[ idx ] = this.readU16();
  517. buffer[ idx + 2 ] = this.readU16();
  518. idx += 3;
  519. }
  520. } else if ( str_type === 3 ) {
  521. // UVS
  522. buffer = new Float32Array( ( str_len / 8 ) * 2 );
  523. attrib = new BufferAttribute( buffer, 2 );
  524. geom.setAttribute( 'uv', attrib );
  525. idx = 0;
  526. while ( this._ptr < str_end ) {
  527. buffer[ idx ] = this.readF32();
  528. buffer[ idx + 1 ] = 1.0 - this.readF32();
  529. idx += 2;
  530. }
  531. } else if ( str_type === 4 ) {
  532. // NORMALS
  533. buffer = new Float32Array( ( str_len / 12 ) * 3 );
  534. attrib = new BufferAttribute( buffer, 3 );
  535. geom.setAttribute( 'normal', attrib );
  536. idx = 0;
  537. while ( this._ptr < str_end ) {
  538. buffer[ idx ] = - this.readF32();
  539. buffer[ idx + 1 ] = this.readF32();
  540. buffer[ idx + 2 ] = this.readF32();
  541. idx += 3;
  542. }
  543. } else {
  544. this._ptr = str_end;
  545. }
  546. }
  547. this.parseUserAttributes();
  548. geom.computeBoundingSphere();
  549. subs_parsed ++;
  550. }
  551. //geom.computeFaceNormals();
  552. this.parseUserAttributes();
  553. //finalizeAsset(geom, name);
  554. return geometries;
  555. },
  556. parseMeshPoseAnimation: function ( poseOnly ) {
  557. var num_frames = 1,
  558. num_submeshes,
  559. frames_parsed,
  560. subMeshParsed,
  561. str_len,
  562. str_end,
  563. geom,
  564. idx = 0,
  565. clip = {},
  566. num_Streams,
  567. streamsParsed,
  568. streamtypes = [],
  569. props,
  570. name = this.readUTF(),
  571. geoAdress = this.readU32();
  572. var mesh = this.getBlock( geoAdress );
  573. if ( mesh === null ) {
  574. console.log( "parseMeshPoseAnimation target mesh not found at:", geoAdress );
  575. return;
  576. }
  577. geom = mesh.geometry;
  578. geom.morphTargets = [];
  579. if ( ! poseOnly )
  580. num_frames = this.readU16();
  581. num_submeshes = this.readU16();
  582. num_Streams = this.readU16();
  583. // debug("VA num_frames : ", num_frames );
  584. // debug("VA num_submeshes : ", num_submeshes );
  585. // debug("VA numstreams : ", num_Streams );
  586. streamsParsed = 0;
  587. while ( streamsParsed < num_Streams ) {
  588. streamtypes.push( this.readU16() );
  589. streamsParsed ++;
  590. }
  591. props = this.parseProperties( { 1: BOOL, 2: BOOL } );
  592. clip.looping = props.get( 1, true );
  593. clip.stitchFinalFrame = props.get( 2, false );
  594. frames_parsed = 0;
  595. while ( frames_parsed < num_frames ) {
  596. this.readU16();
  597. subMeshParsed = 0;
  598. while ( subMeshParsed < num_submeshes ) {
  599. streamsParsed = 0;
  600. str_len = this.readU32();
  601. str_end = this._ptr + str_len;
  602. while ( streamsParsed < num_Streams ) {
  603. if ( streamtypes[ streamsParsed ] === 1 ) {
  604. //geom.setAttribute( 'morphTarget'+frames_parsed, Float32Array, str_len/12, 3 );
  605. var buffer = new Float32Array( str_len / 4 );
  606. geom.morphTargets.push( {
  607. array: buffer
  608. } );
  609. //buffer = geom.attributes['morphTarget'+frames_parsed].array
  610. idx = 0;
  611. while ( this._ptr < str_end ) {
  612. buffer[ idx ] = this.readF32();
  613. buffer[ idx + 1 ] = this.readF32();
  614. buffer[ idx + 2 ] = this.readF32();
  615. idx += 3;
  616. }
  617. subMeshParsed ++;
  618. } else
  619. this._ptr = str_end;
  620. streamsParsed ++;
  621. }
  622. }
  623. frames_parsed ++;
  624. }
  625. this.parseUserAttributes();
  626. return null;
  627. },
  628. getBlock: function ( id ) {
  629. return this._blocks[ id ].data;
  630. },
  631. parseMatrix4: function () {
  632. var mtx = new Matrix4();
  633. var e = mtx.elements;
  634. e[ 0 ] = this.readF32();
  635. e[ 1 ] = this.readF32();
  636. e[ 2 ] = this.readF32();
  637. e[ 3 ] = 0.0;
  638. //e[3] = 0.0;
  639. e[ 4 ] = this.readF32();
  640. e[ 5 ] = this.readF32();
  641. e[ 6 ] = this.readF32();
  642. //e[7] = this.readF32();
  643. e[ 7 ] = 0.0;
  644. e[ 8 ] = this.readF32();
  645. e[ 9 ] = this.readF32();
  646. e[ 10 ] = this.readF32();
  647. //e[11] = this.readF32();
  648. e[ 11 ] = 0.0;
  649. e[ 12 ] = - this.readF32();
  650. e[ 13 ] = this.readF32();
  651. e[ 14 ] = this.readF32();
  652. //e[15] = this.readF32();
  653. e[ 15 ] = 1.0;
  654. return mtx;
  655. },
  656. parseProperties: function ( expected ) {
  657. var list_len = this.readU32();
  658. var list_end = this._ptr + list_len;
  659. var props = new AWDProperties();
  660. if ( expected ) {
  661. while ( this._ptr < list_end ) {
  662. var key = this.readU16();
  663. var len = this.readU32();
  664. var type;
  665. if ( expected.hasOwnProperty( key ) ) {
  666. type = expected[ key ];
  667. props.set( key, this.parseAttrValue( type, len ) );
  668. } else {
  669. this._ptr += len;
  670. }
  671. }
  672. }
  673. return props;
  674. },
  675. parseUserAttributes: function () {
  676. // skip for now
  677. this._ptr = this.readU32() + this._ptr;
  678. return null;
  679. },
  680. parseAttrValue: function ( type, len ) {
  681. var elem_len;
  682. var read_func;
  683. switch ( type ) {
  684. case AWD_FIELD_INT8:
  685. elem_len = 1;
  686. read_func = this.readI8;
  687. break;
  688. case AWD_FIELD_INT16:
  689. elem_len = 2;
  690. read_func = this.readI16;
  691. break;
  692. case AWD_FIELD_INT32:
  693. elem_len = 4;
  694. read_func = this.readI32;
  695. break;
  696. case AWD_FIELD_BOOL:
  697. case AWD_FIELD_UINT8:
  698. elem_len = 1;
  699. read_func = this.readU8;
  700. break;
  701. case AWD_FIELD_UINT16:
  702. elem_len = 2;
  703. read_func = this.readU16;
  704. break;
  705. case AWD_FIELD_UINT32:
  706. case AWD_FIELD_BADDR:
  707. elem_len = 4;
  708. read_func = this.readU32;
  709. break;
  710. case AWD_FIELD_FLOAT32:
  711. elem_len = 4;
  712. read_func = this.readF32;
  713. break;
  714. case AWD_FIELD_FLOAT64:
  715. elem_len = 8;
  716. read_func = this.readF64;
  717. break;
  718. case AWD_FIELD_VECTOR2x1:
  719. case AWD_FIELD_VECTOR3x1:
  720. case AWD_FIELD_VECTOR4x1:
  721. case AWD_FIELD_MTX3x2:
  722. case AWD_FIELD_MTX3x3:
  723. case AWD_FIELD_MTX4x3:
  724. case AWD_FIELD_MTX4x4:
  725. elem_len = 8;
  726. read_func = this.readF64;
  727. break;
  728. }
  729. if ( elem_len < len ) {
  730. var list;
  731. var num_read;
  732. var num_elems;
  733. list = [];
  734. num_read = 0;
  735. num_elems = len / elem_len;
  736. while ( num_read < num_elems ) {
  737. list.push( read_func.call( this ) );
  738. num_read ++;
  739. }
  740. return list;
  741. } else {
  742. return read_func.call( this );
  743. }
  744. },
  745. readU8: function () {
  746. return this._data.getUint8( this._ptr ++ );
  747. },
  748. readI8: function () {
  749. return this._data.getInt8( this._ptr ++ );
  750. },
  751. readU16: function () {
  752. var a = this._data.getUint16( this._ptr, littleEndian );
  753. this._ptr += 2;
  754. return a;
  755. },
  756. readI16: function () {
  757. var a = this._data.getInt16( this._ptr, littleEndian );
  758. this._ptr += 2;
  759. return a;
  760. },
  761. readU32: function () {
  762. var a = this._data.getUint32( this._ptr, littleEndian );
  763. this._ptr += 4;
  764. return a;
  765. },
  766. readI32: function () {
  767. var a = this._data.getInt32( this._ptr, littleEndian );
  768. this._ptr += 4;
  769. return a;
  770. },
  771. readF32: function () {
  772. var a = this._data.getFloat32( this._ptr, littleEndian );
  773. this._ptr += 4;
  774. return a;
  775. },
  776. readF64: function () {
  777. var a = this._data.getFloat64( this._ptr, littleEndian );
  778. this._ptr += 8;
  779. return a;
  780. },
  781. /**
  782. * Converts a UTF-8 byte array to JavaScript's 16-bit Unicode.
  783. * @param {Array.<number>} bytes UTF-8 byte array.
  784. * @return {string} 16-bit Unicode string.
  785. */
  786. readUTF: function () {
  787. var len = this.readU16();
  788. return this.readUTFBytes( len );
  789. },
  790. /**
  791. * Converts a UTF-8 byte array to JavaScript's 16-bit Unicode.
  792. * @param {Array.<number>} bytes UTF-8 byte array.
  793. * @return {string} 16-bit Unicode string.
  794. */
  795. readUTFBytes: function ( len ) {
  796. // TODO(user): Use native implementations if/when available
  797. var out = [], c = 0;
  798. while ( out.length < len ) {
  799. var c1 = this._data.getUint8( this._ptr ++, littleEndian );
  800. if ( c1 < 128 ) {
  801. out[ c ++ ] = String.fromCharCode( c1 );
  802. } else if ( c1 > 191 && c1 < 224 ) {
  803. var c2 = this._data.getUint8( this._ptr ++, littleEndian );
  804. out[ c ++ ] = String.fromCharCode( ( c1 & 31 ) << 6 | c2 & 63 );
  805. } else {
  806. var c2 = this._data.getUint8( this._ptr ++, littleEndian );
  807. var c3 = this._data.getUint8( this._ptr ++, littleEndian );
  808. out[ c ++ ] = String.fromCharCode( ( c1 & 15 ) << 12 | ( c2 & 63 ) << 6 | c3 & 63 );
  809. }
  810. }
  811. return out.join( '' );
  812. }
  813. } );
  814. return AWDLoader;
  815. } )();
  816. export { AWDLoader };