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