AWDLoader.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. {
  49. this[key] = value;
  50. },
  51. get : function(key, fallback)
  52. {
  53. if ( this.hasOwnProperty(key) )
  54. return this[key];
  55. else return fallback;
  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;
  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.XHRLoader( this.manager );
  83. loader.setCrossOrigin( this.crossOrigin );
  84. loader.setResponseType('arraybuffer');
  85. loader.load( url, function ( text ) {
  86. onLoad( scope.parse(text) );
  87. }, onProgress, onError );
  88. },
  89. setCrossOrigin: function ( value ) {
  90. this.crossOrigin = value;
  91. },
  92. parse: function ( data ) {
  93. var blen = data.byteLength;
  94. this._ptr = 0;
  95. this._data = new DataView( data );
  96. this._parseHeader( );
  97. if( this._compression != 0 ) {
  98. console.error( 'compressed AWD not supported' );
  99. }
  100. if (!this._streaming && this._bodylen != data.byteLength - this._ptr ) {
  101. console.error('AWDLoader: body len does not match file length', this._bodylen , blen - this._ptr);
  102. }
  103. while ( this._ptr < blen ) {
  104. this.parseNextBlock();
  105. }
  106. return this.trunk;
  107. },
  108. parseNextBlock: function() {
  109. var assetData,
  110. ns, type, len, block,
  111. blockId = this.readU32(),
  112. ns = this.readU8(),
  113. type = this.readU8(),
  114. flags = this.readU8(),
  115. len = this.readU32();
  116. switch (type) {
  117. case 1:
  118. assetData = this.parseMeshData(len);
  119. break;
  120. case 22:
  121. assetData = this.parseContainer(len);
  122. break;
  123. case 23:
  124. assetData = this.parseMeshInstance(len);
  125. break;
  126. case 81:
  127. assetData = this.parseMaterial(len);
  128. break;
  129. case 82:
  130. assetData = this.parseTexture(len);
  131. break;
  132. case 101:
  133. assetData = this.parseSkeleton(len);
  134. break;
  135. // case 111:
  136. // assetData = this.parseMeshPoseAnimation(len, true);
  137. // break;
  138. case 112:
  139. assetData = this.parseMeshPoseAnimation(len, false);
  140. break;
  141. case 113:
  142. assetData = this.parseVertexAnimationSet(len);
  143. break;
  144. case 102:
  145. assetData = this.parseSkeletonPose(len);
  146. break;
  147. case 103:
  148. assetData = this.parseSkeletonAnimation(len);
  149. break;
  150. case 122:
  151. assetData = this.parseAnimatorSet(len);
  152. break;
  153. // case 121:
  154. // assetData = parseUVAnimation(len);
  155. // break;
  156. default:
  157. //debug('Ignoring block!',type, len);
  158. this._ptr += len;
  159. break;
  160. }
  161. // Store block reference for later use
  162. this._blocks[blockId] = block = new Block();
  163. block.data = assetData;
  164. block.id = blockId;
  165. },
  166. _parseHeader: function () {
  167. var version = this._version,
  168. awdmagic =
  169. ( this.readU8()<<16)
  170. | ( this.readU8()<<8 )
  171. | this.readU8();
  172. if( awdmagic != 4282180 )
  173. throw new Error( "AWDLoader - bad magic" );
  174. version[0] = this.readU8();
  175. version[1] = this.readU8();
  176. var flags = this.readU16();
  177. this._streaming = (flags & 0x1) == 0x1;
  178. if ((version[0] === 2) && (version[1] === 1)) {
  179. this._accuracyMatrix = (flags & 0x2) === 0x2;
  180. this._accuracyGeo = (flags & 0x4) === 0x4;
  181. this._accuracyProps = (flags & 0x8) === 0x8;
  182. }
  183. this._geoNrType = this._accuracyGeo ? FLOAT64 : FLOAT32;
  184. this._matrixNrType = this._accuracyMatrix ? FLOAT64 : FLOAT32;
  185. this._propsNrType = this._accuracyProps ? FLOAT64 : FLOAT32;
  186. this._optimized_for_accuracy = (flags & 0x2) === 0x2;
  187. this._compression = this.readU8();
  188. this._bodylen = this.readU32();
  189. },
  190. parseContainer: function ( len ) {
  191. var parent,
  192. ctr = new THREE.Object3D(),
  193. par_id = this.readU32(),
  194. mtx = this.parseMatrix4();
  195. ctr.name = this.readUTF();
  196. ctr.applyMatrix( mtx );
  197. parent = this._blocks[par_id].data || this.trunk;
  198. parent.add(ctr);
  199. this.parseProperties({
  200. 1:this._matrixNrType,
  201. 2:this._matrixNrType,
  202. 3:this._matrixNrType,
  203. 4:UINT8
  204. });
  205. ctr.extra = this.parseUserAttributes();
  206. return ctr;
  207. },
  208. parseMeshInstance: function ( len ) {
  209. var name,
  210. mesh, geometries, meshLen, meshes,
  211. par_id, data_id,
  212. mtx,
  213. materials, mat, mat_id,
  214. num_materials,
  215. materials_parsed,
  216. parent,
  217. i;
  218. par_id = this.readU32();
  219. mtx = this.parseMatrix4();
  220. name = this.readUTF();
  221. data_id = this.readU32();
  222. num_materials = this.readU16();
  223. geometries = this.getBlock( data_id );
  224. materials = [];
  225. materials_parsed = 0;
  226. for ( i = 0; i < num_materials; i++) {
  227. mat_id = this.readU32();
  228. mat = this.getBlock( mat_id );
  229. materials.push( mat );
  230. }
  231. meshLen = geometries.length;
  232. meshes = [];
  233. // TODO : BufferGeometry don't support "geometryGroups" for now.
  234. // so we create sub meshes for each groups
  235. if( meshLen > 1 ) {
  236. mesh = new THREE.Object3D();
  237. for ( i = 0; i < meshLen; i++) {
  238. var sm = new THREE.Mesh( geometries[i] );
  239. meshes.push( sm );
  240. mesh.add( sm );
  241. }
  242. }
  243. else {
  244. mesh = new THREE.Mesh( geometries[0] );
  245. meshes.push( mesh );
  246. }
  247. mesh.applyMatrix( mtx );
  248. mesh.name = name;
  249. parent = this.getBlock( par_id ) || this.trunk;
  250. parent.add( mesh );
  251. var matLen = materials.length;
  252. var maxLen = Math.max( meshLen, matLen);
  253. for( i = 0; i< maxLen; i++ )
  254. meshes[ i%meshLen ].material = materials[ i % matLen ];
  255. // Ignore for now
  256. this.parseProperties( null );
  257. mesh.extra = this.parseUserAttributes();
  258. return mesh;
  259. },
  260. parseMaterial: function ( len ) {
  261. var name,
  262. type,
  263. props,
  264. mat,
  265. attributes,
  266. finalize,
  267. num_methods,
  268. methods_parsed;
  269. name = this.readUTF();
  270. type = this.readU8();
  271. num_methods = this.readU8();
  272. //log( "AWDLoader parseMaterial ",name )
  273. // Read material numerical properties
  274. // (1=color, 2=bitmap url, 11=alpha_blending, 12=alpha_threshold, 13=repeat)
  275. props = this.parseProperties({
  276. 1: AWD_FIELD_INT32,
  277. 2: AWD_FIELD_BADDR,
  278. 11: AWD_FIELD_BOOL,
  279. 12: AWD_FIELD_FLOAT32,
  280. 13: AWD_FIELD_BOOL
  281. });
  282. methods_parsed = 0;
  283. while( methods_parsed < num_methods ) {
  284. var method_type = this.readU16();
  285. this.parseProperties( null );
  286. this.parseUserAttributes();
  287. }
  288. attributes = this.parseUserAttributes();
  289. if( this.materialFactory !== undefined ) {
  290. mat = this.materialFactory( name );
  291. if( mat ) return mat;
  292. }
  293. mat = new THREE.MeshPhongMaterial();
  294. if (type === 1) { // Color material
  295. mat.color.setHex( props.get(1, 0xcccccc) );
  296. }
  297. else if (type === 2) { // 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 ) { // Array<Bone>
  335. var name = this.readUTF(),
  336. num_joints = this.readU16(),
  337. skeleton = [],
  338. joints_parsed = 0;
  339. this.parseProperties( null );
  340. while (joints_parsed < num_joints) {
  341. var joint, ibp;
  342. // Ignore joint id
  343. this.readU16();
  344. joint = new THREE.Bone();
  345. joint.parent = this.readU16() - 1; // 0=null in AWD
  346. joint.name = this.readUTF();
  347. ibp = this.parseMatrix4();
  348. joint.skinMatrix = ibp;
  349. // Ignore joint props/attributes for now
  350. this.parseProperties(null);
  351. this.parseUserAttributes();
  352. skeleton.push(joint);
  353. joints_parsed++;
  354. }
  355. // Discard attributes for now
  356. this.parseUserAttributes();
  357. return skeleton;
  358. },
  359. parseSkeletonPose: function( blockID ) {
  360. var name = this.readUTF();
  361. var num_joints = this.readU16();
  362. this.parseProperties(null);
  363. // debug( 'parse Skeleton Pose. joints : ' + num_joints);
  364. var pose = [];
  365. var joints_parsed = 0;
  366. while (joints_parsed < num_joints) {
  367. var joint_pose;
  368. var has_transform; //:uint;
  369. var mtx_data;
  370. has_transform = this.readU8();
  371. if (has_transform === 1) {
  372. mtx_data = this.parseMatrix4();
  373. } else
  374. {
  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.offsets.push({
  520. start: 0,
  521. index: 0,
  522. count: str_len/2
  523. });
  524. idx = 0;
  525. while (this._ptr < str_end) {
  526. buffer[idx+1] = this.readU16();
  527. buffer[idx] = this.readU16();
  528. buffer[idx+2] = this.readU16();
  529. idx+=3;
  530. }
  531. }
  532. // UVS
  533. // -------------------
  534. else if (str_type === 3) {
  535. buffer = new Float32Array( ( str_len / 8 ) * 2 );
  536. attrib = new THREE.BufferAttribute( buffer, 2 );
  537. geom.addAttribute( 'uv', attrib );
  538. idx = 0;
  539. while (this._ptr < str_end) {
  540. buffer[idx] = this.readF32();
  541. buffer[idx+1] = 1.0-this.readF32();
  542. idx+=2;
  543. }
  544. }
  545. // NORMALS
  546. else if (str_type === 4) {
  547. buffer = new Float32Array( ( str_len / 12 ) * 3 );
  548. attrib = new THREE.BufferAttribute( buffer, 3 );
  549. geom.addAttribute( 'normal', attrib );
  550. idx = 0;
  551. while (this._ptr < str_end) {
  552. buffer[idx] = -this.readF32();
  553. buffer[idx+1] = this.readF32();
  554. buffer[idx+2] = this.readF32();
  555. idx+=3;
  556. }
  557. }
  558. // else if (str_type == 6) {
  559. // skinI = new Float32Array( str_len>>1 );
  560. // idx = 0
  561. // while (this._ptr < str_end) {
  562. // skinI[idx] = this.readU16();
  563. // idx++;
  564. // }
  565. // }
  566. // else if (str_type == 7) {
  567. // skinW = new Float32Array( str_len>>2 );
  568. // idx = 0;
  569. // while (this._ptr < str_end) {
  570. // skinW[idx] = this.readF32();
  571. // idx++;
  572. // }
  573. // }
  574. else {
  575. this._ptr = str_end;
  576. }
  577. }
  578. this.parseUserAttributes();
  579. geom.computeBoundingSphere();
  580. subs_parsed++;
  581. }
  582. //geom.computeFaceNormals();
  583. this.parseUserAttributes();
  584. //finalizeAsset(geom, name);
  585. return geometries;
  586. },
  587. parseMeshPoseAnimation: function( len, poseOnly ) {
  588. var num_frames = 1,
  589. num_submeshes,
  590. frames_parsed,
  591. subMeshParsed,
  592. frame_dur,
  593. x, y, z,
  594. str_len,
  595. str_end,
  596. geom,
  597. subGeom,
  598. idx = 0,
  599. clip = {},
  600. indices,
  601. verts,
  602. num_Streams,
  603. streamsParsed,
  604. streamtypes = [],
  605. props,
  606. thisGeo,
  607. name = this.readUTF(),
  608. geoAdress = this.readU32();
  609. var mesh = this.getBlock( geoAdress );
  610. if (mesh === null) {
  611. console.log( "parseMeshPoseAnimation target mesh not found at:", geoAdress );
  612. return;
  613. }
  614. geom = mesh.geometry;
  615. geom.morphTargets = [];
  616. if (!poseOnly)
  617. num_frames = this.readU16();
  618. num_submeshes = this.readU16();
  619. num_Streams = this.readU16();
  620. // debug("VA num_frames : ", num_frames );
  621. // debug("VA num_submeshes : ", num_submeshes );
  622. // debug("VA numstreams : ", num_Streams );
  623. streamsParsed = 0;
  624. while ( streamsParsed < num_Streams ) {
  625. streamtypes.push(this.readU16());
  626. streamsParsed++;
  627. }
  628. props = this.parseProperties( { 1:BOOL, 2:BOOL } );
  629. clip.looping = props.get( 1, true );
  630. clip.stitchFinalFrame = props.get( 2, false );
  631. frames_parsed = 0;
  632. while ( frames_parsed < num_frames ) {
  633. frame_dur = this.readU16();
  634. subMeshParsed = 0;
  635. while ( subMeshParsed < num_submeshes ) {
  636. streamsParsed = 0;
  637. str_len = this.readU32();
  638. str_end = this._ptr + str_len;
  639. while ( streamsParsed < num_Streams ) {
  640. if ( streamtypes[streamsParsed] === 1 ) {
  641. //geom.addAttribute( 'morphTarget'+frames_parsed, Float32Array, str_len/12, 3 );
  642. var buffer = new Float32Array(str_len/4);
  643. geom.morphTargets.push( {
  644. array : buffer
  645. });
  646. //buffer = geom.attributes['morphTarget'+frames_parsed].array
  647. idx = 0;
  648. while ( this._ptr < str_end ) {
  649. buffer[idx] = this.readF32();
  650. buffer[idx+1] = this.readF32();
  651. buffer[idx+2] = this.readF32();
  652. idx += 3;
  653. }
  654. subMeshParsed++;
  655. } else
  656. this._ptr = str_end;
  657. streamsParsed++;
  658. }
  659. }
  660. frames_parsed++;
  661. }
  662. this.parseUserAttributes();
  663. return null;
  664. },
  665. getBlock: function ( id ) {
  666. return this._blocks[id].data;
  667. },
  668. parseMatrix4: function () {
  669. var mtx = new THREE.Matrix4();
  670. var e = mtx.elements;
  671. e[0] = this.readF32();
  672. e[1] = this.readF32();
  673. e[2] = this.readF32();
  674. e[3] = 0.0;
  675. //e[3] = 0.0;
  676. e[4] = this.readF32();
  677. e[5] = this.readF32();
  678. e[6] = this.readF32();
  679. //e[7] = this.readF32();
  680. e[7] = 0.0;
  681. e[8] = this.readF32();
  682. e[9] = this.readF32();
  683. e[10] = this.readF32();
  684. //e[11] = this.readF32();
  685. e[11] = 0.0;
  686. e[12] = -this.readF32();
  687. e[13] = this.readF32();
  688. e[14] = this.readF32();
  689. //e[15] = this.readF32();
  690. e[15] = 1.0;
  691. return mtx;
  692. },
  693. parseProperties: function ( expected ) {
  694. var list_len = this.readU32();
  695. var list_end = this._ptr + list_len;
  696. var props = new AWDProperties();
  697. if( expected ) {
  698. while( this._ptr < list_end ) {
  699. var key = this.readU16();
  700. var len = this.readU32();
  701. var type;
  702. if( expected.hasOwnProperty( key ) ) {
  703. type = expected[ key ];
  704. props.set( key, this.parseAttrValue( type, len ) );
  705. } else {
  706. this._ptr += len;
  707. }
  708. }
  709. }
  710. return props;
  711. },
  712. parseUserAttributes: function () {
  713. // skip for now
  714. this._ptr = this.readU32() + this._ptr;
  715. return null;
  716. },
  717. parseAttrValue: function ( type, len ) {
  718. var elem_len;
  719. var read_func;
  720. switch (type) {
  721. case AWD_FIELD_INT8:
  722. elem_len = 1;
  723. read_func = this.readI8;
  724. break;
  725. case AWD_FIELD_INT16:
  726. elem_len = 2;
  727. read_func = this.readI16;
  728. break;
  729. case AWD_FIELD_INT32:
  730. elem_len = 4;
  731. read_func = this.readI32;
  732. break;
  733. case AWD_FIELD_BOOL:
  734. case AWD_FIELD_UINT8:
  735. elem_len = 1;
  736. read_func = this.readU8;
  737. break;
  738. case AWD_FIELD_UINT16:
  739. elem_len = 2;
  740. read_func = this.readU16;
  741. break;
  742. case AWD_FIELD_UINT32:
  743. case AWD_FIELD_BADDR:
  744. elem_len = 4;
  745. read_func = this.readU32;
  746. break;
  747. case AWD_FIELD_FLOAT32:
  748. elem_len = 4;
  749. read_func = this.readF32;
  750. break;
  751. case AWD_FIELD_FLOAT64:
  752. elem_len = 8;
  753. read_func = this.readF64;
  754. break;
  755. case AWD_FIELD_VECTOR2x1:
  756. case AWD_FIELD_VECTOR3x1:
  757. case AWD_FIELD_VECTOR4x1:
  758. case AWD_FIELD_MTX3x2:
  759. case AWD_FIELD_MTX3x3:
  760. case AWD_FIELD_MTX4x3:
  761. case AWD_FIELD_MTX4x4:
  762. elem_len = 8;
  763. read_func = this.readF64;
  764. break;
  765. }
  766. if (elem_len < len) {
  767. var list;
  768. var num_read;
  769. var num_elems;
  770. list = [];
  771. num_read = 0;
  772. num_elems = len / elem_len;
  773. while (num_read < num_elems) {
  774. list.push(read_func.call( this ) );
  775. num_read++;
  776. }
  777. return list;
  778. }
  779. else {
  780. return read_func.call( this );
  781. }
  782. },
  783. readU8: function () {
  784. return this._data.getUint8( this._ptr++ );
  785. },
  786. readI8: function () {
  787. return this._data.getInt8( this._ptr++ );
  788. },
  789. readU16: function () {
  790. var a = this._data.getUint16( this._ptr, littleEndian );
  791. this._ptr += 2;
  792. return a;
  793. },
  794. readI16: function () {
  795. var a = this._data.getInt16( this._ptr, littleEndian );
  796. this._ptr += 2;
  797. return a;
  798. },
  799. readU32: function () {
  800. var a = this._data.getUint32( this._ptr, littleEndian );
  801. this._ptr += 4;
  802. return a;
  803. },
  804. readI32: function () {
  805. var a = this._data.getInt32( this._ptr, littleEndian );
  806. this._ptr += 4;
  807. return a;
  808. },
  809. readF32: function () {
  810. var a = this._data.getFloat32( this._ptr, littleEndian );
  811. this._ptr += 4;
  812. return a;
  813. },
  814. readF64: function () {
  815. var a = this._data.getFloat64( this._ptr, littleEndian );
  816. this._ptr += 8;
  817. return a;
  818. },
  819. /**
  820. * Converts a UTF-8 byte array to JavaScript's 16-bit Unicode.
  821. * @param {Array.<number>} bytes UTF-8 byte array.
  822. * @return {string} 16-bit Unicode string.
  823. */
  824. readUTF: function () {
  825. var len = this.readU16();
  826. return this.readUTFBytes( len );
  827. },
  828. /**
  829. * Converts a UTF-8 byte array to JavaScript's 16-bit Unicode.
  830. * @param {Array.<number>} bytes UTF-8 byte array.
  831. * @return {string} 16-bit Unicode string.
  832. */
  833. readUTFBytes: function ( len ) {
  834. // TODO(user): Use native implementations if/when available
  835. var out = [], c = 0;
  836. while ( out.length < len ) {
  837. var c1 = this._data.getUint8( this._ptr++, littleEndian );
  838. if (c1 < 128) {
  839. out[c++] = String.fromCharCode(c1);
  840. } else if (c1 > 191 && c1 < 224) {
  841. var c2 = this._data.getUint8( this._ptr++, littleEndian );
  842. out[c++] = String.fromCharCode((c1 & 31) << 6 | c2 & 63);
  843. } else {
  844. var c2 = this._data.getUint8( this._ptr++, littleEndian );
  845. var c3 = this._data.getUint8( this._ptr++, littleEndian );
  846. out[c++] = String.fromCharCode(
  847. (c1 & 15) << 12 | (c2 & 63) << 6 | c3 & 63
  848. );
  849. }
  850. }
  851. return out.join('');
  852. }
  853. };
  854. })();