TC_Area2D.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. // using System.Diagnostics;
  7. namespace TerrainComposer2
  8. {
  9. [ExecuteInEditMode]
  10. public class TC_Area2D : MonoBehaviour
  11. {
  12. static public TC_Area2D current;
  13. public TC_TerrainArea[] terrainAreas;
  14. public TC_TerrainMeshArea[] meshTerrainAreas;
  15. [NonSerialized] public Terrain currentTerrain;
  16. [NonSerialized] public TC_Terrain currentTCTerrain;
  17. [NonSerialized] public TCUnityTerrain currentTCUnityTerrain;
  18. [NonSerialized] public MeshTerrain currentMeshTerrain;
  19. public TC_TerrainArea currentTerrainArea;
  20. [NonSerialized] public TC_TerrainMeshArea currentMeshTerrainArea;
  21. public TC_PreviewArea previewArea;
  22. public Rect area, totalArea;
  23. public Bounds totalTerrainBounds;
  24. public TC_TerrainLayer layerLevelC;
  25. public TC_TerrainLayer terrainLayer;
  26. public Vector2 resolution, resolutionPM, resToPreview, worldPos, localPos, pos, localNPos, previewPos, snapOffsetUV, outputOffsetV2;
  27. public Vector3 startPos, terrainSize, outputOffsetV3;
  28. public Bounds bounds;
  29. public Int2 intResolution;
  30. public float heightN, height, angle;
  31. public int splatLength;
  32. public int splatmapLength;
  33. public float frames;
  34. public float[] splatTotal;
  35. public float terrainsToDo, terrainsDone;
  36. public float progress;
  37. public bool showProgressBar;
  38. public int previewResolution;
  39. [NonSerialized] public float resExpandBorderPercentage = 0.0625f;
  40. [NonSerialized] public int resExpandBorder;
  41. [NonSerialized] public float resExpandBorderSize;
  42. public Vector2 terrainHeightRange;
  43. // public List<SpawnObject> spawnObjectList = new List<SpawnObject>();
  44. void Awake() { current = this; }
  45. void Start() { current = this; }
  46. void OnDestroy() { current = null; }
  47. public void CalcProgress()
  48. {
  49. progress = (((localPos.y / area.height) + (localPos.x / (area.width * area.height))) / terrainsToDo) + (terrainsDone / terrainsToDo);
  50. }
  51. void OnEnable()
  52. {
  53. current = this;
  54. currentTerrainArea = terrainAreas[0];
  55. }
  56. void OnDisable()
  57. {
  58. current = null;
  59. }
  60. public void DisposeTextures()
  61. {
  62. for (int i = 0; i < terrainAreas.Length; i++)
  63. {
  64. if (terrainAreas[i] != null) terrainAreas[i].DisposeTextures();
  65. }
  66. }
  67. public bool SetCurrentArea(TCUnityTerrain tcTerrain, int outputId)
  68. {
  69. // Debug.Log(tcTerrain.terrain.name);
  70. Terrain terrain = currentTerrain = tcTerrain.terrain;
  71. currentTCUnityTerrain = tcTerrain;
  72. currentTCTerrain = tcTerrain;
  73. currentTerrainArea = terrainAreas[0];
  74. if (!currentTCUnityTerrain.active) return false;
  75. intResolution = new Int2();
  76. Int2 resolution2 = new Int2();
  77. if (terrain.terrainData.heightmapResolution > 2049)
  78. {
  79. resExpandBorder = 0;
  80. resExpandBorderSize = 0;
  81. }
  82. else
  83. {
  84. resExpandBorder = Mathf.RoundToInt((terrain.terrainData.heightmapResolution - 1) * resExpandBorderPercentage);
  85. resExpandBorderSize = terrain.terrainData.size.x * resExpandBorderPercentage;
  86. }
  87. // Debug.Log(resExpandBorder);
  88. // Debug.Log(resExpandBorderSize);
  89. if (outputId == TC.heightOutput)
  90. {
  91. intResolution.x = intResolution.y = (terrain.terrainData.heightmapResolution) + (resExpandBorder * 2);
  92. resolution2 = new Int2(terrain.terrainData.heightmapResolution, terrain.terrainData.heightmapResolution);
  93. }
  94. else if (outputId == TC.splatOutput)
  95. {
  96. intResolution.x = intResolution.y = terrain.terrainData.alphamapResolution;
  97. resolution2 = intResolution;
  98. splatLength = currentTerrain.terrainData.splatPrototypes.Length;
  99. splatmapLength = currentTerrain.terrainData.alphamapTextures.Length;
  100. }
  101. else if (outputId == TC.treeOutput) { intResolution.x = intResolution.y = (int)(terrain.terrainData.size.x / terrainLayer.treeResolutionPM); resolution2 = intResolution; }
  102. else if (outputId == TC.grassOutput) { intResolution.x = intResolution.y = terrain.terrainData.detailResolution; resolution2 = intResolution; }
  103. else if (outputId == TC.objectOutput)
  104. {
  105. intResolution.x = intResolution.y = (int)(terrain.terrainData.size.x / terrainLayer.objectResolutionPM); resolution2 = intResolution;
  106. //if (false)
  107. //{
  108. // area.center = new Vector2((int)terrainLayer.objectTransform.position.x, (int)terrainLayer.objectTransform.position.z);
  109. // area.size = new Vector2(terrainLayer.objectResolutionPM, terrainLayer.objectResolutionPM);
  110. // totalArea.position = area.position;
  111. // totalArea.size = terrainLayer.objectAreaSize;
  112. // resolutionPM = new Vector2(terrainLayer.objectAreaSize.x / (resolution2.x), terrainLayer.objectAreaSize.y / (resolution2.y));
  113. //}
  114. }
  115. else if (outputId == TC.colorOutput) { intResolution.x = intResolution.y = terrainLayer.colormapResolution; resolution2 = intResolution; }
  116. outputOffsetV2 = new Vector2(terrainLayer.layerGroups[outputId].t.position.x, terrainLayer.layerGroups[outputId].t.position.z);
  117. outputOffsetV3 = new Vector3(outputOffsetV2.x, 0, outputOffsetV2.y);
  118. resolution = intResolution.ToVector2();
  119. if (intResolution.x < TC_Settings.instance.previewResolution) { previewResolution = intResolution.x; TC_Reporter.Log("From " + TC_Settings.instance.previewResolution + " To " + previewResolution); }
  120. else previewResolution = TC_Settings.instance.previewResolution;
  121. resToPreview = new Vector2((previewResolution - 0) / (totalArea.width + 0), (previewResolution - 0) / (totalArea.height + 0));
  122. if (outputId == TC.heightOutput || outputId == TC.splatOutput) resolutionPM = new Vector2(terrain.terrainData.size.x / (resolution2.x - 1), terrain.terrainData.size.z / (resolution2.y - 1));
  123. else resolutionPM = new Vector2(terrain.terrainData.size.x / (resolution2.x - 0), terrain.terrainData.size.z / (resolution2.y - 0));
  124. // if (outputId == TC.objectOutput) Debug.Log(resolutionPM);
  125. // resolutionPM = new Vector2(terrain.terrainData.size.x / (resolution2.x - 1), terrain.terrainData.size.z / (resolution2.y - 1));
  126. if (outputId == TC.heightOutput)
  127. {
  128. // area = new Rect(terrain.transform.position.x - resolutionPM.x, terrain.transform.position.z - resolutionPM.y, intResolution.x - 0, intResolution.y - 0);
  129. area = new Rect(terrain.transform.position.x - (resolutionPM.x * resExpandBorder), terrain.transform.position.z - (resolutionPM.y * resExpandBorder), intResolution.x - 0, intResolution.y - 0);
  130. }
  131. else
  132. {
  133. // resolutionPM = new Vector2(terrain.terrainData.size.x / (resolution2.x), terrain.terrainData.size.z / (resolution2.y));
  134. Vector2 posSnap;
  135. posSnap.x = Mathw.Snap(terrain.transform.position.x, resolutionPM.x);
  136. posSnap.y = Mathw.Snap(terrain.transform.position.z, resolutionPM.y);
  137. if (outputId == TC.treeOutput || outputId == TC.objectOutput)
  138. {
  139. // posSnap += resolutionPM / 2;
  140. }
  141. area = new Rect(posSnap.x, posSnap.y, intResolution.x, intResolution.y);
  142. snapOffsetUV = (new Vector2(terrain.transform.position.x, terrain.transform.position.z) - posSnap);
  143. snapOffsetUV.x /= terrain.terrainData.size.x;
  144. snapOffsetUV.y /= terrain.terrainData.size.z;
  145. // Debug.Log(area);
  146. }
  147. bounds = new Bounds(terrain.transform.position + terrain.terrainData.size / 2, terrain.terrainData.size);
  148. startPos = new Vector3(area.xMin, terrain.transform.position.y, area.yMin);
  149. terrainSize = terrain.terrainData.size;
  150. return true;
  151. }
  152. public void SetManualTotalArea()
  153. {
  154. totalArea = previewArea.area;
  155. resToPreview = new Vector2(current.previewResolution / totalArea.width, previewResolution / totalArea.height);
  156. }
  157. public bool CalcTotalArea()
  158. {
  159. Terrain terrain;
  160. Vector2 minPos = new Vector2(Mathf.Infinity, Mathf.Infinity);
  161. Vector2 maxPos = new Vector2(-Mathf.Infinity, -Mathf.Infinity);
  162. Vector2 heightPos = new Vector2(Mathf.Infinity, -Mathf.Infinity);
  163. Vector2 heightPosTerrain;
  164. Vector2 pos;
  165. Vector3 position, size;
  166. for (int j = 0; j < terrainAreas.Length; j++)
  167. {
  168. for (int i = 0; i < terrainAreas[j].terrains.Count; i++)
  169. {
  170. terrain = terrainAreas[j].terrains[i].terrain;
  171. if (terrain == null) return false;
  172. if (!terrain.gameObject.activeSelf) continue;
  173. position = terrain.transform.position;
  174. size = terrain.terrainData.size;
  175. if (position.x < minPos.x) minPos.x = position.x;
  176. if (position.z < minPos.y) minPos.y = position.z;
  177. pos = new Vector2(position.x + size.x, position.z + size.z);
  178. heightPosTerrain = new Vector2(position.y, position.y + size.y);
  179. if (pos.x > maxPos.x) maxPos.x = pos.x;
  180. if (pos.y > maxPos.y) maxPos.y = pos.y;
  181. if (heightPosTerrain.x < heightPos.x) heightPos.x = heightPosTerrain.x;
  182. if (heightPosTerrain.y > heightPos.y) heightPos.y = heightPosTerrain.y;
  183. }
  184. }
  185. totalArea = new Rect();
  186. totalArea.xMin = minPos.x;
  187. totalArea.yMin = minPos.y;
  188. totalArea.xMax = maxPos.x;
  189. totalArea.yMax = maxPos.y;
  190. totalTerrainBounds = new Bounds();
  191. totalTerrainBounds.min = new Vector3(minPos.x, heightPos.x, minPos.y);
  192. totalTerrainBounds.max = new Vector3(maxPos.x, heightPos.y, maxPos.y);
  193. return true;
  194. }
  195. public void SetTerrainAreasSize(Vector3 size)
  196. {
  197. for (int i = 0; i < terrainAreas.Length; i++)
  198. {
  199. if (terrainAreas[i].active) terrainAreas[i].terrainSize = size;
  200. }
  201. }
  202. public void ApplyTerrainAreasSize()
  203. {
  204. for (int i = 0; i < terrainAreas.Length; i++)
  205. {
  206. if (terrainAreas[i].active) terrainAreas[i].ApplySizeTerrainArea();
  207. }
  208. }
  209. public void AddTerrainArea(TC_TerrainArea terrainArea)
  210. {
  211. // if (!terrainAreas.Contains(terrainArea)) terrainAreas.Add(terrainArea);
  212. }
  213. public void RemoveTerrainArea(TC_TerrainArea terrainArea)
  214. {
  215. // int index = terrainAreas.IndexOf(terrainArea);
  216. // if (index != -1) terrainAreas.RemoveAt(index);
  217. }
  218. public void ApplySizeTerrainAreas()
  219. {
  220. for (int i = 0; i < terrainAreas.Length; i++) terrainAreas[i].ApplySizeTerrainArea();
  221. }
  222. public void ApplyResolutionTerrainAreas(TCUnityTerrain sTerrain)
  223. {
  224. for (int i = 0; i < terrainAreas.Length; i++) terrainAreas[i].ApplyResolutionTerrainArea(sTerrain);
  225. }
  226. public void ApplySplatTexturesTerrainAreas(TCUnityTerrain sTerrain)
  227. {
  228. for (int i = 0; i < terrainAreas.Length; i++) terrainAreas[i].ApplySplatTextures(sTerrain);
  229. }
  230. }
  231. }