Skip to content

Commit 143e25c

Browse files
committed
Disable unused tiles.
Random seed is local to the tile - removed member. Pool size overflow: fixed detection and added console warning.
1 parent b570b9d commit 143e25c

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

Assets/Examples/Scripts/RandomInstancing.cs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public class RandomInstancing : MonoBehaviour
1717
public float m_Size = 100.0f;
1818

1919
List<Transform> m_Instances = new List<Transform>();
20-
int m_Seed;
2120
int m_Used;
2221
int m_LocX, m_LocZ;
2322

@@ -26,6 +25,7 @@ void Awake()
2625
for (int i = 0; i < m_PoolSize; ++i)
2726
{
2827
var go = Instantiate(m_Prefab, Vector3.zero, Quaternion.identity) as GameObject;
28+
go.SetActive(false);
2929
m_Instances.Add(go.transform);
3030
}
3131
}
@@ -67,51 +67,62 @@ void UpdateInstances()
6767
{
6868
for (var j = z - 2; j <= z + 2; ++j)
6969
{
70-
if (m_Used >= m_PoolSize - 1)
70+
var count = UpdateTileInstances(i, j);
71+
if (count != m_InstancesPerTile)
7172
return;
72-
UpdateTileInstances(i, j);
7373
}
7474
}
75+
76+
// Deactivate the remaining active elements in the pool.
77+
// Here we assume all active elements are contiguous and first in the list.
78+
for (int i = m_Used; i < m_PoolSize && m_Instances[i].gameObject.activeSelf; ++i)
79+
m_Instances[i].gameObject.SetActive(false);
7580
}
7681

77-
void UpdateTileInstances(int i, int j)
82+
int UpdateTileInstances(int i, int j)
7883
{
79-
m_Seed = Hash2(i, j) ^ m_BaseHash;
80-
for (var k = 0; k < m_InstancesPerTile; ++k)
84+
var seed = Hash2(i, j) ^ m_BaseHash;
85+
var count = System.Math.Min(m_InstancesPerTile, m_PoolSize - m_Used);
86+
for (var end = m_Used + count; m_Used < end; ++m_Used)
8187
{
8288
float x = 0;
8389
float y = 0;
8490

8591
if (m_RandomPosition)
8692
{
87-
x = Random();
88-
y = Random();
93+
x = Random(ref seed);
94+
y = Random(ref seed);
8995
}
9096
var pos = new Vector3((i + x) * m_Size, m_Height, (j + y) * m_Size);
9197

9298
if (m_RandomOrientation)
9399
{
94-
float r = 360.0f * Random();
100+
float r = 360.0f * Random(ref seed);
95101
m_Instances[m_Used].rotation = Quaternion.AngleAxis(r, Vector3.up);
96102
}
97103
m_Instances[m_Used].position = pos;
98-
m_Used++;
104+
m_Instances[m_Used].gameObject.SetActive(true);
99105
}
106+
107+
if (count < m_InstancesPerTile)
108+
Debug.LogWarning("Pool exhausted", this);
109+
110+
return count;
100111
}
101112

102113
static int Hash2(int i, int j)
103114
{
104115
return (i * 73856093) ^ (j * 19349663);
105116
}
106117

107-
float Random()
118+
static float Random(ref int seed)
108119
{
109-
m_Seed = (m_Seed ^ 123459876);
110-
var k = m_Seed / 127773;
111-
m_Seed = 16807 * (m_Seed - k * 127773) - 2836 * k;
112-
if (m_Seed < 0) m_Seed = m_Seed + 2147483647;
113-
float ran0 = m_Seed * 1.0f / 2147483647.0f;
114-
m_Seed = (m_Seed ^ 123459876);
120+
seed = (seed ^ 123459876);
121+
var k = seed / 127773;
122+
seed = 16807 * (seed - k * 127773) - 2836 * k;
123+
if (seed < 0) seed = seed + 2147483647;
124+
float ran0 = seed * 1.0f / 2147483647.0f;
125+
seed = (seed ^ 123459876);
115126
return ran0;
116127
}
117128
}

0 commit comments

Comments
 (0)