AWDLoader.js 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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. this.namespace = 0;
  45. this.flags = 0;
  46. }
  47. function AWDProperties() {}
  48. AWDProperties.prototype = {
  49. set: function ( key, value ) {
  50. this[ key ] = value;
  51. },
  52. get: function ( key, fallback ) {
  53. if ( this.hasOwnProperty( key ) ) {
  54. return this[ key ];
  55. } else {
  56. return fallback;
  57. }
  58. }
  59. };
  60. var AWDLoader = function ( manager ) {
  61. THREE.Loader.call( this, manager );
  62. this.trunk = new THREE.Object3D();
  63. this.materialFactory = undefined;
  64. this._url = '';
  65. this._baseDir = '';
  66. this._data = undefined;
  67. this._ptr = 0;
  68. this._version = [];
  69. this._streaming = false;
  70. this._optimized_for_accuracy = false;
  71. this._compression = 0;
  72. this._bodylen = 0xFFFFFFFF;
  73. this._blocks = [ new Block() ];
  74. this._accuracyMatrix = false;
  75. this._accuracyGeo = false;
  76. this._accuracyProps = false;
  77. };
  78. AWDLoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), {
  79. constructor: AWDLoader,
  80. load: function ( url, onLoad, onProgress, onError ) {
  81. var scope = this;
  82. this._url = url;
  83. this._baseDir = url.substr( 0, url.lastIndexOf( '/' ) + 1 );
  84. var loader = new THREE.FileLoader( this.manager );
  85. loader.setPath( this.path );
  86. loader.setResponseType( 'arraybuffer' );
  87. loader.load( url, function ( text ) {
  88. onLoad( scope.parse( text ) );
  89. }, onProgress, onError );
  90. },
  91. parse: function ( data ) {
  92. var blen = data.byteLength;
  93. this._ptr = 0;
  94. this._data = new DataView( data );
  95. this._parseHeader( );
  96. if ( this._compression != 0 ) {
  97. console.error( 'compressed AWD not supported' );
  98. }
  99. if ( ! this._streaming && this._bodylen != data.byteLength - this._ptr ) {
  100. console.error( 'AWDLoader: body len does not match file length', this._bodylen, blen - this._ptr );
  101. }
  102. while ( this._ptr < blen ) {
  103. this.parseNextBlock();
  104. }
  105. return this.trunk;
  106. },
  107. parseNextBlock: function () {
  108. var assetData,
  109. block,
  110. blockId = this.readU32(),
  111. ns = this.readU8(),
  112. type = this.readU8(),
  113. flags = this.readU8(),
  114. len = this.readU32();
  115. switch ( type ) {
  116. case 1:
  117. assetData = this.parseMeshData();
  118. break;
  119. case 22:
  120. assetData = this.parseContainer();
  121. break;
  122. case 23:
  123. assetData = this.parseMeshInstance();
  124. break;
  125. case 81:
  126. assetData = this.parseMaterial();
  127. break;
  128. case 82:
  129. assetData = this.parseTexture();
  130. break;
  131. case 101:
  132. assetData = this.parseSkeleton();
  133. break;
  134. case 112:
  135. assetData = this.parseMeshPoseAnimation( false );
  136. break;
  137. case 113:
  138. assetData = this.parseVertexAnimationSet();
  139. break;
  140. case 102:
  141. assetData = this.parseSkeletonPose();
  142. break;
  143. case 103:
  144. assetData = this.parseSkeletonAnimation();
  145. break;
  146. case 122:
  147. assetData = this.parseAnimatorSet();
  148. break;
  149. default:
  150. //debug('Ignoring block!',type, len);
  151. this._ptr += len;
  152. break;
  153. }
  154. // Store block reference for later use
  155. this._blocks[ blockId ] = block = new Block();
  156. block.data = assetData;
  157. block.id = blockId;
  158. block.namespace = ns;
  159. block.flags = flags;
  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. // read method_type before
  273. this.readU16();
  274. this.parseProperties( null );
  275. this.parseUserAttributes();
  276. }
  277. attributes = this.parseUserAttributes();
  278. if ( this.materialFactory !== undefined ) {
  279. mat = this.materialFactory( name );
  280. if ( mat ) return mat;
  281. }
  282. mat = new THREE.MeshPhongMaterial();
  283. if ( type === 1 ) {
  284. // Color material
  285. mat.color.setHex( props.get( 1, 0xcccccc ) );
  286. } else if ( type === 2 ) {
  287. // Bitmap material
  288. var tex_addr = props.get( 2, 0 );
  289. mat.map = this.getBlock( tex_addr );
  290. }
  291. mat.extra = attributes;
  292. mat.alphaThreshold = props.get( 12, 0.0 );
  293. mat.repeat = props.get( 13, false );
  294. return mat;
  295. },
  296. parseTexture: function () {
  297. var name = this.readUTF(),
  298. type = this.readU8(),
  299. asset,
  300. data_len;
  301. // External
  302. if ( type === 0 ) {
  303. data_len = this.readU32();
  304. var url = this.readUTFBytes( data_len );
  305. console.log( url );
  306. asset = this.loadTexture( url );
  307. asset.userData = {};
  308. asset.userData.name = name;
  309. } else {
  310. // embed texture not supported
  311. }
  312. // Ignore for now
  313. this.parseProperties( null );
  314. this.parseUserAttributes();
  315. return asset;
  316. },
  317. loadTexture: function ( url ) {
  318. var tex = new THREE.Texture();
  319. var loader = new THREE.ImageLoader( this.manager );
  320. loader.load( this._baseDir + url, function ( image ) {
  321. tex.image = image;
  322. tex.needsUpdate = true;
  323. } );
  324. return tex;
  325. },
  326. parseSkeleton: function () {
  327. // Array<Bone>
  328. //
  329. this.readUTF();
  330. var num_joints = this.readU16(),
  331. skeleton = [],
  332. joints_parsed = 0;
  333. this.parseProperties( null );
  334. while ( joints_parsed < num_joints ) {
  335. var joint, ibp;
  336. // Ignore joint id
  337. this.readU16();
  338. joint = new THREE.Bone();
  339. joint.parent = this.readU16() - 1; // 0=null in AWD
  340. joint.name = this.readUTF();
  341. ibp = this.parseMatrix4();
  342. joint.skinMatrix = ibp;
  343. // Ignore joint props/attributes for now
  344. this.parseProperties( null );
  345. this.parseUserAttributes();
  346. skeleton.push( joint );
  347. joints_parsed ++;
  348. }
  349. // Discard attributes for now
  350. this.parseUserAttributes();
  351. return skeleton;
  352. },
  353. parseSkeletonPose: function () {
  354. var name = this.readUTF();
  355. var num_joints = this.readU16();
  356. this.parseProperties( null );
  357. // debug( 'parse Skeleton Pose. joints : ' + num_joints);
  358. var pose = [];
  359. var joints_parsed = 0;
  360. while ( joints_parsed < num_joints ) {
  361. var has_transform; //:uint;
  362. var mtx_data;
  363. has_transform = this.readU8();
  364. if ( has_transform === 1 ) {
  365. mtx_data = this.parseMatrix4();
  366. } else {
  367. mtx_data = new THREE.Matrix4();
  368. }
  369. pose[ joints_parsed ] = mtx_data;
  370. joints_parsed ++;
  371. }
  372. // Skip attributes for now
  373. this.parseUserAttributes();
  374. return pose;
  375. },
  376. parseSkeletonAnimation: function () {
  377. var frame_dur;
  378. var pose_addr;
  379. var pose;
  380. var name = this.readUTF();
  381. var clip = [];
  382. var num_frames = this.readU16();
  383. this.parseProperties( null );
  384. var frames_parsed = 0;
  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 () {
  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 () {
  421. var animSetBlockAdress; //:int
  422. var targetAnimationSet; //:AnimationSetBase;
  423. var name = this.readUTF();
  424. var type = this.readU16();
  425. var props = this.parseProperties( { 1: BADDR } );
  426. animSetBlockAdress = this.readU32();
  427. var targetMeshLength = this.readU16();
  428. var meshAdresses = []; //:Vector.<uint> = new Vector.<uint>;
  429. for ( var i = 0; i < targetMeshLength; i ++ )
  430. meshAdresses.push( this.readU32() );
  431. var activeState = this.readU16();
  432. var autoplay = Boolean( this.readU8() );
  433. this.parseUserAttributes();
  434. this.parseUserAttributes();
  435. var targetMeshes = []; //:Vector.<Mesh> = new Vector.<Mesh>;
  436. for ( i = 0; i < meshAdresses.length; i ++ ) {
  437. // returnedArray = getAssetByID(meshAdresses[i], [AssetType.MESH]);
  438. // if (returnedArray[0])
  439. targetMeshes.push( this._blocks[ meshAdresses[ i ] ].data );
  440. }
  441. targetAnimationSet = this._blocks[ animSetBlockAdress ].data;
  442. var thisAnimator;
  443. if ( type == 1 ) {
  444. thisAnimator = {
  445. animationSet: targetAnimationSet,
  446. skeleton: this._blocks[ props.get( 1, 0 ) ].data
  447. };
  448. } else if ( type == 2 ) {
  449. // debug( "vertex Anim???");
  450. }
  451. for ( i = 0; i < targetMeshes.length; i ++ ) {
  452. targetMeshes[ i ].animator = thisAnimator;
  453. }
  454. // debug("Parsed a Animator: Name = " + name);
  455. return thisAnimator;
  456. },
  457. parseMeshData: function () {
  458. var name = this.readUTF(),
  459. num_subs = this.readU16(),
  460. geom,
  461. subs_parsed = 0,
  462. buffer,
  463. geometries = [];
  464. // Ignore for now
  465. this.parseProperties( { 1: this._geoNrType, 2: this._geoNrType } );
  466. // Loop through sub meshes
  467. while ( subs_parsed < num_subs ) {
  468. var sm_len, sm_end, attrib;
  469. geom = new THREE.BufferGeometry();
  470. geom.name = name;
  471. geometries.push( geom );
  472. sm_len = this.readU32();
  473. sm_end = this._ptr + sm_len;
  474. // Ignore for now
  475. this.parseProperties( { 1: this._geoNrType, 2: this._geoNrType } );
  476. // Loop through data streams
  477. while ( this._ptr < sm_end ) {
  478. var idx = 0,
  479. str_type = this.readU8(),
  480. str_ftype = this.readU8(),
  481. str_len = this.readU32(),
  482. str_end = str_len + this._ptr;
  483. if ( str_type === 1 ) {
  484. // VERTICES
  485. buffer = new Float32Array( ( str_len / 12 ) * 3 );
  486. attrib = new THREE.BufferAttribute( buffer, 3 );
  487. geom.addAttribute( 'position', attrib );
  488. idx = 0;
  489. while ( this._ptr < str_end ) {
  490. buffer[ idx ] = - this.readF32();
  491. buffer[ idx + 1 ] = this.readF32();
  492. buffer[ idx + 2 ] = this.readF32();
  493. idx += 3;
  494. }
  495. } else if ( str_type === 2 ) {
  496. // INDICES
  497. buffer = new Uint16Array( str_len / 2 );
  498. attrib = new THREE.BufferAttribute( buffer, 1 );
  499. geom.setIndex( attrib );
  500. idx = 0;
  501. while ( this._ptr < str_end ) {
  502. buffer[ idx + 1 ] = this.readU16();
  503. buffer[ idx ] = this.readU16();
  504. buffer[ idx + 2 ] = this.readU16();
  505. idx += 3;
  506. }
  507. } else if ( str_type === 3 ) {
  508. // UVS
  509. buffer = new Float32Array( ( str_len / 8 ) * 2 );
  510. attrib = new THREE.BufferAttribute( buffer, 2 );
  511. geom.addAttribute( 'uv', attrib );
  512. idx = 0;
  513. while ( this._ptr < str_end ) {
  514. buffer[ idx ] = this.readF32();
  515. buffer[ idx + 1 ] = 1.0 - this.readF32();
  516. idx += 2;
  517. }
  518. } else if ( str_type === 4 ) {
  519. // NORMALS
  520. buffer = new Float32Array( ( str_len / 12 ) * 3 );
  521. attrib = new THREE.BufferAttribute( buffer, 3 );
  522. geom.addAttribute( 'normal', attrib );
  523. idx = 0;
  524. while ( this._ptr < str_end ) {
  525. buffer[ idx ] = - this.readF32();
  526. buffer[ idx + 1 ] = this.readF32();
  527. buffer[ idx + 2 ] = this.readF32();
  528. idx += 3;
  529. }
  530. } else {
  531. this._ptr = str_end;
  532. }
  533. }
  534. this.parseUserAttributes();
  535. geom.computeBoundingSphere();
  536. subs_parsed ++;
  537. }
  538. //geom.computeFaceNormals();
  539. this.parseUserAttributes();
  540. //finalizeAsset(geom, name);
  541. return geometries;
  542. },
  543. parseMeshPoseAnimation: function ( poseOnly ) {
  544. var num_frames = 1,
  545. num_submeshes,
  546. frames_parsed,
  547. subMeshParsed,
  548. str_len,
  549. str_end,
  550. geom,
  551. idx = 0,
  552. clip = {},
  553. num_Streams,
  554. streamsParsed,
  555. streamtypes = [],
  556. props,
  557. name = this.readUTF(),
  558. geoAdress = this.readU32();
  559. var mesh = this.getBlock( geoAdress );
  560. if ( mesh === null ) {
  561. console.log( "parseMeshPoseAnimation target mesh not found at:", geoAdress );
  562. return;
  563. }
  564. geom = mesh.geometry;
  565. geom.morphTargets = [];
  566. if ( ! poseOnly )
  567. num_frames = this.readU16();
  568. num_submeshes = this.readU16();
  569. num_Streams = this.readU16();
  570. // debug("VA num_frames : ", num_frames );
  571. // debug("VA num_submeshes : ", num_submeshes );
  572. // debug("VA numstreams : ", num_Streams );
  573. streamsParsed = 0;
  574. while ( streamsParsed < num_Streams ) {
  575. streamtypes.push( this.readU16() );
  576. streamsParsed ++;
  577. }
  578. props = this.parseProperties( { 1: BOOL, 2: BOOL } );
  579. clip.looping = props.get( 1, true );
  580. clip.stitchFinalFrame = props.get( 2, false );
  581. frames_parsed = 0;
  582. while ( frames_parsed < num_frames ) {
  583. this.readU16();
  584. subMeshParsed = 0;
  585. while ( subMeshParsed < num_submeshes ) {
  586. streamsParsed = 0;
  587. str_len = this.readU32();
  588. str_end = this._ptr + str_len;
  589. while ( streamsParsed < num_Streams ) {
  590. if ( streamtypes[ streamsParsed ] === 1 ) {
  591. //geom.addAttribute( 'morphTarget'+frames_parsed, Float32Array, str_len/12, 3 );
  592. var buffer = new Float32Array( str_len / 4 );
  593. geom.morphTargets.push( {
  594. array: buffer
  595. } );
  596. //buffer = geom.attributes['morphTarget'+frames_parsed].array
  597. idx = 0;
  598. while ( this._ptr < str_end ) {
  599. buffer[ idx ] = this.readF32();
  600. buffer[ idx + 1 ] = this.readF32();
  601. buffer[ idx + 2 ] = this.readF32();
  602. idx += 3;
  603. }
  604. subMeshParsed ++;
  605. } else
  606. this._ptr = str_end;
  607. streamsParsed ++;
  608. }
  609. }
  610. frames_parsed ++;
  611. }
  612. this.parseUserAttributes();
  613. return null;
  614. },
  615. getBlock: function ( id ) {
  616. return this._blocks[ id ].data;
  617. },
  618. parseMatrix4: function () {
  619. var mtx = new THREE.Matrix4();
  620. var e = mtx.elements;
  621. e[ 0 ] = this.readF32();
  622. e[ 1 ] = this.readF32();
  623. e[ 2 ] = this.readF32();
  624. e[ 3 ] = 0.0;
  625. //e[3] = 0.0;
  626. e[ 4 ] = this.readF32();
  627. e[ 5 ] = this.readF32();
  628. e[ 6 ] = this.readF32();
  629. //e[7] = this.readF32();
  630. e[ 7 ] = 0.0;
  631. e[ 8 ] = this.readF32();
  632. e[ 9 ] = this.readF32();
  633. e[ 10 ] = this.readF32();
  634. //e[11] = this.readF32();
  635. e[ 11 ] = 0.0;
  636. e[ 12 ] = - this.readF32();
  637. e[ 13 ] = this.readF32();
  638. e[ 14 ] = this.readF32();
  639. //e[15] = this.readF32();
  640. e[ 15 ] = 1.0;
  641. return mtx;
  642. },
  643. parseProperties: function ( expected ) {
  644. var list_len = this.readU32();
  645. var list_end = this._ptr + list_len;
  646. var props = new AWDProperties();
  647. if ( expected ) {
  648. while ( this._ptr < list_end ) {
  649. var key = this.readU16();
  650. var len = this.readU32();
  651. var type;
  652. if ( expected.hasOwnProperty( key ) ) {
  653. type = expected[ key ];
  654. props.set( key, this.parseAttrValue( type, len ) );
  655. } else {
  656. this._ptr += len;
  657. }
  658. }
  659. }
  660. return props;
  661. },
  662. parseUserAttributes: function () {
  663. // skip for now
  664. this._ptr = this.readU32() + this._ptr;
  665. return null;
  666. },
  667. parseAttrValue: function ( type, len ) {
  668. var elem_len;
  669. var read_func;
  670. switch ( type ) {
  671. case AWD_FIELD_INT8:
  672. elem_len = 1;
  673. read_func = this.readI8;
  674. break;
  675. case AWD_FIELD_INT16:
  676. elem_len = 2;
  677. read_func = this.readI16;
  678. break;
  679. case AWD_FIELD_INT32:
  680. elem_len = 4;
  681. read_func = this.readI32;
  682. break;
  683. case AWD_FIELD_BOOL:
  684. case AWD_FIELD_UINT8:
  685. elem_len = 1;
  686. read_func = this.readU8;
  687. break;
  688. case AWD_FIELD_UINT16:
  689. elem_len = 2;
  690. read_func = this.readU16;
  691. break;
  692. case AWD_FIELD_UINT32:
  693. case AWD_FIELD_BADDR:
  694. elem_len = 4;
  695. read_func = this.readU32;
  696. break;
  697. case AWD_FIELD_FLOAT32:
  698. elem_len = 4;
  699. read_func = this.readF32;
  700. break;
  701. case AWD_FIELD_FLOAT64:
  702. elem_len = 8;
  703. read_func = this.readF64;
  704. break;
  705. case AWD_FIELD_VECTOR2x1:
  706. case AWD_FIELD_VECTOR3x1:
  707. case AWD_FIELD_VECTOR4x1:
  708. case AWD_FIELD_MTX3x2:
  709. case AWD_FIELD_MTX3x3:
  710. case AWD_FIELD_MTX4x3:
  711. case AWD_FIELD_MTX4x4:
  712. elem_len = 8;
  713. read_func = this.readF64;
  714. break;
  715. }
  716. if ( elem_len < len ) {
  717. var list;
  718. var num_read;
  719. var num_elems;
  720. list = [];
  721. num_read = 0;
  722. num_elems = len / elem_len;
  723. while ( num_read < num_elems ) {
  724. list.push( read_func.call( this ) );
  725. num_read ++;
  726. }
  727. return list;
  728. } else {
  729. return read_func.call( this );
  730. }
  731. },
  732. readU8: function () {
  733. return this._data.getUint8( this._ptr ++ );
  734. },
  735. readI8: function () {
  736. return this._data.getInt8( this._ptr ++ );
  737. },
  738. readU16: function () {
  739. var a = this._data.getUint16( this._ptr, littleEndian );
  740. this._ptr += 2;
  741. return a;
  742. },
  743. readI16: function () {
  744. var a = this._data.getInt16( this._ptr, littleEndian );
  745. this._ptr += 2;
  746. return a;
  747. },
  748. readU32: function () {
  749. var a = this._data.getUint32( this._ptr, littleEndian );
  750. this._ptr += 4;
  751. return a;
  752. },
  753. readI32: function () {
  754. var a = this._data.getInt32( this._ptr, littleEndian );
  755. this._ptr += 4;
  756. return a;
  757. },
  758. readF32: function () {
  759. var a = this._data.getFloat32( this._ptr, littleEndian );
  760. this._ptr += 4;
  761. return a;
  762. },
  763. readF64: function () {
  764. var a = this._data.getFloat64( this._ptr, littleEndian );
  765. this._ptr += 8;
  766. return a;
  767. },
  768. /**
  769. * Converts a UTF-8 byte array to JavaScript's 16-bit Unicode.
  770. * @param {Array.<number>} bytes UTF-8 byte array.
  771. * @return {string} 16-bit Unicode string.
  772. */
  773. readUTF: function () {
  774. var len = this.readU16();
  775. return this.readUTFBytes( len );
  776. },
  777. /**
  778. * Converts a UTF-8 byte array to JavaScript's 16-bit Unicode.
  779. * @param {Array.<number>} bytes UTF-8 byte array.
  780. * @return {string} 16-bit Unicode string.
  781. */
  782. readUTFBytes: function ( len ) {
  783. // TODO(user): Use native implementations if/when available
  784. var out = [], c = 0;
  785. while ( out.length < len ) {
  786. var c1 = this._data.getUint8( this._ptr ++, littleEndian );
  787. if ( c1 < 128 ) {
  788. out[ c ++ ] = String.fromCharCode( c1 );
  789. } else if ( c1 > 191 && c1 < 224 ) {
  790. var c2 = this._data.getUint8( this._ptr ++, littleEndian );
  791. out[ c ++ ] = String.fromCharCode( ( c1 & 31 ) << 6 | c2 & 63 );
  792. } else {
  793. var c2 = this._data.getUint8( this._ptr ++, littleEndian );
  794. var c3 = this._data.getUint8( this._ptr ++, littleEndian );
  795. out[ c ++ ] = String.fromCharCode( ( c1 & 15 ) << 12 | ( c2 & 63 ) << 6 | c3 & 63 );
  796. }
  797. }
  798. return out.join( '' );
  799. }
  800. } );
  801. return AWDLoader;
  802. } )();