AWDLoader.js 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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. }
  241. else {
  242. mesh = new THREE.Mesh( geometries[ 0 ] );
  243. meshes.push( mesh );
  244. }
  245. mesh.applyMatrix( mtx );
  246. mesh.name = name;
  247. parent = this.getBlock( par_id ) || this.trunk;
  248. parent.add( mesh );
  249. var matLen = materials.length;
  250. var maxLen = Math.max( meshLen, matLen );
  251. for ( i = 0; i < maxLen; i ++ )
  252. meshes[ i % meshLen ].material = materials[ i % matLen ];
  253. // Ignore for now
  254. this.parseProperties( null );
  255. mesh.extra = this.parseUserAttributes();
  256. return mesh;
  257. },
  258. parseMaterial: function ( len ) {
  259. var name,
  260. type,
  261. props,
  262. mat,
  263. attributes,
  264. finalize,
  265. num_methods,
  266. methods_parsed;
  267. name = this.readUTF();
  268. type = this.readU8();
  269. num_methods = this.readU8();
  270. //log( "AWDLoader parseMaterial ",name )
  271. // Read material numerical properties
  272. // (1=color, 2=bitmap url, 11=alpha_blending, 12=alpha_threshold, 13=repeat)
  273. props = this.parseProperties( {
  274. 1: AWD_FIELD_INT32,
  275. 2: AWD_FIELD_BADDR,
  276. 11: AWD_FIELD_BOOL,
  277. 12: AWD_FIELD_FLOAT32,
  278. 13: AWD_FIELD_BOOL
  279. } );
  280. methods_parsed = 0;
  281. while ( methods_parsed < num_methods ) {
  282. var method_type = this.readU16();
  283. this.parseProperties( null );
  284. this.parseUserAttributes();
  285. }
  286. attributes = this.parseUserAttributes();
  287. if ( this.materialFactory !== undefined ) {
  288. mat = this.materialFactory( name );
  289. if ( mat ) return mat;
  290. }
  291. mat = new THREE.MeshPhongMaterial();
  292. if ( type === 1 ) {
  293. // Color material
  294. mat.color.setHex( props.get( 1, 0xcccccc ) );
  295. }
  296. else if ( type === 2 ) {
  297. // Bitmap material
  298. var tex_addr = props.get( 2, 0 );
  299. mat.map = this.getBlock( tex_addr );
  300. }
  301. mat.extra = attributes;
  302. mat.alphaThreshold = props.get( 12, 0.0 );
  303. mat.repeat = props.get( 13, false );
  304. return mat;
  305. },
  306. parseTexture: function( len ) {
  307. var name = this.readUTF(),
  308. type = this.readU8(),
  309. asset,
  310. data_len;
  311. // External
  312. if ( type === 0 ) {
  313. data_len = this.readU32();
  314. var url = this.readUTFBytes( data_len );
  315. console.log( url );
  316. asset = this.loadTexture( url );
  317. } else {
  318. // embed texture not supported
  319. }
  320. // Ignore for now
  321. this.parseProperties( null );
  322. this.parseUserAttributes();
  323. return asset;
  324. },
  325. loadTexture: function( url ) {
  326. var tex = new THREE.Texture();
  327. var loader = new THREE.ImageLoader( this.manager );
  328. loader.load( this._baseDir + url, function( image ) {
  329. tex.image = image;
  330. tex.needsUpdate = true;
  331. } );
  332. return tex;
  333. },
  334. parseSkeleton: function( len ) {
  335. // Array<Bone>
  336. var name = this.readUTF(),
  337. num_joints = this.readU16(),
  338. skeleton = [],
  339. joints_parsed = 0;
  340. this.parseProperties( null );
  341. while ( joints_parsed < num_joints ) {
  342. var joint, ibp;
  343. // Ignore joint id
  344. this.readU16();
  345. joint = new THREE.Bone();
  346. joint.parent = this.readU16() - 1; // 0=null in AWD
  347. joint.name = this.readUTF();
  348. ibp = this.parseMatrix4();
  349. joint.skinMatrix = ibp;
  350. // Ignore joint props/attributes for now
  351. this.parseProperties( null );
  352. this.parseUserAttributes();
  353. skeleton.push( joint );
  354. joints_parsed ++;
  355. }
  356. // Discard attributes for now
  357. this.parseUserAttributes();
  358. return skeleton;
  359. },
  360. parseSkeletonPose: function( blockID ) {
  361. var name = this.readUTF();
  362. var num_joints = this.readU16();
  363. this.parseProperties( null );
  364. // debug( 'parse Skeleton Pose. joints : ' + num_joints);
  365. var pose = [];
  366. var joints_parsed = 0;
  367. while ( joints_parsed < num_joints ) {
  368. var joint_pose;
  369. var has_transform; //:uint;
  370. var mtx_data;
  371. has_transform = this.readU8();
  372. if ( has_transform === 1 ) {
  373. mtx_data = this.parseMatrix4();
  374. } else {
  375. mtx_data = new THREE.Matrix4();
  376. }
  377. pose[ joints_parsed ] = mtx_data;
  378. joints_parsed ++;
  379. }
  380. // Skip attributes for now
  381. this.parseUserAttributes();
  382. return pose;
  383. },
  384. parseSkeletonAnimation: function( blockID ) {
  385. var frame_dur;
  386. var pose_addr;
  387. var pose;
  388. var name = this.readUTF();
  389. var clip = [];
  390. var num_frames = this.readU16();
  391. this.parseProperties( null );
  392. var frames_parsed = 0;
  393. var returnedArray;
  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( len ) {
  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( len ) {
  430. var targetMesh;
  431. var animSetBlockAdress; //:int
  432. var targetAnimationSet; //:AnimationSetBase;
  433. var outputString = ""; //:String = "";
  434. var name = this.readUTF();
  435. var type = this.readU16();
  436. var props = this.parseProperties( { 1: BADDR } );
  437. animSetBlockAdress = this.readU32();
  438. var targetMeshLength = this.readU16();
  439. var meshAdresses = []; //:Vector.<uint> = new Vector.<uint>;
  440. for ( var i = 0; i < targetMeshLength; i ++ )
  441. meshAdresses.push( this.readU32() );
  442. var activeState = this.readU16();
  443. var autoplay = Boolean( this.readU8() );
  444. this.parseUserAttributes();
  445. this.parseUserAttributes();
  446. var returnedArray;
  447. var targetMeshes = []; //:Vector.<Mesh> = new Vector.<Mesh>;
  448. for ( i = 0; i < meshAdresses.length; i ++ ) {
  449. // returnedArray = getAssetByID(meshAdresses[i], [AssetType.MESH]);
  450. // if (returnedArray[0])
  451. targetMeshes.push( this._blocks[ meshAdresses[ i ]].data );
  452. }
  453. targetAnimationSet = this._blocks[ animSetBlockAdress ].data;
  454. var thisAnimator;
  455. if ( type == 1 ) {
  456. thisAnimator = {
  457. animationSet : targetAnimationSet,
  458. skeleton : this._blocks[ props.get( 1, 0 ) ].data
  459. };
  460. } else if ( type == 2 ) {
  461. // debug( "vertex Anim???");
  462. }
  463. for ( i = 0; i < targetMeshes.length; i ++ ) {
  464. targetMeshes[ i ].animator = thisAnimator;
  465. }
  466. // debug("Parsed a Animator: Name = " + name);
  467. return thisAnimator;
  468. },
  469. parseMeshData: function ( len ) {
  470. var name = this.readUTF(),
  471. num_subs = this.readU16(),
  472. geom,
  473. subs_parsed = 0,
  474. props,
  475. buffer,
  476. skinW, skinI,
  477. geometries = [];
  478. props = this.parseProperties( {
  479. 1: this._geoNrType,
  480. 2: this._geoNrType
  481. } );
  482. // Loop through sub meshes
  483. while ( subs_parsed < num_subs ) {
  484. var sm_len, sm_end, attrib;
  485. geom = new THREE.BufferGeometry();
  486. geom.name = name;
  487. geometries.push( geom );
  488. sm_len = this.readU32();
  489. sm_end = this._ptr + sm_len;
  490. // Ignore for now
  491. this.parseProperties( { 1: this._geoNrType, 2: this._geoNrType } );
  492. // Loop through data streams
  493. while ( this._ptr < sm_end ) {
  494. var idx = 0,
  495. str_type = this.readU8(),
  496. str_ftype = this.readU8(),
  497. str_len = this.readU32(),
  498. str_end = str_len + this._ptr;
  499. // VERTICES
  500. // ------------------
  501. if ( str_type === 1 ) {
  502. buffer = new Float32Array( ( str_len / 12 ) * 3 );
  503. attrib = new THREE.BufferAttribute( buffer, 3 );
  504. geom.addAttribute( 'position', attrib );
  505. idx = 0;
  506. while ( this._ptr < str_end ) {
  507. buffer[ idx ] = - this.readF32();
  508. buffer[ idx + 1 ] = this.readF32();
  509. buffer[ idx + 2 ] = this.readF32();
  510. idx += 3;
  511. }
  512. }
  513. // INDICES
  514. // -----------------
  515. else if ( str_type === 2 ) {
  516. buffer = new Uint16Array( str_len / 2 );
  517. attrib = new THREE.BufferAttribute( buffer, 1 );
  518. geom.addAttribute( 'index', attrib );
  519. geom.addDrawCall( 0, str_len / 2 );
  520. idx = 0;
  521. while ( this._ptr < str_end ) {
  522. buffer[ idx + 1 ] = this.readU16();
  523. buffer[ idx ] = this.readU16();
  524. buffer[ idx + 2 ] = this.readU16();
  525. idx += 3;
  526. }
  527. }
  528. // UVS
  529. // -------------------
  530. else if ( str_type === 3 ) {
  531. buffer = new Float32Array( ( str_len / 8 ) * 2 );
  532. attrib = new THREE.BufferAttribute( buffer, 2 );
  533. geom.addAttribute( 'uv', attrib );
  534. idx = 0;
  535. while ( this._ptr < str_end ) {
  536. buffer[ idx ] = this.readF32();
  537. buffer[ idx + 1 ] = 1.0 - this.readF32();
  538. idx += 2;
  539. }
  540. }
  541. // NORMALS
  542. else if ( str_type === 4 ) {
  543. buffer = new Float32Array( ( str_len / 12 ) * 3 );
  544. attrib = new THREE.BufferAttribute( buffer, 3 );
  545. geom.addAttribute( 'normal', attrib );
  546. idx = 0;
  547. while ( this._ptr < str_end ) {
  548. buffer[ idx ] = - this.readF32();
  549. buffer[ idx + 1 ] = this.readF32();
  550. buffer[ idx + 2 ] = this.readF32();
  551. idx += 3;
  552. }
  553. }
  554. // else if (str_type == 6) {
  555. // skinI = new Float32Array( str_len>>1 );
  556. // idx = 0
  557. // while (this._ptr < str_end) {
  558. // skinI[idx] = this.readU16();
  559. // idx++;
  560. // }
  561. // }
  562. // else if (str_type == 7) {
  563. // skinW = new Float32Array( str_len>>2 );
  564. // idx = 0;
  565. // while (this._ptr < str_end) {
  566. // skinW[idx] = this.readF32();
  567. // idx++;
  568. // }
  569. // }
  570. else {
  571. this._ptr = str_end;
  572. }
  573. }
  574. this.parseUserAttributes();
  575. geom.computeBoundingSphere();
  576. subs_parsed ++;
  577. }
  578. //geom.computeFaceNormals();
  579. this.parseUserAttributes();
  580. //finalizeAsset(geom, name);
  581. return geometries;
  582. },
  583. parseMeshPoseAnimation: function( len, poseOnly ) {
  584. var num_frames = 1,
  585. num_submeshes,
  586. frames_parsed,
  587. subMeshParsed,
  588. frame_dur,
  589. x, y, z,
  590. str_len,
  591. str_end,
  592. geom,
  593. subGeom,
  594. idx = 0,
  595. clip = {},
  596. indices,
  597. verts,
  598. num_Streams,
  599. streamsParsed,
  600. streamtypes = [],
  601. props,
  602. thisGeo,
  603. name = this.readUTF(),
  604. geoAdress = this.readU32();
  605. var mesh = this.getBlock( geoAdress );
  606. if ( mesh === null ) {
  607. console.log( "parseMeshPoseAnimation target mesh not found at:", geoAdress );
  608. return;
  609. }
  610. geom = mesh.geometry;
  611. geom.morphTargets = [];
  612. if ( ! poseOnly )
  613. num_frames = this.readU16();
  614. num_submeshes = this.readU16();
  615. num_Streams = this.readU16();
  616. // debug("VA num_frames : ", num_frames );
  617. // debug("VA num_submeshes : ", num_submeshes );
  618. // debug("VA numstreams : ", num_Streams );
  619. streamsParsed = 0;
  620. while ( streamsParsed < num_Streams ) {
  621. streamtypes.push( this.readU16() );
  622. streamsParsed ++;
  623. }
  624. props = this.parseProperties( { 1: BOOL, 2: BOOL } );
  625. clip.looping = props.get( 1, true );
  626. clip.stitchFinalFrame = props.get( 2, false );
  627. frames_parsed = 0;
  628. while ( frames_parsed < num_frames ) {
  629. frame_dur = this.readU16();
  630. subMeshParsed = 0;
  631. while ( subMeshParsed < num_submeshes ) {
  632. streamsParsed = 0;
  633. str_len = this.readU32();
  634. str_end = this._ptr + str_len;
  635. while ( streamsParsed < num_Streams ) {
  636. if ( streamtypes[ streamsParsed ] === 1 ) {
  637. //geom.addAttribute( 'morphTarget'+frames_parsed, Float32Array, str_len/12, 3 );
  638. var buffer = new Float32Array( str_len / 4 );
  639. geom.morphTargets.push( {
  640. array : buffer
  641. } );
  642. //buffer = geom.attributes['morphTarget'+frames_parsed].array
  643. idx = 0;
  644. while ( this._ptr < str_end ) {
  645. buffer[ idx ] = this.readF32();
  646. buffer[ idx + 1 ] = this.readF32();
  647. buffer[ idx + 2 ] = this.readF32();
  648. idx += 3;
  649. }
  650. subMeshParsed ++;
  651. } else
  652. this._ptr = str_end;
  653. streamsParsed ++;
  654. }
  655. }
  656. frames_parsed ++;
  657. }
  658. this.parseUserAttributes();
  659. return null;
  660. },
  661. getBlock: function ( id ) {
  662. return this._blocks[ id ].data;
  663. },
  664. parseMatrix4: function () {
  665. var mtx = new THREE.Matrix4();
  666. var e = mtx.elements;
  667. e[ 0 ] = this.readF32();
  668. e[ 1 ] = this.readF32();
  669. e[ 2 ] = this.readF32();
  670. e[ 3 ] = 0.0;
  671. //e[3] = 0.0;
  672. e[ 4 ] = this.readF32();
  673. e[ 5 ] = this.readF32();
  674. e[ 6 ] = this.readF32();
  675. //e[7] = this.readF32();
  676. e[ 7 ] = 0.0;
  677. e[ 8 ] = this.readF32();
  678. e[ 9 ] = this.readF32();
  679. e[ 10 ] = this.readF32();
  680. //e[11] = this.readF32();
  681. e[ 11 ] = 0.0;
  682. e[ 12 ] = - this.readF32();
  683. e[ 13 ] = this.readF32();
  684. e[ 14 ] = this.readF32();
  685. //e[15] = this.readF32();
  686. e[ 15 ] = 1.0;
  687. return mtx;
  688. },
  689. parseProperties: function ( expected ) {
  690. var list_len = this.readU32();
  691. var list_end = this._ptr + list_len;
  692. var props = new AWDProperties();
  693. if ( expected ) {
  694. while ( this._ptr < list_end ) {
  695. var key = this.readU16();
  696. var len = this.readU32();
  697. var type;
  698. if ( expected.hasOwnProperty( key ) ) {
  699. type = expected[ key ];
  700. props.set( key, this.parseAttrValue( type, len ) );
  701. } else {
  702. this._ptr += len;
  703. }
  704. }
  705. }
  706. return props;
  707. },
  708. parseUserAttributes: function () {
  709. // skip for now
  710. this._ptr = this.readU32() + this._ptr;
  711. return null;
  712. },
  713. parseAttrValue: function ( type, len ) {
  714. var elem_len;
  715. var read_func;
  716. switch ( type ) {
  717. case AWD_FIELD_INT8:
  718. elem_len = 1;
  719. read_func = this.readI8;
  720. break;
  721. case AWD_FIELD_INT16:
  722. elem_len = 2;
  723. read_func = this.readI16;
  724. break;
  725. case AWD_FIELD_INT32:
  726. elem_len = 4;
  727. read_func = this.readI32;
  728. break;
  729. case AWD_FIELD_BOOL:
  730. case AWD_FIELD_UINT8:
  731. elem_len = 1;
  732. read_func = this.readU8;
  733. break;
  734. case AWD_FIELD_UINT16:
  735. elem_len = 2;
  736. read_func = this.readU16;
  737. break;
  738. case AWD_FIELD_UINT32:
  739. case AWD_FIELD_BADDR:
  740. elem_len = 4;
  741. read_func = this.readU32;
  742. break;
  743. case AWD_FIELD_FLOAT32:
  744. elem_len = 4;
  745. read_func = this.readF32;
  746. break;
  747. case AWD_FIELD_FLOAT64:
  748. elem_len = 8;
  749. read_func = this.readF64;
  750. break;
  751. case AWD_FIELD_VECTOR2x1:
  752. case AWD_FIELD_VECTOR3x1:
  753. case AWD_FIELD_VECTOR4x1:
  754. case AWD_FIELD_MTX3x2:
  755. case AWD_FIELD_MTX3x3:
  756. case AWD_FIELD_MTX4x3:
  757. case AWD_FIELD_MTX4x4:
  758. elem_len = 8;
  759. read_func = this.readF64;
  760. break;
  761. }
  762. if ( elem_len < len ) {
  763. var list;
  764. var num_read;
  765. var num_elems;
  766. list = [];
  767. num_read = 0;
  768. num_elems = len / elem_len;
  769. while ( num_read < num_elems ) {
  770. list.push( read_func.call( this ) );
  771. num_read ++;
  772. }
  773. return list;
  774. }
  775. else {
  776. return read_func.call( this );
  777. }
  778. },
  779. readU8: function () {
  780. return this._data.getUint8( this._ptr ++ );
  781. },
  782. readI8: function () {
  783. return this._data.getInt8( this._ptr ++ );
  784. },
  785. readU16: function () {
  786. var a = this._data.getUint16( this._ptr, littleEndian );
  787. this._ptr += 2;
  788. return a;
  789. },
  790. readI16: function () {
  791. var a = this._data.getInt16( this._ptr, littleEndian );
  792. this._ptr += 2;
  793. return a;
  794. },
  795. readU32: function () {
  796. var a = this._data.getUint32( this._ptr, littleEndian );
  797. this._ptr += 4;
  798. return a;
  799. },
  800. readI32: function () {
  801. var a = this._data.getInt32( this._ptr, littleEndian );
  802. this._ptr += 4;
  803. return a;
  804. },
  805. readF32: function () {
  806. var a = this._data.getFloat32( this._ptr, littleEndian );
  807. this._ptr += 4;
  808. return a;
  809. },
  810. readF64: function () {
  811. var a = this._data.getFloat64( this._ptr, littleEndian );
  812. this._ptr += 8;
  813. return a;
  814. },
  815. /**
  816. * Converts a UTF-8 byte array to JavaScript's 16-bit Unicode.
  817. * @param {Array.<number>} bytes UTF-8 byte array.
  818. * @return {string} 16-bit Unicode string.
  819. */
  820. readUTF: function () {
  821. var len = this.readU16();
  822. return this.readUTFBytes( len );
  823. },
  824. /**
  825. * Converts a UTF-8 byte array to JavaScript's 16-bit Unicode.
  826. * @param {Array.<number>} bytes UTF-8 byte array.
  827. * @return {string} 16-bit Unicode string.
  828. */
  829. readUTFBytes: function ( len ) {
  830. // TODO(user): Use native implementations if/when available
  831. var out = [], c = 0;
  832. while ( out.length < len ) {
  833. var c1 = this._data.getUint8( this._ptr ++, littleEndian );
  834. if ( c1 < 128 ) {
  835. out[ c ++ ] = String.fromCharCode( c1 );
  836. } else if ( c1 > 191 && c1 < 224 ) {
  837. var c2 = this._data.getUint8( this._ptr ++, littleEndian );
  838. out[ c ++ ] = String.fromCharCode( ( c1 & 31 ) << 6 | c2 & 63 );
  839. } else {
  840. var c2 = this._data.getUint8( this._ptr ++, littleEndian );
  841. var c3 = this._data.getUint8( this._ptr ++, littleEndian );
  842. out[ c ++ ] = String.fromCharCode(
  843. ( c1 & 15 ) << 12 | ( c2 & 63 ) << 6 | c3 & 63
  844. );
  845. }
  846. }
  847. return out.join( '' );
  848. }
  849. };
  850. } )();