AWDLoader.js 24 KB

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