VoxelFrameAnimationObject.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using System;
  4. using System.IO;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. namespace VoxelImporter
  8. {
  9. [AddComponentMenu("Voxel Importer/Voxel Frame Animation Object")]
  10. [ExecuteInEditMode, RequireComponent(typeof(MeshFilter)), RequireComponent(typeof(MeshRenderer))]
  11. public class VoxelFrameAnimationObject : VoxelBase
  12. {
  13. [Serializable]
  14. public class FrameData
  15. {
  16. #if UNITY_EDITOR
  17. public string voxelFilePath;
  18. public UnityEngine.Object voxelFileObject;
  19. public int voxelFileSubIndex;
  20. public FileType fileType;
  21. public Vector3 localOffset;
  22. [NonSerialized]
  23. public VoxelData voxelData;
  24. [NonSerialized]
  25. public long voxelDataCreatedVoxelFileTimeTicks;
  26. public DisableData disableData;
  27. public List<MaterialData> materialData;
  28. [NonSerialized]
  29. public Texture2D icon;
  30. #endif
  31. public string name;
  32. public Mesh mesh;
  33. public List<int> materialIndexes;
  34. }
  35. public List<FrameData> frames;
  36. public Mesh mesh;
  37. public List<Material> materials;
  38. public Texture2D atlasTexture;
  39. public Material playMaterial0;
  40. public Material playMaterial1;
  41. public Material playMaterial2;
  42. public Material playMaterial3;
  43. public Material playMaterial4;
  44. public Material playMaterial5;
  45. public Material playMaterial6;
  46. public Material playMaterial7;
  47. //Play
  48. private MeshFilter meshFilterCache;
  49. private Mesh filterMesh;
  50. private MeshRenderer meshRendererCache;
  51. private Material[] rendererMaterials;
  52. private Material rendererMaterial0;
  53. private Material rendererMaterial1;
  54. private Material rendererMaterial2;
  55. private Material rendererMaterial3;
  56. private Material rendererMaterial4;
  57. private Material rendererMaterial5;
  58. private Material rendererMaterial6;
  59. private Material rendererMaterial7;
  60. void Awake()
  61. {
  62. meshFilterCache = GetComponent<MeshFilter>();
  63. filterMesh = null;
  64. meshRendererCache = GetComponent<MeshRenderer>();
  65. rendererMaterials = null;
  66. }
  67. void LateUpdate()
  68. {
  69. #region Mesh
  70. if(filterMesh != mesh)
  71. meshFilterCache.sharedMesh = filterMesh = mesh;
  72. #endregion
  73. #region Material
  74. if (rendererMaterial0 != playMaterial0 ||
  75. rendererMaterial1 != playMaterial1 ||
  76. rendererMaterial2 != playMaterial2 ||
  77. rendererMaterial3 != playMaterial3 ||
  78. rendererMaterial4 != playMaterial4 ||
  79. rendererMaterial5 != playMaterial5 ||
  80. rendererMaterial6 != playMaterial6 ||
  81. rendererMaterial7 != playMaterial7)
  82. {
  83. int count = 0;
  84. if (playMaterial0 != null) count++;
  85. if (playMaterial1 != null) count++;
  86. if (playMaterial2 != null) count++;
  87. if (playMaterial3 != null) count++;
  88. if (playMaterial4 != null) count++;
  89. if (playMaterial5 != null) count++;
  90. if (playMaterial6 != null) count++;
  91. if (playMaterial7 != null) count++;
  92. //
  93. if (rendererMaterials == null || rendererMaterials.Length != count)
  94. rendererMaterials = new Material[count];
  95. //
  96. int i = 0;
  97. if (playMaterial0 != null) rendererMaterials[i++] = playMaterial0;
  98. if (playMaterial1 != null) rendererMaterials[i++] = playMaterial1;
  99. if (playMaterial2 != null) rendererMaterials[i++] = playMaterial2;
  100. if (playMaterial3 != null) rendererMaterials[i++] = playMaterial3;
  101. if (playMaterial4 != null) rendererMaterials[i++] = playMaterial4;
  102. if (playMaterial5 != null) rendererMaterials[i++] = playMaterial5;
  103. if (playMaterial6 != null) rendererMaterials[i++] = playMaterial6;
  104. if (playMaterial7 != null) rendererMaterials[i++] = playMaterial7;
  105. //
  106. if (rendererMaterials.Length == 0)
  107. meshRendererCache.sharedMaterial = null;
  108. else
  109. meshRendererCache.sharedMaterials = rendererMaterials;
  110. //
  111. rendererMaterial0 = playMaterial0;
  112. rendererMaterial1 = playMaterial1;
  113. rendererMaterial2 = playMaterial2;
  114. rendererMaterial3 = playMaterial3;
  115. rendererMaterial4 = playMaterial4;
  116. rendererMaterial5 = playMaterial5;
  117. rendererMaterial6 = playMaterial6;
  118. rendererMaterial7 = playMaterial7;
  119. }
  120. #endregion
  121. }
  122. public void ClearFrame()
  123. {
  124. mesh = null;
  125. playMaterial0 = null;
  126. playMaterial1 = null;
  127. playMaterial2 = null;
  128. playMaterial3 = null;
  129. playMaterial4 = null;
  130. playMaterial5 = null;
  131. playMaterial6 = null;
  132. playMaterial7 = null;
  133. }
  134. public bool ChangeFrame(string frameName)
  135. {
  136. if (!string.IsNullOrEmpty(frameName))
  137. {
  138. foreach (var frame in frames)
  139. {
  140. if (frame.name == frameName)
  141. {
  142. ClearFrame();
  143. mesh = frame.mesh;
  144. {
  145. for (int i = 0; i < frame.materialIndexes.Count; i++)
  146. {
  147. var index = frame.materialIndexes[i];
  148. switch (i)
  149. {
  150. case 0: playMaterial0 = materials[index]; break;
  151. case 1: playMaterial1 = materials[index]; break;
  152. case 2: playMaterial2 = materials[index]; break;
  153. case 3: playMaterial3 = materials[index]; break;
  154. case 4: playMaterial4 = materials[index]; break;
  155. case 5: playMaterial5 = materials[index]; break;
  156. case 6: playMaterial6 = materials[index]; break;
  157. case 7: playMaterial7 = materials[index]; break;
  158. default:
  159. Debug.LogWarningFormat("<color=green>[Voxel Importer]</color> Play material count over. <color=red>{0}/{1}</color>", frame.materialIndexes.Count, 8);
  160. break;
  161. }
  162. }
  163. }
  164. return true;
  165. }
  166. }
  167. }
  168. Debug.LogWarningFormat("<color=green>[Voxel Importer]</color> Frame not found. <color=red>{0}</color>", frameName);
  169. return false;
  170. }
  171. #if UNITY_EDITOR
  172. public override bool EditorInitialize()
  173. {
  174. var result = base.EditorInitialize();
  175. //ver1.1.2
  176. if (frames != null)
  177. {
  178. foreach (var frame in frames)
  179. {
  180. if (frame.voxelFileObject == null && !string.IsNullOrEmpty(frame.voxelFilePath) && frame.voxelFilePath.Contains("Assets/"))
  181. {
  182. var assetPath = frame.voxelFilePath.Substring(frame.voxelFilePath.IndexOf("Assets/"));
  183. var fullPath = Application.dataPath + "/" + assetPath.Remove(0, "Assets/".Length);
  184. if (File.Exists(fullPath))
  185. {
  186. frame.voxelFileObject = UnityEditor.AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetPath);
  187. }
  188. }
  189. }
  190. }
  191. //ver1.1.4
  192. if (dataVersion < 114)
  193. {
  194. if (frames != null)
  195. {
  196. foreach (var frame in frames)
  197. {
  198. if (string.IsNullOrEmpty(frame.name))
  199. {
  200. if (frames.FindIndex((x) => x != frame && x.voxelFilePath == frame.voxelFilePath) >= 0)
  201. {
  202. frame.name = Edit_GetUniqueFrameName(string.Format("{0}_{1}", Path.GetFileNameWithoutExtension(frame.voxelFilePath), frame.voxelFileSubIndex));
  203. }
  204. else
  205. {
  206. frame.name = Edit_GetUniqueFrameName(Path.GetFileNameWithoutExtension(frame.voxelFilePath));
  207. }
  208. }
  209. if (enableFaceFlags != FaceAllFlags && voxelData != null)
  210. {
  211. frame.disableData = new DisableData();
  212. var face = ~enableFaceFlags;
  213. foreach (var voxel in frame.voxelData.voxels)
  214. {
  215. var visible = voxel.visible & face;
  216. if (visible == 0) continue;
  217. frame.disableData.SetDisable(voxel.position, visible);
  218. }
  219. }
  220. }
  221. }
  222. }
  223. return result;
  224. }
  225. [NonSerialized]
  226. public bool edit_animationFoldout = true;
  227. [NonSerialized]
  228. public int edit_frameIndex = -1;
  229. public bool edit_frameEnable { get { return frames != null && edit_frameIndex >= 0 && edit_frameIndex < frames.Count && frames[edit_frameIndex] != null; } }
  230. public FrameData edit_currentFrame { get { return edit_frameEnable ? frames[edit_frameIndex] : null; } }
  231. public enum Edit_CameraMode
  232. {
  233. forward,
  234. back,
  235. up,
  236. down,
  237. right,
  238. left,
  239. }
  240. [NonSerialized]
  241. public Edit_CameraMode edit_previewCameraMode;
  242. public void Edit_SetFrameCurrentVoxelOtherData()
  243. {
  244. if (!edit_frameEnable) return;
  245. voxelData = edit_currentFrame.voxelData;
  246. disableData = edit_currentFrame.disableData;
  247. materialData = edit_currentFrame.materialData;
  248. materialIndexes = edit_currentFrame.materialIndexes;
  249. }
  250. public void Edit_ClearPlayMaterials()
  251. {
  252. playMaterial0 = null;
  253. playMaterial1 = null;
  254. playMaterial2 = null;
  255. playMaterial3 = null;
  256. playMaterial4 = null;
  257. playMaterial5 = null;
  258. playMaterial6 = null;
  259. playMaterial7 = null;
  260. }
  261. public void Edit_SetPlayMaterials(Material[] mats)
  262. {
  263. Edit_ClearPlayMaterials();
  264. if (mats.Length > 0) playMaterial0 = mats[0];
  265. if (mats.Length > 1) playMaterial1 = mats[1];
  266. if (mats.Length > 2) playMaterial2 = mats[2];
  267. if (mats.Length > 3) playMaterial3 = mats[3];
  268. if (mats.Length > 4) playMaterial4 = mats[4];
  269. if (mats.Length > 5) playMaterial5 = mats[5];
  270. if (mats.Length > 6) playMaterial6 = mats[6];
  271. if (mats.Length > 7) playMaterial7 = mats[7];
  272. if (mats.Length > 8)
  273. {
  274. Debug.LogWarningFormat("<color=green>[Voxel Importer]</color> Play material count over. <color=red>{0}/{1}</color>", mats.Length, 8);
  275. }
  276. }
  277. public string Edit_GetUniqueFrameName(string name)
  278. {
  279. var result = name;
  280. bool unique;
  281. do
  282. {
  283. unique = true;
  284. foreach (var frame in frames)
  285. {
  286. if (frame.name == result)
  287. {
  288. result += " 0";
  289. unique = false;
  290. break;
  291. }
  292. }
  293. } while (!unique);
  294. return result;
  295. }
  296. #region Asset
  297. public override bool IsUseAssetObject(UnityEngine.Object obj)
  298. {
  299. if (mesh == obj) return true;
  300. if (materials != null)
  301. {
  302. for (int i = 0; i < materials.Count; i++)
  303. {
  304. if (materials[i] == obj) return true;
  305. }
  306. }
  307. if (atlasTexture == obj) return true;
  308. if (frames != null)
  309. {
  310. for (int i = 0; i < frames.Count; i++)
  311. {
  312. if (frames[i] == null) continue;
  313. if (frames[i].mesh == obj) return true;
  314. }
  315. }
  316. return false;
  317. }
  318. #endregion
  319. #region Undo
  320. public class RefreshCheckerFrameAnimation : RefreshChecker
  321. {
  322. public RefreshCheckerFrameAnimation(VoxelFrameAnimationObject voxelObject) : base(voxelObject)
  323. {
  324. controllerFrameAnimation = voxelObject;
  325. }
  326. public VoxelFrameAnimationObject controllerFrameAnimation;
  327. public override void Save()
  328. {
  329. base.Save();
  330. }
  331. public override bool Check()
  332. {
  333. if (base.Check())
  334. return true;
  335. return false;
  336. }
  337. }
  338. #endregion
  339. #endif
  340. }
  341. }