Anime.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /**
  2. * 魔力宝贝图档解析脚本 - CGTool
  3. *
  4. * @Author HonorLee (dev@honorlee.me)
  5. * @Version 1.0 (2023-04-15)
  6. * @License GPL-3.0
  7. *
  8. * Anime.cs 动画基础类
  9. */
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using UnityEngine;
  15. namespace CrossgateToolkit
  16. {
  17. //动画信息
  18. public class AnimeInfo
  19. {
  20. // 版本
  21. public string Version;
  22. //4 bytes 动画序号
  23. public uint Serial;
  24. //4 bytes 动画文件地址
  25. public uint Addr;
  26. //2 bytes 动作数量
  27. public int ActionCount;
  28. //2 bytes 未知字节
  29. public byte[] Unknow;
  30. //动画数据 Direction -> ActionType -> AnimeData
  31. public Dictionary<int, Dictionary<int, AnimeDetail>> AnimeDatas = new Dictionary<int, Dictionary<int, AnimeDetail>>();
  32. }
  33. //动画帧数据
  34. public class AnimeFrameInfo
  35. {
  36. //图档编号
  37. public uint GraphicIndex;
  38. //宽度
  39. public int Width;
  40. //高度
  41. public int Height;
  42. //偏移X
  43. public int OffsetX;
  44. //偏移Y
  45. public int OffsetY;
  46. //音效编号
  47. public int AudioIndex;
  48. //动效编号
  49. public Anime.EffectType Effect;
  50. //GraphicInfo;
  51. public GraphicInfoData GraphicInfo;
  52. //动画Sprite
  53. public Dictionary<int,Sprite> AnimeSprites = new Dictionary<int, Sprite>();
  54. }
  55. //动画数据
  56. public class AnimeDetail
  57. {
  58. // 动画编号
  59. public uint Serial;
  60. // 动画版本
  61. public string Version;
  62. // 方向
  63. public int Direction;
  64. // 动作
  65. public int ActionType;
  66. // 动画循环时间
  67. public uint CycleTime;
  68. // 帧数
  69. public uint FrameCount;
  70. // 高版本 - 标识
  71. public bool IsHighVersion;
  72. // 高版本 - 调色板
  73. public int Palet;
  74. // 高版本 - 图像反转
  75. public AnimeFlag FLAG;
  76. // 高版本 - 结束标识
  77. public byte[] FLAG_END;
  78. public Dictionary<int,Texture2D> AnimeTextures = new Dictionary<int, Texture2D>();
  79. // public Texture2D AnimeTexture;
  80. public List<AnimeFrameInfo> AnimeFrameInfos;
  81. // public byte[] unknown;
  82. }
  83. public class AnimeFlag
  84. {
  85. public bool REVERSE_X;
  86. public bool REVERSE_Y;
  87. public bool LOCK_PAL;
  88. public bool LIGHT_THROUGH;
  89. }
  90. //动画相关Enum类型
  91. public class Anime : MonoBehaviour
  92. {
  93. //方向
  94. public enum DirectionType
  95. {
  96. NULL = -1,
  97. North = 0,
  98. NorthEast = 1,
  99. East = 2,
  100. SouthEast = 3,
  101. South = 4,
  102. SouthWest = 5,
  103. West = 6,
  104. NorthWest = 7
  105. }
  106. //方向九宫映射表
  107. public static DirectionType[,] DirectionTypeMap = new DirectionType[3,3]
  108. {
  109. {DirectionType.North,DirectionType.NorthEast,DirectionType.East},
  110. {DirectionType.NorthWest,DirectionType.NULL,DirectionType.SouthEast},
  111. {DirectionType.West,DirectionType.SouthWest,DirectionType.South}
  112. };
  113. //动作
  114. public enum ActionType
  115. {
  116. NULL = -1,
  117. Stand = 0,
  118. Walk = 1,
  119. BeforeRun = 2,
  120. Run = 3,
  121. AfterRun = 4,
  122. Attack = 5,
  123. Magic = 6,
  124. Throw = 7,
  125. Hurt = 8,
  126. Defence = 9,
  127. Dead = 10,
  128. Sit = 11,
  129. Hi = 12,
  130. Happy = 13,
  131. Angry = 14,
  132. Sad = 15,
  133. Shake = 16,
  134. Rock = 17,
  135. Scissors = 18,
  136. Paper = 19,
  137. Fishing = 20,
  138. }
  139. //动效
  140. public enum EffectType
  141. {
  142. Hit =1,
  143. HitOver =2
  144. }
  145. public enum PlayType
  146. {
  147. Loop,
  148. Once,
  149. OnceAndDestroy
  150. }
  151. private static byte[] highVersionFlag = { 0xFF, 0xFF, 0xFF, 0xFF };
  152. //动画列表缓存 Serial -> AnimeInfo
  153. private static Dictionary<uint, AnimeInfo> _animeInfoCache = new Dictionary<uint, AnimeInfo>();
  154. //加载动画数据
  155. public static void Init(string Version,FileInfo animeInfoFile,FileInfo animeFile)
  156. {
  157. //创建流读取器
  158. FileStream infoFileStream = animeInfoFile.OpenRead();
  159. FileStream dataFileStream = animeFile.OpenRead();
  160. BinaryReader infoFileReader = new BinaryReader(infoFileStream);
  161. BinaryReader dataFileReader = new BinaryReader(dataFileStream);
  162. long DataLength = infoFileStream.Length / 12;
  163. // 循环初始化动画数据
  164. for (int i = 0; i < DataLength; i++)
  165. {
  166. //初始化对象
  167. AnimeInfo animeInfo = new AnimeInfo();
  168. animeInfo.Version = Version;
  169. animeInfo.Serial = BitConverter.ToUInt32(infoFileReader.ReadBytes(4),0);
  170. // Debug.Log(animeInfo.Serial);
  171. animeInfo.Addr = BitConverter.ToUInt32(infoFileReader.ReadBytes(4),0);
  172. animeInfo.ActionCount = infoFileReader.ReadUInt16();
  173. animeInfo.Unknow = infoFileReader.ReadBytes(2);
  174. if (animeInfo.Addr > dataFileStream.Length) break;
  175. dataFileStream.Position = animeInfo.Addr;
  176. for (int j = 0; j < animeInfo.ActionCount; j++)
  177. {
  178. // 高版本标识
  179. bool isHighVersion = false;
  180. dataFileStream.Position += 16;
  181. if(dataFileReader.ReadBytes(4).SequenceEqual(highVersionFlag)) isHighVersion = true;
  182. dataFileStream.Position -= 20;
  183. AnimeDetail animeData = new AnimeDetail();
  184. animeData.Version = Version;
  185. animeData.Serial = animeInfo.Serial;
  186. animeData.Direction = dataFileReader.ReadUInt16();
  187. animeData.ActionType = dataFileReader.ReadUInt16();
  188. animeData.CycleTime = BitConverter.ToUInt32(dataFileReader.ReadBytes(4),0);
  189. animeData.FrameCount = BitConverter.ToUInt32(dataFileReader.ReadBytes(4),0);
  190. // 高版本
  191. if (isHighVersion)
  192. {
  193. animeData.IsHighVersion = true;
  194. animeData.Palet = dataFileReader.ReadUInt16();
  195. int flag = dataFileReader.ReadUInt16();
  196. if (flag > 0)
  197. {
  198. animeData.FLAG = new AnimeFlag();
  199. if ((flag & 1) == (1 << 0)) animeData.FLAG.REVERSE_X = true;
  200. if ((flag & 2) == (1 << 1)) animeData.FLAG.REVERSE_Y = true;
  201. if ((flag & 4) == (1 << 2)) animeData.FLAG.LOCK_PAL = true;
  202. if ((flag & 8) == (1 << 3)) animeData.FLAG.LIGHT_THROUGH = true;
  203. }
  204. animeData.FLAG_END = dataFileReader.ReadBytes(4);
  205. }
  206. animeData.AnimeFrameInfos = new List<AnimeFrameInfo>();
  207. // if (animeInfo.Index == 101201) Debug.Log("----------------------------------");
  208. for (int k = 0; k < animeData.FrameCount; k++)
  209. {
  210. byte[] frameBytes = dataFileReader.ReadBytes(10);
  211. if (frameBytes.Length <10) break;
  212. BinaryReader frameReader = new BinaryReader(new MemoryStream(frameBytes));
  213. AnimeFrameInfo animeFrameInfo = new AnimeFrameInfo();
  214. //GraphicIndex序号
  215. animeFrameInfo.GraphicIndex = BitConverter.ToUInt32(frameReader.ReadBytes(4),0);
  216. animeFrameInfo.OffsetX = BitConverter.ToInt16(frameReader.ReadBytes(2),0);
  217. animeFrameInfo.OffsetY = BitConverter.ToInt16(frameReader.ReadBytes(2), 0);
  218. //标识位
  219. int flag = BitConverter.ToInt16(frameReader.ReadBytes(2),0);
  220. if (flag>20000)
  221. {
  222. //击打判定
  223. animeFrameInfo.Effect = EffectType.Hit;
  224. animeFrameInfo.AudioIndex = flag - 20000;
  225. }
  226. else if(flag>10000)
  227. {
  228. //攻击动作结束判定
  229. animeFrameInfo.Effect = EffectType.HitOver;
  230. animeFrameInfo.AudioIndex = flag - 10000;
  231. }
  232. else
  233. {
  234. animeFrameInfo.AudioIndex = flag;
  235. }
  236. animeData.AnimeFrameInfos.Add(animeFrameInfo);
  237. }
  238. animeData.FrameCount = (uint) animeData.AnimeFrameInfos.Count;
  239. if (!animeInfo.AnimeDatas.ContainsKey(animeData.Direction))
  240. animeInfo.AnimeDatas.Add(animeData.Direction, new Dictionary<int, AnimeDetail>());
  241. animeInfo.AnimeDatas[animeData.Direction][animeData.ActionType] = animeData;
  242. _animeInfoCache[animeInfo.Serial] = animeInfo;
  243. }
  244. }
  245. infoFileReader.Dispose();
  246. infoFileReader.Close();
  247. dataFileReader.Dispose();
  248. dataFileReader.Close();
  249. infoFileStream.Close();
  250. dataFileStream.Close();
  251. Debug.Log("[CGTool] 加载AnimeInfo - 文件: [" +
  252. // (Graphic.Flag_HighVersion[Version] ? "H" : "N") + "] [" +
  253. Version + "] " +
  254. animeInfoFile.Name +
  255. " 动画总量: " + DataLength);
  256. }
  257. //获取动画数据信息
  258. public static AnimeInfo GetAnimeInfo(uint serial)
  259. {
  260. _animeInfoCache.TryGetValue(serial, out var animeInfo);
  261. return animeInfo;
  262. }
  263. //获取动画数据
  264. public static AnimeDetail GetAnimeDetail(uint serial,DirectionType Direction,ActionType Action)
  265. {
  266. AnimeInfo animeInfo = GetAnimeInfo(serial);
  267. if (animeInfo == null) return null;
  268. if (animeInfo.AnimeDatas.ContainsKey((int)Direction))
  269. {
  270. if (animeInfo.AnimeDatas[(int) Direction].ContainsKey((int) Action))
  271. {
  272. AnimeDetail animeDetail = animeInfo.AnimeDatas[(int) Direction][(int) Action];
  273. // if(animeDetail.AnimeTexture == null) prepareAnimeFrames(animeDetail);
  274. return animeDetail;
  275. }
  276. }
  277. return null;
  278. }
  279. //预处理动画图形合批烘焙
  280. public static void BakeAnimeFrames(AnimeDetail animeDetail,int palet = 0)
  281. {
  282. if(animeDetail.AnimeTextures.ContainsKey(palet)) return;
  283. //所有帧的图形数据
  284. GraphicDetail[] graphicDetails = new GraphicDetail[animeDetail.FrameCount];
  285. //合并后的Texture2D尺寸
  286. uint textureWidth = 0;
  287. uint textureHeight = 0;
  288. for (var i = 0; i < animeDetail.FrameCount; i++)
  289. {
  290. //载入图档
  291. GraphicInfoData graphicInfoData = GraphicInfo.GetGraphicInfoDataByIndex(animeDetail.Version,animeDetail.AnimeFrameInfos[i].GraphicIndex);
  292. if (graphicInfoData == null) continue;
  293. int subPaletIndex = 0;
  294. if (animeDetail.IsHighVersion) subPaletIndex = (int)animeDetail.Serial;
  295. GraphicDetail graphicDetail = GraphicData.GetGraphicDetail(graphicInfoData, palet, subPaletIndex);
  296. if(graphicDetail == null) continue;
  297. graphicDetails[i] = graphicDetail;
  298. if(graphicDetail.Height > textureHeight) textureHeight = graphicDetail.Height;
  299. textureWidth += graphicDetail.Width + 5;
  300. animeDetail.AnimeFrameInfos[i].Width = (int) graphicDetail.Width;
  301. animeDetail.AnimeFrameInfos[i].Height = (int) graphicDetail.Height;
  302. animeDetail.AnimeFrameInfos[i].OffsetX = (int) graphicInfoData.OffsetX;
  303. animeDetail.AnimeFrameInfos[i].OffsetY = (int) graphicInfoData.OffsetY;
  304. animeDetail.AnimeFrameInfos[i].GraphicInfo = graphicInfoData;
  305. }
  306. //合并图档
  307. Texture2D texture2dMix = new Texture2D((int) textureWidth, (int) textureHeight, TextureFormat.RGBA4444, false,false);
  308. texture2dMix.filterMode = FilterMode.Point;
  309. Color32 transparentColor = new Color32(0, 0, 0, 0);
  310. Color32[] transparentColors = new Color32[texture2dMix.width * texture2dMix.height];
  311. for (var i = 0; i < transparentColors.Length; i++)
  312. {
  313. transparentColors[i] = transparentColor;
  314. }
  315. texture2dMix.SetPixels32(transparentColors,0);
  316. int offsetX = 0;
  317. for (var i = 0; i < animeDetail.FrameCount; i++)
  318. {
  319. GraphicDetail graphicDetail = graphicDetails[i];
  320. if(graphicDetail == null) continue;
  321. texture2dMix.SetPixels32((int) offsetX, 0, (int) graphicDetail.Width,
  322. (int) graphicDetail.Height,
  323. graphicDetail.Sprite.texture.GetPixels32());
  324. offsetX += (int) graphicDetail.Width + 5;
  325. }
  326. texture2dMix.Apply();
  327. animeDetail.AnimeTextures.Add(palet,texture2dMix);
  328. //创建动画每帧Sprite
  329. offsetX = 0;
  330. for (var l = 0; l < animeDetail.FrameCount; l++)
  331. {
  332. if(graphicDetails[l] == null) continue;
  333. AnimeFrameInfo animeFrameInfo = animeDetail.AnimeFrameInfos[l];
  334. Vector2 pivot = new Vector2(0f, 1f);
  335. pivot.x += -(animeFrameInfo.OffsetX * 1f) / animeFrameInfo.Width;
  336. pivot.y -= (-animeFrameInfo.OffsetY * 1f) / animeFrameInfo.Height;
  337. Sprite sprite = Sprite.Create(texture2dMix, new Rect(offsetX, 0,
  338. animeDetail.AnimeFrameInfos[l].Width, animeDetail.AnimeFrameInfos[l].Height),
  339. pivot, 1, 1, SpriteMeshType.FullRect);
  340. offsetX += animeDetail.AnimeFrameInfos[l].Width + 5;
  341. animeFrameInfo.AnimeSprites.Add(palet, sprite);
  342. }
  343. }
  344. }
  345. }