AWDLoader.js 22 KB

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