Advertisement
logancberrypie

Object Placer

Jun 10th, 2020
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class ObjectPlacer : MonoBehaviour
  7. {
  8. private float maxWeight = 0;
  9.  
  10.  
  11. public float radius = 32;
  12. public Vector2 regionSize = new Vector2(500, 500);
  13. public int rejectionSamples = 30;
  14. public float displayRadius = 8;
  15.  
  16. public bool generate = false;
  17.  
  18. List<Vector2> points;
  19.  
  20. public float defaultheight = 80;
  21. public float maxRayCastDistance = 100;
  22. public GameObject prefab;
  23. public Transform parant;
  24.  
  25. [SerializeField]
  26. private List<GOArray> myPrefabs;
  27.  
  28.  
  29. [Header("Prefab Instances")]
  30. [Space(20)]
  31. public GameObject GameAsset_Environment_Bush_Azalea_Low;
  32. public GameObject GameAsset_Environment_Bush_AzaleaFlowers_Pink;
  33. public GameObject GameAsset_Environment_Bush_AzaleaFlowers_White;
  34. public GameObject GameAsset_Environment_Bush_AzaleaPatch_Flowers_Low;
  35. public GameObject GameAsset_Environment_Rock_Ice3;
  36. public GameObject GameAsset_Environment_Rock_Sandstone17;
  37. public GameObject GameAsset_Environment_Rock_Snowy2;
  38. public GameObject GameAsset_Environment_Tree_AlaskaCedar_Young1;
  39. public GameObject GameAsset_Environment_Tree_Blue_Spruce_Broken1;
  40. public GameObject GameAsset_Environment_Tree_Broadleaf6;
  41. public GameObject NodePosition;
  42.  
  43.  
  44. public void Update()
  45. {
  46. if (generate)
  47. {
  48. DestroyChildren();
  49. points = ObjectPlacement.GeneratePoints(radius, regionSize, rejectionSamples);
  50. PlaceObjects(points);
  51. generate = false;
  52. }
  53.  
  54. }
  55.  
  56. void OnDrawGizmos()
  57. {
  58. Gizmos.DrawWireCube(regionSize / 2, regionSize);
  59. if (points != null)
  60. {
  61. foreach (Vector2 point in points)
  62. {
  63. Gizmos.DrawSphere(point, displayRadius);
  64. }
  65. }
  66. }
  67. private void PlaceObjects(List<Vector2> points)
  68. {
  69. Vector3 testDirection = new Vector3(0, -1, 0);
  70. foreach (Vector2 point in points)
  71. {
  72. Vector3 testPosition = new Vector3(point.x, defaultheight, point.y);
  73. RaycastHit hit;
  74. Ray raycast = new Ray(testPosition, testDirection);
  75. if (Physics.Raycast(raycast, out hit, maxRayCastDistance))
  76. {
  77. Debug.Log("HIT");
  78. float distance = hit.distance;
  79. float angle = Vector3.Angle(hit.normal, testDirection);
  80. //Debug.Log(angle);
  81. testPosition.y = testPosition.y - distance;
  82. GameObject prefabForInstantiate = getPrefabObject();
  83. Instantiate(prefabForInstantiate, testPosition, Quaternion.identity, parant);
  84.  
  85. }
  86. else
  87. {
  88. Debug.DrawRay(raycast.origin, raycast.direction * maxRayCastDistance, Color.white, 20);
  89. Debug.Log("Hit fail");
  90. }
  91.  
  92. }
  93. }
  94.  
  95. private GameObject getPrefabObject()
  96. {
  97. maxWeight = 0;
  98. foreach (GOArray tmp in myPrefabs)
  99. {
  100. maxWeight += tmp.prefabs.relativeWeight;
  101. }
  102. float currentWeight = maxWeight;
  103. float ranValue = UnityEngine.Random.Range(0, maxWeight);
  104. foreach (GOArray tmp in myPrefabs)
  105. {
  106. if (ranValue <= tmp.prefabs.relativeWeight)
  107. {
  108. return EnumToPrefab(tmp.prefabs.Prefab);
  109. }
  110. else
  111. {
  112. ranValue -= tmp.prefabs.relativeWeight;
  113. }
  114. }
  115. Debug.LogError("No valid ENUM/PREFAB selected");
  116. return EnumToPrefab(PrefabEnums.SpawnNode);
  117. }
  118.  
  119. private void DestroyChildren()
  120. {
  121. foreach (Transform child in parant)
  122. {
  123. GameObject.Destroy(child.gameObject);
  124. }
  125. }
  126. [System.Serializable]
  127. public struct weightedPrefab
  128. {
  129. [SerializeField]
  130. public PrefabEnums Prefab;
  131. [SerializeField]
  132. [Range(0,1)]
  133. public float relativeWeight;
  134. }
  135.  
  136. [System.Serializable]
  137. public class GOArray
  138. {
  139. [SerializeField]
  140. public weightedPrefab prefabs;
  141. }
  142.  
  143. public enum PrefabEnums {
  144. GameAsset_Environment_Bush_Azalea_Low,
  145. GameAsset_Environment_Bush_AzaleaFlowers_Pink,
  146. GameAsset_Environment_Bush_AzaleaFlowers_White,
  147. GameAsset_Environment_Bush_AzaleaPatch_Flowers_Low,
  148. GameAsset_Environment_Rock_Ice3,
  149. GameAsset_Environment_Rock_Sandstone17,
  150. GameAsset_Environment_Rock_Snowy2,
  151. GameAsset_Environment_Tree_AlaskaCedar_Young1,
  152. GameAsset_Environment_Tree_Blue_Spruce_Broken1,
  153. GameAsset_Environment_Tree_Broadleaf6,
  154. SpawnNode }
  155.  
  156. public void Start()
  157. {
  158. var tmp = Enum.GetValues(typeof(PrefabEnums));
  159. foreach(PrefabEnums tempEnum in tmp)
  160. {
  161. GOArray temp = new GOArray();
  162. temp.prefabs = new weightedPrefab();
  163. temp.prefabs.Prefab = tempEnum;
  164. temp.prefabs.relativeWeight = 1;
  165. myPrefabs.Add(temp);
  166. }
  167. Debug.Log("Finished");
  168. }
  169.  
  170. private GameObject EnumToPrefab(PrefabEnums inputEnum)
  171. {
  172. switch (inputEnum)
  173. {
  174. case PrefabEnums.GameAsset_Environment_Bush_Azalea_Low:
  175. return GameAsset_Environment_Bush_Azalea_Low;
  176. case PrefabEnums.GameAsset_Environment_Bush_AzaleaFlowers_Pink:
  177. return GameAsset_Environment_Bush_AzaleaFlowers_Pink;
  178. case PrefabEnums.GameAsset_Environment_Bush_AzaleaFlowers_White:
  179. return GameAsset_Environment_Bush_AzaleaFlowers_White;
  180. case PrefabEnums.GameAsset_Environment_Bush_AzaleaPatch_Flowers_Low:
  181. return GameAsset_Environment_Bush_AzaleaPatch_Flowers_Low;
  182. case PrefabEnums.GameAsset_Environment_Rock_Ice3:
  183. return GameAsset_Environment_Rock_Ice3;
  184. case PrefabEnums.GameAsset_Environment_Rock_Sandstone17:
  185. return GameAsset_Environment_Rock_Sandstone17;
  186. case PrefabEnums.GameAsset_Environment_Rock_Snowy2:
  187. return GameAsset_Environment_Rock_Snowy2;
  188. case PrefabEnums.GameAsset_Environment_Tree_AlaskaCedar_Young1:
  189. return GameAsset_Environment_Tree_AlaskaCedar_Young1;
  190. case PrefabEnums.GameAsset_Environment_Tree_Blue_Spruce_Broken1:
  191. return GameAsset_Environment_Tree_Blue_Spruce_Broken1;
  192. case PrefabEnums.GameAsset_Environment_Tree_Broadleaf6:
  193. return GameAsset_Environment_Tree_Broadleaf6;
  194. case PrefabEnums.SpawnNode:
  195. return NodePosition;
  196. default:
  197. Debug.LogError("Invalid ENUM has been entered");
  198. return NodePosition;
  199. }
  200.  
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement