-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparticleSystem.js
More file actions
133 lines (117 loc) · 4.29 KB
/
particleSystem.js
File metadata and controls
133 lines (117 loc) · 4.29 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
class ParticleSystem {
constructor() {
this.positions = [];
this.velocities = [];
this.masses = [];
this.time = 0;
this.MAX_NUM_PARTICLES = 1000;
this.initializeSystem();
this.program = initShaders(
gl,
"vshader_particles.glsl",
"fshader_particles.glsl"
);
this.vID = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.vID);
gl.bufferData(gl.ARRAY_BUFFER, flatten(this.positions), gl.STATIC_DRAW);
this.aPosition = gl.getAttribLocation(this.program, "aPosition");
this.modelMatrix = mat4();
this.modelMatrixID = gl.getUniformLocation(this.program, "modelMatrix");
this.cameraMatrixID = gl.getUniformLocation(
this.program,
"cameraMatrix"
);
this.projMatrixID = gl.getUniformLocation(this.program, "projMatrix");
this.colorID = gl.getUniformLocation(this.program, "uColor");
}
initializeSystem() {
for (var i = 0; i < this.MAX_NUM_PARTICLES; i++) {
var mass = 1.0;
var position = vec3();
var velocity = vec3();
for (var j = 0; j < 3; j++) {
position[j] = Math.random() * 20 - 10;
velocity[j] = j == 1 ? Math.random() * -1 - 1.0 : 0;
mass = Math.random();
}
this.positions.push(position);
this.velocities.push(velocity);
this.masses.push(mass);
}
}
updateSystem() {
for (var i = 0; i < this.MAX_NUM_PARTICLES; i++) {
for (var j = 0; j < 3; j++) {
this.positions[i][j] += 0.1 * this.velocities[i][j];
}
this.gravity(i);
var pyrX = 0.5 + 0.5 * 4;
var pyrY = 2.6;
var pyrZ = 0 + 0.5 * 8.5;
var pyrXSc = 2.35;
var pyrZSc = 2 * 2.35;
if (
this.positions[i][1] < 0 ||
(Math.abs(this.positions[i][0] - pyrX) <= pyrXSc &&
Math.abs(this.positions[i][2] - pyrZ) <= pyrZSc &&
this.positions[i][1] < pyrY)
) {
this.positions[i][1] = 10;
}
}
gl.bindBuffer(gl.ARRAY_BUFFER, this.vID);
gl.bufferData(gl.ARRAY_BUFFER, flatten(this.positions), gl.STATIC_DRAW);
}
collision(n) {
var coef = 0.5; //how strong it bounces back
for (var i = 0; i < 3; i++) {
if (this.positions[n][i] > 1.0) {
this.velocities[n][i] *= -coef;
this.positions[n][i] =
1.0 - coef * (this.positions[n][i] - 1.0);
}
if (this.positions[n][i] < -1.0) {
this.velocities[n][i] *= -coef;
this.positions[n][i] =
-1.0 - coef * (this.positions[n][i] + 1.0);
}
}
}
gravity(n) {
this.velocities[n][1] -= 0.0001 / this.masses[n];
}
flock() {
var cm = vec3();
for (var j = 0; j < 3; j++) {
for (var i = 0; i < this.MAX_NUM_PARTICLES; i++) {
cm[j] += this.positions[i][j];
}
cm[j] /= this.MAX_NUM_PARTICLES;
}
const PERCENT_FLOCK = 0.001;
for (var n = 0; n < this.MAX_NUM_PARTICLES; n++) {
var flockVec = subtract(cm, this.positions[n]);
for (var j = 0; j < 3; j++) {
this.velocities[n][j] =
(1.0 - PERCENT_FLOCK) * this.velocities[n][j] +
PERCENT_FLOCK * flockVec[j];
}
}
}
draw(camera, projection) {
gl.useProgram(this.program);
gl.bindBuffer(gl.ARRAY_BUFFER, this.vID);
gl.vertexAttribPointer(this.aPosition, 3, gl.FLOAT, false, 0, 0);
gl.uniformMatrix4fv(
this.modelMatrixID,
false,
flatten(this.modelMatrix)
);
gl.uniformMatrix4fv(this.cameraMatrixID, false, flatten(camera));
gl.uniformMatrix4fv(this.projMatrixID, false, flatten(projection));
gl.uniform4fv(this.colorID, vec4(0, 0, 1, 1));
gl.enableVertexAttribArray(this.aPosition);
gl.drawArrays(gl.POINTS, 0, this.positions.length);
gl.disableVertexAttribArray(this.aPosition);
}
}