AWDLoader.js 23 KB

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