TC_NodeGroup.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace TerrainComposer2
  6. {
  7. public class TC_NodeGroup : TC_GroupBehaviour
  8. {
  9. [NonSerialized] public List<TC_ItemBehaviour> itemList = new List<TC_ItemBehaviour>();
  10. public int nodeGroupLevel;
  11. public NodeGroupType type;
  12. public RenderTexture rtColorPreview;
  13. public bool useConstant;
  14. public float seed = 0;
  15. public override void Awake()
  16. {
  17. rtColorPreview = null;
  18. base.Awake();
  19. }
  20. public override void OnDestroy()
  21. {
  22. DisposeTextures();
  23. }
  24. public override void DisposeTextures()
  25. {
  26. base.DisposeTextures();
  27. TC_Compute.DisposeRenderTexture(ref rtColorPreview);
  28. }
  29. public void LinkClone(TC_NodeGroup nodeGroupS)
  30. {
  31. preview = nodeGroupS.preview;
  32. for (int i = 0; i < itemList.Count; i++)
  33. {
  34. TC_Node node = itemList[i] as TC_Node;
  35. if (node != null)
  36. {
  37. TC_Node sNode = nodeGroupS.itemList[i] as TC_Node;
  38. node.preview = sNode.preview;
  39. node.Init();
  40. }
  41. }
  42. }
  43. public override void SetLockChildrenPosition(bool lockPos)
  44. {
  45. lockPosParent = lockPos;
  46. for (int i = 0; i < itemList.Count; i++)
  47. {
  48. TC_Node node = itemList[i] as TC_Node;
  49. if (node != null) node.lockPosParent = lockPosParent || lockPosChildren;
  50. }
  51. }
  52. public override void UpdateTransforms()
  53. {
  54. // ct.CopySpecial(this);
  55. for (int i = 0; i < itemList.Count; i++)
  56. {
  57. TC_Node node = itemList[i] as TC_Node;
  58. if (node != null) node.ct.CopySpecial(node);
  59. }
  60. }
  61. public ComputeBuffer ComputeValue(float seedParent)
  62. {
  63. TC_Compute compute = TC_Compute.instance;
  64. if (compute == null) Debug.Log("Compute is null");
  65. ComputeBuffer nodeBuffer = null;
  66. ComputeBuffer totalBuffer = null;
  67. bool inputCurrent;
  68. if (totalActive > 1) InitPreviewRenderTexture(true, name);
  69. int length = useConstant ? 1 : itemList.Count;
  70. float seedTotal = seed + seedParent;
  71. for (int i = 0; i < length; i++)
  72. {
  73. TC_Node node = itemList[i] as TC_Node;
  74. if (node != null)
  75. {
  76. node.Init();
  77. if (!node.active) continue;
  78. if (node.clamp)
  79. {
  80. // if (node.OutOfBounds()) continue;
  81. }
  82. inputCurrent = (node.inputKind == InputKind.Current);
  83. node.InitPreviewRenderTexture(true, node.name);
  84. if (totalBuffer == null && !inputCurrent)
  85. {
  86. totalBuffer = compute.RunNodeCompute(this, node, seedTotal);
  87. }
  88. else
  89. {
  90. if (!inputCurrent)
  91. {
  92. // Debug.Log(totalBuffer == null);
  93. nodeBuffer = compute.RunNodeCompute(this, node, seedTotal, totalBuffer, false);
  94. }
  95. else
  96. {
  97. for (int j = 0; j < node.iterations; j++) totalBuffer = compute.RunNodeCompute(this, node, seedTotal, totalBuffer, true);
  98. // if (preview && totalBuffer != null) { compute.DisposeBuffer(ref totalBuffer); }
  99. }
  100. // if (preview && nodeBuffer != null) { compute.DisposeBuffer(ref nodeBuffer); }
  101. }
  102. if (totalBuffer != null && nodeBuffer != null && !inputCurrent) compute.RunComputeMethod(this, node, totalBuffer, ref nodeBuffer, itemList.Count, i == lastActive ? rtPreview : null);
  103. }
  104. else
  105. {
  106. TC_NodeGroup nodeGroup = itemList[i] as TC_NodeGroup;
  107. if (nodeGroup != null)
  108. {
  109. if (!nodeGroup.active) continue;
  110. if (totalBuffer == null) totalBuffer = nodeGroup.ComputeValue(seedTotal);
  111. else nodeBuffer = nodeGroup.ComputeValue(seedTotal);
  112. if (totalBuffer != null && nodeBuffer != null) compute.RunComputeMethod(this, nodeGroup, totalBuffer, ref nodeBuffer, itemList.Count, i == lastActive ? rtPreview : null);
  113. }
  114. }
  115. }
  116. if (totalActive == 1)
  117. {
  118. TC_Compute.DisposeRenderTexture(ref rtPreview);
  119. rtDisplay = itemList[firstActive].rtDisplay;
  120. }
  121. if (isPortalCount > 0 && totalBuffer != null) TC_Compute.instance.MakePortalBuffer(this, totalBuffer);
  122. return totalBuffer;
  123. }
  124. public override void ChangeYPosition(float y) { for (int i = 0; i < itemList.Count; i++) itemList[i].ChangeYPosition(y); }
  125. public override void SetFirstLoad(bool active)
  126. {
  127. base.SetFirstLoad(active);
  128. for (int i = 0; i < itemList.Count; i++) itemList[i].SetFirstLoad(active);
  129. }
  130. public override bool ContainsCollisionNode()
  131. {
  132. for (int i = 0; i < itemList.Count; i++)
  133. {
  134. TC_Node node = itemList[i] as TC_Node;
  135. if (node != null)
  136. {
  137. if (node.active && node.visible)
  138. {
  139. if (node.inputKind == InputKind.Terrain && node.inputTerrain == InputTerrain.Collision) return true;
  140. }
  141. }
  142. }
  143. return false;
  144. }
  145. public override void GetItems(bool refresh, bool rebuildGlobalLists, bool resetTextures)
  146. {
  147. if (resetTextures) DisposeTextures();
  148. int childCount = transform.childCount;
  149. // Init();
  150. itemList.Clear();
  151. active = visible;
  152. firstActive = lastActive = -1;
  153. totalActive = 0;
  154. bool newBounds = true;
  155. int listIndex = 0;
  156. for (int i = childCount - 1; i >= 0; i--)
  157. {
  158. Transform child = t.GetChild(i);
  159. TC_Node node = child.GetComponent<TC_Node>();
  160. if (node != null)
  161. {
  162. if (resetTextures) node.DisposeTextures();
  163. node.active = true;
  164. node.Init();
  165. if (node.inputKind == InputKind.Current && totalActive == 0)
  166. {
  167. TC.AddMessage("'Current' can only be used if there is active node/s before it.");
  168. node.active = false;
  169. }
  170. if (!node.visible)
  171. {
  172. node.active = false;
  173. // Debug.Log(node.name);
  174. }
  175. node.SetParameters(this, listIndex);
  176. node.nodeGroupLevel = nodeGroupLevel + 1;
  177. node.nodeType = type;
  178. node.UpdateVersion();
  179. if (node.active)
  180. {
  181. if (node.clamp) node.CalcBounds();
  182. if (newBounds) { bounds = node.bounds; newBounds = false; }
  183. else bounds.Encapsulate(node.bounds);
  184. lastActive = listIndex;
  185. if (firstActive == -1) firstActive = lastActive;
  186. ++totalActive;
  187. }
  188. if (i == childCount - 1) // TODO: Consider hide and do in calculation
  189. {
  190. if (node.method != Method.Add && node.method != Method.Subtract) node.method = Method.Add;
  191. }
  192. itemList.Add(node);
  193. ++listIndex;
  194. }
  195. else
  196. {
  197. TC_NodeGroup nodeGroup = child.GetComponent<TC_NodeGroup>();
  198. if (nodeGroup != null)
  199. {
  200. nodeGroup.SetParameters(this, listIndex);
  201. nodeGroup.nodeGroupLevel = nodeGroupLevel + 1;
  202. itemList.Add(nodeGroup);
  203. ++listIndex;
  204. nodeGroup.GetItems(refresh, rebuildGlobalLists, resetTextures);
  205. if (nodeGroup.active)
  206. {
  207. lastActive = listIndex;
  208. if (firstActive == -1) firstActive = lastActive;
  209. ++totalActive;
  210. }
  211. }
  212. //else
  213. //{
  214. // TC_NodeClone nodeClone = child.GetComponent<TC_NodeClone>();
  215. //}
  216. }
  217. }
  218. if (itemList.Count == 1)
  219. {
  220. if (itemList[0].active) active = visible = true;
  221. }
  222. if (!active) totalActive = 0;
  223. if (totalActive == 0) active = false;
  224. }
  225. }
  226. }