-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathSlimeSim.compute
More file actions
168 lines (130 loc) · 4.39 KB
/
SlimeSim.compute
File metadata and controls
168 lines (130 loc) · 4.39 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
#pragma kernel Update
struct Agent {
float2 position;
float angle;
int4 speciesMask;
int speciesIndex;
};
struct SpeciesSettings {
float moveSpeed;
float turnSpeed;
float sensorAngleDegrees;
float sensorOffsetDst;
int sensorSize;
};
StructuredBuffer<SpeciesSettings> speciesSettings;
RWStructuredBuffer<Agent> agents;
uint numAgents;
RWTexture2D<float4> TrailMap;
RWTexture2D<float4> DiffusedTrailMap;
int width;
int height;
float trailWeight;
float deltaTime;
float time;
// Hash function www.cs.ubc.ca/~rbridson/docs/schechter-sca08-turbulence.pdf
uint hash(uint state)
{
state ^= 2747636419u;
state *= 2654435769u;
state ^= state >> 16;
state *= 2654435769u;
state ^= state >> 16;
state *= 2654435769u;
return state;
}
float scaleToRange01(uint state)
{
return state / 4294967295.0;
}
float sense(Agent agent, SpeciesSettings settings, float sensorAngleOffset) {
float sensorAngle = agent.angle + sensorAngleOffset;
float2 sensorDir = float2(cos(sensorAngle), sin(sensorAngle));
float2 sensorPos = agent.position + sensorDir * settings.sensorOffsetDst;
int sensorCentreX = (int) sensorPos.x;
int sensorCentreY = (int) sensorPos.y;
float sum = 0;
int4 senseWeight = agent.speciesMask * 2 - 1;
for (int offsetX = -settings.sensorSize; offsetX <= settings.sensorSize; offsetX ++) {
for (int offsetY = -settings.sensorSize; offsetY <= settings.sensorSize; offsetY ++) {
int sampleX = min(width - 1, max(0, sensorCentreX + offsetX));
int sampleY = min(height - 1, max(0, sensorCentreY + offsetY));
sum += dot(senseWeight, DiffusedTrailMap[int2(sampleX,sampleY)]);
}
}
return sum;
}
[numthreads(16,1,1)]
void Update (uint3 id : SV_DispatchThreadID)
{
if (id.x >= numAgents) {
return;
}
Agent agent = agents[id.x];
SpeciesSettings settings = speciesSettings[agent.speciesIndex];
float2 pos = agent.position;
uint random = hash(pos.y * width + pos.x + hash(id.x + time * 100000));
// Steer based on sensory data
float sensorAngleRad = settings.sensorAngleDegrees * (3.1415 / 180);
float weightForward = sense(agent, settings, 0);
float weightLeft = sense(agent, settings, sensorAngleRad);
float weightRight = sense(agent, settings, -sensorAngleRad);
float randomSteerStrength = scaleToRange01(random);
float turnSpeed = settings.turnSpeed * 2 * 3.1415;
// Continue in same direction
if (weightForward > weightLeft && weightForward > weightRight) {
agents[id.x].angle += 0;
}
else if (weightForward < weightLeft && weightForward < weightRight) {
agents[id.x].angle += (randomSteerStrength - 0.5) * 2 * turnSpeed * deltaTime;
}
// Turn right
else if (weightRight > weightLeft) {
agents[id.x].angle -= randomSteerStrength * turnSpeed * deltaTime;
}
// Turn left
else if (weightLeft > weightRight) {
agents[id.x].angle += randomSteerStrength * turnSpeed * deltaTime;
}
// Update position
float2 direction = float2(cos(agent.angle), sin(agent.angle));
float2 newPos = agent.position + direction * deltaTime * settings.moveSpeed;
// Clamp position to map boundaries, and pick new random move dir if hit boundary
if (newPos.x < 0 || newPos.x >= width || newPos.y < 0 || newPos.y >= height) {
random = hash(random);
float randomAngle = scaleToRange01(random) * 2 * 3.1415;
newPos.x = min(width-1,max(0, newPos.x));
newPos.y = min(height-1,max(0, newPos.y));
agents[id.x].angle = randomAngle;
}
else {
float4 oldTrail = DiffusedTrailMap[int2(newPos)];
TrailMap[int2(newPos)] = min(1, oldTrail + agent.speciesMask * trailWeight * deltaTime);
}
agents[id.x].position = newPos;
}
#pragma kernel Diffuse
float decayRate;
float diffuseRate;
[numthreads(8,8,1)]
void Diffuse (uint3 id : SV_DispatchThreadID)
{
if (id.x < 0 || id.x >= (uint)width || id.y < 0 || id.y >= (uint)height) {
return;
}
float4 sum = 0;
float4 originalCol = TrailMap[id.xy];
// 3x3 blur
for (int offsetX = -1; offsetX <= 1; offsetX ++) {
for (int offsetY = -1; offsetY <= 1; offsetY ++) {
int sampleX = min(width-1, max(0, id.x + offsetX));
int sampleY = min(height-1, max(0, id.y + offsetY));
sum += TrailMap[int2(sampleX,sampleY)];
}
}
float4 blurredCol = sum / 9;
float diffuseWeight = saturate(diffuseRate * deltaTime);
blurredCol = originalCol * (1 - diffuseWeight) + blurredCol * (diffuseWeight);
//DiffusedTrailMap[id.xy] = blurredCol * saturate(1 - decayRate * deltaTime);
DiffusedTrailMap[id.xy] = max(0, blurredCol - decayRate * deltaTime);
}