-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
executable file
·331 lines (277 loc) · 8.62 KB
/
script.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
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
330
331
window.onload = loadScene;
function backingScale(context) {
if ("devicePixelRatio" in window) {
if (window.devicePixelRatio > 1) {
return window.devicePixelRatio;
}
}
return 1;
}
var tempCanvas = document.createElement("canvas");
var tempCtx = tempCanvas.getContext("2d");
var scaleFactor = backingScale(tempCtx);
var canvas,
gl,
ratio,
vertices,
velocities,
freqArr,
cw,
ch,
colorLoc,
thetaArr,
velThetaArr,
velRadArr,
boldRateArr,
drawType,
numLines = 80000;
var targetArr = [];
var randomTargetXArr = [],
randomTargetYArr = [];
drawType = 2;
/**
* Initialises WebGL and creates the 3D scene.
*/
function Target(rad) {
this.x = 0;
this.y = 0;
this.rotate = 0;
this.radius = 0;
this.bold_rate = rad;
this.update();
}
Target.prototype = {
update: function () {
this.rotate_speed = Math.random() * 0.02 + 0.02;
this.friction = Math.random(1) * 0.8 + 0.1;
this.speed = Math.random(1) * 0.2 + 0.03;
this.step = Math.random() * 0.2 + 0.1;
this.freq = Math.random(1) * 0.2 + 0.5;
// this.bold_rate = Math.random() * .5 + 0.;
},
};
function loadScene() {
// Get the canvas element
canvas = document.getElementById("c");
// Get the WebGL context
gl = canvas.getContext("experimental-webgl");
// Check whether the WebGL context is available or not
// if it's not available exit
if (!gl) {
alert("There's no WebGL context available.");
return;
}
// Set the viewport to the canvas width and height
cw = window.innerWidth;
ch = window.innerHeight;
canvas.width = cw;
canvas.height = ch;
gl.viewport(0, 0, canvas.width, canvas.height);
var vertexShaderScript = document.getElementById("shader-vs");
var vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, vertexShaderScript.text);
gl.compileShader(vertexShader);
if (!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)) {
alert("Couldn't compile the vertex shader");
gl.deleteShader(vertexShader);
return;
}
var fragmentShaderScript = document.getElementById("shader-fs");
var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, fragmentShaderScript.text);
gl.compileShader(fragmentShader);
if (!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)) {
alert("Couldn't compile the fragment shader");
gl.deleteShader(fragmentShader);
return;
}
// Create a shader program.
gl.program = gl.createProgram();
gl.attachShader(gl.program, vertexShader);
gl.attachShader(gl.program, fragmentShader);
gl.linkProgram(gl.program);
if (!gl.getProgramParameter(gl.program, gl.LINK_STATUS)) {
alert("Unable to initialise shaders");
gl.deleteProgram(gl.program);
gl.deleteProgram(vertexShader);
gl.deleteProgram(fragmentShader);
return;
}
gl.useProgram(gl.program);
var vertexPosition = gl.getAttribLocation(gl.program, "vertexPosition");
gl.enableVertexAttribArray(vertexPosition);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clearDepth(1.0);
gl.enable(gl.BLEND);
gl.disable(gl.DEPTH_TEST);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE);
var vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
// ------------------
setup();
// ------------------
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.DYNAMIC_DRAW);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Define the viewing frustum parameters
// More info: http://en.wikipedia.org/wiki/Viewing_frustum
// More info: https://knol.google.com/k/view-frustum
var fieldOfView = 30.0;
var aspectRatio = canvas.width / canvas.height;
var nearPlane = 1.0;
var farPlane = 10000.0;
var top = nearPlane * Math.tan((fieldOfView * Math.PI) / 360.0);
var bottom = -top;
var right = top * aspectRatio;
var left = -right;
// Create the perspective matrix. The OpenGL function that's normally used for this,
// glFrustum() is not included in the WebGL API. That's why we have to do it manually here.
// More info: http://www.cs.utk.edu/~vose/c-stuff/opengl/glFrustum.html
var a = (right + left) / (right - left);
var b = (top + bottom) / (top - bottom);
var c = (farPlane + nearPlane) / (farPlane - nearPlane);
var d = (2 * farPlane * nearPlane) / (farPlane - nearPlane);
var x = (2 * nearPlane) / (right - left);
var y = (2 * nearPlane) / (top - bottom);
var perspectiveMatrix = [x, 0, a, 0, 0, y, b, 0, 0, 0, c, d, 0, 0, -1, 0];
// Create the modelview matrix
// More info about the modelview matrix: http://3dengine.org/Modelview_matrix
// More info about the identity matrix: http://en.wikipedia.org/wiki/Identity_matrix
var modelViewMatrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
// Get the vertex position attribute location from the shader program
var vertexPosAttribLocation = gl.getAttribLocation(
gl.program,
"vertexPosition"
);
// Specify the location and format of the vertex position attribute
gl.vertexAttribPointer(vertexPosAttribLocation, 3.0, gl.FLOAT, false, 0, 0);
//gl.vertexAttribPointer(colorLoc, 4.0, gl.FLOAT, false, 0, 0);
// Get the location of the "modelViewMatrix" uniform variable from the
// shader program
var uModelViewMatrix = gl.getUniformLocation(gl.program, "modelViewMatrix");
// Get the location of the "perspectiveMatrix" uniform variable from the
// shader program
var uPerspectiveMatrix = gl.getUniformLocation(
gl.program,
"perspectiveMatrix"
);
// Set the values
gl.uniformMatrix4fv(
uModelViewMatrix,
false,
new Float32Array(perspectiveMatrix)
);
gl.uniformMatrix4fv(
uPerspectiveMatrix,
false,
new Float32Array(modelViewMatrix)
);
animate();
setTimeout(timer, 1500);
}
var count = 0;
var cn = 0;
function animate() {
requestAnimationFrame(animate);
drawScene();
}
function drawScene() {
draw();
gl.lineWidth(2);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.DYNAMIC_DRAW);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
//gl.drawArrays( gl.LINES_STRIP, 0, numLines );
gl.drawArrays(gl.LINES, 0, numLines);
//gl.drawArrays( gl.QUAD_STRIP, 0, numLines );
gl.flush();
}
// ===================================
function setup() {
setup00();
}
function draw() {
/*
switch (drawType) {
case 0:
draw0();
break;
case 1:
draw1();
break;
case 2:
draw2();
break;
}
*/
draw00();
}
// ===================================
function setup00() {
vertices = [];
velThetaArr = [];
velRadArr = [];
ratio = cw / ch;
velocities = [];
thetaArr = [];
freqArr = [];
boldRateArr = [];
// -------------------------------
//var minSize = Math.min(window.innerWidth, scale.innerHeight);
var widthScale = window.innerWidth / window.innerHeight;
var height = 110 * scaleFactor;
var X_MAX_NUM = parseInt(window.innerWidth / height);
var Y_MAX_NUM = parseInt(window.innerHeight / height);
// 2 -> window.innerHeight
// ? -> 50
var targetRad = (2 / (Y_MAX_NUM * 2 - 1)) * 0.35;
var xPos, yPos;
for (var xx = -X_MAX_NUM + 1; xx < X_MAX_NUM; xx++) {
for (var yy = -Y_MAX_NUM + 1; yy < Y_MAX_NUM; yy++) {
var target = new Target(targetRad);
target.x = (xx / X_MAX_NUM) * widthScale;
target.y = yy / Y_MAX_NUM;
targetArr.push(target);
}
}
for (var ii = 0; ii < numLines; ii++) {
vertices.push(0, 0, 1.83);
vertices.push(0, 0, 1.83);
}
vertices = new Float32Array(vertices);
velocities = new Float32Array(velocities);
}
// -------------------------------
function draw00() {
for (var ii = 0; ii < targetArr.length; ii++) {
targetArr[ii].rotate += targetArr[ii].rotate_speed;
}
var tRad, tX, tY;
var bp, px, py;
var target;
for (var ii = 0; ii < numLines * 2; ii += 2) {
bp = ii * 3;
// copy old positions
//vertices[bp] = vertices[bp + 3];
//vertices[bp + 1] = vertices[bp + 4];
target = targetArr[ii % targetArr.length];
tRad =
Math.cos(target.rotate * 2.321 + target.freq * ii) * target.bold_rate;
tX = target.x + Math.cos(target.rotate + target.step * ii) * tRad;
tY = target.y + Math.sin(target.rotate + target.step * ii) * tRad;
px = vertices[bp + 3];
py = vertices[bp + 4];
px += (tX - px) * 0.1;
py += (tY - py) * 0.1;
vertices[bp + 3] = px;
vertices[bp + 4] = py;
vertices[bp] = px - 0.001;
vertices[bp + 1] = py - 0.001;
}
}
// ===================================
// -------------------------------
function timer() {
for (var ii = 0; ii < targetArr.length; ii++) {
targetArr[ii].update();
}
setTimeout(timer, 1500);
}