AWDLoader.js 24 KB

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