TC_Settings.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. using System.IO;
  6. namespace TerrainComposer2
  7. {
  8. [ExecuteInEditMode]
  9. public class TC_Settings : MonoBehaviour
  10. {
  11. public enum ImageExportFormat { JPG, PNG }
  12. static public TC_Settings instance;
  13. // Node Window
  14. public float version;
  15. public Vector2 scrollOffset = Vector2.zero;
  16. public Vector2 scrollAdd = Vector2.zero;
  17. public float scale = 1;
  18. public bool drawDefaultInspector;
  19. public bool debugMode = true;
  20. public bool hideTerrainGroup = true;
  21. public bool useTCRuntime = false;
  22. public float defaultTerrainHeight = 1000;
  23. public Vector3 generateOffset;
  24. public bool showFps = true;
  25. public bool hideMenuBar = false;
  26. public Transform dustbinT;
  27. public Transform selectionOld;
  28. public Terrain masterTerrain;
  29. public bool hasMasterTerrain;
  30. public PresetMode presetMode;
  31. public float seed = 0;
  32. public string lastPath = "";
  33. public bool preview;
  34. public int previewResolution = 128;
  35. public ImageExportFormat imageExportFormat;
  36. public bool combineHeightmapImage = true;
  37. public string heightmapFilename = "Heightmap";
  38. public string normalmapFilename = "Normalmap";
  39. public string splatmapFilename = "Splatmap";
  40. public string colormapFilename = "Colormap";
  41. public float normalmapStrength = 0.5f;
  42. public List<TC_RawImage> rawFiles = new List<TC_RawImage>();
  43. public List<TC_Image> imageList = new List<TC_Image>();
  44. public Int2 importTiles = new Int2(1, 1);
  45. public enum ImportSource { TC2_TerrainArea, TerrainData_Files };
  46. public ImportSource importSource;
  47. public List<TerrainData> importTerrains = new List<TerrainData>();
  48. public bool isRTPDetected;
  49. public bool autoNormalmapRTP = true, autoColormapRTP = true;
  50. public TC_GlobalSettings global;
  51. public string exportPath;
  52. void Awake()
  53. {
  54. instance = this;
  55. if (exportPath == "") exportPath = Application.dataPath;
  56. }
  57. void OnEnable()
  58. {
  59. instance = this;
  60. if (transform.parent != null) transform.parent.hideFlags = HideFlags.NotEditable;
  61. }
  62. void OnDestroy()
  63. {
  64. TC_Reporter.Log("OnDestroy");
  65. instance = null;
  66. }
  67. static public void GetInstance()
  68. {
  69. GameObject go = GameObject.Find("Settings");
  70. if (go != null)
  71. {
  72. instance = go.GetComponent<TC_Settings>();
  73. }
  74. }
  75. public void DisposeTextures()
  76. {
  77. for (int i = 0; i < rawFiles.Count; i++)
  78. {
  79. if (rawFiles[i] != null)
  80. {
  81. #if UNITY_EDITOR
  82. DestroyImmediate(rawFiles[i].gameObject);
  83. #else
  84. Destroy(rawFiles[i].gameObject);
  85. #endif
  86. }
  87. }
  88. }
  89. public void HasMasterTerrain()
  90. {
  91. if (masterTerrain == null)
  92. {
  93. TC_Area2D area2D = TC_Area2D.current;
  94. if (area2D.currentTerrainArea != null)
  95. {
  96. if (area2D.currentTerrainArea.terrains.Count > 0)
  97. {
  98. masterTerrain = area2D.currentTerrainArea.terrains[0].terrain;
  99. }
  100. }
  101. if (masterTerrain == null)
  102. {
  103. hasMasterTerrain = false;
  104. return;
  105. }
  106. }
  107. if (masterTerrain.terrainData == null) { hasMasterTerrain = false; return; }
  108. hasMasterTerrain = true;
  109. }
  110. public TC_RawImage AddRawFile(string fullPath, bool isResourcesFolder)
  111. {
  112. for (int i = 0; i < rawFiles.Count; i++)
  113. {
  114. if (rawFiles[i] == null) { rawFiles.RemoveAt(i); i--; continue; }
  115. if (rawFiles[i].path == fullPath)
  116. {
  117. ++rawFiles[i].referenceCount;
  118. if (rawFiles[i].tex == null) rawFiles[i].LoadRawImage(fullPath);
  119. return rawFiles[i];
  120. }
  121. }
  122. string label = TC.GetFileName(fullPath);
  123. GameObject go = new GameObject(label);
  124. go.transform.parent = transform;
  125. TC_RawImage rawImage = go.AddComponent<TC_RawImage>();
  126. // Debug.Log(fullPath);
  127. rawImage.isResourcesFolder = isResourcesFolder;
  128. rawImage.LoadRawImage(fullPath);
  129. rawImage.referenceCount = 1;
  130. rawFiles.Add(rawImage);
  131. return rawImage;
  132. }
  133. public TC_Image AddImage(Texture2D tex)
  134. {
  135. TC_Image image;
  136. for (int i = 0; i < imageList.Count; i++)
  137. {
  138. image = imageList[i];
  139. // if (image.tex == tex) return image;
  140. }
  141. image = new TC_Image();
  142. // image.tex = tex;
  143. imageList.Add(image);
  144. return image;
  145. }
  146. public void CreateDustbin()
  147. {
  148. GameObject go = new GameObject("Dustbin");
  149. dustbinT = go.transform;
  150. dustbinT.parent = transform.parent;
  151. }
  152. static public Transform GetTransformFromLevel(int index, Transform t)
  153. {
  154. Transform root = t.root;
  155. List<Transform> transforms = new List<Transform>();
  156. Transform parent = t;
  157. do
  158. {
  159. parent = parent.parent;
  160. transforms.Insert(0, parent);
  161. }
  162. while (parent != root);
  163. return transforms[index];
  164. }
  165. }
  166. [Serializable]
  167. public class CachedTransform
  168. {
  169. public Vector3 position;
  170. public Vector3 posOffset;
  171. public Quaternion rotation;
  172. public Vector3 scale;
  173. public float positionYOld;
  174. public void CopySpecial(TC_ItemBehaviour item)
  175. {
  176. TC_Node node = item as TC_Node;
  177. bool maskNode = false;
  178. if (node != null)
  179. {
  180. if (node.nodeType == NodeGroupType.Mask) maskNode = true;
  181. }
  182. posOffset = item.posOffset;
  183. bool lockPosParent = item.lockPosParent;
  184. if (item.lockTransform || lockPosParent)
  185. {
  186. Vector3 posTemp = Vector3.zero;
  187. Quaternion rotTemp;
  188. Vector3 scaleTemp = Vector3.zero;
  189. if (!(item.lockPosX || lockPosParent)) posTemp.x = item.t.position.x; else posTemp.x = position.x;
  190. if (!(item.lockPosY || lockPosParent)) posTemp.y = item.posY * scale.y; else posTemp.y = position.y;
  191. if (!(item.lockPosZ || lockPosParent)) posTemp.z = item.t.position.z; else posTemp.z = position.z;
  192. if (!(item.lockRotY && item.lockTransform)) rotTemp = Quaternion.Euler(0, item.t.eulerAngles.y, 0); else rotTemp = rotation;
  193. if (!(item.lockScaleX && item.lockTransform)) scaleTemp.x = item.t.lossyScale.x; else scaleTemp.x = scale.x;
  194. if (!(item.lockScaleY && item.lockTransform))
  195. {
  196. if (maskNode) scaleTemp.y = item.t.localScale.y; else scaleTemp.y = item.t.lossyScale.y * item.opacity;
  197. }
  198. else scaleTemp.y = scale.y;
  199. if (!(item.lockScaleZ && item.lockTransform)) scaleTemp.z = item.t.lossyScale.z; else scaleTemp.z = scale.z;
  200. position = posTemp;
  201. rotation = rotTemp;
  202. scale = scaleTemp;
  203. if (item.t.position != position) item.t.position = position;
  204. if (item.t.rotation != rotation) item.t.rotation = rotation;
  205. item.t.hasChanged = false;
  206. }
  207. else
  208. {
  209. rotation = Quaternion.Euler(0, item.t.eulerAngles.y, 0);
  210. scale.x = item.t.lossyScale.x;
  211. scale.z = item.t.lossyScale.z;
  212. if (maskNode) scale.y = item.t.localScale.y; else scale.y = item.t.lossyScale.y;
  213. scale.y *= item.opacity;
  214. position = item.t.position;
  215. position.y = item.posY * scale.y;
  216. }
  217. // if (scale.x == 0) scale.x = 1;
  218. // if (scale.y == 0) scale.y = 1;
  219. // if (scale.z == 0) scale.z = 1;
  220. }
  221. public void Copy(TC_ItemBehaviour item)
  222. {
  223. position.x = item.t.position.x;
  224. position.z = item.t.position.z;
  225. posOffset = item.posOffset;
  226. rotation = item.t.rotation;
  227. scale = item.t.lossyScale;
  228. positionYOld = item.posY;
  229. }
  230. public void Copy(Transform t)
  231. {
  232. position = t.position;
  233. rotation = t.rotation;
  234. scale = t.lossyScale;
  235. }
  236. public bool hasChanged(Transform t)
  237. {
  238. if (t == null) return false;
  239. if (position != t.position || rotation != t.rotation || scale != t.lossyScale) return true; else return false;
  240. }
  241. public bool hasChanged(TC_ItemBehaviour item)
  242. {
  243. // Debug.Log(item.posY + " " + posYOld);
  244. if (position.x != item.t.position.x || position.z != item.t.position.z || item.posY != positionYOld)
  245. {
  246. // if (position.x != item.t.position.x) Debug.Log("x " + position.x + ", " + item.t.position.x);
  247. // if (position.y != item.t.position.y) Debug.Log("y " + position.y + ", " + item.t.position.y);
  248. // if (position.z != item.t.position.z) Debug.Log("z " + position.z + ", " + item.t.position.z);
  249. // if (positionYOld != item.positionY) Debug.Log("posY " + positionYOld + ", " + item.positionY);
  250. // Debug.Log(TC.outputNames[item.outputId]);
  251. return true;
  252. }
  253. // if (position != item.t.position) return false;
  254. if (rotation != item.t.rotation) return true;
  255. if (scale != item.t.lossyScale) return true;
  256. return false;
  257. }
  258. }
  259. }