AWDLoader.js 24 KB

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