TC.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. namespace TerrainComposer2
  6. {
  7. static public class TC
  8. {
  9. static private int refreshOutputReferences;
  10. static public bool refreshPreviewImages;
  11. static public bool repaintNodeWindow;
  12. static public List<MessageCommand> messages = new List<MessageCommand>();
  13. static public float autoGenerateCallTimeStart;
  14. public const int splatLimit = 16;
  15. public const int grassLimit = 16;
  16. public const int heightOutput = 0;
  17. public const int splatOutput = 1;
  18. public const int colorOutput = 2;
  19. public const int treeOutput = 3;
  20. public const int grassOutput = 4;
  21. public const int objectOutput = 5;
  22. public const int allOutput = 6;
  23. public const int nodeLabelLength = 19;
  24. static readonly public string[] outputNames = new string[6] { "Height", "Splat", "Color", "Tree", "Grass", "Object" };
  25. static readonly public string[] colChannelNames = new string[4] { "Red", "Green", "Blue", "Alpha" };
  26. static readonly public string[] colChannelNamesLowerCase = new string[4] { "red", "green", "blue", "alpha" };
  27. static readonly public Color[] colChannel = new Color[4] { new Color(1, 0.60f, 0.60f, 1), new Color(0.60f, 1, 0.60f, 1), new Color(0.60f, 0.60f, 1, 1), new Color(1, 1, 1, 1) };
  28. static public string installPath;
  29. static public string fullInstallPath;
  30. static public Type FindRTP()
  31. {
  32. Type t = Type.GetType("ReliefTerrain");
  33. TC_Settings.instance.isRTPDetected = t != null ? true : false;
  34. return t;
  35. }
  36. static public float GetVersionNumber()
  37. {
  38. return 2.6f;
  39. }
  40. static public int OutputNameToOutputID(string outputName)
  41. {
  42. for (int i = 0; i < outputNames.Length; i++) if (outputName == outputNames[i]) return i;
  43. return -1;
  44. }
  45. static public void AutoGenerate(bool waitForNextFrame = true)
  46. {
  47. AutoGenerate (new Rect (0,0,1,1), waitForNextFrame);
  48. }
  49. static public void AutoGenerate(Rect generateRect, bool waitForNextFrame = true)
  50. {
  51. // Debug.Log("Auto Generate");
  52. if (TC_Generate.instance != null)
  53. {
  54. TC_Generate.instance.cmdGenerate = true;
  55. TC_Generate.instance.autoGenerateRect = Mathw.ClampRect (generateRect, new Rect (0,0,1,1));
  56. }
  57. }
  58. static public void RefreshOutputReferences(int outputId)
  59. {
  60. // Debug.Log("Call refresh " + outputId);
  61. refreshOutputReferences = outputId;
  62. }
  63. static public int GetRefreshOutputReferences() { return refreshOutputReferences; }
  64. static public void RefreshOutputReferences(int outputId, bool autoGenerate)
  65. {
  66. // Debug.Log("Call refresh " + outputId);
  67. refreshOutputReferences = outputId;
  68. if (autoGenerate) AutoGenerate();
  69. }
  70. static public void Swap<T>(ref T source, ref T dest)
  71. {
  72. T temp = source;
  73. source = dest;
  74. dest = temp;
  75. }
  76. static public void Swap<T>(List<T> source, int indexS, List<T> dest, int indexD)
  77. {
  78. if (indexD < 0 || indexD >= dest.Count) return;
  79. T temp = source[indexS];
  80. source[indexS] = dest[indexD];
  81. dest[indexD] = temp;
  82. }
  83. static public void Swap<T>(ref T[] source, ref T[] dest)
  84. {
  85. for (int i = 0; i < source.Length; i++) Swap(ref source[i], ref dest[i]);
  86. }
  87. static public void InitArray<T>(ref T[] array, int resolution)
  88. {
  89. if (array == null) array = new T[resolution];
  90. else if (array.Length != resolution) array = new T[resolution];
  91. }
  92. static public void InitArray<T>(ref T[,] array, int resolutionX, int resolutionY)
  93. {
  94. if (array == null) array = new T[resolutionX, resolutionY];
  95. else if (array.Length != resolutionX * resolutionY) array = new T[resolutionX, resolutionY];
  96. }
  97. static public void DestroyChildrenTransform(Transform t)
  98. {
  99. int childCount = t.childCount;
  100. for (int i = 0; i < childCount; i++)
  101. {
  102. #if UNITY_EDITOR
  103. UnityEngine.GameObject.DestroyImmediate(t.GetChild(childCount - i - 1).gameObject);
  104. #else
  105. UnityEngine.GameObject.Destroy(t.GetChild(childCount - i - 1).gameObject);
  106. #endif
  107. }
  108. }
  109. static public void MoveToDustbinChildren(Transform t, int index)
  110. {
  111. int childCount = t.childCount;
  112. if (childCount >= index)
  113. {
  114. int length = childCount - index;
  115. for (int i = 0; i < length; i++) MoveToDustbin(t.GetChild(t.childCount - 1));
  116. }
  117. }
  118. static public void SetTextureReadWrite(Texture2D tex)
  119. {
  120. if (tex == null) return;
  121. #if UNITY_EDITOR
  122. string path = UnityEditor.AssetDatabase.GetAssetPath(tex);
  123. UnityEditor.TextureImporter textureImporter = (UnityEditor.TextureImporter)UnityEditor.AssetImporter.GetAtPath(path);
  124. if (textureImporter != null)
  125. {
  126. if (!textureImporter.isReadable)
  127. {
  128. textureImporter.isReadable = true;
  129. UnityEditor.AssetDatabase.ImportAsset(path, UnityEditor.ImportAssetOptions.ForceUpdate);
  130. }
  131. }
  132. #endif
  133. }
  134. static public string GetFileName(string path)
  135. {
  136. int index = path.LastIndexOf("/");
  137. if (index != -1)
  138. {
  139. string file = path.Substring(index + 1);
  140. index = file.LastIndexOf(".");
  141. if (index != -1) return file.Substring(0, index);
  142. return "";
  143. }
  144. return "";
  145. }
  146. static public string GetPath(string path)
  147. {
  148. int index = path.LastIndexOf("/");
  149. if (index != -1) return path.Substring(0, index);
  150. return "";
  151. }
  152. static public string GetAssetDatabasePath(string path)
  153. {
  154. return path.Replace(Application.dataPath, "Assets");
  155. }
  156. static public bool FileExists(string fullPath)
  157. {
  158. if (fullPath == null) { Debug.Log("Path doesn't exists."); return false; }
  159. System.IO.FileInfo file_info = new System.IO.FileInfo(fullPath);
  160. if (file_info.Exists) return true; else return false;
  161. }
  162. static public bool FileExistsPath(string path)
  163. {
  164. if (path == null) { Debug.Log("Path doesn't exists."); return false; }
  165. path = Application.dataPath.Replace("Assets", "") + path;
  166. // Debug.Log(path);
  167. System.IO.FileInfo file_info = new System.IO.FileInfo(path);
  168. if (file_info.Exists) return true; else return false;
  169. }
  170. static public long GetFileLength(string fullPath)
  171. {
  172. #if !UNITY_WEBPLAYER
  173. System.IO.FileInfo fileInfo = new System.IO.FileInfo(fullPath);
  174. return fileInfo.Length;
  175. #else
  176. return 0;
  177. #endif
  178. }
  179. static public void GetInstallPath()
  180. {
  181. #if UNITY_EDITOR
  182. if (TC_Settings.instance != null)
  183. {
  184. installPath = UnityEditor.AssetDatabase.GetAssetPath(UnityEditor.MonoScript.FromMonoBehaviour(TC_Settings.instance));
  185. installPath = installPath.Replace("/Scripts/Settings/TC_Settings.cs", "");
  186. }
  187. #endif
  188. }
  189. static public bool LoadGlobalSettings()
  190. {
  191. #if UNITY_EDITOR
  192. string file = Application.dataPath.Replace("Assets", "") + installPath + "/Defaults/";
  193. if (!FileExists(file + "TC_GlobalSettings.asset"))
  194. {
  195. UnityEditor.FileUtil.CopyFileOrDirectory(file + "TC_GlobalSettingsDefault.asset", file + "TC_GlobalSettings.asset");
  196. UnityEditor.AssetDatabase.Refresh();
  197. }
  198. GameObject go = GameObject.Find("TerrainComposer2");
  199. Transform settingsT = go.transform.Find("Settings");
  200. if (settingsT != null)
  201. {
  202. TC_Settings settings = settingsT.GetComponent<TC_Settings>();
  203. if (settings != null)
  204. {
  205. settings.global = UnityEditor.AssetDatabase.LoadAssetAtPath(installPath + "/Defaults/TC_GlobalSettings.asset", typeof(TC_GlobalSettings)) as TC_GlobalSettings;
  206. if (settings.global == null) return false;
  207. }
  208. else return false;
  209. }
  210. else return false;
  211. #endif
  212. return true;
  213. }
  214. static public void MoveToDustbin(Transform t)
  215. {
  216. TC_Settings settings = TC_Settings.instance;
  217. if (settings.dustbinT == null) settings.CreateDustbin();
  218. t.parent = settings.dustbinT;
  219. AddMessage(t.name + " is not compatible with the hierarchy of TerrainComposer\n It is moved to the 'Dustbin' GameObject.", 0);
  220. AddMessage("If you pressed the delete key you can undo it with control-z", 3);
  221. }
  222. static public void AddMessage(string message, float delay = 0, float duration = 2)
  223. {
  224. for (int i = 0; i < messages.Count; i++) if (messages[i].message.Contains(message)) return;
  225. messages.Add(new MessageCommand(message, delay, duration));
  226. }
  227. static public bool VerifyPortal(TC_ItemBehaviour item)
  228. {
  229. if (item == null) return false;
  230. if (item.outputId == heightOutput) return true;
  231. if (item is TC_Node || item is TC_NodeGroup) return true;
  232. AddMessage("This node cannot be used as a portal. All nodes in Height output can be used. And for the rest of the outputs only Nodes and NodeGroups can be used.");
  233. return false;
  234. }
  235. public class MessageCommand
  236. {
  237. public string message;
  238. public float delay;
  239. public float duration;
  240. public float startTime;
  241. public MessageCommand(string message, float delay, float duration)
  242. {
  243. this.message = message;
  244. this.delay = delay;
  245. this.duration = duration;
  246. startTime = Time.realtimeSinceStartup;
  247. }
  248. }
  249. }
  250. }