AWDLoader.js 24 KB

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