-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicosohedron.js
508 lines (420 loc) · 13.5 KB
/
icosohedron.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// Icosphere Simulator
// Ethan Bliss
// September 2021
// Allows the user to interact with and view an icosphere
// Config
// On start:
let subdivisions = 2;
let scale = 1.0;
let velX = 40.0;
let velY = 20.0;
// Constant variables:
const resist = 0.02;
const lightCol = [1, 1, 1];
const lightPos = [3, 1, 1];
const cullFace = "back";
// Unpack gl-matrix types
const { mat4, vec3 } = glMatrix;
// Get gl context
const canvas = document.querySelector("#glCanvas");
const gl = canvas.getContext("webgl2");
main();
// Main
function main() {
// Check for gl
if (!gl) {
alert("Unable to init WebGL");
return;
}
// Vertex Source
const vertexSource = `#version 300 es
uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;
uniform mediump vec3 lightColor;
uniform mediump vec3 lightPos;
uniform mediump float scale;
uniform mediump vec3 icoColor;
uniform lowp int defaultColor;
in vec3 vertexPos;
flat out vec3 vColor;
flat out vec3 vNormal;
out vec3 vCurPos;
out vec3 vLightColor;
out vec3 vLightPos;
void main(void) {
// Send varyings to frag
vLightColor = lightColor;
vLightPos = scale * lightPos;
// Apply model matrix to position
vCurPos = vec3(scale * model * vec4(vertexPos, 1.0));
// Vertex normals are their positions for a sphere around origin
vNormal = vCurPos;
// Choose rainbow or picker
if (defaultColor == 1) {
vColor = vertexPos + vec3(0.15, 0.15, 0.15);
} else {
vColor = icoColor;
}
// Apply matrices for final positions
gl_Position = proj * view * vec4(vCurPos, 1.0);
}
`;
// Fragment Source
const fragSource = `#version 300 es
in mediump vec3 vLightColor;
in mediump vec3 vLightPos;
flat in mediump vec3 vColor;
flat in mediump vec3 vNormal;
in mediump vec3 vCurPos;
out highp vec4 fragColor;
// Light from a point
highp vec4 pointLight(vec3 lightPos) {
mediump vec3 camPos = vec3(0.0, 0.0, 5.0);
// Calculate variables
mediump vec3 lightVec = lightPos - vCurPos;
mediump vec3 lightDir = normalize(lightVec);
mediump vec3 normal = normalize(vNormal);
mediump float dist = length(lightVec);
mediump float intst = 1.0 / (0.4 * dist * dist + 0.2 * dist + 1.0);
// Ambient
mediump float ambient = 0.2;
// Diffuse
mediump float diffuse = max(dot(normal, lightDir), 0.0);
// Specular
mediump vec3 viewDir = normalize(camPos - vCurPos);
mediump vec3 reflcDir = reflect(-lightDir, normal);
mediump float specular = pow(max(dot(viewDir, reflcDir), 0.0), 32.0);
// Calculate final (ambient optional when using other lighting)
highp vec3 res = vColor * vLightColor * ((diffuse + specular) * intst); // + ambient);
return vec4(res, 1.0);
}
// Directional Light (e.g. sun)
highp vec4 direcLight(vec3 lightPos) {
mediump vec3 lightDir = normalize(lightPos);
mediump vec3 normal = normalize(vNormal);
mediump float ambient = 0.3;
mediump float direc = max(dot(normal, lightDir), 0.0) / 2.0;
return vec4(vColor * vLightColor * (direc + ambient), 1.0);
}
void main(void) {
// Calculate from all light sources
fragColor = (direcLight(-vLightPos)
+ direcLight(vec3(0.0, 2.0, 5.0))
+ pointLight(vLightPos)) / 1.5;
}
`;
shaderProgram = createShaderProgram(vertexSource, fragSource);
// Collect program info
const programInfo = {
program: shaderProgram,
attribLocations: {
vertexPos: gl.getAttribLocation(shaderProgram, "vertexPos"),
},
uniformLocations: {
projMat: gl.getUniformLocation(shaderProgram, "proj"),
viewMat: gl.getUniformLocation(shaderProgram, "view"),
modelMat: gl.getUniformLocation(shaderProgram, "model"),
lightColor: gl.getUniformLocation(shaderProgram, "lightColor"),
lightPos: gl.getUniformLocation(shaderProgram, "lightPos"),
scale: gl.getUniformLocation(shaderProgram, "scale"),
color: gl.getUniformLocation(shaderProgram, "icoColor"),
defaultColor: gl.getUniformLocation(shaderProgram, "defaultColor"),
},
};
// Find initial vertex positions
let verts = generatePositions(programInfo, subdivisions);
// Generate matrices
const modelMatrix = generateMatrices(programInfo);
const rotMatrix = mat4.create();
// Upload shader config
gl.uniform3fv(programInfo.uniformLocations.lightColor, lightCol);
gl.uniform3fv(programInfo.uniformLocations.lightPos, lightPos);
gl.uniform1i(programInfo.uniformLocations.defaultColor, 1);
gl.uniform1f(programInfo.uniformLocations.scale, scale);
// Spawn color picker
initColorPicker(programInfo);
// Set gl config
gl.clearColor(0.0, 0.0, 0.0, 0.0);
gl.clearDepth(1.0);
gl.enable(gl.CULL_FACE);
if (cullFace === "back") gl.cullFace(gl.BACK);
else if (cullFace === "front") gl.cullFace(gl.FRONT);
// Get start time
let sTime = Date.now();
let cTime, eTime;
// Use shader program
gl.useProgram(programInfo.program);
// Render function
function render() {
// Elapsed time
cTime = Date.now();
eTime = cTime - sTime;
sTime = cTime;
// Clear color (no depth - taken care of by cullFace)
gl.clear(gl.COLOR_BUFFER_BIT);
// Calculate velocity
icoVelocity(eTime);
// Refresh model matrix
gl.uniformMatrix4fv(
programInfo.uniformLocations.modelMat,
false,
modelMatrix
);
// Draw
gl.drawArrays(gl.TRIANGLES, 0, 3 * verts);
// Render next frame
requestAnimationFrame(render);
}
// Add event listeners
document.addEventListener("keypress", updateVerts);
gl.canvas.addEventListener("wheel", scaleIco);
gl.canvas.addEventListener("mousedown", () => {
gl.canvas.addEventListener("mousemove", rotateIco);
document.addEventListener("mouseup", () => {
gl.canvas.removeEventListener("mousemove", rotateIco);
});
});
// Call Subdivide / Un-subdivide function
function updateVerts(e) {
if (e.code === "KeyS") {
// "Subdivide"
if (subdivisions < 8) {
subdivisions += 1;
verts = generatePositions(programInfo, subdivisions);
}
} else if (e.code === "KeyD") {
// "Decimate / Unsubdivide"
if (subdivisions > 0) {
subdivisions -= 1;
verts = generatePositions(programInfo, subdivisions);
}
}
}
// Scale based on scroll wheel
function scaleIco(e) {
if (e.wheelDelta > 0) {
if (scale < 4.7) {
scale *= 1.05;
}
} else {
if (scale > 0.02) {
scale /= 1.05;
}
}
gl.uniform1f(programInfo.uniformLocations.scale, scale);
}
// Set velocity to mouse movement
function rotateIco(e) {
velX = e.movementX;
velY = e.movementY;
icoVelocity(eTime);
}
// Calculate velocity using time (framerate independent)
function icoVelocity(eTime) {
resistFac = resist * eTime;
// Slow down velocity (BUG: wobbles instead of vel = 0)
if (velX > 0) velX -= resistFac;
else if (velX < 0) velX += resistFac;
if (velY > 0) velY -= resistFac;
else if (velY < 0) velY += resistFac;
// Rotate matrix
mat4.fromYRotation(rotMatrix, 0.004 * velX);
mat4.rotateX(rotMatrix, rotMatrix, 0.004 * velY);
mat4.multiply(modelMatrix, rotMatrix, modelMatrix);
}
// Call initial render
requestAnimationFrame(render);
}
// Generate initial 12-vert icosohedron
function generateBasicIco() {
let icoPos = [];
const phi = (1 + Math.sqrt(5)) / 2; // constant phi
const invRad = 1 / Math.sqrt((5 + Math.sqrt(5)) / 2); // will make rad = 1
// Each vert is +/- on each axis
let negt = [1, 1, 1, -1, -1, 1, -1, -1].map((i) => {
return i * invRad;
});
// Calculate the 3 rectangles
let pos = [];
for (let i = 0; i < 8; i += 2) {
pos.push([0, negt[i], negt[i + 1] * phi]);
pos.push([negt[i], negt[i + 1] * phi, 0]);
pos.push([negt[i] * phi, 0, negt[i + 1]]);
}
const vIndex1 = [1, 7, 8, 6, 2, 1]; // CCW around top
const vIndex2 = [3, 5, 4, 10, 11, 3]; // CCW around bottom
const vIndex3 = [1, 3, 7, 11, 8, 10, 6, 4, 2, 5, 1, 3]; // L->R middle
// Find CCW order of each triangle
for (let i = 0; i < 5; i++) {
icoPos.push(pos[0], pos[vIndex1[i]], pos[vIndex1[i + 1]]);
icoPos.push(pos[9], pos[vIndex2[i]], pos[vIndex2[i + 1]]);
icoPos.push(
pos[vIndex3[i * 2]],
pos[vIndex3[i * 2 + 1]],
pos[vIndex3[i * 2 + 2]]
);
icoPos.push(
pos[vIndex3[i * 2 + 2]],
pos[vIndex3[i * 2 + 1]],
pos[vIndex3[i * 2 + 3]]
);
}
return icoPos;
}
// Subdivide Ico (1 triangle -> 4)
function subdivIco(icoPos) {
newIco = [];
// Get triangle
for (let i = 0; i < icoPos.length; i += 3) {
// Get points
const vertA = icoPos[i];
const vertB = icoPos[i + 1];
const vertC = icoPos[i + 2];
const posA = vec3.fromValues(vertA[0], vertA[1], vertA[2]);
const posB = vec3.fromValues(vertB[0], vertB[1], vertB[2]);
const posC = vec3.fromValues(vertC[0], vertC[1], vertC[2]);
// Get midpoints
let midpointP = vec3.create();
let midpointQ = vec3.create();
let midpointR = vec3.create();
midpointP = vec3.add(midpointP, posA, posB);
midpointQ = vec3.add(midpointQ, posB, posC);
midpointR = vec3.add(midpointR, posA, posC);
vec3.scale(midpointP, midpointP, 1 / 2);
vec3.scale(midpointQ, midpointQ, 1 / 2);
vec3.scale(midpointR, midpointR, 1 / 2);
// Scale to sphere (easy since r = 1)
vec3.normalize(midpointP, midpointP);
vec3.normalize(midpointQ, midpointQ);
vec3.normalize(midpointR, midpointR);
// vec3 -> array
const midP = [midpointP[0], midpointP[1], midpointP[2]];
const midQ = [midpointQ[0], midpointQ[1], midpointQ[2]];
const midR = [midpointR[0], midpointR[1], midpointR[2]];
// Push new triangles (4)
newIco.push(vertA);
newIco.push(midP);
newIco.push(midR);
newIco.push(vertB);
newIco.push(midQ);
newIco.push(midP);
newIco.push(vertC);
newIco.push(midR);
newIco.push(midQ);
newIco.push(midP);
newIco.push(midQ);
newIco.push(midR);
}
return newIco;
}
// Get basic ico, subdivide, and upload
function generatePositions(programInfo, subdivisions) {
let icoPos = generateBasicIco(programInfo);
for (let i = 0; i < subdivisions; i++) {
icoPos = subdivIco(icoPos);
}
// Create position buffer and load data
const posBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, posBuffer);
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array(icoPos.flat()),
gl.STATIC_DRAW
);
// Upload vertex attrib for position
gl.bindBuffer(gl.ARRAY_BUFFER, posBuffer);
gl.vertexAttribPointer(
programInfo.attribLocations.vertexPos,
3,
gl.FLOAT,
false,
0,
0
);
gl.enableVertexAttribArray(programInfo.attribLocations.vertexPos);
// Display triangles and subdivisions
const verts = 20 * Math.pow(4, subdivisions);
document.getElementById("subdivisions").innerHTML =
"Subdivisions: " + subdivisions;
document.getElementById("triangles").innerHTML = "Triangles: " + verts;
return verts;
}
// Create and upload matrices
function generateMatrices(programInfo) {
// Create model matrix
const modelMatrix = mat4.create();
// Create view matrix
const viewMatrix = mat4.create();
mat4.lookAt(
viewMatrix,
vec3.fromValues(0, 0, 5), // eye position
vec3.fromValues(0, 0, 0), // target pos
vec3.fromValues(0, 1, 0) // up vector
);
// Create projection matrix
const projMatrix = mat4.create();
mat4.perspective(
projMatrix,
60 * (Math.PI / 180), // fov
gl.canvas.clientWidth / gl.canvas.clientHeight, // aspect ratio
0.1, // near z
100.0 // far z
);
gl.useProgram(programInfo.program);
// Upload matrices
gl.uniformMatrix4fv(programInfo.uniformLocations.viewMat, false, viewMatrix);
gl.uniformMatrix4fv(programInfo.uniformLocations.projMat, false, projMatrix);
return modelMatrix;
}
// Create shaders and shader program
function createShaderProgram(vertSource, fragSource) {
// Create and compile shaders
const vertShader = gl.createShader(gl.VERTEX_SHADER, vertSource);
const fragShader = gl.createShader(gl.FRAGMENT_SHADER, fragSource);
gl.shaderSource(vertShader, vertSource);
gl.shaderSource(fragShader, fragSource);
gl.compileShader(vertShader);
gl.compileShader(fragShader);
// Error check vert shader
if (!gl.getShaderParameter(vertShader, gl.COMPILE_STATUS)) {
alert(
"An error occured compiling the vert shader: " +
gl.getShaderInfoLog(vertShader)
);
gl.deleteShader(vertShader);
}
// Error check frag shader
if (!gl.getShaderParameter(fragShader, gl.COMPILE_STATUS)) {
alert(
"An error occured compiling the frag shader: " +
gl.getShaderInfoLog(fragShader)
);
gl.deleteShader(fragShader);
}
// Create and link shader program
const shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertShader);
gl.attachShader(shaderProgram, fragShader);
gl.linkProgram(shaderProgram);
return shaderProgram;
}
// Create color picker
function initColorPicker(programInfo) {
const colorPicker = new iro.ColorPicker("#color", {
width: 160,
color: "#00f",
handleSvg: "#hex",
handleProps: { x: 2, y: 0 },
});
colorPicker.on("color:change", function (color) {
console.log(color.red);
gl.uniform3f(
programInfo.uniformLocations.color,
color.red / 255,
color.green / 255,
color.blue / 255
);
gl.uniform1i(programInfo.uniformLocations.defaultColor, 0);
});
}