RandomInstancing.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. // Fill 5x5 tiles around the local position procedurally by instantiating prefabs at random positions/orientations
  4. [DefaultExecutionOrder(-200)]
  5. public class RandomInstancing : MonoBehaviour
  6. {
  7. public GameObject m_Prefab;
  8. public int m_PoolSize = 250;
  9. public int m_InstancesPerTile = 10;
  10. public bool m_RandomPosition = true;
  11. public bool m_RandomOrientation = true;
  12. public float m_Height;
  13. public int m_BaseHash = 347652783;
  14. public float m_Size = 100.0f;
  15. List<Transform> m_Instances = new List<Transform>();
  16. int m_Used;
  17. int m_LocX, m_LocZ;
  18. void Awake()
  19. {
  20. for (int i = 0; i < m_PoolSize; ++i)
  21. {
  22. var go = Instantiate(m_Prefab, Vector3.zero, Quaternion.identity) as GameObject;
  23. go.SetActive(false);
  24. m_Instances.Add(go.transform);
  25. }
  26. }
  27. void OnEnable()
  28. {
  29. m_LocX = ~0;
  30. m_LocZ = ~0;
  31. UpdateInstances();
  32. }
  33. void OnDestroy()
  34. {
  35. for (int i = 0; i < m_Instances.Count; ++i)
  36. {
  37. if (m_Instances[i])
  38. Destroy(m_Instances[i].gameObject);
  39. }
  40. m_Instances.Clear();
  41. }
  42. void Update()
  43. {
  44. UpdateInstances();
  45. }
  46. void UpdateInstances()
  47. {
  48. var x = (int)Mathf.Floor(transform.position.x / m_Size);
  49. var z = (int)Mathf.Floor(transform.position.z / m_Size);
  50. if (x == m_LocX && z == m_LocZ)
  51. return;
  52. m_LocX = x;
  53. m_LocZ = z;
  54. m_Used = 0;
  55. for (var i = x - 2; i <= x + 2; ++i)
  56. {
  57. for (var j = z - 2; j <= z + 2; ++j)
  58. {
  59. var count = UpdateTileInstances(i, j);
  60. if (count != m_InstancesPerTile)
  61. return;
  62. }
  63. }
  64. // Deactivate the remaining active elements in the pool.
  65. // Here we assume all active elements are contiguous and first in the list.
  66. for (int i = m_Used; i < m_PoolSize && m_Instances[i].gameObject.activeSelf; ++i)
  67. m_Instances[i].gameObject.SetActive(false);
  68. }
  69. int UpdateTileInstances(int i, int j)
  70. {
  71. var seed = Hash2(i, j) ^ m_BaseHash;
  72. var count = System.Math.Min(m_InstancesPerTile, m_PoolSize - m_Used);
  73. for (var end = m_Used + count; m_Used < end; ++m_Used)
  74. {
  75. float x = 0;
  76. float y = 0;
  77. if (m_RandomPosition)
  78. {
  79. x = Random(ref seed);
  80. y = Random(ref seed);
  81. }
  82. var pos = new Vector3((i + x) * m_Size, m_Height, (j + y) * m_Size);
  83. if (m_RandomOrientation)
  84. {
  85. float r = 360.0f * Random(ref seed);
  86. m_Instances[m_Used].rotation = Quaternion.AngleAxis(r, Vector3.up);
  87. }
  88. m_Instances[m_Used].position = pos;
  89. m_Instances[m_Used].gameObject.SetActive(true);
  90. }
  91. if (count < m_InstancesPerTile)
  92. Debug.LogWarning("Pool exhausted", this);
  93. return count;
  94. }
  95. static int Hash2(int i, int j)
  96. {
  97. return (i * 73856093) ^ (j * 19349663);
  98. }
  99. static float Random(ref int seed)
  100. {
  101. seed = (seed ^ 123459876);
  102. var k = seed / 127773;
  103. seed = 16807 * (seed - k * 127773) - 2836 * k;
  104. if (seed < 0) seed = seed + 2147483647;
  105. float ran0 = seed * 1.0f / 2147483647.0f;
  106. seed = (seed ^ 123459876);
  107. return ran0;
  108. }
  109. }