AWDLoader.js 22 KB

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