-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathSimulation.cs
More file actions
178 lines (135 loc) · 4.88 KB
/
Simulation.cs
File metadata and controls
178 lines (135 loc) · 4.88 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using ComputeShaderUtility;
public class Simulation : MonoBehaviour
{
public enum SpawnMode { Random, Point, InwardCircle, RandomCircle }
const int updateKernel = 0;
const int diffuseMapKernel = 1;
public ComputeShader compute;
public ComputeShader drawAgentsCS;
public SlimeSettings settings;
[Header("Display Settings")]
public bool showAgentsOnly;
public FilterMode filterMode = FilterMode.Point;
public GraphicsFormat format = ComputeHelper.defaultGraphicsFormat;
[SerializeField, HideInInspector] protected RenderTexture trailMap;
[SerializeField, HideInInspector] protected RenderTexture diffusedTrailMap;
[SerializeField, HideInInspector] protected RenderTexture displayTexture;
ComputeBuffer agentBuffer;
ComputeBuffer settingsBuffer;
Texture2D colourMapTexture;
protected virtual void Start()
{
Init();
transform.GetComponentInChildren<MeshRenderer>().material.mainTexture = displayTexture;
}
void Init()
{
// Create render textures
ComputeHelper.CreateRenderTexture(ref trailMap, settings.width, settings.height, filterMode, format);
ComputeHelper.CreateRenderTexture(ref diffusedTrailMap, settings.width, settings.height, filterMode, format);
ComputeHelper.CreateRenderTexture(ref displayTexture, settings.width, settings.height, filterMode, format);
// Create agents with initial positions and angles
Agent[] agents = new Agent[settings.numAgents];
for (int i = 0; i < agents.Length; i++)
{
Vector2 centre = new Vector2(settings.width / 2, settings.height / 2);
Vector2 startPos = Vector2.zero;
float randomAngle = Random.value * Mathf.PI * 2;
float angle = 0;
if (settings.spawnMode == SpawnMode.Point)
{
startPos = centre;
angle = randomAngle;
}
else if (settings.spawnMode == SpawnMode.Random)
{
startPos = new Vector2(Random.Range(0, settings.width), Random.Range(0, settings.height));
angle = randomAngle;
}
else if (settings.spawnMode == SpawnMode.InwardCircle)
{
startPos = centre + Random.insideUnitCircle * settings.height * 0.5f;
angle = Mathf.Atan2((centre - startPos).normalized.y, (centre - startPos).normalized.x);
}
else if (settings.spawnMode == SpawnMode.RandomCircle)
{
startPos = centre + Random.insideUnitCircle * settings.height * 0.15f;
angle = randomAngle;
}
Vector3Int speciesMask;
int speciesIndex = 0;
int numSpecies = settings.speciesSettings.Length;
if (numSpecies == 1)
{
speciesMask = Vector3Int.one;
}
else
{
int species = Random.Range(1, numSpecies + 1);
speciesIndex = species - 1;
speciesMask = new Vector3Int((species == 1) ? 1 : 0, (species == 2) ? 1 : 0, (species == 3) ? 1 : 0);
}
agents[i] = new Agent() { position = startPos, angle = angle, speciesMask = speciesMask, speciesIndex = speciesIndex };
}
ComputeHelper.CreateAndSetBuffer<Agent>(ref agentBuffer, agents, compute, "agents", updateKernel);
compute.SetInt("numAgents", settings.numAgents);
drawAgentsCS.SetBuffer(0, "agents", agentBuffer);
drawAgentsCS.SetInt("numAgents", settings.numAgents);
compute.SetInt("width", settings.width);
compute.SetInt("height", settings.height);
}
void FixedUpdate()
{
for (int i = 0; i < settings.stepsPerFrame; i++)
{
RunSimulation();
}
}
void LateUpdate()
{
if (showAgentsOnly)
{
ComputeHelper.ClearRenderTexture(displayTexture);
drawAgentsCS.SetTexture(0, "TargetTexture", displayTexture);
ComputeHelper.Dispatch(drawAgentsCS, settings.numAgents, 1, 1, 0);
}
else
{
ComputeHelper.CopyRenderTexture(trailMap, displayTexture);
}
}
void RunSimulation()
{
var speciesSettings = settings.speciesSettings;
ComputeHelper.CreateStructuredBuffer(ref settingsBuffer, speciesSettings);
compute.SetBuffer(0, "speciesSettings", settingsBuffer);
// Assign textures
compute.SetTexture(updateKernel, "TrailMap", trailMap);
compute.SetTexture(updateKernel, "DiffusedTrailMap", diffusedTrailMap);
compute.SetTexture(diffuseMapKernel, "TrailMap", trailMap);
compute.SetTexture(diffuseMapKernel, "DiffusedTrailMap", diffusedTrailMap);
// Assign settings
compute.SetFloat("deltaTime", Time.fixedDeltaTime);
compute.SetFloat("time", Time.fixedTime);
compute.SetFloat("trailWeight", settings.trailWeight);
compute.SetFloat("decayRate", settings.decayRate);
compute.SetFloat("diffuseRate", settings.diffuseRate);
ComputeHelper.Dispatch(compute, settings.numAgents, 1, 1, kernelIndex: updateKernel);
ComputeHelper.Dispatch(compute, settings.width, settings.height, 1, kernelIndex: diffuseMapKernel);
ComputeHelper.CopyRenderTexture(diffusedTrailMap, trailMap);
}
void OnDestroy()
{
ComputeHelper.Release(agentBuffer, settingsBuffer);
}
public struct Agent
{
public Vector2 position;
public float angle;
public Vector3Int speciesMask;
int unusedSpeciesChannel;
public int speciesIndex;
}
}