AWDLoader.js 24 KB

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