-
Notifications
You must be signed in to change notification settings - Fork 1
/
particles.js
116 lines (100 loc) · 3.02 KB
/
particles.js
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
// ╔════╤════╤════╤════╦════╤════╤════╤════╦══┄
// ║ X │ Y │ dX │ dY ║ X │ Y │ dX │ dY ║ ...
// ╚════╧════╧════╧════╩════╧════╧════╧════╩══┄
// ├────┤
// float32
const PARTICLES_COUNT = 200000;
async function initState() {
const res = await WebAssembly.instantiateStreaming(
fetch('physics.wasm'),
{
Math,
conf: { count: PARTICLES_COUNT }
}
);
const exports = res.instance.exports;
console.groupCollapsed('wasm');
console.log(res);
console.groupEnd();
return {
fire: exports.fire,
physics: exports.physics,
buffer: () => exports.mem.buffer,
};
};
async function _getShader(url, gl, flavor) {
const resp = await fetch(url);
const source = await resp.text();
const shader = gl.createShader(flavor);
gl.shaderSource(shader, source);
gl.compileShader(shader);
console.groupCollapsed(url);
console.log(source);
console.log(gl.getShaderInfoLog(shader));
console.groupEnd();
return shader;
}
async function initView(canvas) {
const gl = canvas.getContext('webgl', { alpha: false, antialias: false });
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
gl.clearColor(0, 0, 0, 1);
const [vert, frag] = await Promise.all([
_getShader('vert.glsl', gl, gl.VERTEX_SHADER),
_getShader('frag.glsl', gl, gl.FRAGMENT_SHADER),
]);
const program = gl.createProgram();
gl.attachShader(program, vert);
gl.attachShader(program, frag);
gl.linkProgram(program);
function render(buffer) {
gl.clear(gl.COLOR_BUFFER_BIT);
gl.useProgram(program);
{
const attribLocation = gl.getAttribLocation(program, 'scale');
gl.disableVertexAttribArray(attribLocation);
gl.vertexAttrib2f(attribLocation, 2 / canvas.width, -2 / canvas.height);
}
{
const attribLocation = gl.getAttribLocation(program, 'coord');
gl.enableVertexAttribArray(attribLocation);
gl.vertexAttribPointer(
attribLocation, // index
2, // size (X and Y)
gl.FLOAT, // float32 each
false, // normalized. Has no effect on float
16, // stride
0 // start index
);
}
gl.bufferData(gl.ARRAY_BUFFER, buffer, gl.DYNAMIC_DRAW);
gl.drawArrays(gl.POINTS, 0, PARTICLES_COUNT);
}
function resize(width, height) {
canvas.width = width;
canvas.height = height;
gl.viewport(0, 0, width, height);
}
return { render, resize }
}
async function main() {
const canvas = document.body.appendChild(document.createElement('canvas'));
const [ state, view ] = await Promise.all([
initState(),
initView(canvas),
]);
function onresize() {
view.resize(canvas.clientWidth, canvas.clientHeight);
}
function onclick(e) {
state.fire(e.offsetX - canvas.clientWidth / 2, e.offsetY - canvas.clientHeight / 2);
}
canvas.addEventListener('click', onclick);
window.addEventListener('resize', onresize);
onresize();
(function frame() {
state.physics();
view.render(state.buffer());
requestAnimationFrame(frame);
})();
}
main();