forked from tsherif/webgl2examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
particles.html
329 lines (261 loc) · 12.3 KB
/
particles.html
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<!DOCTYPE html>
<!--
The MIT License (MIT)
Copyright (c) 2017 Tarek Sherif
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<html>
<head>
<title>WebGL 2 Example: Particles</title>
<meta charset="utf-8">
<script src="utils/gl-matrix.js"></script>
<link rel="stylesheet" href="css/webgl2examples.css">
</head>
<body>
<div id="example-title">
<header>WebGL 2 Example: Particles</header>
<div id="features">
Features: Vertex Arrays, Uniform Buffers, Transform Feedback
</div>
<div>
<a href="https://github.com/tsherif/webgl2examples/blob/master/particles.html">Source code</a>
</div>
</div>
<canvas id="gl-canvas"></canvas>
<script id="main-vs" type="x-shader/vs">
#version 300 es
layout(std140) uniform;
layout(location=0) in vec3 aPosition;
layout(location=1) in vec3 aVelocity;
layout(location=2) in vec3 aColor;
uniform Mass {
float mass1Factor;
float mass2Factor;
float mass3Factor;
vec4 mass1Position;
vec4 mass2Position;
vec4 mass3Position;
};
out vec3 vPosition;
out vec3 vVelocity;
out vec3 vColor;
void main() {
vec3 position = aPosition;
vec3 velocity = aVelocity;
vec3 massVec = mass1Position.xyz - position;
float massDist2 = max(0.01, dot(massVec, massVec));
vec3 acceleration = mass1Factor * normalize(massVec) / massDist2;
massVec = mass2Position.xyz - position;
massDist2 = max(0.01, dot(massVec, massVec));
acceleration += mass2Factor * normalize(massVec) / massDist2;
massVec = mass3Position.xyz - position;
massDist2 = max(0.01, dot(massVec, massVec));
acceleration += mass3Factor * normalize(massVec) / massDist2;
velocity += acceleration;
velocity *= 0.9999;
vPosition = position + velocity;
vVelocity = velocity;
vColor = aColor;
gl_PointSize = 2.0;
gl_Position = vec4(position, 1.0);
}
</script>
<script id="main-fs" type="x-fragment-shader">
#version 300 es
precision highp float;
in vec3 vColor;
out vec4 fragColor;
void main() {
float alpha = 0.3;
fragColor = vec4(vColor * alpha, alpha);
}
</script>
<script type="text/javascript">
var canvas = document.getElementById("gl-canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var gl = canvas.getContext("webgl2");
if (!gl) {
console.error("WebGL 2 not available");
document.body.innerHTML = "This example requires WebGL 2 which is unavailable on this system."
}
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.BLEND);
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
///////////////////////////
// SET UP PROGRAM
///////////////////////////
var vsSource = document.getElementById("main-vs").text.trim();
var fsSource = document.getElementById("main-fs").text.trim();
var vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, vsSource);
gl.compileShader(vertexShader);
if (!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)) {
console.error(gl.getShaderInfoLog(vertexShader));
}
var fragment = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragment, fsSource);
gl.compileShader(fragment);
if (!gl.getShaderParameter(fragment, gl.COMPILE_STATUS)) {
console.error(gl.getShaderInfoLog(fragment));
}
var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragment);
gl.transformFeedbackVaryings(program, ["vPosition", "vVelocity"], gl.SEPARATE_ATTRIBS);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
console.error(gl.getProgramInfoLog(program));
}
gl.useProgram(program);
///////////////////////////////////////////
// GET UNIFORM LOCATIONS
///////////////////////////////////////////
var massUniformsLocation = gl.getUniformBlockIndex(program, "Mass");
gl.uniformBlockBinding(program, massUniformsLocation, 0);
//////////////////////////
// SET UP GEOMETRY
//////////////////////////
var NUM_PARTICLES = 200000;
var positionData = new Float32Array(NUM_PARTICLES * 3);
var velocityData = new Float32Array(NUM_PARTICLES * 3);
var colorData = new Float32Array(NUM_PARTICLES * 3);
for (var i = 0; i < NUM_PARTICLES; ++i) {
var vec3i = i * 3;
positionData[vec3i] = Math.random() * 2 - 1;
positionData[vec3i + 1] = Math.random() * 2 - 1;
positionData[vec3i + 2] = Math.random() * 2 - 1;
colorData[vec3i] = Math.random();
colorData[vec3i + 1] = Math.random();
colorData[vec3i + 2] = Math.random();
}
//////////////////////////////////////
// SET UP TRANSFORM FEEDBACK
//////////////////////////////////////
// Vertex array handles input
var vertexArrayA = gl.createVertexArray();
gl.bindVertexArray(vertexArrayA);
var positionBufferA = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBufferA);
gl.bufferData(gl.ARRAY_BUFFER, positionData, gl.STREAM_COPY);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(0);
var velocityBufferA = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, velocityBufferA);
gl.bufferData(gl.ARRAY_BUFFER, velocityData, gl.STREAM_COPY);
gl.vertexAttribPointer(1, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(1);
var colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, colorData, gl.STATIC_DRAW);
gl.vertexAttribPointer(2, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(2);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
// Transform feedback handles output
var transformFeedbackA = gl.createTransformFeedback();
gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, transformFeedbackA);
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, positionBufferA);
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 1, velocityBufferA);
// Vertex array handles input
var vertexArrayB = gl.createVertexArray();
gl.bindVertexArray(vertexArrayB);
var positionBufferB = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBufferB);
gl.bufferData(gl.ARRAY_BUFFER, positionData, gl.STREAM_COPY);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(0);
var velocityBufferB = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, velocityBufferB);
gl.bufferData(gl.ARRAY_BUFFER, velocityData, gl.STREAM_COPY);
gl.vertexAttribPointer(1, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(1);
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.vertexAttribPointer(2, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(2);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
// Transform feedback handles output
var transformFeedbackB = gl.createTransformFeedback();
gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, transformFeedbackB);
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, positionBufferB);
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 1, velocityBufferB);
gl.bindVertexArray(null);
gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, null);
var currentVertexArray = vertexArrayA;
var currentTransformFeedback = transformFeedbackB;
///////////////////
// UNIFORM DATA
///////////////////
var massUniformData = new Float32Array(20);
massUniformData[0] = Math.random() / 30000;
massUniformData[1] = Math.random() / 30000;
massUniformData[2] = Math.random() / 30000;
massUniformData.set(new Float32Array([
Math.random() * 2.0 - 1.0,
Math.random() * 2.0 - 1.0,
Math.random() * 2.0 - 1.0
]), 4);
massUniformData.set(new Float32Array([
Math.random() * 2.0 - 1.0,
Math.random() * 2.0 - 1.0,
Math.random() * 2.0 - 1.0
]), 8);
massUniformData.set(new Float32Array([
Math.random() * 2.0 - 1.0,
Math.random() * 2.0 - 1.0,
Math.random() * 2.0 - 1.0
]), 16);
var massUniformBuffer = gl.createBuffer();
gl.bindBufferBase(gl.UNIFORM_BUFFER, 0, massUniformBuffer);
gl.bufferData(gl.UNIFORM_BUFFER, massUniformData, gl.STATIC_DRAW);
function draw() {
// NOTE(Tarek): Putting the clear after the transform feedback
// pass seems to cause it to be ignored on OSX 10.12/Intel Iris
gl.clear(gl.COLOR_BUFFER_BIT);
//////////////////////////////
// DRAW
//////////////////////////////
gl.bindVertexArray(currentVertexArray);
gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, currentTransformFeedback);
// NOTE: THIS PART IS NECESSARY DUE TO A BUG IN ANGLE'S HANDLING
// OF TRANFORM FEEDBACK OBJECTS.
// TO BE REMOVED WHEN THAT'S FIXED.
if (currentTransformFeedback === transformFeedbackA) {
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, positionBufferA);
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 1, velocityBufferA);
} else {
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, positionBufferB);
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 1, velocityBufferB);
}
gl.beginTransformFeedback(gl.POINTS);
gl.drawArrays(gl.POINTS, 0, NUM_PARTICLES);
gl.endTransformFeedback();
//////////////////
// SWAP BUFFERS
//////////////////
if (currentVertexArray === vertexArrayA) {
currentVertexArray = vertexArrayB;
currentTransformFeedback = transformFeedbackA;
} else {
currentVertexArray = vertexArrayA;
currentTransformFeedback = transformFeedbackB;
}
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
</script>
<a href="https://github.com/tsherif/webgl2examples" id="github-ribbon"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"></a>
</body>
</html>