TC_SelectItem.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace TerrainComposer2
  6. {
  7. public class TC_SelectItem : TC_ItemBehaviour
  8. {
  9. [NonSerialized] public new TC_SelectItemGroup parentItem;
  10. public Vector2 range;
  11. public ImageWrapMode wrapMode = ImageWrapMode.Repeat;
  12. public Vector3 size = new Vector3(2048, 0, 2048);
  13. public GameObject oldSpawnObject;
  14. public Tree tree;
  15. public SpawnObject spawnObject;
  16. public Color color = Color.white;
  17. public Texture texColor;
  18. public float brightness = 1;
  19. public float saturation = 1;
  20. public int selectIndex;
  21. public float[] splatCustomValues;
  22. public bool splatCustom;
  23. public float splatCustomTotal;
  24. // public List<DistanceRule> distanceRules;
  25. public int globalListIndex = -1;
  26. public int placed;
  27. [NonSerialized] TC_Settings settings;
  28. public override void Awake()
  29. {
  30. // Debug.Log("Awake SelectItem");
  31. base.Awake();
  32. // t.hideFlags = HideFlags.NotEditable | HideFlags.HideInInspector;
  33. t.hideFlags = HideFlags.None;
  34. }
  35. public override void OnEnable()
  36. {
  37. base.OnEnable();
  38. // t.hideFlags = HideFlags.NotEditable | HideFlags.HideInInspector;
  39. t.hideFlags = HideFlags.None;
  40. }
  41. public void CalcSplatCustomTotal()
  42. {
  43. splatCustomTotal = 0;
  44. for (int i = 0; i < splatCustomValues.Length; i++) splatCustomTotal += splatCustomValues[i];
  45. // Debug.Log(splatCustomTotal);
  46. }
  47. public void ResetObjects()
  48. {
  49. if (spawnObject != null)
  50. {
  51. if (spawnObject.parentMode == SpawnObject.ParentMode.Create)
  52. {
  53. if (spawnObject.newParentT != null) DestroyImmediate(spawnObject.newParentT.gameObject);
  54. }
  55. else if (spawnObject.parentMode == SpawnObject.ParentMode.Existing)
  56. {
  57. if (spawnObject.parentT != null) TC.DestroyChildrenTransform(spawnObject.parentT);
  58. }
  59. }
  60. }
  61. public void SetPreviewColor()
  62. {
  63. TC_GlobalSettings globalSettings = TC_Settings.instance.global;
  64. if (selectIndex == -1) selectIndex = 0;
  65. if (outputId == TC.splatOutput)
  66. {
  67. if (splatCustom)
  68. {
  69. color = Color.black;
  70. for (int i = 0; i < splatCustomValues.Length; i++) color += splatCustomValues[i] * globalSettings.GetVisualizeColor(i);
  71. color /= splatCustomTotal;
  72. }
  73. else color = globalSettings.GetVisualizeColor(selectIndex);
  74. }
  75. else if (outputId == TC.treeOutput || outputId == TC.objectOutput) color = globalSettings.GetVisualizeColor(listIndex); else color = globalSettings.GetVisualizeColor(selectIndex);
  76. }
  77. public int GetItemTotalFromTerrain()
  78. {
  79. TC_Settings localSettings = TC_Settings.instance;
  80. if (!localSettings.hasMasterTerrain) return 0;
  81. if (outputId == TC.splatOutput) return localSettings.masterTerrain.terrainData.splatPrototypes.Length;
  82. if (outputId == TC.grassOutput) return localSettings.masterTerrain.terrainData.detailPrototypes.Length;
  83. if (outputId == TC.treeOutput) return localSettings.masterTerrain.terrainData.treePrototypes.Length;
  84. return 0;
  85. }
  86. public void Refresh()
  87. {
  88. // Debug.Log("Refresh");
  89. // SetPreviewItemTexture();
  90. // SetPreviewColor();
  91. // parentItem.RefreshRanges();
  92. //parentItem.CalcPreview();
  93. parentItem.GetItems(true, false, false);
  94. }
  95. public void SetPreviewItemTexture()
  96. {
  97. if (outputId == TC.splatOutput) SetPreviewSplatTexture();
  98. else if (outputId == TC.colorOutput) SetPreviewColorTexture();
  99. else if (outputId == TC.treeOutput) SetPreviewTreeTexture();
  100. else if (outputId == TC.grassOutput) SetPreviewGrassTexture();
  101. else if (outputId == TC.objectOutput) SetPreviewObjectTexture();
  102. }
  103. public void SetPreviewSplatTexture()
  104. {
  105. TC_Settings localSettings = TC_Settings.instance;
  106. if (localSettings.hasMasterTerrain)
  107. {
  108. if (selectIndex < localSettings.masterTerrain.terrainData.splatPrototypes.Length && selectIndex >= 0)
  109. {
  110. preview.tex = localSettings.masterTerrain.terrainData.splatPrototypes[selectIndex].texture;
  111. if (preview.tex != null) name = Mathw.CutString(preview.tex.name, TC.nodeLabelLength);
  112. }
  113. else active = false;
  114. }
  115. else preview.tex = null;
  116. }
  117. public void SetPreviewTreeTexture()
  118. {
  119. #if UNITY_EDITOR
  120. TC_Settings settings = TC_Settings.instance;
  121. if (settings.hasMasterTerrain)
  122. {
  123. if (selectIndex < settings.masterTerrain.terrainData.treePrototypes.Length && selectIndex >= 0)
  124. {
  125. preview.tex = UnityEditor.AssetPreview.GetAssetPreview(settings.masterTerrain.terrainData.treePrototypes[selectIndex].prefab);
  126. name = Mathw.CutString(settings.masterTerrain.terrainData.treePrototypes[selectIndex].prefab.name, TC.nodeLabelLength);
  127. }
  128. else active = false;
  129. }
  130. else preview.tex = null;
  131. #endif
  132. }
  133. public void SetPreviewObjectTexture()
  134. {
  135. #if UNITY_EDITOR
  136. if (spawnObject == null) { preview.tex = null; return; }
  137. if (spawnObject.go == null)
  138. {
  139. // Debug.Log("preview tex is set to null");
  140. preview.tex = null;
  141. return;
  142. }
  143. // Debug.Log("Yes");
  144. preview.tex = UnityEditor.AssetPreview.GetAssetPreview(spawnObject.go);
  145. name = Mathw.CutString(spawnObject.go.name, TC.nodeLabelLength);
  146. #endif
  147. // Debug.Log("SetPreviewObjectTexture");
  148. }
  149. public void SetPreviewGrassTexture()
  150. {
  151. TC_Settings localSettings = TC_Settings.instance;
  152. if (localSettings.hasMasterTerrain)
  153. {
  154. if (selectIndex < localSettings.masterTerrain.terrainData.detailPrototypes.Length && selectIndex >= 0)
  155. {
  156. DetailPrototype detailPrototype = localSettings.masterTerrain.terrainData.detailPrototypes[selectIndex];
  157. if (detailPrototype.usePrototypeMesh)
  158. {
  159. #if UNITY_EDITOR
  160. preview.tex = UnityEditor.AssetPreview.GetAssetPreview(detailPrototype.prototype);
  161. #endif
  162. }
  163. else preview.tex = detailPrototype.prototypeTexture;
  164. if (preview.tex != null) name = Mathw.CutString(preview.tex.name, TC.nodeLabelLength);
  165. }
  166. else active = false;
  167. }
  168. else preview.tex = null;
  169. }
  170. public void SetPreviewColorTexture()
  171. {
  172. #if UNITY_EDITOR
  173. if (preview.tex != Texture2D.whiteTexture) preview.tex = Texture2D.whiteTexture;
  174. #endif
  175. }
  176. [Serializable]
  177. public class SpawnObject
  178. {
  179. public enum ParentMode { Terrain, Existing, Create };
  180. public GameObject go;
  181. public bool linkToPrefab = false;
  182. public ParentMode parentMode;
  183. public string parentName;
  184. public Transform parentT;
  185. public Transform newParentT;
  186. public bool parentToTerrain;
  187. public float randomPosition = 1;
  188. public Vector2 heightRange = Vector2.zero;
  189. public bool includeScale = false;
  190. public float heightOffset = 0;
  191. public bool includeTerrainHeight = true;
  192. public bool includeTerrainAngle = false;
  193. public Vector2 rotRangeX = Vector2.zero;
  194. public Vector2 rotRangeY = Vector2.zero;
  195. public Vector2 rotRangeZ = Vector2.zero;
  196. public bool isSnapRot, isSnapRotX = true, isSnapRotY = true, isSnapRotZ = true;
  197. public float snapRotX = 45 , snapRotY = 45, snapRotZ = 45;
  198. public bool customScaleRange;
  199. public Vector2 scaleRangeX = Vector2.one;
  200. public Vector2 scaleRangeY = Vector2.one;
  201. public Vector2 scaleRangeZ = Vector2.one;
  202. public Vector2 scaleRange = Vector2.one;
  203. public float scaleMulti = 1;
  204. public float nonUniformScale = 0f;
  205. public AnimationCurve scaleCurve = AnimationCurve.Linear(0, 0, 1, 1);
  206. public Transform lookAtTarget;
  207. public bool lookAtX;
  208. }
  209. [Serializable]
  210. public class Tree
  211. {
  212. public float randomPosition = 1;
  213. public float heightOffset = 0;
  214. public Vector2 scaleRange = new Vector2(0.5f, 2f);
  215. public float nonUniformScale = 0.2f;
  216. public float scaleMulti = 1;
  217. public AnimationCurve scaleCurve = AnimationCurve.Linear(0, 0, 1, 1);
  218. }
  219. [Serializable]
  220. public class DistanceRule
  221. {
  222. public List<TC_ItemBehaviour> items;
  223. public Vector2 range;
  224. }
  225. }
  226. }