-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
executable file
·178 lines (143 loc) · 4.33 KB
/
main.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
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
const canvas = document.getElementById("canvas");
const gl = canvas.getContext('webgl2', {antialias: false});
const rS = new rStats({CSSPath: 'libs/'});
const mousePos = [0, 0];
/* Check WebGL2 */
if (!gl) {
document.querySelector('.no-webgl2').style.display = 'block';
}
/* Set number of points */
const pointsEl = document.getElementById('nrPoints');
const numberOfPoints = getJsonFromUrl().points
? parseInt(getJsonFromUrl().points)
: 2e6;
pointsEl.innerHTML = new Intl.NumberFormat().format(numberOfPoints);
/* Update mouse position */
canvas.addEventListener('mousemove', event => {
mousePos[0] = event.clientX / canvas.width * 2 - 1;
mousePos[1] = (event.clientY / canvas.height * 2 - 1) * -1;
});
/* Handle canvas size */
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
gl.viewport(0, 0, canvas.width, canvas.height);
}
window.addEventListener('resize', resize);
resize();
/* Use predefined attribute locations */
const VERTEX_ATTRIBUTE_POS = 0;
const VELOCITY_ATTRIBUTE_POS = 1;
/* Enable blending */
gl.enable(gl.BLEND);
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
/* Create Vertex Buffers */
const vertices = new Float32Array(numberOfPoints * 2);
for (let i = 0; i < vertices.length; i++) {
vertices[i] = Math.random() * 2 - 1;
}
const vertexBuffers = [
createBufferFromArray(vertices),
createBufferWithSize(numberOfPoints * 2 * 4)
];
/* Create Velocity Buffer */
const velocities = new Float32Array(numberOfPoints * 2);
for (let i = 0; i < velocities.length; i++) {
velocities[i] = 0;
}
const velocityBuffers = [
createBufferFromArray(velocities),
createBufferWithSize(numberOfPoints * 2 * 4)
];
/* Create program with feedback */
const programFeedback = createProgram(gl,
vertexFeedbackShader,
fragmentEmptyShader,
['v_position', 'v_velocity'],
gl.SEPARATE_ATTRIBS
);
// get uniform location for mouse position
const mousePosLocation = gl.getUniformLocation(programFeedback, "u_mouse");
/* Create program to display particles */
const programDisplay = createProgram(gl,
vertexDisplayShader,
fragmentDisplayShader
);
/* Create VAOs */
const feedbackVAOs = [];
const displayVAOs = [];
feedbackVAOs.push(createVAO([{
data: vertexBuffers[0],
location: VERTEX_ATTRIBUTE_POS,
elementSize: 2
},
{
data: velocityBuffers[0],
location: VELOCITY_ATTRIBUTE_POS,
elementSize: 2
}]
));
feedbackVAOs.push(createVAO([{
data: vertexBuffers[1],
location: VERTEX_ATTRIBUTE_POS,
elementSize: 2
},
{
data: velocityBuffers[1],
location: VELOCITY_ATTRIBUTE_POS,
elementSize: 2
}]
));
displayVAOs.push(createVAO([{
data: vertexBuffers[0],
location: VERTEX_ATTRIBUTE_POS,
elementSize: 2
}]));
displayVAOs.push(createVAO([{
data: vertexBuffers[1],
location: VERTEX_ATTRIBUTE_POS,
elementSize: 2
}]));
/* Draw a VAO */
function draw(vao) {
gl.bindVertexArray(vao);
gl.drawArrays(gl.POINTS, 0, numberOfPoints);
}
/* Create transform feedback */
const transformFeedback = gl.createTransformFeedback();
/* Fill the current feedback buffer */
function calculateFeedback(currentIndex) {
const invertedIndex = invert(currentIndex);
// Disable rasterization, vertex processing only
gl.enable(gl.RASTERIZER_DISCARD);
gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, transformFeedback);
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, vertexBuffers[invertedIndex]);
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 1, velocityBuffers[invertedIndex]);
gl.useProgram(programFeedback);
gl.uniform2fv(mousePosLocation, mousePos);
gl.beginTransformFeedback(gl.POINTS);
draw(feedbackVAOs[currentIndex]);
gl.endTransformFeedback();
/* Re-activate rasterizer for next draw calls */
gl.disable(gl.RASTERIZER_DISCARD);
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, null);
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 1, null);
}
/* Ping Pong index */
let currentIndex = 0;
function loop() {
rS('rAF').tick();
const invertedIndex = invert(currentIndex);
gl.clearColor(1.0, 1.0, 1.0, 1.0);
gl.clear(gl.CLEAR_COLOR_BIT);
calculateFeedback(currentIndex);
// draw result from previous iteration
gl.useProgram(programDisplay);
draw(displayVAOs[invertedIndex]);
// switch index for next iteration
currentIndex = invert(currentIndex);
requestAnimationFrame(loop);
rS().update();
}
/* Start the render loop */
loop();