AWDLoader.js 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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.addDrawCall( 0, str_len / 2 );
  520. idx = 0;
  521. while (this._ptr < str_end) {
  522. buffer[idx+1] = this.readU16();
  523. buffer[idx] = this.readU16();
  524. buffer[idx+2] = this.readU16();
  525. idx+=3;
  526. }
  527. }
  528. // UVS
  529. // -------------------
  530. else if (str_type === 3) {
  531. buffer = new Float32Array( ( str_len / 8 ) * 2 );
  532. attrib = new THREE.BufferAttribute( buffer, 2 );
  533. geom.addAttribute( 'uv', attrib );
  534. idx = 0;
  535. while (this._ptr < str_end) {
  536. buffer[idx] = this.readF32();
  537. buffer[idx+1] = 1.0-this.readF32();
  538. idx+=2;
  539. }
  540. }
  541. // NORMALS
  542. else if (str_type === 4) {
  543. buffer = new Float32Array( ( str_len / 12 ) * 3 );
  544. attrib = new THREE.BufferAttribute( buffer, 3 );
  545. geom.addAttribute( 'normal', attrib );
  546. idx = 0;
  547. while (this._ptr < str_end) {
  548. buffer[idx] = -this.readF32();
  549. buffer[idx+1] = this.readF32();
  550. buffer[idx+2] = this.readF32();
  551. idx+=3;
  552. }
  553. }
  554. // else if (str_type == 6) {
  555. // skinI = new Float32Array( str_len>>1 );
  556. // idx = 0
  557. // while (this._ptr < str_end) {
  558. // skinI[idx] = this.readU16();
  559. // idx++;
  560. // }
  561. // }
  562. // else if (str_type == 7) {
  563. // skinW = new Float32Array( str_len>>2 );
  564. // idx = 0;
  565. // while (this._ptr < str_end) {
  566. // skinW[idx] = this.readF32();
  567. // idx++;
  568. // }
  569. // }
  570. else {
  571. this._ptr = str_end;
  572. }
  573. }
  574. this.parseUserAttributes();
  575. geom.computeBoundingSphere();
  576. subs_parsed++;
  577. }
  578. //geom.computeFaceNormals();
  579. this.parseUserAttributes();
  580. //finalizeAsset(geom, name);
  581. return geometries;
  582. },
  583. parseMeshPoseAnimation: function( len, poseOnly ) {
  584. var num_frames = 1,
  585. num_submeshes,
  586. frames_parsed,
  587. subMeshParsed,
  588. frame_dur,
  589. x, y, z,
  590. str_len,
  591. str_end,
  592. geom,
  593. subGeom,
  594. idx = 0,
  595. clip = {},
  596. indices,
  597. verts,
  598. num_Streams,
  599. streamsParsed,
  600. streamtypes = [],
  601. props,
  602. thisGeo,
  603. name = this.readUTF(),
  604. geoAdress = this.readU32();
  605. var mesh = this.getBlock( geoAdress );
  606. if (mesh === null) {
  607. console.log( "parseMeshPoseAnimation target mesh not found at:", geoAdress );
  608. return;
  609. }
  610. geom = mesh.geometry;
  611. geom.morphTargets = [];
  612. if (!poseOnly)
  613. num_frames = this.readU16();
  614. num_submeshes = this.readU16();
  615. num_Streams = this.readU16();
  616. // debug("VA num_frames : ", num_frames );
  617. // debug("VA num_submeshes : ", num_submeshes );
  618. // debug("VA numstreams : ", num_Streams );
  619. streamsParsed = 0;
  620. while ( streamsParsed < num_Streams ) {
  621. streamtypes.push(this.readU16());
  622. streamsParsed++;
  623. }
  624. props = this.parseProperties( { 1:BOOL, 2:BOOL } );
  625. clip.looping = props.get( 1, true );
  626. clip.stitchFinalFrame = props.get( 2, false );
  627. frames_parsed = 0;
  628. while ( frames_parsed < num_frames ) {
  629. frame_dur = this.readU16();
  630. subMeshParsed = 0;
  631. while ( subMeshParsed < num_submeshes ) {
  632. streamsParsed = 0;
  633. str_len = this.readU32();
  634. str_end = this._ptr + str_len;
  635. while ( streamsParsed < num_Streams ) {
  636. if ( streamtypes[streamsParsed] === 1 ) {
  637. //geom.addAttribute( 'morphTarget'+frames_parsed, Float32Array, str_len/12, 3 );
  638. var buffer = new Float32Array(str_len/4);
  639. geom.morphTargets.push( {
  640. array : buffer
  641. });
  642. //buffer = geom.attributes['morphTarget'+frames_parsed].array
  643. idx = 0;
  644. while ( this._ptr < str_end ) {
  645. buffer[idx] = this.readF32();
  646. buffer[idx+1] = this.readF32();
  647. buffer[idx+2] = this.readF32();
  648. idx += 3;
  649. }
  650. subMeshParsed++;
  651. } else
  652. this._ptr = str_end;
  653. streamsParsed++;
  654. }
  655. }
  656. frames_parsed++;
  657. }
  658. this.parseUserAttributes();
  659. return null;
  660. },
  661. getBlock: function ( id ) {
  662. return this._blocks[id].data;
  663. },
  664. parseMatrix4: function () {
  665. var mtx = new THREE.Matrix4();
  666. var e = mtx.elements;
  667. e[0] = this.readF32();
  668. e[1] = this.readF32();
  669. e[2] = this.readF32();
  670. e[3] = 0.0;
  671. //e[3] = 0.0;
  672. e[4] = this.readF32();
  673. e[5] = this.readF32();
  674. e[6] = this.readF32();
  675. //e[7] = this.readF32();
  676. e[7] = 0.0;
  677. e[8] = this.readF32();
  678. e[9] = this.readF32();
  679. e[10] = this.readF32();
  680. //e[11] = this.readF32();
  681. e[11] = 0.0;
  682. e[12] = -this.readF32();
  683. e[13] = this.readF32();
  684. e[14] = this.readF32();
  685. //e[15] = this.readF32();
  686. e[15] = 1.0;
  687. return mtx;
  688. },
  689. parseProperties: function ( expected ) {
  690. var list_len = this.readU32();
  691. var list_end = this._ptr + list_len;
  692. var props = new AWDProperties();
  693. if( expected ) {
  694. while( this._ptr < list_end ) {
  695. var key = this.readU16();
  696. var len = this.readU32();
  697. var type;
  698. if( expected.hasOwnProperty( key ) ) {
  699. type = expected[ key ];
  700. props.set( key, this.parseAttrValue( type, len ) );
  701. } else {
  702. this._ptr += len;
  703. }
  704. }
  705. }
  706. return props;
  707. },
  708. parseUserAttributes: function () {
  709. // skip for now
  710. this._ptr = this.readU32() + this._ptr;
  711. return null;
  712. },
  713. parseAttrValue: function ( type, len ) {
  714. var elem_len;
  715. var read_func;
  716. switch (type) {
  717. case AWD_FIELD_INT8:
  718. elem_len = 1;
  719. read_func = this.readI8;
  720. break;
  721. case AWD_FIELD_INT16:
  722. elem_len = 2;
  723. read_func = this.readI16;
  724. break;
  725. case AWD_FIELD_INT32:
  726. elem_len = 4;
  727. read_func = this.readI32;
  728. break;
  729. case AWD_FIELD_BOOL:
  730. case AWD_FIELD_UINT8:
  731. elem_len = 1;
  732. read_func = this.readU8;
  733. break;
  734. case AWD_FIELD_UINT16:
  735. elem_len = 2;
  736. read_func = this.readU16;
  737. break;
  738. case AWD_FIELD_UINT32:
  739. case AWD_FIELD_BADDR:
  740. elem_len = 4;
  741. read_func = this.readU32;
  742. break;
  743. case AWD_FIELD_FLOAT32:
  744. elem_len = 4;
  745. read_func = this.readF32;
  746. break;
  747. case AWD_FIELD_FLOAT64:
  748. elem_len = 8;
  749. read_func = this.readF64;
  750. break;
  751. case AWD_FIELD_VECTOR2x1:
  752. case AWD_FIELD_VECTOR3x1:
  753. case AWD_FIELD_VECTOR4x1:
  754. case AWD_FIELD_MTX3x2:
  755. case AWD_FIELD_MTX3x3:
  756. case AWD_FIELD_MTX4x3:
  757. case AWD_FIELD_MTX4x4:
  758. elem_len = 8;
  759. read_func = this.readF64;
  760. break;
  761. }
  762. if (elem_len < len) {
  763. var list;
  764. var num_read;
  765. var num_elems;
  766. list = [];
  767. num_read = 0;
  768. num_elems = len / elem_len;
  769. while (num_read < num_elems) {
  770. list.push(read_func.call( this ) );
  771. num_read++;
  772. }
  773. return list;
  774. }
  775. else {
  776. return read_func.call( this );
  777. }
  778. },
  779. readU8: function () {
  780. return this._data.getUint8( this._ptr++ );
  781. },
  782. readI8: function () {
  783. return this._data.getInt8( this._ptr++ );
  784. },
  785. readU16: function () {
  786. var a = this._data.getUint16( this._ptr, littleEndian );
  787. this._ptr += 2;
  788. return a;
  789. },
  790. readI16: function () {
  791. var a = this._data.getInt16( this._ptr, littleEndian );
  792. this._ptr += 2;
  793. return a;
  794. },
  795. readU32: function () {
  796. var a = this._data.getUint32( this._ptr, littleEndian );
  797. this._ptr += 4;
  798. return a;
  799. },
  800. readI32: function () {
  801. var a = this._data.getInt32( this._ptr, littleEndian );
  802. this._ptr += 4;
  803. return a;
  804. },
  805. readF32: function () {
  806. var a = this._data.getFloat32( this._ptr, littleEndian );
  807. this._ptr += 4;
  808. return a;
  809. },
  810. readF64: function () {
  811. var a = this._data.getFloat64( this._ptr, littleEndian );
  812. this._ptr += 8;
  813. return a;
  814. },
  815. /**
  816. * Converts a UTF-8 byte array to JavaScript's 16-bit Unicode.
  817. * @param {Array.<number>} bytes UTF-8 byte array.
  818. * @return {string} 16-bit Unicode string.
  819. */
  820. readUTF: function () {
  821. var len = this.readU16();
  822. return this.readUTFBytes( len );
  823. },
  824. /**
  825. * Converts a UTF-8 byte array to JavaScript's 16-bit Unicode.
  826. * @param {Array.<number>} bytes UTF-8 byte array.
  827. * @return {string} 16-bit Unicode string.
  828. */
  829. readUTFBytes: function ( len ) {
  830. // TODO(user): Use native implementations if/when available
  831. var out = [], c = 0;
  832. while ( out.length < len ) {
  833. var c1 = this._data.getUint8( this._ptr++, littleEndian );
  834. if (c1 < 128) {
  835. out[c++] = String.fromCharCode(c1);
  836. } else if (c1 > 191 && c1 < 224) {
  837. var c2 = this._data.getUint8( this._ptr++, littleEndian );
  838. out[c++] = String.fromCharCode((c1 & 31) << 6 | c2 & 63);
  839. } else {
  840. var c2 = this._data.getUint8( this._ptr++, littleEndian );
  841. var c3 = this._data.getUint8( this._ptr++, littleEndian );
  842. out[c++] = String.fromCharCode(
  843. (c1 & 15) << 12 | (c2 & 63) << 6 | c3 & 63
  844. );
  845. }
  846. }
  847. return out.join('');
  848. }
  849. };
  850. })();