-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboidsSimGPU.html
More file actions
235 lines (218 loc) · 7.48 KB
/
boidsSimGPU.html
File metadata and controls
235 lines (218 loc) · 7.48 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>GPU Boids WebGL2 (Fixed)</title>
<style>
html, body { margin: 0; padding: 0; overflow: hidden; background: black; }
canvas { display: block; width: 100vw; height: 100vh; }
</style>
</head>
<body>
<canvas id="glCanvas"></canvas>
<script src="https://cdn.jsdelivr.net/npm/dat.gui@0.7.9/build/dat.gui.min.js"></script>
<script type="module">
// Simplified working GPU boids example with fixed framebuffer formats
const canvas = document.getElementById('glCanvas');
const gl = canvas.getContext('webgl2');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
if (!gl) alert("WebGL2 is required.");
const settings = {
numBoids: 256,
align: 1.0,
cohesion: 0.5,
separation: 1.5,
speed: 1.0,
perception: 0.05
};
const gui = new dat.GUI();
gui.add(settings, 'numBoids', 64, 1024).step(1).onChange(() => location.reload());
gui.add(settings, 'align', 0, 2);
gui.add(settings, 'cohesion', 0, 2);
gui.add(settings, 'separation', 0, 2);
gui.add(settings, 'speed', 0.1, 5);
gui.add(settings, 'perception', 0.01, 0.2);
const simSize = Math.ceil(Math.sqrt(settings.numBoids));
const texSize = simSize;
function createFBO(w, h) {
const tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, w, h, 0, gl.RGBA, gl.FLOAT, null);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
const fbo = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
return { fbo, tex };
}
const posA = createFBO(texSize, texSize);
const posB = createFBO(texSize, texSize);
const velA = createFBO(texSize, texSize);
const velB = createFBO(texSize, texSize);
function fillTex(fbo, fill) {
const data = new Float32Array(texSize * texSize * 4);
for (let i = 0; i < texSize * texSize; i++) fill(data, i);
gl.bindTexture(gl.TEXTURE_2D, fbo.tex);
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, texSize, texSize, gl.RGBA, gl.FLOAT, data);
}
fillTex(posA, (d, i) => { d[i*4] = Math.random(); d[i*4+1] = Math.random(); });
fillTex(velA, (d, i) => {
const a = Math.random() * 6.28;
d[i*4] = Math.cos(a) * settings.speed;
d[i*4+1] = Math.sin(a) * settings.speed;
});
const quadVAO = gl.createVertexArray();
const quadVBO = gl.createBuffer();
gl.bindVertexArray(quadVAO);
gl.bindBuffer(gl.ARRAY_BUFFER, quadVBO);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1,-1,1,-1,-1,1,1,1]), gl.STATIC_DRAW);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
function compile(type, src) {
const s = gl.createShader(type);
gl.shaderSource(s, src);
gl.compileShader(s);
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) throw gl.getShaderInfoLog(s);
return s;
}
function link(vs, fs) {
const p = gl.createProgram();
gl.attachShader(p, compile(gl.VERTEX_SHADER, vs));
gl.attachShader(p, compile(gl.FRAGMENT_SHADER, fs));
gl.linkProgram(p);
if (!gl.getProgramParameter(p, gl.LINK_STATUS)) throw gl.getProgramInfoLog(p);
return p;
}
const simProg = link(`#version 300 es
in vec2 a_position;
out vec2 v_uv;
void main() {
v_uv = a_position * 0.5 + 0.5;
gl_Position = vec4(a_position, 0, 1);
}`, `#version 300 es
precision highp float;
uniform sampler2D u_pos, u_vel;
uniform float u_speed, u_align, u_cohesion, u_separation, u_perception, u_size;
in vec2 v_uv;
out vec4 outColor;
void main() {
vec2 pos = texture(u_pos, v_uv).xy;
vec2 vel = texture(u_vel, v_uv).xy;
vec2 align = vec2(0), cohesion = vec2(0), separation = vec2(0);
int count = 0;
for (int y = 0; y < int(u_size); y++) {
for (int x = 0; x < int(u_size); x++) {
vec2 uv = (vec2(x,y)+0.5)/u_size;
vec2 p = texture(u_pos, uv).xy;
vec2 v = texture(u_vel, uv).xy;
float d = distance(p, pos);
if (d < u_perception && d > 0.0) {
align += v;
cohesion += p;
separation += (pos - p)/d;
count++;
}
}
}
if (count > 0) {
align = normalize(align/float(count)) * u_speed - vel;
cohesion = (cohesion/float(count) - pos) * 0.1;
separation = separation / float(count);
}
vec2 acc = align * u_align + cohesion * u_cohesion + separation * u_separation;
vel += acc;
vel = normalize(vel) * u_speed;
outColor = vec4(vel, 0, 1);
}`);
const posProg = link(`#version 300 es
in vec2 a_position;
out vec2 v_uv;
void main() {
v_uv = a_position * 0.5 + 0.5;
gl_Position = vec4(a_position, 0, 1);
}`, `#version 300 es
precision highp float;
uniform sampler2D u_pos, u_vel;
in vec2 v_uv;
out vec4 outColor;
void main() {
vec2 p = texture(u_pos, v_uv).xy;
vec2 v = texture(u_vel, v_uv).xy;
p += v * 0.01;
p = mod(p, 1.0);
outColor = vec4(p, 0, 1);
}`);
const drawProg = link(`#version 300 es
layout(location=0) in float id;
uniform sampler2D u_pos;
uniform float u_size;
void main() {
float i = mod(id, u_size);
float j = floor(id / u_size);
vec2 uv = (vec2(i, j) + 0.5) / u_size;
vec2 p = texture(u_pos, uv).xy;
gl_Position = vec4(p*2.0 - 1.0, 0, 1);
gl_PointSize = 2.0;
}`, `#version 300 es
precision mediump float;
out vec4 outColor;
void main() {
outColor = vec4(0.1, 1.0, 1.0, 1.0);
}`);
const ids = new Float32Array(texSize*texSize).map((_, i) => i);
const idBuf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, idBuf);
gl.bufferData(gl.ARRAY_BUFFER, ids, gl.STATIC_DRAW);
let posIn = posA, posOut = posB;
let velIn = velA, velOut = velB;
function swap() {
[posIn, posOut] = [posOut, posIn];
[velIn, velOut] = [velOut, velIn];
}
function step() {
gl.useProgram(simProg);
gl.bindFramebuffer(gl.FRAMEBUFFER, velOut.fbo);
gl.viewport(0, 0, texSize, texSize);
gl.bindVertexArray(quadVAO);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, posIn.tex);
gl.uniform1i(gl.getUniformLocation(simProg, 'u_pos'), 0);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, velIn.tex);
gl.uniform1i(gl.getUniformLocation(simProg, 'u_vel'), 1);
gl.uniform1f(gl.getUniformLocation(simProg, 'u_speed'), settings.speed);
gl.uniform1f(gl.getUniformLocation(simProg, 'u_align'), settings.align);
gl.uniform1f(gl.getUniformLocation(simProg, 'u_cohesion'), settings.cohesion);
gl.uniform1f(gl.getUniformLocation(simProg, 'u_separation'), settings.separation);
gl.uniform1f(gl.getUniformLocation(simProg, 'u_perception'), settings.perception);
gl.uniform1f(gl.getUniformLocation(simProg, 'u_size'), texSize);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
gl.useProgram(posProg);
gl.bindFramebuffer(gl.FRAMEBUFFER, posOut.fbo);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, posIn.tex);
gl.uniform1i(gl.getUniformLocation(posProg, 'u_pos'), 0);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, velOut.tex);
gl.uniform1i(gl.getUniformLocation(posProg, 'u_vel'), 1);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
gl.useProgram(drawProg);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.bindBuffer(gl.ARRAY_BUFFER, idBuf);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 1, gl.FLOAT, false, 0, 0);
gl.uniform1f(gl.getUniformLocation(drawProg, 'u_size'), texSize);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, posOut.tex);
gl.uniform1i(gl.getUniformLocation(drawProg, 'u_pos'), 0);
gl.drawArrays(gl.POINTS, 0, texSize * texSize);
swap();
requestAnimationFrame(step);
}
step();
</script>
</body>
</html>