AWDLoader.js 22 KB

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