PageRenderTime 37ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/away3d/loaders/parsers/AWD2Parser.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 2525 lines | 2175 code | 222 blank | 128 comment | 451 complexity | 91d69d19eb815a7708649ac5d008dfc6 MD5 | raw file
Possible License(s): Apache-2.0
  1. package away3d.loaders.parsers
  2. {
  3. import away3d.*;
  4. import away3d.animators.*;
  5. import away3d.animators.data.*;
  6. import away3d.animators.nodes.*;
  7. import away3d.cameras.*;
  8. import away3d.cameras.lenses.*;
  9. import away3d.containers.*;
  10. import away3d.core.base.*;
  11. import away3d.entities.*;
  12. import away3d.library.assets.*;
  13. import away3d.lights.*;
  14. import away3d.lights.shadowmaps.*;
  15. import away3d.loaders.misc.*;
  16. import away3d.loaders.parsers.utils.*;
  17. import away3d.materials.*;
  18. import away3d.materials.lightpickers.*;
  19. import away3d.materials.methods.*;
  20. import away3d.materials.utils.*;
  21. import away3d.primitives.*;
  22. import away3d.textures.*;
  23. import away3d.tools.utils.*;
  24. import flash.display.*;
  25. import flash.geom.*;
  26. import flash.net.*;
  27. import flash.utils.*;
  28. use namespace arcane;
  29. /**
  30. * AWDParser provides a parser for the AWD data type.
  31. */
  32. public class AWD2Parser extends ParserBase
  33. {
  34. //set to "true" to have some traces in the Console
  35. private var _debug:Boolean = false;
  36. private var _byteData:ByteArray;
  37. private var _cur_block_id:uint;
  38. private var _blocks:Vector.<AWDBlock>;
  39. private var _newBlockBytes:ByteArray;
  40. private var _version:Array;
  41. private var _compression:uint;
  42. private var _accuracyOnBlocks:Boolean;
  43. private var _accuracyMatrix:Boolean;
  44. private var _accuracyGeo:Boolean;
  45. private var _accuracyProps:Boolean;
  46. private var _matrixNrType:uint;
  47. private var _geoNrType:uint;
  48. private var _propsNrType:uint;
  49. private var _streaming:Boolean;
  50. private var _texture_users:Object;
  51. private var _body:ByteArray;
  52. private var _defaultTexture:BitmapTexture;
  53. private var _defaultCubeTexture:BitmapCubeTexture;
  54. private var _defaultBitmapMaterial:TextureMaterial;
  55. private var _cubeTextures:Array;
  56. public static const COMPRESSIONMODE_LZMA:String = "lzma";
  57. public static const UNCOMPRESSED:uint = 0;
  58. public static const DEFLATE:uint = 1;
  59. public static const LZMA:uint = 2;
  60. public static const INT8:uint = 1;
  61. public static const INT16:uint = 2;
  62. public static const INT32:uint = 3;
  63. public static const UINT8:uint = 4;
  64. public static const UINT16:uint = 5;
  65. public static const UINT32:uint = 6;
  66. public static const FLOAT32:uint = 7;
  67. public static const FLOAT64:uint = 8;
  68. public static const BOOL:uint = 21;
  69. public static const COLOR:uint = 22;
  70. public static const BADDR:uint = 23;
  71. public static const AWDSTRING:uint = 31;
  72. public static const AWDBYTEARRAY:uint = 32;
  73. public static const VECTOR2x1:uint = 41;
  74. public static const VECTOR3x1:uint = 42;
  75. public static const VECTOR4x1:uint = 43;
  76. public static const MTX3x2:uint = 44;
  77. public static const MTX3x3:uint = 45;
  78. public static const MTX4x3:uint = 46;
  79. public static const MTX4x4:uint = 47;
  80. private var blendModeDic:Vector.<String>;
  81. private var _depthSizeDic:Vector.<uint>;
  82. /**
  83. * Creates a new AWDParser object.
  84. * @param uri The url or id of the data or file to be parsed.
  85. * @param extra The holder for extra contextual data that the parser might need.
  86. */
  87. public function AWD2Parser()
  88. {
  89. super(ParserDataFormat.BINARY);
  90. blendModeDic = new Vector.<String>(); // used to translate ints to blendMode-strings
  91. blendModeDic.push(BlendMode.NORMAL);
  92. blendModeDic.push(BlendMode.ADD);
  93. blendModeDic.push(BlendMode.ALPHA);
  94. blendModeDic.push(BlendMode.DARKEN);
  95. blendModeDic.push(BlendMode.DIFFERENCE);
  96. blendModeDic.push(BlendMode.ERASE);
  97. blendModeDic.push(BlendMode.HARDLIGHT);
  98. blendModeDic.push(BlendMode.INVERT);
  99. blendModeDic.push(BlendMode.LAYER);
  100. blendModeDic.push(BlendMode.LIGHTEN);
  101. blendModeDic.push(BlendMode.MULTIPLY);
  102. blendModeDic.push(BlendMode.NORMAL);
  103. blendModeDic.push(BlendMode.OVERLAY);
  104. blendModeDic.push(BlendMode.SCREEN);
  105. blendModeDic.push(BlendMode.SHADER);
  106. blendModeDic.push(BlendMode.OVERLAY);
  107. _depthSizeDic = new Vector.<uint>(); // used to translate ints to depthSize-values
  108. _depthSizeDic.push(256);
  109. _depthSizeDic.push(512);
  110. _depthSizeDic.push(2048);
  111. _depthSizeDic.push(1024);
  112. }
  113. /**
  114. * Indicates whether or not a given file extension is supported by the parser.
  115. * @param extension The file extension of a potential file to be parsed.
  116. * @return Whether or not the given file type is supported.
  117. */
  118. public static function supportsType(extension:String):Boolean
  119. {
  120. extension = extension.toLowerCase();
  121. return extension == "awd";
  122. }
  123. /**
  124. * Tests whether a data block can be parsed by the parser.
  125. * @param data The data block to potentially be parsed.
  126. * @return Whether or not the given data is supported.
  127. */
  128. public static function supportsData(data:*):Boolean
  129. {
  130. return (ParserUtil.toString(data, 3) == 'AWD');
  131. }
  132. /**
  133. * @inheritDoc
  134. */
  135. override arcane function resolveDependency(resourceDependency:ResourceDependency):void
  136. {
  137. // this function will be called when Dependency has finished loading.
  138. // the Assets waiting for this Bitmap, can be Texture or CubeTexture.
  139. // if the Bitmap is awaited by a CubeTexture, we need to check if its the last Bitmap of the CubeTexture,
  140. // so we know if we have to finalize the Asset (CubeTexture) or not.
  141. if (resourceDependency.assets.length == 1) {
  142. var isCubeTextureArray:Array = resourceDependency.id.split("#");
  143. var ressourceID:String = isCubeTextureArray[0];
  144. var asset:TextureProxyBase;
  145. var thisBitmapTexture:Texture2DBase;
  146. var block:AWDBlock;
  147. if (isCubeTextureArray.length == 1) {
  148. asset = resourceDependency.assets[0] as Texture2DBase;
  149. if (asset) {
  150. var mat:TextureMaterial;
  151. var users:Array;
  152. block = _blocks[parseInt(resourceDependency.id)];
  153. block.data = asset; // Store finished asset
  154. // Reset name of texture to the one defined in the AWD file,
  155. // as opposed to whatever the image parser came up with.
  156. asset.resetAssetPath(block.name, null, true);
  157. block.name = asset.name;
  158. // Finalize texture asset to dispatch texture event, which was
  159. // previously suppressed while the dependency was loaded.
  160. finalizeAsset(asset);
  161. if (_debug) {
  162. trace("Successfully loadet Bitmap for texture");
  163. trace("Parsed CubeTexture: Name = " + block.name);
  164. }
  165. }
  166. }
  167. if (isCubeTextureArray.length > 1) {
  168. thisBitmapTexture = resourceDependency.assets[0] as BitmapTexture;
  169. _cubeTextures[uint(isCubeTextureArray[1])] = BitmapTexture(thisBitmapTexture).bitmapData;
  170. _texture_users[ressourceID].push(1);
  171. if (_debug)
  172. trace("Successfully loadet Bitmap " + _texture_users[ressourceID].length + " / 6 for Cubetexture");
  173. if (_texture_users[ressourceID].length == _cubeTextures.length) {
  174. asset = new BitmapCubeTexture(_cubeTextures[0], _cubeTextures[1], _cubeTextures[2], _cubeTextures[3], _cubeTextures[4], _cubeTextures[5]);
  175. block = _blocks[ressourceID];
  176. block.data = asset; // Store finished asset
  177. // Reset name of texture to the one defined in the AWD file,
  178. // as opposed to whatever the image parser came up with.
  179. asset.resetAssetPath(block.name, null, true);
  180. block.name = asset.name;
  181. // Finalize texture asset to dispatch texture event, which was
  182. // previously suppressed while the dependency was loaded.
  183. finalizeAsset(asset);
  184. if (_debug)
  185. trace("Parsed CubeTexture: Name = " + block.name);
  186. }
  187. }
  188. }
  189. }
  190. /**
  191. * @inheritDoc
  192. */
  193. override arcane function resolveDependencyFailure(resourceDependency:ResourceDependency):void
  194. {
  195. //not used - if a dependcy fails, the awaiting Texture or CubeTexture will never be finalized, and the default-bitmaps will be used.
  196. // this means, that if one Bitmap of a CubeTexture fails, the CubeTexture will have the DefaultTexture applied for all six Bitmaps.
  197. }
  198. /**
  199. * Resolve a dependency name
  200. *
  201. * @param resourceDependency The dependency to be resolved.
  202. */
  203. arcane override function resolveDependencyName(resourceDependency:ResourceDependency, asset:IAsset):String
  204. {
  205. var oldName:String = asset.name;
  206. if (asset) {
  207. var block:AWDBlock = _blocks[parseInt(resourceDependency.id)];
  208. // Reset name of texture to the one defined in the AWD file,
  209. // as opposed to whatever the image parser came up with.
  210. asset.resetAssetPath(block.name, null, true);
  211. }
  212. var newName:String = asset.name;
  213. asset.name = oldName;
  214. return newName;
  215. }
  216. /**
  217. * @inheritDoc
  218. */
  219. protected override function startParsing(frameLimit:Number):void
  220. {
  221. super.startParsing(frameLimit);
  222. _texture_users = {};
  223. _byteData = getByteData();
  224. _blocks = new Vector.<AWDBlock>();
  225. _blocks[0] = new AWDBlock();
  226. _blocks[0].data = null; // Zero address means null in AWD
  227. _version = []; // will contain 2 int (major-version, minor-version) for awd-version-check
  228. //parse header
  229. _byteData.endian = Endian.LITTLE_ENDIAN;
  230. // Parse header and decompress body if needed
  231. parseHeader();
  232. switch (_compression) {
  233. case DEFLATE:
  234. _body = new ByteArray();
  235. _byteData.readBytes(_body, 0, _byteData.bytesAvailable);
  236. _body.uncompress();
  237. break;
  238. case LZMA:
  239. _body = new ByteArray();
  240. _byteData.readBytes(_body, 0, _byteData.bytesAvailable);
  241. _body.uncompress(COMPRESSIONMODE_LZMA);
  242. break;
  243. case UNCOMPRESSED:
  244. _body = _byteData;
  245. break;
  246. }
  247. _body.endian = Endian.LITTLE_ENDIAN;
  248. }
  249. /**
  250. * @inheritDoc
  251. */
  252. protected override function proceedParsing():Boolean
  253. {
  254. while (_body.bytesAvailable > 0 && !parsingPaused && hasTime())
  255. parseNextBlock();
  256. // Return complete status
  257. if (_body.bytesAvailable == 0)
  258. return PARSING_DONE;
  259. else
  260. return MORE_TO_PARSE;
  261. }
  262. private function parseHeader():void
  263. {
  264. var flags:uint;
  265. var body_len:Number;
  266. _byteData.position = 3; // Skip magic string and parse version
  267. _version[0] = _byteData.readUnsignedByte();
  268. _version[1] = _byteData.readUnsignedByte();
  269. flags = _byteData.readUnsignedShort(); // Parse bit flags
  270. _streaming = bitFlags.test(flags, bitFlags.FLAG1);
  271. if ((_version[0] == 2) && (_version[1] == 1)) {
  272. _accuracyMatrix = bitFlags.test(flags, bitFlags.FLAG2);
  273. _accuracyGeo = bitFlags.test(flags, bitFlags.FLAG3);
  274. _accuracyProps = bitFlags.test(flags, bitFlags.FLAG4);
  275. }
  276. // if we set _accuracyOnBlocks, the precision-values are read from each block-header.
  277. // set storagePrecision types
  278. _geoNrType = FLOAT32;
  279. if (_accuracyGeo)
  280. _geoNrType = FLOAT64;
  281. _matrixNrType = FLOAT32;
  282. if (_accuracyMatrix)
  283. _matrixNrType = FLOAT64;
  284. _propsNrType = FLOAT32;
  285. if (_accuracyProps)
  286. _propsNrType = FLOAT64;
  287. _compression = _byteData.readUnsignedByte(); // compression
  288. if (_debug) {
  289. trace("Import AWDFile of version = " + _version[0] + " - " + _version[1]);
  290. trace("Global Settings = Compression = " + _compression + " | Streaming = " + _streaming + " | Matrix-Precision = " + _accuracyMatrix + " | Geometry-Precision = " + _accuracyGeo + " | Properties-Precision = " + _accuracyProps);
  291. }
  292. // Check file integrity
  293. body_len = _byteData.readUnsignedInt();
  294. if (!_streaming && body_len != _byteData.bytesAvailable)
  295. dieWithError('AWD2 body length does not match header integrity field');
  296. }
  297. private function parseNextBlock():void
  298. {
  299. var block:AWDBlock;
  300. var assetData:IAsset;
  301. var isParsed:Boolean;
  302. var ns:uint, type:uint, flags:uint, len:uint;
  303. _cur_block_id = _body.readUnsignedInt();
  304. ns = _body.readUnsignedByte();
  305. type = _body.readUnsignedByte();
  306. flags = _body.readUnsignedByte();
  307. len = _body.readUnsignedInt();
  308. var blockCompression:Boolean = bitFlags.test(flags, bitFlags.FLAG4);
  309. var blockCompressionLZMA:Boolean = bitFlags.test(flags, bitFlags.FLAG5);
  310. if (_accuracyOnBlocks) {
  311. _accuracyMatrix = bitFlags.test(flags, bitFlags.FLAG1);
  312. _accuracyGeo = bitFlags.test(flags, bitFlags.FLAG2);
  313. _accuracyProps = bitFlags.test(flags, bitFlags.FLAG3);
  314. _geoNrType = FLOAT32;
  315. if (_accuracyGeo)
  316. _geoNrType = FLOAT64;
  317. _matrixNrType = FLOAT32;
  318. if (_accuracyMatrix)
  319. _matrixNrType = FLOAT64;
  320. _propsNrType = FLOAT32;
  321. if (_accuracyProps)
  322. _propsNrType = FLOAT64;
  323. }
  324. var blockEndAll:uint = _body.position + len;
  325. if (len > _body.bytesAvailable) {
  326. dieWithError('AWD2 block length is bigger than the bytes that are available!');
  327. _body.position += _body.bytesAvailable;
  328. return;
  329. }
  330. _newBlockBytes = new ByteArray();
  331. _body.readBytes(_newBlockBytes, 0, len);
  332. if (blockCompression) {
  333. if (blockCompressionLZMA)
  334. _newBlockBytes.uncompress(COMPRESSIONMODE_LZMA);
  335. else
  336. _newBlockBytes.uncompress();
  337. }
  338. _newBlockBytes.endian = Endian.LITTLE_ENDIAN;
  339. _newBlockBytes.position = 0;
  340. block = new AWDBlock();
  341. block.len = _newBlockBytes.position + len;
  342. block.id = _cur_block_id;
  343. var blockEndBlock:uint = _newBlockBytes.position + len;
  344. if (blockCompression) {
  345. blockEndBlock = _newBlockBytes.position + _newBlockBytes.length;
  346. block.len = blockEndBlock;
  347. }
  348. if (_debug)
  349. trace("AWDBlock: ID = " + _cur_block_id + " | TypeID = " + type + " | Compression = " + blockCompression + " | Matrix-Precision = " + _accuracyMatrix + " | Geometry-Precision = " + _accuracyGeo + " | Properties-Precision = " + _accuracyProps);
  350. _blocks[_cur_block_id] = block;
  351. if ((_version[0] == 2) && (_version[1] == 1)) {
  352. switch (type) {
  353. case 11:
  354. parsePrimitves(_cur_block_id);
  355. isParsed = true;
  356. break;
  357. case 31:
  358. parseSkyBoxInstance(_cur_block_id);
  359. isParsed = true;
  360. break;
  361. case 41:
  362. parseLight(_cur_block_id);
  363. isParsed = true;
  364. break;
  365. case 42:
  366. parseCamera(_cur_block_id);
  367. isParsed = true;
  368. break;
  369. case 43:
  370. parseTextureProjector(_cur_block_id);
  371. isParsed = true;
  372. break;
  373. case 51:
  374. parseLightPicker(_cur_block_id);
  375. isParsed = true;
  376. break;
  377. case 81:
  378. parseMaterial_v1(_cur_block_id);
  379. isParsed = true;
  380. break;
  381. case 83:
  382. parseCubeTexture(_cur_block_id);
  383. isParsed = true;
  384. break;
  385. case 91:
  386. parseSharedMethodBlock(_cur_block_id);
  387. isParsed = true;
  388. break;
  389. case 92:
  390. parseShadowMethodBlock(_cur_block_id);
  391. isParsed = true;
  392. break;
  393. case 111:
  394. parseMeshPoseAnimation(_cur_block_id, true);
  395. isParsed = true;
  396. break;
  397. case 112:
  398. parseMeshPoseAnimation(_cur_block_id);
  399. isParsed = true;
  400. break;
  401. case 113:
  402. parseVertexAnimationSet(_cur_block_id);
  403. isParsed = true;
  404. break;
  405. case 122:
  406. parseAnimatorSet(_cur_block_id);
  407. isParsed = true;
  408. break;
  409. case 253:
  410. parseCommand(_cur_block_id);
  411. isParsed = true;
  412. break;
  413. }
  414. }
  415. if (isParsed == false) {
  416. switch (type) {
  417. case 1:
  418. parseTriangleGeometrieBlock(_cur_block_id);
  419. break;
  420. case 22:
  421. parseContainer(_cur_block_id);
  422. break;
  423. case 23:
  424. parseMeshInstance(_cur_block_id);
  425. break;
  426. case 81:
  427. parseMaterial(_cur_block_id);
  428. break;
  429. case 82:
  430. parseTexture(_cur_block_id);
  431. break;
  432. case 101:
  433. parseSkeleton(_cur_block_id);
  434. break;
  435. case 102:
  436. parseSkeletonPose(_cur_block_id);
  437. break;
  438. case 103:
  439. parseSkeletonAnimation(_cur_block_id);
  440. break;
  441. case 121:
  442. parseUVAnimation(_cur_block_id);
  443. break;
  444. case 254:
  445. parseNameSpace(_cur_block_id);
  446. break;
  447. case 255:
  448. parseMetaData(_cur_block_id);
  449. break;
  450. default:
  451. if (_debug)
  452. trace("AWDBlock: Unknown BlockType (BlockID = " + _cur_block_id + ") - Skip " + len + " bytes");
  453. _newBlockBytes.position += len;
  454. break;
  455. }
  456. }
  457. var msgCnt:uint = 0;
  458. if (_newBlockBytes.position == blockEndBlock) {
  459. if (_debug) {
  460. if (block.errorMessages) {
  461. while (msgCnt < block.errorMessages.length) {
  462. trace(" (!) Error: " + block.errorMessages[msgCnt] + " (!)");
  463. msgCnt++;
  464. }
  465. }
  466. }
  467. if (_debug)
  468. trace("\n");
  469. } else {
  470. if (_debug) {
  471. trace(" (!)(!)(!) Error while reading AWDBlock ID " + _cur_block_id + " = skip to next block");
  472. if (block.errorMessages) {
  473. while (msgCnt < block.errorMessages.length) {
  474. trace(" (!) Error: " + block.errorMessages[msgCnt] + " (!)");
  475. msgCnt++;
  476. }
  477. }
  478. }
  479. }
  480. _body.position = blockEndAll;
  481. _newBlockBytes = null;
  482. }
  483. //Block ID = 1
  484. private function parseTriangleGeometrieBlock(blockID:uint):void
  485. {
  486. var geom:Geometry = new Geometry();
  487. // Read name and sub count
  488. var name:String = parseVarStr();
  489. var num_subs:uint = _newBlockBytes.readUnsignedShort();
  490. // Read optional properties
  491. var props:AWDProperties = parseProperties({1:_geoNrType, 2:_geoNrType});
  492. var geoScaleU:Number = props.get(1, 1);
  493. var geoScaleV:Number = props.get(2, 1);
  494. var sub_geoms:Vector.<ISubGeometry> = new Vector.<ISubGeometry>;
  495. // Loop through sub meshes
  496. var subs_parsed:uint = 0;
  497. while (subs_parsed < num_subs) {
  498. var i:uint;
  499. var sm_len:uint, sm_end:uint;
  500. var w_indices:Vector.<Number>;
  501. var weights:Vector.<Number>;
  502. sm_len = _newBlockBytes.readUnsignedInt();
  503. sm_end = _newBlockBytes.position + sm_len;
  504. // Ignore for now
  505. var subProps:AWDProperties = parseProperties({1:_geoNrType, 2:_geoNrType});
  506. // Loop through data streams
  507. while (_newBlockBytes.position < sm_end) {
  508. var idx:uint = 0;
  509. var str_ftype:uint, str_type:uint, str_len:uint, str_end:uint;
  510. // Type, field type, length
  511. str_type = _newBlockBytes.readUnsignedByte();
  512. str_ftype = _newBlockBytes.readUnsignedByte();
  513. str_len = _newBlockBytes.readUnsignedInt();
  514. str_end = _newBlockBytes.position + str_len;
  515. var x:Number, y:Number, z:Number;
  516. if (str_type == 1) {
  517. var verts:Vector.<Number> = new Vector.<Number>();
  518. while (_newBlockBytes.position < str_end) {
  519. // TODO: Respect stream field type
  520. x = readNumber(_accuracyGeo);
  521. y = readNumber(_accuracyGeo);
  522. z = readNumber(_accuracyGeo);
  523. verts[idx++] = x;
  524. verts[idx++] = y;
  525. verts[idx++] = z;
  526. }
  527. } else if (str_type == 2) {
  528. var indices:Vector.<uint> = new Vector.<uint>();
  529. while (_newBlockBytes.position < str_end) {
  530. // TODO: Respect stream field type
  531. indices[idx++] = _newBlockBytes.readUnsignedShort();
  532. }
  533. } else if (str_type == 3) {
  534. var uvs:Vector.<Number> = new Vector.<Number>();
  535. while (_newBlockBytes.position < str_end)
  536. uvs[idx++] = readNumber(_accuracyGeo);
  537. } else if (str_type == 4) {
  538. var normals:Vector.<Number> = new Vector.<Number>();
  539. while (_newBlockBytes.position < str_end)
  540. normals[idx++] = readNumber(_accuracyGeo);
  541. } else if (str_type == 6) {
  542. w_indices = new Vector.<Number>();
  543. while (_newBlockBytes.position < str_end)
  544. w_indices[idx++] = _newBlockBytes.readUnsignedShort()*3; // TODO: Respect stream field type
  545. } else if (str_type == 7) {
  546. weights = new Vector.<Number>();
  547. while (_newBlockBytes.position < str_end)
  548. weights[idx++] = readNumber(_accuracyGeo);
  549. } else
  550. _newBlockBytes.position = str_end;
  551. }
  552. parseUserAttributes(); // Ignore sub-mesh attributes for now
  553. sub_geoms = GeomUtil.fromVectors(verts, indices, uvs, normals, null, weights, w_indices);
  554. var scaleU:Number = subProps.get(1, 1);
  555. var scaleV:Number = subProps.get(2, 1);
  556. var setSubUVs:Boolean = false; //this should remain false atm, because in AwayBuilder the uv is only scaled by the geometry
  557. if ((geoScaleU != scaleU) || (geoScaleV != scaleV)) {
  558. trace("set sub uvs");
  559. setSubUVs = true;
  560. scaleU = geoScaleU/scaleU;
  561. scaleV = geoScaleV/scaleV;
  562. }
  563. for (i = 0; i < sub_geoms.length; i++) {
  564. if (setSubUVs)
  565. sub_geoms[i].scaleUV(scaleU, scaleV);
  566. geom.addSubGeometry(sub_geoms[i]);
  567. // TODO: Somehow map in-sub to out-sub indices to enable look-up
  568. // when creating meshes (and their material assignments.)
  569. }
  570. subs_parsed++;
  571. }
  572. if ((geoScaleU != 1) || (geoScaleV != 1))
  573. geom.scaleUV(geoScaleU, geoScaleV);
  574. parseUserAttributes();
  575. finalizeAsset(geom, name);
  576. _blocks[blockID].data = geom;
  577. if (_debug)
  578. trace("Parsed a TriangleGeometry: Name = " + name + "| SubGeometries = " + sub_geoms.length);
  579. }
  580. //Block ID = 11
  581. private function parsePrimitves(blockID:uint):void
  582. {
  583. var name:String;
  584. var geom:Geometry;
  585. var primType:uint;
  586. var subs_parsed:uint;
  587. var props:AWDProperties;
  588. var bsm:Matrix3D;
  589. // Read name and sub count
  590. name = parseVarStr();
  591. primType = _newBlockBytes.readUnsignedByte();
  592. props = parseProperties({101:_geoNrType, 102:_geoNrType, 103:_geoNrType, 110:_geoNrType, 111:_geoNrType, 301:UINT16, 302:UINT16, 303:UINT16, 701:BOOL, 702:BOOL, 703:BOOL, 704:BOOL});
  593. var primitveTypes:Array = ["Unsupported Type-ID", "PlaneGeometry", "CubeGeometry", "SphereGeometry", "CylinderGeometry", "ConeGeometry", "CapsuleGeometry", "TorusGeometry"]
  594. switch (primType) {
  595. // to do, not all properties are set on all primitives
  596. case 1:
  597. geom = new PlaneGeometry(props.get(101, 100), props.get(102, 100), props.get(301, 1), props.get(302, 1), props.get(701, true), props.get(702, false));
  598. break;
  599. case 2:
  600. geom = new CubeGeometry(props.get(101, 100), props.get(102, 100), props.get(103, 100), props.get(301, 1), props.get(302, 1), props.get(303, 1), props.get(701, true));
  601. break;
  602. case 3:
  603. geom = new SphereGeometry(props.get(101, 50), props.get(301, 16), props.get(302, 12), props.get(701, true));
  604. break;
  605. case 4:
  606. geom = new CylinderGeometry(props.get(101, 50), props.get(102, 50), props.get(103, 100), props.get(301, 16), props.get(302, 1), true, true, true); // bool701, bool702, bool703, bool704);
  607. if (!props.get(701, true))
  608. CylinderGeometry(geom).topClosed = false;
  609. if (!props.get(702, true))
  610. CylinderGeometry(geom).bottomClosed = false;
  611. if (!props.get(703, true))
  612. CylinderGeometry(geom).yUp = false;
  613. break;
  614. case 5:
  615. geom = new ConeGeometry(props.get(101, 50), props.get(102, 100), props.get(301, 16), props.get(302, 1), props.get(701, true), props.get(702, true));
  616. break;
  617. case 6:
  618. geom = new CapsuleGeometry(props.get(101, 50), props.get(102, 100), props.get(301, 16), props.get(302, 15), props.get(701, true));
  619. break;
  620. case 7:
  621. geom = new TorusGeometry(props.get(101, 50), props.get(102, 50), props.get(301, 16), props.get(302, 8), props.get(701, true));
  622. break;
  623. default:
  624. geom = new Geometry();
  625. trace("ERROR: UNSUPPORTED PRIMITIVE_TYPE");
  626. break;
  627. }
  628. if ((props.get(110, 1) != 1) || (props.get(111, 1) != 1)) {
  629. geom.subGeometries;
  630. geom.scaleUV(props.get(110, 1), props.get(111, 1));
  631. }
  632. parseUserAttributes();
  633. geom.name = name;
  634. finalizeAsset(geom, name);
  635. _blocks[blockID].data = geom;
  636. if (_debug) {
  637. if ((primType < 0) || (primType > 7))
  638. primType = 0;
  639. trace("Parsed a Primivite: Name = " + name + "| type = " + primitveTypes[primType]);
  640. }
  641. }
  642. // Block ID = 22
  643. private function parseContainer(blockID:uint):void
  644. {
  645. var name:String;
  646. var par_id:uint;
  647. var mtx:Matrix3D;
  648. var ctr:ObjectContainer3D;
  649. var parent:ObjectContainer3D;
  650. par_id = _newBlockBytes.readUnsignedInt();
  651. mtx = parseMatrix3D();
  652. name = parseVarStr();
  653. var parentName:String = "Root (TopLevel)";
  654. ctr = new ObjectContainer3D();
  655. ctr.transform = mtx;
  656. var returnedArray:Array = getAssetByID(par_id, [AssetType.CONTAINER, AssetType.LIGHT, AssetType.MESH, AssetType.ENTITY, AssetType.SEGMENT_SET])
  657. if (returnedArray[0]) {
  658. ObjectContainer3D(returnedArray[1]).addChild(ctr);
  659. parentName = ObjectContainer3D(returnedArray[1]).name;
  660. } else if (par_id > 0)
  661. _blocks[blockID].addError("Could not find a parent for this ObjectContainer3D");
  662. // in AWD version 2.1 we read the Container properties
  663. if ((_version[0] == 2) && (_version[1] == 1)) {
  664. var props:Object = parseProperties({1:_matrixNrType, 2:_matrixNrType, 3:_matrixNrType, 4:UINT8});
  665. ctr.pivotPoint = new Vector3D(props.get(1, 0), props.get(2, 0), props.get(3, 0));
  666. }
  667. // in other versions we do not read the Container properties
  668. else
  669. parseProperties(null);
  670. // the extraProperties should only be set for AWD2.1-Files, but is read for both versions
  671. ctr.extra = parseUserAttributes();
  672. finalizeAsset(ctr, name);
  673. _blocks[blockID].data = ctr;
  674. if (_debug)
  675. trace("Parsed a Container: Name = '" + name + "' | Parent-Name = " + parentName);
  676. }
  677. // Block ID = 23
  678. private function parseMeshInstance(blockID:uint):void
  679. {
  680. var num_materials:uint;
  681. var materials_parsed:uint;
  682. var parent:ObjectContainer3D;
  683. var par_id:uint = _newBlockBytes.readUnsignedInt();
  684. var mtx:Matrix3D = parseMatrix3D();
  685. var name:String = parseVarStr();
  686. var parentName:String = "Root (TopLevel)";
  687. var data_id:uint = _newBlockBytes.readUnsignedInt();
  688. var geom:Geometry;
  689. var returnedArrayGeometry:Array = getAssetByID(data_id, [AssetType.GEOMETRY])
  690. if (returnedArrayGeometry[0])
  691. geom = returnedArrayGeometry[1] as Geometry;
  692. else {
  693. _blocks[blockID].addError("Could not find a Geometry for this Mesh. A empty Geometry is created!");
  694. geom = new Geometry();
  695. }
  696. _blocks[blockID].geoID = data_id;
  697. var materials:Vector.<MaterialBase> = new Vector.<MaterialBase>();
  698. num_materials = _newBlockBytes.readUnsignedShort();
  699. var materialNames:Array = new Array();
  700. materials_parsed = 0;
  701. var returnedArrayMaterial:Array;
  702. while (materials_parsed < num_materials) {
  703. var mat_id:uint;
  704. mat_id = _newBlockBytes.readUnsignedInt();
  705. returnedArrayMaterial = getAssetByID(mat_id, [AssetType.MATERIAL])
  706. if ((!returnedArrayMaterial[0]) && (mat_id > 0))
  707. _blocks[blockID].addError("Could not find Material Nr " + materials_parsed + " (ID = " + mat_id + " ) for this Mesh");
  708. materials.push(returnedArrayMaterial[1] as MaterialBase);
  709. materialNames.push(MaterialBase(returnedArrayMaterial[1]).name);
  710. materials_parsed++;
  711. }
  712. var mesh:Mesh = new Mesh(geom, null);
  713. mesh.transform = mtx;
  714. var returnedArrayParent:Array = getAssetByID(par_id, [AssetType.CONTAINER, AssetType.LIGHT, AssetType.MESH, AssetType.ENTITY, AssetType.SEGMENT_SET])
  715. if (returnedArrayParent[0]) {
  716. ObjectContainer3D(returnedArrayParent[1]).addChild(mesh);
  717. parentName = ObjectContainer3D(returnedArrayParent[1]).name;
  718. } else if (par_id > 0)
  719. _blocks[blockID].addError("Could not find a parent for this Mesh");
  720. if (materials.length >= 1 && mesh.subMeshes.length == 1)
  721. mesh.material = materials[0];
  722. else if (materials.length > 1) {
  723. var i:uint;
  724. // Assign each sub-mesh in the mesh a material from the list. If more sub-meshes
  725. // than materials, repeat the last material for all remaining sub-meshes.
  726. for (i = 0; i < mesh.subMeshes.length; i++)
  727. mesh.subMeshes[i].material = materials[Math.min(materials.length - 1, i)];
  728. }
  729. if ((_version[0] == 2) && (_version[1] == 1)) {
  730. var props:Object = parseProperties({1:_matrixNrType, 2:_matrixNrType, 3:_matrixNrType, 4:UINT8, 5:BOOL});
  731. mesh.pivotPoint = new Vector3D(props.get(1, 0), props.get(2, 0), props.get(3, 0));
  732. mesh.castsShadows = props.get(5, true);
  733. } else
  734. parseProperties(null);
  735. mesh.extra = parseUserAttributes();
  736. finalizeAsset(mesh, name);
  737. _blocks[blockID].data = mesh;
  738. if (_debug)
  739. trace("Parsed a Mesh: Name = '" + name + "' | Parent-Name = " + parentName + "| Geometry-Name = " + geom.name + " | SubMeshes = " + mesh.subMeshes.length + " | Mat-Names = " + materialNames.toString());
  740. }
  741. //Block ID 31
  742. private function parseSkyBoxInstance(blockID:uint):void
  743. {
  744. var name:String = parseVarStr();
  745. var cubeTexAddr:uint = _newBlockBytes.readUnsignedInt();
  746. var returnedArrayCubeTex:Array = getAssetByID(cubeTexAddr, [AssetType.TEXTURE], "CubeTexture");
  747. if ((!returnedArrayCubeTex[0]) && (cubeTexAddr != 0))
  748. _blocks[blockID].addError("Could not find the Cubetexture (ID = " + cubeTexAddr + " ) for this SkyBox");
  749. var asset:SkyBox = new SkyBox(returnedArrayCubeTex[1] as BitmapCubeTexture);
  750. parseProperties(null)
  751. asset.extra = parseUserAttributes();
  752. finalizeAsset(asset, name);
  753. _blocks[blockID].data = asset;
  754. if (_debug)
  755. trace("Parsed a SkyBox: Name = '" + name + "' | CubeTexture-Name = " + BitmapCubeTexture(returnedArrayCubeTex[1]).name);
  756. }
  757. //Block ID = 41
  758. private function parseLight(blockID:uint):void
  759. {
  760. var light:LightBase;
  761. var newShadowMapper:ShadowMapperBase;
  762. var par_id:uint = _newBlockBytes.readUnsignedInt();
  763. var mtx:Matrix3D = parseMatrix3D();
  764. var name:String = parseVarStr();
  765. var lightType:uint = _newBlockBytes.readUnsignedByte();
  766. var props:AWDProperties = parseProperties({1:_propsNrType, 2:_propsNrType, 3:COLOR, 4:_propsNrType, 5:_propsNrType, 6:BOOL, 7:COLOR, 8:_propsNrType, 9:UINT8, 10:UINT8, 11:_propsNrType, 12:UINT16, 21:_matrixNrType, 22:_matrixNrType, 23:_matrixNrType});
  767. var shadowMapperType:uint = props.get(9, 0);
  768. var parentName:String = "Root (TopLevel)";
  769. var lightTypes:Array = ["Unsupported LightType", "PointLight", "DirectionalLight"];
  770. var shadowMapperTypes:Array = ["No ShadowMapper", "DirectionalShadowMapper", "NearDirectionalShadowMapper", "CascadeShadowMapper", "CubeMapShadowMapper"];
  771. if (lightType == 1) {
  772. light = new PointLight();
  773. PointLight(light).radius = props.get(1, 90000);
  774. PointLight(light).fallOff = props.get(2, 100000);
  775. if (shadowMapperType > 0) {
  776. if (shadowMapperType == 4)
  777. newShadowMapper = new CubeMapShadowMapper();
  778. }
  779. light.transform = mtx;
  780. }
  781. if (lightType == 2) {
  782. light = new DirectionalLight(props.get(21, 0), props.get(22, -1), props.get(23, 1));
  783. if (shadowMapperType > 0) {
  784. if (shadowMapperType == 1)
  785. newShadowMapper = new DirectionalShadowMapper();
  786. if (shadowMapperType == 2)
  787. newShadowMapper = new NearDirectionalShadowMapper(props.get(11, 0.5));
  788. if (shadowMapperType == 3)
  789. newShadowMapper = new CascadeShadowMapper(props.get(12, 3));
  790. }
  791. }
  792. if ((lightType != 2) && (lightType != 1)){
  793. _blocks[blockID].addError("Unsuported lighttype = "+lightType);
  794. return
  795. }
  796. light.color = props.get(3, 0xffffff);
  797. light.specular = props.get(4, 1.0);
  798. light.diffuse = props.get(5, 1.0);
  799. light.ambientColor = props.get(7, 0xffffff);
  800. light.ambient = props.get(8, 0.0);
  801. // if a shadowMapper has been created, adjust the depthMapSize if needed, assign to light and set castShadows to true
  802. if (newShadowMapper) {
  803. if (newShadowMapper is CubeMapShadowMapper) {
  804. if (props.get(10, 1) != 1)
  805. newShadowMapper.depthMapSize = _depthSizeDic[props.get(10, 1)];
  806. } else {
  807. if (props.get(10, 2) != 2)
  808. newShadowMapper.depthMapSize = _depthSizeDic[props.get(10, 2)];
  809. }
  810. light.shadowMapper = newShadowMapper;
  811. light.castsShadows = true;
  812. }
  813. if (par_id != 0) {
  814. var returnedArrayParent:Array = getAssetByID(par_id, [AssetType.CONTAINER, AssetType.LIGHT, AssetType.MESH, AssetType.ENTITY, AssetType.SEGMENT_SET])
  815. if (returnedArrayParent[0]) {
  816. ObjectContainer3D(returnedArrayParent[1]).addChild(light);
  817. parentName = ObjectContainer3D(returnedArrayParent[1]).name;
  818. } else
  819. _blocks[blockID].addError("Could not find a parent for this Light");
  820. }
  821. parseUserAttributes();
  822. finalizeAsset(light, name);
  823. _blocks[blockID].data = light;
  824. if (_debug)
  825. trace("Parsed a Light: Name = '" + name + "' | Type = " + lightTypes[lightType] + " | Parent-Name = " + parentName + " | ShadowMapper-Type = " + shadowMapperTypes[shadowMapperType]);
  826. }
  827. //Block ID = 43
  828. private function parseCamera(blockID:uint):void
  829. {
  830. var par_id:uint = _newBlockBytes.readUnsignedInt();
  831. var mtx:Matrix3D = parseMatrix3D();
  832. var name:String = parseVarStr();
  833. var parentName:String = "Root (TopLevel)";
  834. var lens:LensBase;
  835. _newBlockBytes.readUnsignedByte(); //set as active camera
  836. _newBlockBytes.readShort(); //lengthof lenses - not used yet
  837. var lenstype:uint = _newBlockBytes.readShort();
  838. var props:AWDProperties = parseProperties({101:_propsNrType, 102:_propsNrType, 103:_propsNrType, 104:_propsNrType});
  839. switch (lenstype) {
  840. case 5001:
  841. lens = new PerspectiveLens(props.get(101, 60));
  842. break;
  843. case 5002:
  844. lens = new OrthographicLens(props.get(101, 500));
  845. break;
  846. case 5003:
  847. lens = new OrthographicOffCenterLens(props.get(101, -400), props.get(102, 400), props.get(103, -300), props.get(104, 300));
  848. break;
  849. default:
  850. trace("unsupportedLenstype");
  851. return;
  852. }
  853. var camera:Camera3D = new Camera3D(lens);
  854. camera.transform = mtx;
  855. var returnedArrayParent:Array = getAssetByID(par_id, [AssetType.CONTAINER, AssetType.LIGHT, AssetType.MESH, AssetType.ENTITY, AssetType.SEGMENT_SET])
  856. if (returnedArrayParent[0]) {
  857. ObjectContainer3D(returnedArrayParent[1]).addChild(camera);
  858. parentName = ObjectContainer3D(returnedArrayParent[1]).name;
  859. } else if (par_id > 0)
  860. _blocks[blockID].addError("Could not find a parent for this Camera");
  861. camera.name = name;
  862. props = parseProperties({1:_matrixNrType, 2:_matrixNrType, 3:_matrixNrType, 4:UINT8, 101:_propsNrType, 102:_propsNrType});
  863. camera.pivotPoint = new Vector3D(props.get(1, 0), props.get(2, 0), props.get(3, 0));
  864. camera.lens.near = props.get(101, 20);
  865. camera.lens.far = props.get(102, 3000);
  866. camera.extra = parseUserAttributes();
  867. finalizeAsset(camera, name);
  868. _blocks[blockID].data = camera
  869. if (_debug)
  870. trace("Parsed a Camera: Name = '" + name + "' | Lenstype = " + lens + " | Parent-Name = " + parentName);
  871. }
  872. //Block ID = 43
  873. private function parseTextureProjector(blockID:uint):void
  874. {
  875. var par_id:uint = _newBlockBytes.readUnsignedInt();
  876. var mtx:Matrix3D = parseMatrix3D();
  877. var name:String = parseVarStr();
  878. var parentName:String = "Root (TopLevel)";
  879. var tex_id:uint = _newBlockBytes.readUnsignedInt();
  880. var returnedArrayGeometry:Array = getAssetByID(tex_id, [AssetType.TEXTURE])
  881. if ((!returnedArrayGeometry[0]) && (tex_id != 0))
  882. _blocks[blockID].addError("Could not find the Texture (ID = " + tex_id + " ( for this TextureProjector!");
  883. var textureProjector:TextureProjector = new TextureProjector(returnedArrayGeometry[1]);
  884. textureProjector.name = name;
  885. textureProjector.aspectRatio = _newBlockBytes.readFloat();
  886. textureProjector.fieldOfView = _newBlockBytes.readFloat();
  887. textureProjector.transform = mtx;
  888. var props:AWDProperties = parseProperties({1:_matrixNrType, 2:_matrixNrType, 3:_matrixNrType, 4:UINT8});
  889. textureProjector.pivotPoint = new Vector3D(props.get(1, 0), props.get(2, 0), props.get(3, 0));
  890. textureProjector.extra = parseUserAttributes();
  891. finalizeAsset(textureProjector, name);
  892. _blocks[blockID].data = textureProjector
  893. if (_debug)
  894. trace("Parsed a TextureProjector: Name = '" + name + "' | Texture-Name = " + Texture2DBase(returnedArrayGeometry[1]).name + " | Parent-Name = " + parentName);
  895. }
  896. //Block ID = 51
  897. private function parseLightPicker(blockID:uint):void
  898. {
  899. var name:String = parseVarStr();
  900. var numLights:uint = _newBlockBytes.readUnsignedShort();
  901. var lightsArray:Array = new Array();
  902. var k:int = 0;
  903. var lightID:int = 0;
  904. var returnedArrayLight:Array;
  905. var lightsArrayNames:Array = new Array();
  906. for (k = 0; k < numLights; k++) {
  907. lightID = _newBlockBytes.readUnsignedInt();
  908. returnedArrayLight = getAssetByID(lightID, [AssetType.LIGHT])
  909. if (returnedArrayLight[0]) {
  910. lightsArray.push(returnedArrayLight[1] as LightBase);
  911. lightsArrayNames.push(LightBase(returnedArrayLight[1]).name);
  912. } else
  913. _blocks[blockID].addError("Could not find a Light Nr " + k + " (ID = " + lightID + " ) for this LightPicker");
  914. }
  915. if (lightsArray.length == 0) {
  916. _blocks[blockID].addError("Could not create this LightPicker, cause no Light was found.");
  917. parseUserAttributes();
  918. return; //return without any more parsing for this block
  919. }
  920. var lightPick:LightPickerBase = new StaticLightPicker(lightsArray);
  921. lightPick.name = name;
  922. parseUserAttributes();
  923. finalizeAsset(lightPick, name);
  924. _blocks[blockID].data = lightPick
  925. if (_debug)
  926. trace("Parsed a StaticLightPicker: Name = '" + name + "' | Texture-Name = " + lightsArrayNames.toString());
  927. }
  928. //Block ID = 81
  929. private function parseMaterial(blockID:uint):void
  930. {
  931. // TODO: not used
  932. ////blockLength = block.len;
  933. var name:String;
  934. var type:uint;
  935. var props:AWDProperties;
  936. var mat:MaterialBase;
  937. var attributes:Object;
  938. var finalize:Boolean;
  939. var num_methods:uint;
  940. var methods_parsed:uint;
  941. var returnedArray:Array;
  942. name = parseVarStr();
  943. type = _newBlockBytes.readUnsignedByte();
  944. num_methods = _newBlockBytes.readUnsignedByte();
  945. // Read material numerical properties
  946. // (1=color, 2=bitmap url, 10=alpha, 11=alpha_blending, 12=alpha_threshold, 13=repeat)
  947. props = parseProperties({1:INT32, 2:BADDR, 10:_propsNrType, 11:BOOL, 12:_propsNrType, 13:BOOL});
  948. methods_parsed = 0;
  949. while (methods_parsed < num_methods) {
  950. var method_type:uint;
  951. method_type = _newBlockBytes.readUnsignedShort();
  952. parseProperties(null);
  953. parseUserAttributes();
  954. methods_parsed += 1;
  955. }
  956. var debugString:String = "";
  957. attributes = parseUserAttributes();
  958. if (type == 1) { // Color material
  959. debugString += "Parsed a ColorMaterial(SinglePass): Name = '" + name + "' | ";
  960. var color:uint;
  961. color = props.get(1, 0xcccccc);
  962. if (materialMode < 2)
  963. mat = new ColorMaterial(color, props.get(10, 1.0));
  964. else
  965. mat = new ColorMultiPassMaterial(color);
  966. } else if (type == 2) {
  967. var tex_addr:uint = props.get(2, 0);
  968. returnedArray = getAssetByID(tex_addr, [AssetType.TEXTURE])
  969. if ((!returnedArray[0]) && (tex_addr > 0))
  970. _blocks[blockID].addError("Could not find the DiffsueTexture (ID = " + tex_addr + " ) for this Material");
  971. if (materialMode < 2) {
  972. mat = new TextureMaterial(returnedArray[1]);
  973. TextureMaterial(mat).alphaBlending = props.get(11, false);
  974. TextureMaterial(mat).alpha = props.get(10, 1.0);
  975. debugString += "Parsed a TextureMaterial(SinglePass): Name = '" + name + "' | Texture-Name = " + mat.name;
  976. } else {
  977. mat = new TextureMultiPassMaterial(returnedArray[1]);
  978. debugString += "Parsed a TextureMaterial(MultipAss): Name = '" + name + "' | Texture-Name = " + mat.name;
  979. }
  980. }
  981. mat.extra = attributes;
  982. if (materialMode < 2)
  983. SinglePassMaterialBase(mat).alphaThreshold = props.get(12, 0.0);
  984. else
  985. MultiPassMaterialBase(mat).alphaThreshold = props.get(12, 0.0);
  986. mat.repeat = props.get(13, false);
  987. finalizeAsset(mat, name);
  988. _blocks[blockID].data = mat;
  989. if (_debug)
  990. trace(debugString);
  991. }
  992. // Block ID = 81 AWD2.1
  993. private function parseMaterial_v1(blockID:uint):void
  994. {
  995. var mat:MaterialBase;
  996. var normalTexture:Texture2DBase;
  997. var specTexture:Texture2DBase;
  998. var returnedArray:Array;
  999. var name:String = parseVarStr();
  1000. var type:uint = _newBlockBytes.readUnsignedByte();
  1001. var num_methods:uint = _newBlockBytes.readUnsignedByte();
  1002. var props:AWDProperties = parseProperties({1:UINT32, 2:BADDR, 3:BADDR, 4:UINT8, 5:BOOL, 6:BOOL, 7:BOOL, 8:BOOL, 9:UINT8, 10:_propsNrType, 11:BOOL, 12:_propsNrType, 13:BOOL, 15:_propsNrType, 16:UINT32, 17:BADDR, 18:_propsNrType, 19:_propsNrType, 20:UINT32, 21:BADDR, 22:BADDR});
  1003. var spezialType:uint = props.get(4, 0);
  1004. var debugString:String = "";
  1005. if (spezialType >= 2) { //this is no supported material
  1006. _blocks[blockID].addError("Material-spezialType '" + spezialType + "' is not supported, can only be 0:singlePass, 1:MultiPass !");
  1007. return;
  1008. }
  1009. if (materialMode == 1)
  1010. spezialType = 0;
  1011. else if (materialMode == 2)
  1012. spezialType = 1;
  1013. if (spezialType < 2) { //this is SinglePass or MultiPass
  1014. if (type == 1) { // Color material
  1015. var color:uint = color = props.get(1, 0xcccccc);
  1016. if (spezialType == 1) { // MultiPassMaterial
  1017. mat = new ColorMultiPassMaterial(color);
  1018. debugString += "Parsed a ColorMaterial(MultiPass): Name = '" + name + "' | ";
  1019. } else { // SinglePassMaterial
  1020. mat = new ColorMaterial(color, props.get(10, 1.0));
  1021. ColorMaterial(mat).alphaBlending = props.get(11, false);
  1022. debugString += "Parsed a ColorMaterial(SinglePass): Name = '" + name + "' | ";
  1023. }
  1024. } else if (type == 2) { // texture material
  1025. var tex_addr:uint = props.get(2, 0);
  1026. returnedArray = getAssetByID(tex_addr, [AssetType.TEXTURE])
  1027. if ((!returnedArray[0]) && (tex_addr > 0))
  1028. _blocks[blockID].addError("Could not find the DiffsueTexture (ID = " + tex_addr + " ) for this TextureMaterial");
  1029. var texture:Texture2DBase = returnedArray[1];
  1030. var ambientTexture:Texture2DBase;
  1031. var ambientTex_addr:uint = props.get(17, 0);
  1032. returnedArray = getAssetByID(ambientTex_addr, [AssetType.TEXTURE])
  1033. if ((!returnedArray[0]) && (ambientTex_addr != 0))
  1034. _blocks[blockID].addError("Could not find the AmbientTexture (ID = " + ambientTex_addr + " ) for this TextureMaterial");
  1035. if (returnedArray[0])
  1036. ambientTexture = returnedArray[1]
  1037. if (spezialType == 1) { // MultiPassMaterial
  1038. mat = new TextureMultiPassMaterial(texture);
  1039. debugString += "Parsed a TextureMaterial(MultiPass): Name = '" + name + "' | Texture-Name = " + texture.name;
  1040. if (ambientTexture) {
  1041. TextureMultiPassMaterial(mat).ambientTexture = ambientTexture;
  1042. debugString += " | AmbientTexture-Name = " + ambientTexture.name;
  1043. }
  1044. } else { // SinglePassMaterial
  1045. mat = new TextureMaterial(texture);
  1046. debugString += "Parsed a TextureMaterial(SinglePass): Name = '" + name + "' | Texture-Name = " + texture.name;
  1047. if (ambientTexture) {
  1048. TextureMaterial(mat).ambientTexture = ambientTexture;
  1049. debugString += " | AmbientTexture-Name = " + ambientTexture.name;
  1050. }
  1051. TextureMaterial(mat).alpha = props.get(10, 1.0);
  1052. TextureMaterial(mat).alphaBlending = props.get(11, false);
  1053. }
  1054. }
  1055. var normalTex_addr:uint = props.get(3, 0);
  1056. returnedArray = getAssetByID(normalTex_addr, [AssetType.TEXTURE])
  1057. if ((!returnedArray[0]) && (normalTex_addr != 0))
  1058. _blocks[blockID].addError("Could not find the NormalTexture (ID = " + normalTex_addr + " ) for this TextureMaterial");
  1059. if (returnedArray[0]) {
  1060. normalTexture = returnedArray[1];
  1061. debugString += " | NormalTexture-Name = " + normalTexture.name;
  1062. }
  1063. var specTex_addr:uint = props.get(21, 0);
  1064. returnedArray = getAssetByID(specTex_addr, [AssetType.TEXTURE])
  1065. if ((!returnedArray[0]) && (specTex_addr != 0))
  1066. _blocks[blockID].addError("Could not find the SpecularTexture (ID = " + specTex_addr + " ) for this TextureMaterial");
  1067. if (returnedArray[0]) {
  1068. specTexture = returnedArray[1];
  1069. debugString += " | SpecularTexture-Name = " + specTexture.name;
  1070. }
  1071. var lightPickerAddr:uint = props.get(22, 0);
  1072. returnedArray = getAssetByID(lightPickerAddr, [AssetType.LIGHT_PICKER])
  1073. if ((!returnedArray[0]) && (lightPickerAddr))
  1074. _blocks[blockID].addError("Could not find the LightPicker (ID = " + lightPickerAddr + " ) for this TextureMaterial");
  1075. else {
  1076. MaterialBase(mat).lightPicker = returnedArray[1] as LightPickerBase;
  1077. //debugString+=" | Lightpicker-Name = "+LightPickerBase(returnedArray[1]).name;
  1078. }
  1079. MaterialBase(mat).smooth = props.get(5, true);
  1080. MaterialBase(mat).mipmap = props.get(6, true);
  1081. MaterialBase(mat).bothSides = props.get(7, false);
  1082. MaterialBase(mat).alphaPremultiplied = props.get(8, false);
  1083. MaterialBase(mat).blendMode = blendModeDic[props.get(9, 0)];
  1084. MaterialBase(mat).repeat = props.get(13, false);
  1085. if (spezialType == 0) { // this is a SinglePassMaterial
  1086. if (normalTexture)
  1087. SinglePassMaterialBase(mat).normalMap = normalTexture;
  1088. if (specTexture)
  1089. SinglePassMaterialBase(mat).specularMap = specTexture;
  1090. SinglePassMaterialBase(mat).alphaThreshold = props.get(12, 0.0);
  1091. SinglePassMaterialBase(mat).ambient = props.get(15, 1.0);
  1092. SinglePassMaterialBase(mat).ambientColor = props.get(16, 0xffffff);
  1093. SinglePassMaterialBase(mat).specular = props.get(18, 1.0);
  1094. SinglePassMaterialBase(mat).gloss = props.get(19, 50);
  1095. SinglePassMaterialBase(mat).specularColor = props.get(20, 0xffffff);
  1096. }
  1097. else { // this is MultiPassMaterial
  1098. if (normalTexture)
  1099. MultiPassMaterialBase(mat).normalMap = normalTexture;
  1100. if (specTexture)
  1101. MultiPassMaterialBase(mat).specularMap = specTexture;
  1102. MultiPassMaterialBase(mat).alphaThreshold = props.get(12, 0.0);
  1103. MultiPassMaterialBase(mat).ambient = props.get(15, 1.0);
  1104. MultiPassMaterialBase(mat).ambientColor = props.get(16, 0xffffff);
  1105. MultiPassMaterialBase(mat).specular = props.get(18, 1.0);
  1106. MultiPassMaterialBase(mat).gloss = props.get(19, 50);
  1107. MultiPassMaterialBase(mat).specularColor = props.get(20, 0xffffff);
  1108. }
  1109. var methods_parsed:uint = 0;
  1110. var targetID:uint;
  1111. while (methods_parsed < num_methods) {
  1112. var method_type:uint;
  1113. method_type = _newBlockBytes.readUnsignedShort();
  1114. props = parseProperties({1:BADDR, 2:BADDR, 3:BADDR, 101:_propsNrType, 102:_propsNrType, 103:_propsNrType, 201:UINT32, 202:UINT32, 301:UINT16, 302:UINT16, 401:UINT8, 402:UINT8, 601:COLOR, 602:COLOR, 701:BOOL, 702:BOOL, 801:MTX4x4});
  1115. switch (method_type) {
  1116. case 999: //wrapper-Methods that will load a previous parsed EffektMethod returned
  1117. targetID = props.get(1, 0);
  1118. returnedArray = getAssetByID(targetID, [AssetType.EFFECTS_METHOD]);
  1119. if (!returnedArray[0])
  1120. _blocks[blockID].addError("Could not find the EffectMethod (ID = " + targetID + " ) for this Material");
  1121. else {
  1122. if (spezialType == 0)
  1123. SinglePassMaterialBase(mat).addMethod(returnedArray[1]);
  1124. if (spezialType == 1)
  1125. MultiPassMaterialBase(mat).addMethod(returnedArray[1]);
  1126. debugString += " | EffectMethod-Name = " + EffectMethodBase(returnedArray[1]).name;
  1127. }
  1128. break;
  1129. case 998: //wrapper-Methods that will load a previous parsed ShadowMapMethod
  1130. targetID = props.get(1, 0);
  1131. returnedArray = getAssetByID(targetID, [AssetType.SHADOW_MAP_METHOD]);
  1132. if (!returnedArray[0])
  1133. _blocks[blockID].addError("Could not find the ShadowMethod (ID = " + targetID + " ) for this Material");
  1134. else {
  1135. if (spezialType == 0)
  1136. SinglePassMaterialBase(mat).shadowMethod = returnedArray[1];
  1137. if (spezialType == 1)
  1138. MultiPassMaterialBase(mat).shadowMethod = returnedArray[1];
  1139. debugString += " | ShadowMethod-Name = " + ShadowMapMethodBase(returnedArray[1]).name;
  1140. }
  1141. break;
  1142. case 1: //EnvMapAmbientMethod
  1143. targetID = props.get(1, 0);
  1144. returnedArray = getAssetByID(targetID, [AssetType.TEXTURE], "CubeTexture");
  1145. if (!returnedArray[0])
  1146. _blocks[blockID].addError("Could not find the EnvMap (ID = " + targetID + " ) for this EnvMapAmbientMethodMaterial");
  1147. if (spezialType == 0)
  1148. SinglePassMaterialBase(mat).ambientMethod = new EnvMapAmbientMethod(returnedArray[1]);
  1149. if (spezialType == 1)
  1150. MultiPassMaterialBase(mat).ambientMethod = new EnvMapAmbientMethod(returnedArray[1]);
  1151. debugString += " | EnvMapAmbientMethod | EnvMap-Name =" + CubeTextureBase(returnedArray[1]).name;
  1152. break;
  1153. case 51: //DepthDiffuseMethod
  1154. if (spezialType == 0)
  1155. SinglePassMaterialBase(mat).diffuseMethod = new DepthDiffuseMethod();
  1156. if (spezialType == 1)
  1157. MultiPassMaterialBase(mat).diffuseMethod = new DepthDiffuseMethod();
  1158. debugString += " | DepthDiffuseMethod";
  1159. break;
  1160. case 52: //GradientDiffuseMethod
  1161. targetID = props.get(1, 0);
  1162. returnedArray = getAssetByID(targetID, [AssetType.TEXTURE]);
  1163. if (!returnedArray[0])
  1164. _blocks[blockID].addError("Could not find the GradientDiffuseTexture (ID = " + targetID + " ) for this GradientDiffuseMethod");
  1165. if (spezialType == 0)
  1166. SinglePassMaterialBase(mat).diffuseMethod = new GradientDiffuseMethod(returnedArray[1]);
  1167. if (spezialType == 1)
  1168. MultiPassMaterialBase(mat).diffuseMethod = new GradientDiffuseMethod(returnedArray[1]);
  1169. debugString += " | GradientDiffuseMethod | GradientDiffuseTexture-Name =" + Texture2DBase(returnedArray[1]).name;
  1170. break;
  1171. case 53: //WrapDiffuseMethod
  1172. if (spezialType == 0)
  1173. SinglePassMaterialBase(mat).diffuseMethod = new WrapDiffuseMethod(props.get(101, 5));
  1174. if (spezialType == 1)
  1175. MultiPassMaterialBase(mat).diffuseMethod = new WrapDiffuseMethod(props.get(101, 5));
  1176. debugString += " | WrapDiffuseMethod";
  1177. break;
  1178. case 54: //LightMapDiffuseMethod
  1179. targetID = props.get(1, 0);
  1180. returnedArray = getAssetByID(targetID, [AssetType.TEXTURE]);
  1181. if (!returnedArray[0])
  1182. _blocks[blockID].addError("Could not find the LightMap (ID = " + targetID + " ) for this LightMapDiffuseMethod");
  1183. if (spezialType == 0)
  1184. SinglePassMaterialBase(mat).diffuseMethod = new LightMapDiffuseMethod(returnedArray[1], blendModeDic[props.get(401, 10)], false, SinglePassMaterialBase(mat).diffuseMethod);
  1185. if (spezialType == 1)
  1186. MultiPassMaterialBase(mat).diffuseMethod = new LightMapDiffuseMethod(returnedArray[1], blendModeDic[props.get(401, 10)], false, MultiPassMaterialBase(mat).diffuseMethod);
  1187. debugString += " | LightMapDiffuseMethod | LightMapTexture-Name =" + Texture2DBase(returnedArray[1]).name;
  1188. break;
  1189. case 55: //CelDiffuseMethod
  1190. if (spezialType == 0) {
  1191. SinglePassMaterialBase(mat).diffuseMethod = new CelDiffuseMethod(props.get(401, 3), SinglePassMaterialBase(mat).diffuseMethod);
  1192. CelDiffuseMethod(SinglePassMaterialBase(mat).diffuseMethod).smoothness = props.get(101, 0.1);
  1193. }
  1194. if (spezialType == 1) {
  1195. MultiPassMaterialBase(mat).diffuseMethod = new CelDiffuseMethod(props.get(401, 3), MultiPassMaterialBase(mat).diffuseMethod);
  1196. CelDiffuseMethod(MultiPassMaterialBase(mat).diffuseMethod).smoothness = props.get(101, 0.1);
  1197. }
  1198. debugString += " | CelDiffuseMethod";
  1199. break;
  1200. case 56: //SubSurfaceScatteringMethod
  1201. if (spezialType == 0) {
  1202. SinglePassMaterialBase(mat).diffuseMethod = new SubsurfaceScatteringDiffuseMethod(); //depthMapSize and depthMapOffset ?
  1203. SubsurfaceScatteringDiffuseMethod(SinglePassMaterialBase(mat).diffuseMethod).scattering = props.get(101, 0.2);
  1204. SubsurfaceScatteringDiffuseMethod(SinglePassMaterialBase(mat).diffuseMethod).translucency = props.get(102, 1);
  1205. SubsurfaceScatteringDiffuseMethod(SinglePassMaterialBase(mat).diffuseMethod).scatterColor = props.get(601, 0xffffff);
  1206. }
  1207. if (spezialType == 1) {
  1208. MultiPassMaterialBase(mat).diffuseMethod = new SubsurfaceScatteringDiffuseMethod(); //depthMapSize and depthMapOffset ?
  1209. SubsurfaceScatteringDiffuseMethod(MultiPassMaterialBase(mat).diffuseMethod).scattering = props.get(101, 0.2);
  1210. SubsurfaceScatteringDiffuseMethod(MultiPassMaterialBase(mat).diffuseMethod).translucency = props.get(102, 1);
  1211. SubsurfaceScatteringDiffuseMethod(MultiPassMaterialBase(mat).diffuseMethod).scatterColor = props.get(601, 0xffffff);
  1212. }
  1213. debugString += " | SubSurfaceScatteringMethod";
  1214. break;
  1215. case 101: //AnisotropicSpecularMethod
  1216. if (spezialType == 0)
  1217. SinglePassMaterialBase(mat).specularMethod = new AnisotropicSpecularMethod();
  1218. if (spezialType == 1)
  1219. MultiPassMaterialBase(mat).specularMethod = new AnisotropicSpecularMethod();
  1220. debugString += " | AnisotropicSpecularMethod";
  1221. break;
  1222. case 102: //PhongSpecularMethod
  1223. if (spezialType == 0)
  1224. SinglePassMaterialBase(mat).specularMethod = new PhongSpecularMethod();
  1225. if (spezialType == 1)
  1226. MultiPassMaterialBase(mat).specularMethod = new PhongSpecularMethod();
  1227. debugString += " | PhongSpecularMethod";
  1228. break;
  1229. case 103: //CellSpecularMethod
  1230. if (spezialType == 0) {
  1231. SinglePassMaterialBase(mat).specularMethod = new CelSpecularMethod(props.get(101, 0.5), SinglePassMaterialBase(mat).specularMethod);
  1232. CelSpecularMethod(SinglePassMaterialBase(mat).specularMethod).smoothness = props.get(102, 0.1);
  1233. }
  1234. if (spezialType == 1) {
  1235. MultiPassMaterialBase(mat).specularMethod = new CelSpecularMethod(props.get(101, 0.5), MultiPassMaterialBase(mat).specularMethod);
  1236. CelSpecularMethod(MultiPassMaterialBase(mat).specularMethod).smoothness = props.get(102, 0.1);
  1237. }
  1238. debugString += " | CellSpecularMethod";
  1239. break;
  1240. case 104: //FresnelSpecularMethod
  1241. if (spezialType == 0) {
  1242. SinglePassMaterialBase(mat).specularMethod = new FresnelSpecularMethod(props.get(701, true), SinglePassMaterialBase(mat).specularMethod);
  1243. FresnelSpecularMethod(SinglePassMaterialBase(mat).specularMethod).fresnelPower = props.get(101, 5);
  1244. FresnelSpecularMethod(SinglePassMaterialBase(mat).specularMethod).normalReflectance = props.get(102, 0.1);
  1245. }
  1246. if (spezialType == 1) {
  1247. MultiPassMaterialBase(mat).specularMethod = new FresnelSpecularMethod(props.get(701, true), MultiPassMaterialBase(mat).specularMethod);
  1248. FresnelSpecularMethod(MultiPassMaterialBase(mat).specularMethod).fresnelPower = props.get(101, 5);
  1249. FresnelSpecularMethod(MultiPassMaterialBase(mat).specularMethod).normalReflectance = props.get(102, 0.1);
  1250. }
  1251. debugString += " | FresnelSpecularMethod";
  1252. break;
  1253. //case 151://HeightMapNormalMethod - thios is not implemented for now, but might appear later
  1254. //break;
  1255. case 152: //SimpleWaterNormalMethod
  1256. targetID = props.get(1, 0);
  1257. returnedArray = getAssetByID(targetID, [AssetType.TEXTURE]);
  1258. if (!returnedArray[0])
  1259. _blocks[blockID].addError("Could not find the SecoundNormalMap (ID = " + targetID + " ) for this SimpleWaterNormalMethod");
  1260. if (spezialType == 0) {
  1261. if (!SinglePassMaterialBase(mat).normalMap)
  1262. _blocks[blockID].addError("Could not find a normal Map on this Material to use with this SimpleWaterNormalMethod");
  1263. SinglePassMaterialBase(mat).normalMap = returnedArray[1];
  1264. SinglePassMaterialBase(mat).normalMethod = new SimpleWaterNormalMethod(SinglePassMaterialBase(mat).normalMap, returnedArray[1]);
  1265. }
  1266. if (spezialType == 1) {
  1267. if (!MultiPassMaterialBase(mat).normalMap)
  1268. _blocks[blockID].addError("Could not find a normal Map on this Material to use with this SimpleWaterNormalMethod");
  1269. MultiPassMaterialBase(mat).normalMap = returnedArray[1];
  1270. MultiPassMaterialBase(mat).normalMethod = new SimpleWaterNormalMethod(MultiPassMaterialBase(mat).normalMap, returnedArray[1]);
  1271. }
  1272. debugString += " | SimpleWaterNormalMethod | Second-NormalTexture-Name = " + Texture2DBase(returnedArray[1]).name;
  1273. break;
  1274. }
  1275. parseUserAttributes();
  1276. methods_parsed += 1;
  1277. }
  1278. }
  1279. MaterialBase(mat).extra = parseUserAttributes();
  1280. finalizeAsset(mat, name);
  1281. _blocks[blockID].data = mat;
  1282. if (_debug)
  1283. trace(debugString);
  1284. }
  1285. //Block ID = 82
  1286. private function parseTexture(blockID:uint):void
  1287. {
  1288. var asset:Texture2DBase;
  1289. _blocks[blockID].name = parseVarStr();
  1290. var type:uint = _newBlockBytes.readUnsignedByte();
  1291. var data_len:uint;
  1292. _texture_users[_cur_block_id.toString()] = [];
  1293. // External
  1294. if (type == 0) {
  1295. data_len = _newBlockBytes.readUnsignedInt();
  1296. var url:String;
  1297. url = _newBlockBytes.readUTFBytes(data_len);
  1298. addDependency(_cur_block_id.toString(), new URLRequest(url), false, null, true);
  1299. } else {
  1300. data_len = _newBlockBytes.readUnsignedInt();
  1301. var data:ByteArray;
  1302. data = new ByteArray();
  1303. _newBlockBytes.readBytes(data, 0, data_len);
  1304. addDependency(_cur_block_id.toString(), null, false, data, true);
  1305. }
  1306. // Ignore for now
  1307. parseProperties(null);
  1308. _blocks[blockID].extras = parseUserAttributes();
  1309. pauseAndRetrieveDependencies();
  1310. _blocks[blockID].data = asset;
  1311. if (_debug) {
  1312. var textureStylesNames:Array = ["external", "embed"]
  1313. trace("Start parsing a " + textureStylesNames[type] + " Bitmap for Texture");
  1314. }
  1315. }
  1316. //Block ID = 83
  1317. private function parseCubeTexture(blockID:uint):void
  1318. {
  1319. //blockLength = block.len;
  1320. var data_len:uint;
  1321. var asset:CubeTextureBase;
  1322. var i:int;
  1323. _cubeTextures = new Array();
  1324. _texture_users[_cur_block_id.toString()] = [];
  1325. var type:uint = _newBlockBytes.readUnsignedByte();
  1326. _blocks[blockID].name = parseVarStr();
  1327. for (i = 0; i < 6; i++) {
  1328. _texture_users[_cur_block_id.toString()] = [];
  1329. _cubeTextures.push(null);
  1330. // External
  1331. if (type == 0) {
  1332. data_len = _newBlockBytes.readUnsignedInt();
  1333. var url:String;
  1334. url = _newBlockBytes.readUTFBytes(data_len);
  1335. addDependency(_cur_block_id.toString() + "#" + i, new URLRequest(url), false, null, true);
  1336. } else {
  1337. data_len = _newBlockBytes.readUnsignedInt();
  1338. var data:ByteArray;
  1339. data = new ByteArray();
  1340. _newBlockBytes.readBytes(data, 0, data_len);
  1341. addDependency(_cur_block_id.toString() + "#" + i, null, false, data, true);
  1342. }
  1343. }
  1344. // Ignore for now
  1345. parseProperties(null);
  1346. _blocks[blockID].extras = parseUserAttributes();
  1347. pauseAndRetrieveDependencies();
  1348. _blocks[blockID].data = asset;
  1349. if (_debug) {
  1350. var textureStylesNames:Array = ["external", "embed"]
  1351. trace("Start parsing 6 " + textureStylesNames[type] + " Bitmaps for CubeTexture");
  1352. }
  1353. }
  1354. //Block ID = 91
  1355. private function parseSharedMethodBlock(blockID:uint):void
  1356. {
  1357. var asset:EffectMethodBase;
  1358. _blocks[blockID].name = parseVarStr();
  1359. asset = parseSharedMethodList(blockID);
  1360. parseUserAttributes();
  1361. _blocks[blockID].data = asset;
  1362. finalizeAsset(asset, _blocks[blockID].name);
  1363. _blocks[blockID].data = asset;
  1364. if (_debug)
  1365. trace("Parsed a EffectMethod: Name = " + asset.name + " Type = " + asset);
  1366. }
  1367. // this functions reads and creates a EffectMethod
  1368. private function parseSharedMethodList(blockID:uint):EffectMethodBase
  1369. {
  1370. var methodType:uint = _newBlockBytes.readUnsignedShort();
  1371. var effectMethodReturn:EffectMethodBase;
  1372. var props:AWDProperties = parseProperties({1:BADDR, 2:BADDR, 3:BADDR, 101:_propsNrType, 102:_propsNrType, 103:_propsNrType, 104:_propsNrType, 105:_propsNrType, 106:_propsNrType, 107:_propsNrType, 201:UINT32, 202:UINT32, 301:UINT16, 302:UINT16, 401:UINT8, 402:UINT8, 601:COLOR, 602:COLOR, 701:BOOL, 702:BOOL});
  1373. var targetID:uint;
  1374. var returnedArray:Array;
  1375. switch (methodType) {
  1376. // Effect Methods
  1377. case 401: //ColorMatrix
  1378. effectMethodReturn = new ColorMatrixMethod(props.get(101, new Array(0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)));
  1379. break;
  1380. case 402: //ColorTransform
  1381. effectMethodReturn = new ColorTransformMethod();
  1382. var offCol:uint = props.get(601, 0x00000000);
  1383. var newColorTransform:ColorTransform = new ColorTransform(props.get(102, 1), props.get(103, 1), props.get(104, 1), props.get(101, 1), ((offCol >> 16) & 0xFF), ((offCol >> 8) & 0xFF), (offCol & 0xFF), ((offCol >> 24) & 0xFF));
  1384. ColorTransformMethod(effectMethodReturn).colorTransform = newColorTransform;
  1385. break;
  1386. case 403: //EnvMap
  1387. targetID = props.get(1, 0);
  1388. returnedArray = getAssetByID(targetID, [AssetType.TEXTURE], "CubeTexture");
  1389. if (!returnedArray[0])
  1390. _blocks[blockID].addError("Could not find the EnvMap (ID = " + targetID + " ) for this EnvMapMethod");
  1391. effectMethodReturn = new EnvMapMethod(returnedArray[1], props.get(101, 1));
  1392. targetID = props.get(2, 0);
  1393. if (targetID > 0) {
  1394. returnedArray = getAssetByID(targetID, [AssetType.TEXTURE]);
  1395. if (!returnedArray[0])
  1396. _blocks[blockID].addError("Could not find the Mask-texture (ID = " + targetID + " ) for this EnvMapMethod");
  1397. EnvMapMethod(effectMethodReturn).mask = returnedArray[1];
  1398. }
  1399. break;
  1400. case 404: //LightMapMethod
  1401. targetID = props.get(1, 0);
  1402. returnedArray = getAssetByID(targetID, [AssetType.TEXTURE]);
  1403. if (!returnedArray[0])
  1404. _blocks[blockID].addError("Could not find the LightMap (ID = " + targetID + " ) for this LightMapMethod");
  1405. effectMethodReturn = new LightMapMethod(returnedArray[1], blendModeDic[props.get(401, 10)]); //usesecondaryUV not set
  1406. break;
  1407. case 405: //ProjectiveTextureMethod
  1408. targetID = props.get(1, 0);
  1409. returnedArray = getAssetByID(targetID, [AssetType.TEXTURE_PROJECTOR]);
  1410. if (!returnedArray[0])
  1411. _blocks[blockID].addError("Could not find the TextureProjector (ID = " + targetID + " ) for this ProjectiveTextureMethod");
  1412. effectMethodReturn = new ProjectiveTextureMethod(returnedArray[1], blendModeDic[props.get(401, 10)]);
  1413. break;
  1414. case 406: //RimLightMethod
  1415. effectMethodReturn = new RimLightMethod(props.get(601, 0xffffff), props.get(101, 0.4), props.get(102, 2));
  1416. break;
  1417. case 407: //AlphaMaskMethod
  1418. targetID = props.get(1, 0);
  1419. returnedArray = getAssetByID(targetID, [AssetType.TEXTURE]);
  1420. if (!returnedArray[0])
  1421. _blocks[blockID].addError("Could not find the Alpha-texture (ID = " + targetID + " ) for this AlphaMaskMethod");
  1422. effectMethodReturn = new AlphaMaskMethod(returnedArray[1], props.get(701, false));
  1423. break;
  1424. case 408: //RefractionEnvMapMethod
  1425. targetID = props.get(1, 0);
  1426. returnedArray = getAssetByID(targetID, [AssetType.TEXTURE], "CubeTexture");
  1427. if (!returnedArray[0])
  1428. _blocks[blockID].addError("Could not find the EnvMap (ID = " + targetID + " ) for this RefractionEnvMapMethod");
  1429. effectMethodReturn = new RefractionEnvMapMethod(returnedArray[1], props.get(101, 0.1), props.get(102, 0.01), props.get(103, 0.01), props.get(104, 0.01));
  1430. RefractionEnvMapMethod(effectMethodReturn).alpha = props.get(104, 1);
  1431. break;
  1432. case 409: //OutlineMethod
  1433. effectMethodReturn = new OutlineMethod(props.get(601, 0x00000000), props.get(101, 1), props.get(701, true), props.get(702, false));
  1434. break;
  1435. case 410: //FresnelEnvMapMethod
  1436. targetID = props.get(1, 0);
  1437. returnedArray = getAssetByID(targetID, [AssetType.TEXTURE], "CubeTexture");
  1438. if (!returnedArray[0])
  1439. _blocks[blockID].addError("Could not find the EnvMap (ID = " + targetID + " ) for this FresnelEnvMapMethod");
  1440. effectMethodReturn = new FresnelEnvMapMethod(returnedArray[1], props.get(101, 1));
  1441. break;
  1442. case 411: //FogMethod
  1443. effectMethodReturn = new FogMethod(props.get(101, 0), props.get(102, 1000), props.get(601, 0x808080));
  1444. break;
  1445. }
  1446. parseUserAttributes();
  1447. return effectMethodReturn;
  1448. }
  1449. //Block ID = 92
  1450. private function parseShadowMethodBlock(blockID:uint):void
  1451. {
  1452. var type:uint;
  1453. var data_len:uint;
  1454. var asset:ShadowMapMethodBase;
  1455. var shadowLightID:uint;
  1456. _blocks[blockID].name = parseVarStr();
  1457. shadowLightID = _newBlockBytes.readUnsignedInt();
  1458. var returnedArray:Array = getAssetByID(shadowLightID, [AssetType.LIGHT]);
  1459. if (!returnedArray[0]) {
  1460. _blocks[blockID].addError("Could not find the TargetLight (ID = " + shadowLightID + " ) for this ShadowMethod - ShadowMethod not created");
  1461. return;
  1462. }
  1463. asset = parseShadowMethodList(returnedArray[1] as LightBase, blockID);
  1464. if (!asset)
  1465. return;
  1466. parseUserAttributes(); // Ignore for now
  1467. finalizeAsset(asset, _blocks[blockID].name);
  1468. _blocks[blockID].data = asset;
  1469. if (_debug)
  1470. trace("Parsed a ShadowMapMethodMethod: Name = " + asset.name + " | Type = " + asset + " | Light-Name = " + LightBase(returnedArray[1]));
  1471. }
  1472. // this functions reads and creates a ShadowMethodMethod
  1473. private function parseShadowMethodList(light:LightBase, blockID:uint):ShadowMapMethodBase
  1474. {
  1475. var methodType:uint = _newBlockBytes.readUnsignedShort();
  1476. var shadowMethod:ShadowMapMethodBase;
  1477. var props:AWDProperties = parseProperties({1:BADDR, 2:BADDR, 3:BADDR, 101:_propsNrType, 102:_propsNrType, 103:_propsNrType, 201:UINT32, 202:UINT32, 301:UINT16, 302:UINT16, 401:UINT8, 402:UINT8, 601:COLOR, 602:COLOR, 701:BOOL, 702:BOOL, 801:MTX4x4});
  1478. var targetID:uint;
  1479. var returnedArray:Array;
  1480. switch (methodType) {
  1481. case 1001: //CascadeShadowMapMethod
  1482. targetID = props.get(1, 0);
  1483. returnedArray = getAssetByID(targetID, [AssetType.SHADOW_MAP_METHOD]);
  1484. if (!returnedArray[0]) {
  1485. _blocks[blockID].addError("Could not find the ShadowBaseMethod (ID = " + targetID + " ) for this CascadeShadowMapMethod - ShadowMethod not created");
  1486. return shadowMethod;
  1487. }
  1488. shadowMethod = new CascadeShadowMapMethod(returnedArray[1]);
  1489. break;
  1490. case 1002: //NearShadowMapMethod
  1491. targetID = props.get(1, 0);
  1492. returnedArray = getAssetByID(targetID, [AssetType.SHADOW_MAP_METHOD]);
  1493. if (!returnedArray[0]) {
  1494. _blocks[blockID].addError("Could not find the ShadowBaseMethod (ID = " + targetID + " ) for this NearShadowMapMethod - ShadowMethod not created");
  1495. return shadowMethod;
  1496. }
  1497. shadowMethod = new NearShadowMapMethod(returnedArray[1]);
  1498. break;
  1499. case 1101: //FilteredShadowMapMethod
  1500. shadowMethod = new FilteredShadowMapMethod(DirectionalLight(light));
  1501. FilteredShadowMapMethod(shadowMethod).alpha = props.get(101, 1);
  1502. FilteredShadowMapMethod(shadowMethod).epsilon = props.get(102, 0.002);
  1503. break;
  1504. case 1102: //DitheredShadowMapMethod
  1505. shadowMethod = new DitheredShadowMapMethod(DirectionalLight(light), props.get(201, 5));
  1506. DitheredShadowMapMethod(shadowMethod).alpha = props.get(101, 1);
  1507. DitheredShadowMapMethod(shadowMethod).epsilon = props.get(102, 0.002);
  1508. DitheredShadowMapMethod(shadowMethod).range = props.get(103, 1);
  1509. break;
  1510. case 1103: //SoftShadowMapMethod
  1511. shadowMethod = new SoftShadowMapMethod(DirectionalLight(light), props.get(201, 5));
  1512. SoftShadowMapMethod(shadowMethod).alpha = props.get(101, 1);
  1513. SoftShadowMapMethod(shadowMethod).epsilon = props.get(102, 0.002);
  1514. SoftShadowMapMethod(shadowMethod).range = props.get(103, 1);
  1515. break;
  1516. case 1104: //HardShadowMapMethod
  1517. shadowMethod = new HardShadowMapMethod(light);
  1518. HardShadowMapMethod(shadowMethod).alpha = props.get(101, 1);
  1519. HardShadowMapMethod(shadowMethod).epsilon = props.get(102, 0.002);
  1520. break;
  1521. }
  1522. parseUserAttributes();
  1523. return shadowMethod;
  1524. }
  1525. //Block ID 101
  1526. private function parseSkeleton(blockID:uint):void
  1527. {
  1528. var name:String = parseVarStr();
  1529. var num_joints:uint = _newBlockBytes.readUnsignedShort();
  1530. var skeleton:Skeleton = new Skeleton();
  1531. parseProperties(null); // Discard properties for now
  1532. var joints_parsed:uint = 0;
  1533. while (joints_parsed < num_joints) {
  1534. var joint:SkeletonJoint;
  1535. var ibp:Matrix3D;
  1536. // Ignore joint id
  1537. _newBlockBytes.readUnsignedShort();
  1538. joint = new SkeletonJoint();
  1539. joint.parentIndex = _newBlockBytes.readUnsignedShort() - 1; // 0=null in AWD
  1540. joint.name = parseVarStr();
  1541. ibp = parseMatrix3D();
  1542. joint.inverseBindPose = ibp.rawData;
  1543. // Ignore joint props/attributes for now
  1544. parseProperties(null);
  1545. parseUserAttributes();
  1546. skeleton.joints.push(joint);
  1547. joints_parsed++;
  1548. }
  1549. // Discard attributes for now
  1550. parseUserAttributes();
  1551. finalizeAsset(skeleton, name);
  1552. _blocks[blockID].data = skeleton;
  1553. if (_debug)
  1554. trace("Parsed a Skeleton: Name = " + skeleton.name + " | Number of Joints = " + joints_parsed);
  1555. }
  1556. //Block ID = 102
  1557. private function parseSkeletonPose(blockID:uint):void
  1558. {
  1559. var name:String = parseVarStr();
  1560. var num_joints:uint = _newBlockBytes.readUnsignedShort();
  1561. parseProperties(null); // Ignore properties for now
  1562. var pose:SkeletonPose = new SkeletonPose();
  1563. var joints_parsed:uint = 0;
  1564. while (joints_parsed < num_joints) {
  1565. var joint_pose:JointPose;
  1566. var has_transform:uint;
  1567. joint_pose = new JointPose();
  1568. has_transform = _newBlockBytes.readUnsignedByte();
  1569. if (has_transform == 1) {
  1570. var mtx_data:Vector.<Number> = parseMatrix43RawData();
  1571. var mtx:Matrix3D = new Matrix3D(mtx_data);
  1572. joint_pose.orientation.fromMatrix(mtx);
  1573. joint_pose.translation.copyFrom(mtx.position);
  1574. pose.jointPoses[joints_parsed] = joint_pose;
  1575. }
  1576. joints_parsed++;
  1577. }
  1578. // Skip attributes for now
  1579. parseUserAttributes();
  1580. finalizeAsset(pose, name);
  1581. _blocks[blockID].data = pose;
  1582. if (_debug)
  1583. trace("Parsed a SkeletonPose: Name = " + pose.name + " | Number of Joints = " + joints_parsed);
  1584. }
  1585. //blockID 103
  1586. private function parseSkeletonAnimation(blockID:uint):void
  1587. {
  1588. var frame_dur:Number;
  1589. var pose_addr:uint;
  1590. var name:String = parseVarStr();
  1591. var clip:SkeletonClipNode = new SkeletonClipNode();
  1592. var num_frames:uint = _newBlockBytes.readUnsignedShort();
  1593. parseProperties(null); // Ignore properties for now
  1594. var frames_parsed:uint = 0;
  1595. var returnedArray:Array;
  1596. while (frames_parsed < num_frames) {
  1597. pose_addr = _newBlockBytes.readUnsignedInt();
  1598. frame_dur = _newBlockBytes.readUnsignedShort();
  1599. returnedArray = getAssetByID(pose_addr, [AssetType.SKELETON_POSE]);
  1600. if (!returnedArray[0])
  1601. _blocks[blockID].addError("Could not find the SkeletonPose Frame # " + frames_parsed + " (ID = " + pose_addr + " ) for this SkeletonClipNode");
  1602. else
  1603. clip.addFrame(_blocks[pose_addr].data as SkeletonPose, frame_dur);
  1604. frames_parsed++;
  1605. }
  1606. if (clip.frames.length == 0) {
  1607. _blocks[blockID].addError("Could not this SkeletonClipNode, because no Frames where set.");
  1608. return;
  1609. }
  1610. // Ignore attributes for now
  1611. parseUserAttributes();
  1612. finalizeAsset(clip, name);
  1613. _blocks[blockID].data = clip;
  1614. if (_debug)
  1615. trace("Parsed a SkeletonClipNode: Name = " + clip.name + " | Number of Frames = " + clip.frames.length);
  1616. }
  1617. //Block ID = 111 / Block ID = 112
  1618. private function parseMeshPoseAnimation(blockID:uint, poseOnly:Boolean = false):void
  1619. {
  1620. var num_frames:uint = 1;
  1621. var num_submeshes:uint;
  1622. var frames_parsed:uint;
  1623. var subMeshParsed:uint;
  1624. var frame_dur:Number;
  1625. var x:Number;
  1626. var y:Number;
  1627. var z:Number;
  1628. var str_len:Number;
  1629. var str_end:Number;
  1630. var geometry:Geometry;
  1631. var subGeom:CompactSubGeometry;
  1632. var idx:int = 0;
  1633. var clip:VertexClipNode = new VertexClipNode();
  1634. var indices:Vector.<uint>;
  1635. var verts:Vector.<Number>;
  1636. var num_Streams:int = 0;
  1637. var streamsParsed:int = 0;
  1638. var streamtypes:Vector.<int> = new Vector.<int>;
  1639. var props:AWDProperties;
  1640. var thisGeo:Geometry;
  1641. var name:String = parseVarStr();
  1642. var geoAdress:int = _newBlockBytes.readUnsignedInt();
  1643. var returnedArray:Array = getAssetByID(geoAdress, [AssetType.GEOMETRY]);
  1644. if (!returnedArray[0]) {
  1645. _blocks[blockID].addError("Could not find the target-Geometry-Object " + geoAdress + " ) for this VertexClipNode");
  1646. return;
  1647. }
  1648. var uvs:Vector.<Vector.<Number>> = getUVForVertexAnimation(geoAdress);
  1649. if (!poseOnly)
  1650. num_frames = _newBlockBytes.readUnsignedShort();
  1651. num_submeshes = _newBlockBytes.readUnsignedShort();
  1652. num_Streams = _newBlockBytes.readUnsignedShort();
  1653. streamsParsed = 0;
  1654. while (streamsParsed < num_Streams) {
  1655. streamtypes.push(_newBlockBytes.readUnsignedShort());
  1656. streamsParsed++;
  1657. }
  1658. props = parseProperties({1:BOOL, 2:BOOL});
  1659. clip.looping = props.get(1, true);
  1660. clip.stitchFinalFrame = props.get(2, false);
  1661. frames_parsed = 0;
  1662. while (frames_parsed < num_frames) {
  1663. frame_dur = _newBlockBytes.readUnsignedShort();
  1664. geometry = new Geometry();
  1665. subMeshParsed = 0;
  1666. while (subMeshParsed < num_submeshes) {
  1667. streamsParsed = 0;
  1668. str_len = _newBlockBytes.readUnsignedInt();
  1669. str_end = _newBlockBytes.position + str_len;
  1670. while (streamsParsed < num_Streams) {
  1671. if (streamtypes[streamsParsed] == 1) {
  1672. indices = returnedArray[1].subGeometries[subMeshParsed].indexData;
  1673. verts = new Vector.<Number>();
  1674. idx = 0;
  1675. while (_newBlockBytes.position < str_end) {
  1676. x = readNumber(_accuracyGeo)
  1677. y = readNumber(_accuracyGeo)
  1678. z = readNumber(_accuracyGeo)
  1679. verts[idx++] = x;
  1680. verts[idx++] = y;
  1681. verts[idx++] = z;
  1682. }
  1683. subGeom = new CompactSubGeometry();
  1684. subGeom.fromVectors(verts, uvs[subMeshParsed], null, null);
  1685. subGeom.updateIndexData(indices);
  1686. subGeom.vertexNormalData;
  1687. subGeom.vertexTangentData;
  1688. subGeom.autoDeriveVertexNormals = false;
  1689. subGeom.autoDeriveVertexTangents = false;
  1690. subMeshParsed++;
  1691. geometry.addSubGeometry(subGeom)
  1692. } else
  1693. _newBlockBytes.position = str_end;
  1694. streamsParsed++;
  1695. }
  1696. }
  1697. clip.addFrame(geometry, frame_dur);
  1698. frames_parsed++;
  1699. }
  1700. parseUserAttributes();
  1701. finalizeAsset(clip, name);
  1702. _blocks[blockID].data = clip;
  1703. if (_debug)
  1704. trace("Parsed a VertexClipNode: Name = " + clip.name + " | Target-Geometry-Name = " + Geometry(returnedArray[1]).name + " | Number of Frames = " + clip.frames.length);
  1705. }
  1706. //BlockID 113
  1707. private function parseVertexAnimationSet(blockID:uint):void
  1708. {
  1709. var poseBlockAdress:int
  1710. var outputString:String = "";
  1711. var name:String = parseVarStr();
  1712. var num_frames:uint = _newBlockBytes.readUnsignedShort();
  1713. var props:AWDProperties = parseProperties({1:UINT16});
  1714. var frames_parsed:uint = 0;
  1715. var skeletonFrames:Vector.<SkeletonClipNode> = new Vector.<SkeletonClipNode>;
  1716. var vertexFrames:Vector.<VertexClipNode> = new Vector.<VertexClipNode>;
  1717. while (frames_parsed < num_frames) {
  1718. poseBlockAdress = _newBlockBytes.readUnsignedInt();
  1719. var returnedArray:Array = getAssetByID(poseBlockAdress, [AssetType.ANIMATION_NODE]);
  1720. if (!returnedArray[0])
  1721. _blocks[blockID].addError("Could not find the AnimationClipNode Nr " + frames_parsed + " ( " + poseBlockAdress + " ) for this AnimationSet");
  1722. else {
  1723. if (returnedArray[1] is VertexClipNode)
  1724. vertexFrames.push(returnedArray[1])
  1725. if (returnedArray[1] is SkeletonClipNode)
  1726. skeletonFrames.push(returnedArray[1])
  1727. }
  1728. frames_parsed++;
  1729. }
  1730. if ((vertexFrames.length == 0) && (skeletonFrames.length == 0)) {
  1731. _blocks[blockID].addError("Could not create this AnimationSet, because it contains no animations");
  1732. return;
  1733. }
  1734. parseUserAttributes();
  1735. if (vertexFrames.length > 0) {
  1736. var newVertexAnimationSet:VertexAnimationSet = new VertexAnimationSet();
  1737. for each (var vertexFrame:VertexClipNode in vertexFrames)
  1738. newVertexAnimationSet.addAnimation(vertexFrame);
  1739. finalizeAsset(newVertexAnimationSet, name);
  1740. _blocks[blockID].data = newVertexAnimationSet;
  1741. if (_debug)
  1742. trace("Parsed a VertexAnimationSet: Name = " + name + " | Animations = " + newVertexAnimationSet.animations.length + " | Animation-Names = " + newVertexAnimationSet.animationNames.toString());
  1743. } else if (skeletonFrames.length > 0) {
  1744. returnedArray = getAssetByID(poseBlockAdress, [AssetType.ANIMATION_NODE]);
  1745. var newSkeletonAnimationSet:SkeletonAnimationSet = new SkeletonAnimationSet(props.get(1, 4)); //props.get(1,4));
  1746. for each (var skeletFrame:SkeletonClipNode in skeletonFrames)
  1747. newSkeletonAnimationSet.addAnimation(skeletFrame);
  1748. finalizeAsset(newSkeletonAnimationSet, name);
  1749. _blocks[blockID].data = newSkeletonAnimationSet;
  1750. if (_debug)
  1751. trace("Parsed a SkeletonAnimationSet: Name = " + name + " | Animations = " + newSkeletonAnimationSet.animations.length + " | Animation-Names = " + newSkeletonAnimationSet.animationNames.toString());
  1752. }
  1753. }
  1754. //blockID 121
  1755. private function parseUVAnimation(blockID:uint):void
  1756. {
  1757. var name:String = parseVarStr();
  1758. var num_frames:uint = _newBlockBytes.readUnsignedShort();
  1759. var props:AWDProperties = parseProperties(null);
  1760. var clip:UVClipNode = new UVClipNode();
  1761. var dummy:Sprite = new Sprite();
  1762. var frames_parsed:uint = 0;
  1763. while (frames_parsed < num_frames) {
  1764. // TODO: Replace this with some reliable way to decompose a 2d matrix
  1765. var mtx:Matrix = parseMatrix2D();
  1766. mtx.scale(100, 100);
  1767. dummy.transform.matrix = mtx;
  1768. var frame_dur:uint = _newBlockBytes.readUnsignedShort();
  1769. var frame:UVAnimationFrame = new UVAnimationFrame(dummy.x*0.01, dummy.y*0.01, dummy.scaleX/100, dummy.scaleY/100, dummy.rotation);
  1770. clip.addFrame(frame, frame_dur);
  1771. frames_parsed++;
  1772. }
  1773. // Ignore for now
  1774. parseUserAttributes();
  1775. finalizeAsset(clip, name);
  1776. _blocks[blockID].data = clip;
  1777. if (_debug)
  1778. trace("Parsed a UVClipNode: Name = " + name + " | Number of Frames = " + frames_parsed);
  1779. }
  1780. //BlockID 122
  1781. private function parseAnimatorSet(blockID:uint):void
  1782. {
  1783. var targetMesh:Mesh;
  1784. var animSetBlockAdress:int
  1785. var targetAnimationSet:AnimationSetBase;
  1786. var outputString:String = "";
  1787. var name:String = parseVarStr();
  1788. var type:uint = _newBlockBytes.readUnsignedShort();
  1789. var props:AWDProperties = parseProperties({1:BADDR});
  1790. animSetBlockAdress = _newBlockBytes.readUnsignedInt();
  1791. var targetMeshLength:uint = _newBlockBytes.readUnsignedShort();
  1792. var meshAdresses:Vector.<uint> = new Vector.<uint>;
  1793. for (var i:int = 0; i < targetMeshLength; i++)
  1794. meshAdresses.push(_newBlockBytes.readUnsignedInt());
  1795. var activeState:uint = _newBlockBytes.readUnsignedShort();
  1796. var autoplay:Boolean = Boolean(_newBlockBytes.readUnsignedByte());
  1797. parseUserAttributes();
  1798. parseUserAttributes();
  1799. var returnedArray:Array;
  1800. var targetMeshes:Vector.<Mesh> = new Vector.<Mesh>;
  1801. for (i = 0; i < meshAdresses.length; i++) {
  1802. returnedArray = getAssetByID(meshAdresses[i], [AssetType.MESH]);
  1803. if (returnedArray[0])
  1804. targetMeshes.push(returnedArray[1] as Mesh);
  1805. }
  1806. returnedArray = getAssetByID(animSetBlockAdress, [AssetType.ANIMATION_SET]);
  1807. if (!returnedArray[0]) {
  1808. _blocks[blockID].addError("Could not find the AnimationSet ( " + animSetBlockAdress + " ) for this Animator");;
  1809. return
  1810. }
  1811. targetAnimationSet = returnedArray[1] as AnimationSetBase;
  1812. var thisAnimator:AnimatorBase;
  1813. if (type == 1) {
  1814. returnedArray = getAssetByID(props.get(1, 0), [AssetType.SKELETON]);
  1815. if (!returnedArray[0]) {
  1816. _blocks[blockID].addError("Could not find the Skeleton ( " + props.get(1, 0) + " ) for this Animator");
  1817. return
  1818. }
  1819. thisAnimator = new SkeletonAnimator(targetAnimationSet as SkeletonAnimationSet, returnedArray[1] as Skeleton);
  1820. } else if (type == 2)
  1821. thisAnimator = new VertexAnimator(targetAnimationSet as VertexAnimationSet);
  1822. finalizeAsset(thisAnimator, name);
  1823. _blocks[blockID].data = thisAnimator;
  1824. for (i = 0; i < targetMeshes.length; i++) {
  1825. if (type == 1)
  1826. targetMeshes[i].animator = SkeletonAnimator(thisAnimator);
  1827. if (type == 2)
  1828. targetMeshes[i].animator = VertexAnimator(thisAnimator);
  1829. }
  1830. if (_debug)
  1831. trace("Parsed a Animator: Name = " + name);
  1832. }
  1833. //Block ID = 253
  1834. private function parseCommand(blockID:uint):void
  1835. {
  1836. var hasBlocks:Boolean = Boolean(_newBlockBytes.readUnsignedByte());
  1837. var par_id:uint = _newBlockBytes.readUnsignedInt();
  1838. var mtx:Matrix3D = parseMatrix3D();
  1839. var name:String = parseVarStr();
  1840. var parentObject:ObjectContainer3D;
  1841. var targetObject:ObjectContainer3D;
  1842. var returnedArray:Array = getAssetByID(par_id, [AssetType.CONTAINER, AssetType.LIGHT, AssetType.MESH, AssetType.ENTITY, AssetType.SEGMENT_SET]);
  1843. if (returnedArray[0])
  1844. parentObject = ObjectContainer3D(returnedArray[1]);
  1845. var numCommands:uint = _newBlockBytes.readShort();
  1846. var typeCommand:uint = _newBlockBytes.readShort();
  1847. var props:AWDProperties = parseProperties({1:BADDR});
  1848. switch (typeCommand) {
  1849. case 1:
  1850. var targetID:uint = props.get(1, 0);
  1851. var returnedArrayTarget:Array = getAssetByID(targetID, [AssetType.LIGHT, AssetType.TEXTURE_PROJECTOR]); //for no only light is requested!!!!
  1852. if ((!returnedArrayTarget[0]) && (targetID != 0)) {
  1853. _blocks[blockID].addError("Could not find the light (ID = " + targetID + " ( for this CommandBock!");
  1854. return;
  1855. }
  1856. targetObject = returnedArrayTarget[1];
  1857. if (parentObject)
  1858. parentObject.addChild(targetObject);
  1859. targetObject.transform = mtx;
  1860. break;
  1861. }
  1862. if (targetObject) {
  1863. props = parseProperties({1:_matrixNrType, 2:_matrixNrType, 3:_matrixNrType, 4:UINT8});
  1864. targetObject.pivotPoint = new Vector3D(props.get(1, 0), props.get(2, 0), props.get(3, 0));
  1865. targetObject.extra = parseUserAttributes();
  1866. }
  1867. _blocks[blockID].data = targetObject
  1868. if (_debug)
  1869. trace("Parsed a CommandBlock: Name = '" + name);
  1870. }
  1871. //blockID 254
  1872. private function parseNameSpace(blockID:uint):void
  1873. {
  1874. var id:uint = _newBlockBytes.readUnsignedByte();
  1875. var nameSpaceString:String = parseVarStr();
  1876. if (_debug)
  1877. trace("Parsed a NameSpaceBlock: ID = " + id + " | String = " + nameSpaceString);
  1878. }
  1879. //blockID 255
  1880. private function parseMetaData(blockID:uint):void
  1881. {
  1882. var props:AWDProperties = parseProperties({1:UINT32, 2:AWDSTRING, 3:AWDSTRING, 4:AWDSTRING, 5:AWDSTRING});
  1883. if (_debug) {
  1884. trace("Parsed a MetaDataBlock: TimeStamp = " + props.get(1, 0));
  1885. trace(" EncoderName = " + props.get(2, "unknown"));
  1886. trace(" EncoderVersion = " + props.get(3, "unknown"));
  1887. trace(" GeneratorName = " + props.get(4, "unknown"));
  1888. trace(" GeneratorVersion = " + props.get(5, "unknown"));
  1889. }
  1890. }
  1891. // Helper - functions
  1892. private function getUVForVertexAnimation(meshID:uint):Vector.<Vector.<Number>>
  1893. {
  1894. if (_blocks[meshID].data is Mesh)
  1895. meshID = _blocks[meshID].geoID;
  1896. if (_blocks[meshID].uvsForVertexAnimation)
  1897. return _blocks[meshID].uvsForVertexAnimation;
  1898. var geometry:Geometry = Geometry(_blocks[[meshID]].data);
  1899. var geoCnt:int = 0;
  1900. var ud:Vector.<Number>;
  1901. var uStride:uint;
  1902. var uOffs:uint;
  1903. var numPoints:uint;
  1904. var i:int;
  1905. var newUvs:Vector.<Number>;
  1906. _blocks[meshID].uvsForVertexAnimation = new Vector.<Vector.<Number>>;
  1907. while (geoCnt < geometry.subGeometries.length) {
  1908. newUvs = new Vector.<Number>;
  1909. numPoints = geometry.subGeometries[geoCnt].numVertices;
  1910. ud = geometry.subGeometries[geoCnt].UVData;
  1911. uStride = geometry.subGeometries[geoCnt].UVStride;
  1912. uOffs = geometry.subGeometries[geoCnt].UVOffset;
  1913. for (i = 0; i < numPoints; i++) {
  1914. newUvs.push(ud[uOffs + i*uStride + 0]);
  1915. newUvs.push(ud[uOffs + i*uStride + 1]);
  1916. }
  1917. _blocks[meshID].uvsForVertexAnimation.push(newUvs);
  1918. geoCnt++;
  1919. }
  1920. return _blocks[meshID].uvsForVertexAnimation;
  1921. }
  1922. private function parseVarStr():String
  1923. {
  1924. var len:uint = _newBlockBytes.readUnsignedShort();
  1925. return _newBlockBytes.readUTFBytes(len);
  1926. }
  1927. private function parseProperties(expected:Object):AWDProperties
  1928. {
  1929. var list_end:uint;
  1930. var list_len:uint;
  1931. var propertyCnt:uint = 0;
  1932. var props:AWDProperties = new AWDProperties();
  1933. list_len = _newBlockBytes.readUnsignedInt();
  1934. list_end = _newBlockBytes.position + list_len;
  1935. if (expected) {
  1936. while (_newBlockBytes.position < list_end) {
  1937. var len:uint;
  1938. var key:uint;
  1939. var type:uint;
  1940. key = _newBlockBytes.readUnsignedShort();
  1941. len = _newBlockBytes.readUnsignedInt();
  1942. if ((_newBlockBytes.position + len) > list_end) {
  1943. trace(" Error in reading property # " + propertyCnt + " = skipped to end of propertie-list");
  1944. _newBlockBytes.position = list_end;
  1945. return props;
  1946. }
  1947. if (expected.hasOwnProperty(key.toString())) {
  1948. type = expected[key];
  1949. props.set(key, parseAttrValue(type, len));
  1950. } else
  1951. _newBlockBytes.position += len;
  1952. propertyCnt += 1;
  1953. }
  1954. } else
  1955. _newBlockBytes.position = list_end;
  1956. return props;
  1957. }
  1958. private function parseUserAttributes():Object
  1959. {
  1960. var attributes:Object;
  1961. var list_len:uint;
  1962. var attibuteCnt:uint;
  1963. list_len = _newBlockBytes.readUnsignedInt();
  1964. if (list_len > 0) {
  1965. var list_end:uint;
  1966. attributes = {};
  1967. list_end = _newBlockBytes.position + list_len;
  1968. while (_newBlockBytes.position < list_end) {
  1969. var ns_id:uint;
  1970. var attr_key:String;
  1971. var attr_type:uint;
  1972. var attr_len:uint;
  1973. var attr_val:*;
  1974. // TODO: Properly tend to namespaces in attributes
  1975. ns_id = _newBlockBytes.readUnsignedByte();
  1976. attr_key = parseVarStr();
  1977. attr_type = _newBlockBytes.readUnsignedByte();
  1978. attr_len = _newBlockBytes.readUnsignedInt();
  1979. if ((_newBlockBytes.position + attr_len) > list_end) {
  1980. trace(" Error in reading attribute # " + attibuteCnt + " = skipped to end of attribute-list");
  1981. _newBlockBytes.position = list_end;
  1982. return attributes;
  1983. }
  1984. switch (attr_type) {
  1985. case AWDSTRING:
  1986. attr_val = _newBlockBytes.readUTFBytes(attr_len);
  1987. break;
  1988. case INT8:
  1989. attr_val = _newBlockBytes.readByte();
  1990. break;
  1991. case INT16:
  1992. attr_val = _newBlockBytes.readShort();
  1993. break;
  1994. case INT32:
  1995. attr_val = _newBlockBytes.readInt();
  1996. break;
  1997. case BOOL:
  1998. case UINT8:
  1999. attr_val = _newBlockBytes.readUnsignedByte();
  2000. break;
  2001. case UINT16:
  2002. attr_val = _newBlockBytes.readUnsignedShort();
  2003. break;
  2004. case UINT32:
  2005. case BADDR:
  2006. attr_val = _newBlockBytes.readUnsignedInt();
  2007. break;
  2008. case FLOAT32:
  2009. attr_val = _newBlockBytes.readFloat();
  2010. break;
  2011. case FLOAT64:
  2012. attr_val = _newBlockBytes.readDouble();
  2013. break;
  2014. default:
  2015. attr_val = 'unimplemented attribute type ' + attr_type;
  2016. _newBlockBytes.position += attr_len;
  2017. break;
  2018. }
  2019. if (_debug)
  2020. trace("attribute = name: " + attr_key + " / value = " + attr_val);
  2021. attributes[attr_key] = attr_val;
  2022. attibuteCnt += 1;
  2023. }
  2024. }
  2025. return attributes;
  2026. }
  2027. private function getDefaultMaterial():IAsset
  2028. {
  2029. if (!_defaultBitmapMaterial)
  2030. _defaultBitmapMaterial = DefaultMaterialManager.getDefaultMaterial();
  2031. return _defaultBitmapMaterial;
  2032. }
  2033. private function getDefaultTexture():IAsset
  2034. {
  2035. if (!_defaultTexture)
  2036. _defaultTexture = DefaultMaterialManager.getDefaultTexture();
  2037. return _defaultTexture;
  2038. }
  2039. private function getDefaultCubeTexture():IAsset
  2040. {
  2041. if (!_defaultCubeTexture) {
  2042. if (!_defaultTexture)
  2043. _defaultTexture = DefaultMaterialManager.getDefaultTexture();
  2044. var defaultBitmap:BitmapData = _defaultTexture.bitmapData;
  2045. _defaultCubeTexture = new BitmapCubeTexture(defaultBitmap, defaultBitmap, defaultBitmap, defaultBitmap, defaultBitmap, defaultBitmap);
  2046. _defaultCubeTexture.name = "defaultTexture";
  2047. }
  2048. return _defaultCubeTexture;
  2049. }
  2050. private function getDefaultAsset(assetType:String, extraTypeInfo:String):IAsset
  2051. {
  2052. switch (true) {
  2053. case (assetType == AssetType.TEXTURE):
  2054. if (extraTypeInfo == "CubeTexture")
  2055. return getDefaultCubeTexture();
  2056. if (extraTypeInfo == "SingleTexture")
  2057. return getDefaultTexture();
  2058. break;
  2059. case (assetType == AssetType.MATERIAL):
  2060. return getDefaultMaterial()
  2061. break;
  2062. default:
  2063. break;
  2064. }
  2065. return null;
  2066. }
  2067. private function getAssetByID(assetID:uint, assetTypesToGet:Array, extraTypeInfo:String = "SingleTexture"):Array
  2068. {
  2069. var returnArray:Array = new Array();
  2070. var typeCnt:int = 0;
  2071. if (assetID > 0) {
  2072. if (_blocks[assetID]) {
  2073. if (_blocks[assetID].data) {
  2074. while (typeCnt < assetTypesToGet.length) {
  2075. if (IAsset(_blocks[assetID].data).assetType == assetTypesToGet[typeCnt]) {
  2076. //if the right assetType was found
  2077. if ((assetTypesToGet[typeCnt] == AssetType.TEXTURE) && (extraTypeInfo == "CubeTexture")) {
  2078. if (_blocks[assetID].data is BitmapCubeTexture) {
  2079. returnArray.push(true);
  2080. returnArray.push(_blocks[assetID].data);
  2081. return returnArray;
  2082. }
  2083. }
  2084. if ((assetTypesToGet[typeCnt] == AssetType.TEXTURE) && (extraTypeInfo == "SingleTexture")) {
  2085. if (_blocks[assetID].data is BitmapTexture) {
  2086. returnArray.push(true);
  2087. returnArray.push(_blocks[assetID].data);
  2088. return returnArray;
  2089. }
  2090. } else {
  2091. returnArray.push(true);
  2092. returnArray.push(_blocks[assetID].data);
  2093. return returnArray;
  2094. }
  2095. }
  2096. if ((assetTypesToGet[typeCnt] == AssetType.GEOMETRY) && (IAsset(_blocks[assetID].data).assetType == AssetType.MESH)) {
  2097. returnArray.push(true);
  2098. returnArray.push(Mesh(_blocks[assetID].data).geometry);
  2099. return returnArray;
  2100. }
  2101. typeCnt++;
  2102. }
  2103. }
  2104. }
  2105. }
  2106. // if the function has not returned anything yet, the asset is not found, or the found asset is not the right type.
  2107. returnArray.push(false);
  2108. returnArray.push(getDefaultAsset(assetTypesToGet[0], extraTypeInfo));
  2109. return returnArray;
  2110. }
  2111. private function parseAttrValue(type:uint, len:uint):*
  2112. {
  2113. var elem_len:uint;
  2114. var read_func:Function;
  2115. switch (type) {
  2116. case BOOL:
  2117. case INT8:
  2118. elem_len = 1;
  2119. read_func = _newBlockBytes.readByte;
  2120. break;
  2121. case INT16:
  2122. elem_len = 2;
  2123. read_func = _newBlockBytes.readShort;
  2124. break;
  2125. case INT32:
  2126. elem_len = 4;
  2127. read_func = _newBlockBytes.readInt;
  2128. break;
  2129. case UINT8:
  2130. elem_len = 1;
  2131. read_func = _newBlockBytes.readUnsignedByte;
  2132. break;
  2133. case UINT16:
  2134. elem_len = 2;
  2135. read_func = _newBlockBytes.readUnsignedShort;
  2136. break;
  2137. case UINT32:
  2138. case COLOR:
  2139. case BADDR:
  2140. elem_len = 4;
  2141. read_func = _newBlockBytes.readUnsignedInt;
  2142. break;
  2143. case FLOAT32:
  2144. elem_len = 4;
  2145. read_func = _newBlockBytes.readFloat;
  2146. break;
  2147. case FLOAT64:
  2148. elem_len = 8;
  2149. read_func = _newBlockBytes.readDouble;
  2150. break;
  2151. case AWDSTRING:
  2152. return _newBlockBytes.readUTFBytes(len);
  2153. case VECTOR2x1:
  2154. case VECTOR3x1:
  2155. case VECTOR4x1:
  2156. case MTX3x2:
  2157. case MTX3x3:
  2158. case MTX4x3:
  2159. case MTX4x4:
  2160. elem_len = 8;
  2161. read_func = _newBlockBytes.readDouble;
  2162. break;
  2163. }
  2164. if (elem_len < len) {
  2165. var list:Array;
  2166. var num_read:uint;
  2167. var num_elems:uint;
  2168. list = [];
  2169. num_read = 0;
  2170. num_elems = len/elem_len;
  2171. while (num_read < num_elems) {
  2172. list.push(read_func());
  2173. num_read++;
  2174. }
  2175. return list;
  2176. } else {
  2177. var val:*;
  2178. val = read_func();
  2179. return val;
  2180. }
  2181. }
  2182. private function parseMatrix2D():Matrix
  2183. {
  2184. var mtx:Matrix;
  2185. var mtx_raw:Vector.<Number> = parseMatrix32RawData();
  2186. mtx = new Matrix(mtx_raw[0], mtx_raw[1], mtx_raw[2], mtx_raw[3], mtx_raw[4], mtx_raw[5]);
  2187. return mtx;
  2188. }
  2189. private function parseMatrix3D():Matrix3D
  2190. {
  2191. return new Matrix3D(parseMatrix43RawData());
  2192. }
  2193. private function parseMatrix32RawData():Vector.<Number>
  2194. {
  2195. var i:uint;
  2196. var mtx_raw:Vector.<Number> = new Vector.<Number>(6, true);
  2197. for (i = 0; i < 6; i++)
  2198. mtx_raw[i] = _newBlockBytes.readFloat();
  2199. return mtx_raw;
  2200. }
  2201. private function readNumber(precision:Boolean = false):Number
  2202. {
  2203. if (precision)
  2204. return _newBlockBytes.readDouble();
  2205. return _newBlockBytes.readFloat();
  2206. }
  2207. private function parseMatrix43RawData():Vector.<Number>
  2208. {
  2209. var mtx_raw:Vector.<Number> = new Vector.<Number>(16, true);
  2210. mtx_raw[0] = readNumber(_accuracyMatrix);
  2211. mtx_raw[1] = readNumber(_accuracyMatrix);
  2212. mtx_raw[2] = readNumber(_accuracyMatrix);
  2213. mtx_raw[3] = 0.0;
  2214. mtx_raw[4] = readNumber(_accuracyMatrix);
  2215. mtx_raw[5] = readNumber(_accuracyMatrix);
  2216. mtx_raw[6] = readNumber(_accuracyMatrix);
  2217. mtx_raw[7] = 0.0;
  2218. mtx_raw[8] = readNumber(_accuracyMatrix);
  2219. mtx_raw[9] = readNumber(_accuracyMatrix);
  2220. mtx_raw[10] = readNumber(_accuracyMatrix);
  2221. mtx_raw[11] = 0.0;
  2222. mtx_raw[12] = readNumber(_accuracyMatrix);
  2223. mtx_raw[13] = readNumber(_accuracyMatrix);
  2224. mtx_raw[14] = readNumber(_accuracyMatrix);
  2225. mtx_raw[15] = 1.0;
  2226. //TODO: fix max exporter to remove NaN values in joint 0 inverse bind pose
  2227. if (isNaN(mtx_raw[0])) {
  2228. mtx_raw[0] = 1;
  2229. mtx_raw[1] = 0;
  2230. mtx_raw[2] = 0;
  2231. mtx_raw[4] = 0;
  2232. mtx_raw[5] = 1;
  2233. mtx_raw[6] = 0;
  2234. mtx_raw[8] = 0;
  2235. mtx_raw[9] = 0;
  2236. mtx_raw[10] = 1;
  2237. mtx_raw[12] = 0;
  2238. mtx_raw[13] = 0;
  2239. mtx_raw[14] = 0;
  2240. }
  2241. return mtx_raw;
  2242. }
  2243. }
  2244. }
  2245. import flash.utils.ByteArray;
  2246. internal class AWDBlock
  2247. {
  2248. public var id:uint;
  2249. public var name:String;
  2250. public var data:*;
  2251. public var len:*;
  2252. public var geoID:uint;
  2253. public var extras:Object;
  2254. public var bytes:ByteArray;
  2255. public var errorMessages:Vector.<String>;
  2256. public var uvsForVertexAnimation:Vector.<Vector.<Number>>;
  2257. public function AWDBlock()
  2258. {
  2259. }
  2260. public function addError(errorMsg:String):void
  2261. {
  2262. if (!errorMessages)
  2263. errorMessages = new Vector.<String>();
  2264. errorMessages.push(errorMsg);
  2265. }
  2266. }
  2267. internal class bitFlags
  2268. {
  2269. public static const FLAG1:uint = 1;
  2270. public static const FLAG2:uint = 2;
  2271. public static const FLAG3:uint = 4;
  2272. public static const FLAG4:uint = 8;
  2273. public static const FLAG5:uint = 16;
  2274. public static const FLAG6:uint = 32;
  2275. public static const FLAG7:uint = 64;
  2276. public static const FLAG8:uint = 128;
  2277. public static const FLAG9:uint = 256;
  2278. public static const FLAG10:uint = 512;
  2279. public static const FLAG11:uint = 1024;
  2280. public static const FLAG12:uint = 2048;
  2281. public static const FLAG13:uint = 4096;
  2282. public static const FLAG14:uint = 8192;
  2283. public static const FLAG15:uint = 16384;
  2284. public static const FLAG16:uint = 32768;
  2285. public static function test(flags:uint, testFlag:uint):Boolean
  2286. {
  2287. return (flags & testFlag) == testFlag;
  2288. }
  2289. }
  2290. internal dynamic class AWDProperties
  2291. {
  2292. public function set(key:uint, value:*):void
  2293. {
  2294. this[key.toString()] = value;
  2295. }
  2296. public function get(key:uint, fallback:*):*
  2297. {
  2298. if (this.hasOwnProperty(key.toString()))
  2299. return this[key.toString()];
  2300. else
  2301. return fallback;
  2302. }
  2303. }