AWDLoader.js 23 KB

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