AWDLoader.js 23 KB

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