AWDLoader.js 23 KB

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