-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlimeShaderColour.compute
221 lines (181 loc) · 6.42 KB
/
SlimeShaderColour.compute
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel Update
int width;
int height;
int numAgents;
int sensorSize;
float sensorAngleDegrees;
float sensorOffsetDist;
float turnSpeed;
float moveSpeed;
float trailWeight;
const float constDeltaTime = 1 / 60;
float deltaTime;
float time;
struct Agent
{
float2 position;
float angle;
};
RWStructuredBuffer<Agent> agents;
RWTexture2D<float4> TrailMap;
float rand(float2 sd)
{
return (frac(sin(dot(sd.xy, float2(12.9898, 78.233)))) * 43758.5435) * 1;
}
// 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, float sensorAngleOffset)
{
float sensorAngle = agent.angle + sensorAngleOffset;
float2 sensorDir = float2(cos(sensorAngle), sin(sensorAngle));
float2 sensorPos = agent.position + sensorDir * sensorOffsetDist;
int sensorCentreX = (int)sensorPos.x;
int sensorCentreY = (int)sensorPos.y;
float sum = 0;
// Sensor domain is square of side length 2*sensorSize + 1
for (int offsetX = -sensorSize; offsetX <= sensorSize; offsetX ++)
{
for (int offsetY = -sensorSize; offsetY <= sensorSize; offsetY ++)
{
int sampleX = min(width - 1, max(0, sensorCentreX + offsetX));
int sampleY = min(height - 1, max(0, sensorCentreY + offsetY));
sum += TrailMap[int2(sampleX,sampleY)];
}
}
return sum;
}
// Run through agents not pixels
[numthreads(16,1,1)]
void Update (uint3 id : SV_DispatchThreadID)
{
// Exits when exceeding
if (id.x >= numAgents) {return;}
Agent agent = agents[id.x];
float2 pos = agent.position;
uint random = hash(pos.y * width + pos.x + hash(id.x + time * 100000));
// Sensory stuff
float sensorAngleRad = (sensorAngleDegrees / 180) * 3.1415;
float weightForward = sense(agent, 0);
float weightLeft = sense(agent, sensorAngleRad);
float weightRight = sense(agent, -sensorAngleRad);
float randomSteerStrength = scaleToRange01(random);
turnSpeed *= 2 * 3.14;
// 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;
}
// Move agent
float2 direction = float2(cos(agent.angle), sin(agent.angle));
float2 newPos = pos + direction * moveSpeed * deltaTime;
// Clamp pos
if (newPos.x < 0 || newPos.x >= width || newPos.y < 0 || newPos.y >= height)
{
random = hash(random);
newPos.x = min(width-1, max(0, newPos.x));
newPos.y = min(height-1, max(0, newPos.y));
agents[id.x].angle = scaleToRange01(random) * 2 * 3.1415;
}
// Set new pos and draw trail
agents[id.x].position = newPos;
float4 value = TrailMap[int2(newPos.x, newPos.y)];
//TrailMap[int2(newPos.x, newPos.y)] = value.r + 0.2;
//TrailMap[int2(newPos.x, newPos.y)] = min(float4(1,1,1,1), value + float4(1,1,1,0) * trailWeight);
TrailMap[int2(newPos.x, newPos.y)] = min(float4(1,1,1,0), value + trailWeight);
}
#pragma kernel ProcessTrailMap
RWTexture2D<float4> ProcessedTrailMap;
float diffusionSpeed;
float evaporationSpeed;
[numthreads(8,8,1)]
void ProcessTrailMap (uint3 id : SV_DispatchThreadID)
{
if (id.x < 0 || id.x >= width || id.y < 0 || id.y >= height) { return; }
float4 originalValue = TrailMap[id.xy]; // .xy is a float2
float4 sum = 0;
// 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 blurResult = sum / 9;
// Lerp between original value and new to create blur
float4 diffusedValue = lerp(originalValue, blurResult, diffusionSpeed * deltaTime);
// Evaporation
float4 diffusedAndEvaporatedValue = max(0, diffusedValue - evaporationSpeed * deltaTime);
//float4 diffusedAndEvaporatedValue = max(0, originalValue - evaporationSpeed * deltaTime);
ProcessedTrailMap[id.xy] = diffusedAndEvaporatedValue;
}
#pragma kernel PostProcessEffects
RWTexture2D<float4> PostProcessedTrailMap;
Texture2D<float4> Image;
int numColors;
float colorPointDecay;
struct ColorPoint
{
float2 position;
float2 velocity;
float4 color;
};
RWStructuredBuffer<ColorPoint> colorPoints;
[numthreads(8,8,1)]
void PostProcessEffects (uint3 id : SV_DispatchThreadID)
{
if (id.x < 0 || id.x >= width || id.y < 0 || id.y >= height) { return; }
// Stupid I know
if (id.x == 0 && id.y == 0){
for (int i=0; i<numColors; i++)
{
ColorPoint cp = colorPoints[i];
cp.position += cp.velocity * deltaTime;
if(cp.position.x <= 0 || cp.position.x >= width){cp.velocity.x = abs(cp.velocity.x)*-sign(cp.velocity.x);}
if(cp.position.y <= 0 || cp.position.y >= height){cp.velocity.y = abs(cp.velocity.y)*-sign(cp.velocity.y);}
colorPoints[i] = cp;
}
}
// Pixel color stuff
float4 originalValue = ProcessedTrailMap[id.xy];
float4 color = float4(1,1,1,1);
for (int i=0; i<numColors; i++)
{
ColorPoint cp = colorPoints[i];
float dist = distance(cp.position, float2(id.x, id.y));
color = lerp(color, cp.color, max(1-pow(colorPointDecay * dist, 2), 0));
}
color = lerp(float4(1,1,1,1), color, pow(originalValue.x,1));
if (color.r>=0.9 && color.g>=0.9 && color.b>=0.9) {
color = lerp(color, float4(0,0,0,1), distance(float2(id.x,id.y), float2(width/2,height/2))/500);}
originalValue = color;
// Pixel image stuff
// color = lerp(color, Image[id.xy], originalValue);
// originalValue = color;
//PostProcessedTrailMap[id.xy] = float4(1 - originalValue.x, 1 - originalValue.y, 1 - originalValue.z, 1);
PostProcessedTrailMap[id.xy] = originalValue;
}