-
Notifications
You must be signed in to change notification settings - Fork 1
/
gasket4.js
265 lines (205 loc) · 6.04 KB
/
gasket4.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
"use strict";
var canvas;
var gl;
var points = [];
var colors = [];
let modelViewMatrixLoc;
function init() {
canvas = document.getElementById("gl-canvas");
gl = WebGLUtils.setupWebGL(canvas);
if (!gl) {
alert("WebGL isn't available");
}
//
// Initialize our data for the Sierpinski Gasket
//
// First, initialize the vertices of our 3D gasket
// Four vertices on unit circle
// Initial tetrahedron with equal length sides
var vertices = [
vec3(0.0, 0.0, -1.0),
vec3(0.0, 0.9428, 0.3333),
vec3(-0.8165, -0.4714, 0.3333),
vec3(0.8165, -0.4714, 0.3333),
];
divideTetra(
vertices[0],
vertices[1],
vertices[2],
vertices[3],
configurations.subdivisions
);
//
// Configure WebGL
//
gl.viewport(0, 0, canvas.width, canvas.height);
// enable hidden-surface removal
gl.enable(gl.DEPTH_TEST);
// Load shaders and initialize attribute buffers
var program = initShaders(gl, "vertex-shader", "fragment-shader");
gl.useProgram(program);
// Create a buffer object, initialize it, and associate it with the
// associated attribute variable in our vertex shader
var cBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);
var vColor = gl.getAttribLocation(program, "vColor");
gl.vertexAttribPointer(vColor, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vColor);
var vBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(points), gl.STATIC_DRAW);
var vPosition = gl.getAttribLocation(program, "vPosition");
gl.vertexAttribPointer(vPosition, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vPosition);
modelViewMatrixLoc = gl.getUniformLocation(program, "modelViewMatrix");
const recalculateTriangle = () => {
let config = {};
if (animation) {
config = animationData;
} else {
config = configurations;
}
points = [];
colors = [];
divideTetra(
vertices[0],
vertices[1],
vertices[2],
vertices[3],
config.subdivisions
);
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(points), gl.STATIC_DRAW);
};
const inputs = [
"subdivisions",
"colour-face-1-rgb",
"colour-face-1-a",
"colour-face-2-rgb",
"colour-face-2-a",
"colour-face-3-rgb",
"colour-face-3-a",
"colour-face-4-rgb",
"colour-face-4-a",
];
for (const input of inputs) {
const element = document.getElementById(input);
element.addEventListener("input", recalculateTriangle);
}
document.addEventListener("subdivision", recalculateTriangle);
const recalculateBackgroundColour = () => {
const { colour_solid: colours } = configurations;
const hex = colours.background[0];
const alpha = colours.background[1];
const RGBA = [
Number.parseInt(hex.slice(1, 3), 16) / 255,
Number.parseInt(hex.slice(3, 5), 16) / 255,
Number.parseInt(hex.slice(5, 7), 16) / 255,
alpha,
];
gl.clearColor(...RGBA);
};
recalculateBackgroundColour();
const backgroundInputs = ["colour-background-rgb", "colour-background-a"];
for (const input of backgroundInputs) {
const element = document.getElementById(input);
element.addEventListener("input", recalculateBackgroundColour);
}
render();
}
function triangle(a, b, c, color) {
// add colors and vertices for one triangle
// var baseColors = [
// vec3(1.0, 0.0, 0.0),
// vec3(0.0, 1.0, 0.0),
// vec3(0.0, 0.0, 1.0),
// vec3(0.0, 0.0, 0.0),
// ];
let baseColors = [];
const { colour_solid: colours } = configurations;
const faces = ["face_1", "face_2", "face_3", "face_4"];
for (const face of faces) {
const hex = colours[face][0];
const alpha = colours[face][1];
const RGBA = [
Number.parseInt(hex.slice(1, 3), 16) / 255,
Number.parseInt(hex.slice(3, 5), 16) / 255,
Number.parseInt(hex.slice(5, 7), 16) / 255,
alpha,
];
baseColors.push(vec4(...RGBA));
}
colors.push(baseColors[color]);
points.push(a);
colors.push(baseColors[color]);
points.push(b);
colors.push(baseColors[color]);
points.push(c);
}
function tetra(a, b, c, d) {
// tetrahedron with each side using
// a different color
triangle(a, c, b, 0);
triangle(a, c, d, 1);
triangle(a, b, d, 2);
triangle(b, c, d, 3);
}
function divideTetra(a, b, c, d, count) {
// check for end of recursion
if (count === 0) {
tetra(a, b, c, d);
}
// find midpoints of sides
// divide four smaller tetrahedra
else {
var ab = mix(a, b, 0.5);
var ac = mix(a, c, 0.5);
var ad = mix(a, d, 0.5);
var bc = mix(b, c, 0.5);
var bd = mix(b, d, 0.5);
var cd = mix(c, d, 0.5);
--count;
divideTetra(a, ab, ac, ad, count);
divideTetra(ab, b, bc, bd, count);
divideTetra(ac, bc, c, cd, count);
divideTetra(ad, bd, cd, d, count);
}
}
function render() {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
let config = {};
if (animation) {
config = animationData;
} else {
config = configurations;
}
const { x: scaleX, y: scaleY, z: scaleZ } = config.scaling_factor;
const scaleW = 2;
const theta = config.rotation_angle;
const {
x: translateX,
y: translateY,
z: translateZ,
} = config.translation_magnitude;
// prettier-ignore
let scaleFactor = mat4(
scaleX, 0, 0, 0,
0, scaleY, 0, 0,
0, 0, scaleZ, 0,
0, 0, 0, scaleW
);
let modelViewMatrix = rotateX(theta.x);
modelViewMatrix = mult(modelViewMatrix, rotateY(theta.y));
modelViewMatrix = mult(modelViewMatrix, rotateZ(theta.z));
modelViewMatrix = mult(modelViewMatrix, scaleFactor);
modelViewMatrix = mult(
modelViewMatrix,
translate(translateX, translateY, translateZ)
);
gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(modelViewMatrix));
gl.drawArrays(gl.TRIANGLES, 0, points.length);
requestAnimFrame(render);
}