AWDLoader.js 24 KB

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