-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
323 lines (298 loc) · 7.85 KB
/
sketch.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
const repeatSix = (attr) => [attr, attr, attr, attr, attr, attr];
const element_attributes = [
//Near
...repeatSix([255, 255, 255]),
//Bottom
...repeatSix([0, 0, 255]),
//Far
...repeatSix([0, 255, 255]),
//Left
...repeatSix([255, 0, 255]),
//Right
...repeatSix([255, 255, 0]),
//Top
...repeatSix([0, 255, 0]),
];
const elements = [
//Near
[0, 6, 2],
[0, 4, 6],
//Bottom
[0, 2, 1],
[1, 2, 3],
//Far
[7, 5, 3],
[3, 5, 1],
//Left
[5, 4, 1],
[1, 4, 0],
//Right
[6, 7, 2],
[2, 7, 3],
//Top
[5, 7, 4],
[4, 7, 6],
].flat();
const vertices_model_space = [
[0, 0, 0],
[0, 0, 3],
[3, 0, 0],
[3, 0, 3],
[0, 3, 0],
[0, 3, 3],
[3, 3, 0],
[3, 3, 3],
];
function clamp(x, low, high) {
if (x < low) {
return low;
}
if (x > high) {
return high;
}
return x;
}
function drawOneTriangle(ctx, attributes, fragShader) {
const A = attributes[0];
const B = attributes[1];
const C = attributes[2];
const xLeft = new Array(ctx.H).fill(Number.POSITIVE_INFINITY);
const xRight = new Array(ctx.H).fill(Number.NEGATIVE_INFINITY);
function logAttributesAndCacheHorizontalEndpoints(attribute) {
const x = Math.round(attribute[0]);
const y = Math.round(attribute[1]);
ctx.setFragmentAttribute(x, y, attribute);
xLeft[y] = Math.min(x, xLeft[y]);
xRight[y] = Math.max(x, xRight[y]);
}
const roundXY = (T) => [Math.round(T[0]), Math.round(T[1]), ...T.slice(2)];
for (const attribute of DdaInterpolation(roundXY(A), roundXY(B))) {
logAttributesAndCacheHorizontalEndpoints(attribute);
}
for (const attribute of DdaInterpolation(roundXY(B), roundXY(C))) {
logAttributesAndCacheHorizontalEndpoints(attribute);
}
for (const attribute of DdaInterpolation(roundXY(C), roundXY(A))) {
logAttributesAndCacheHorizontalEndpoints(attribute);
}
for (let y = 0; y < ctx.H; y++) {
const leftEnd = xLeft[y];
const rightEnd = xRight[y];
const shouldPaint =
leftEnd !== Number.POSITIVE_INFINITY &&
rightEnd !== Number.NEGATIVE_INFINITY;
if (shouldPaint) {
for (const attribute of DdaInterpolation(
ctx.getFragmentAttribute(leftEnd, y),
ctx.getFragmentAttribute(rightEnd, y),
)) {
const x = Math.ceil(attribute[0]);
ctx.setFragmentAttribute(x, y, attribute);
}
const left = clamp(leftEnd, 0, ctx.W - 1);
const right = clamp(rightEnd, 0, ctx.W - 1);
for (let i = left; i < right + 1; i++) {
const a = ctx.getFragmentAttribute(i, y);
if (a !== undefined) {
const z = a[2];
if (z < ctx.getDepthBuffer(i, y)) {
ctx.setDepthBuffer(i, y, z);
setPixel(i, y, fragShader(a));
}
} else {
setPixel(i, y, [255, 0, 0]);
}
}
}
}
}
const toEye = [0, 0, -1];
function drawTriangles(
ctx,
vertices,
elements,
element_attributes,
fragShader,
) {
console.assert(
elements.length % 3 === 0,
"drawTriangles asserts that the number of elements are a multiply of 3.",
);
for (let i = 0; i < elements.length; i += 3) {
const A = vertices[elements[i]];
const B = vertices[elements[i + 1]];
const C = vertices[elements[i + 2]];
if (Dot(toEye, Cross(Sub(B, C), Sub(A, B))) > 0) {
const attributes = [
[...A, ...element_attributes[i]],
[...B, ...element_attributes[i + 1]],
[...C, ...element_attributes[i + 2]],
];
drawOneTriangle(ctx, attributes, fragShader);
}
}
}
const GpuCtx = class {
attributesLookUp = new Array(screenW * screenH).fill(undefined);
depthBuffer = new Array(screenW * screenH).fill(Number.POSITIVE_INFINITY);
W;
H;
constructor(W, H) {
this.W = W;
this.H = H;
}
getFragmentAttribute(x, y) {
return this.attributesLookUp[y * this.W + x];
}
setFragmentAttribute(x, y, attr) {
this.attributesLookUp[y * this.W + x] = attr;
}
setDepthBuffer(x, y, depth) {
this.depthBuffer[y * this.W + x] = depth;
}
getDepthBuffer(x, y) {
return this.depthBuffer[y * this.W + x];
}
};
const PRIMARY_BTN = 1;
const SECONDARY_BTN = 2;
let panHorizontal = 0.4;
let panVertical = 0.17;
let axisHorizontal = 5;
let axisVertical = 16;
let zoomFactor = 1;
const zoomStep = -0.06;
function withinCanvas(event) {
return event.toElement === canvasElt;
}
function mouseDragged(event) {
if (!withinCanvas(event)) return;
if (event.buttons === SECONDARY_BTN) {
panHorizontal += event.movementX / cW;
panVertical += event.movementY / cH;
}
if (event.buttons === PRIMARY_BTN) {
axisVertical += event.movementY;
axisHorizontal += event.movementX;
}
return false;
}
function mouseWheel(event) {
if (!withinCanvas(event)) return;
zoomFactor += event.deltaY > 0 ? zoomStep : -zoomStep;
}
function plzPerspective() {
const n = -1;
const f = 1;
const fov = Radians(60 * (2 - zoomFactor));
const s = 1 / Math.tan(fov / 2);
return [n * s, 0, 0, 0, 0, n * s, 0, 0, 0, 0, n + f, -1, 0, 0, -f * n, 0];
}
const aspect_ratio = 1;
const orthogonal_projection_W = 2;
const orthogonal_projection_H = orthogonal_projection_W / aspect_ratio;
const orthogonal_projection_D = 2;
const clip_space_W = 2;
const clip_space_H = 2;
const clip_space_D = 2;
function plzOrthogonal() {
return plzMany(
plzScale(
clip_space_W / orthogonal_projection_W,
clip_space_H / orthogonal_projection_H,
clip_space_D / orthogonal_projection_D,
),
plzScale(zoomFactor, zoomFactor, zoomFactor),
);
}
function plzMoveCamera(position, spinX, spinY, spinZ) {
return plzMany(
plzRotateX(spinX),
plzRotateY(spinY),
plzRotateZ(spinZ),
plzTranslate(...Times(-1, position)),
);
}
const defaultShader = interpolateVertexAttributes;
// FIXME: very repetitive, refactor when a third config is added
let useMSAA = false;
const useMSAASelector = "[data-msaa-toggle]";
const msaaToggle = document.querySelector(useMSAASelector);
if (msaaToggle !== null) {
msaaToggle.checked = useMSAA;
msaaToggle.addEventListener("change", (ev) => {
useMSAA = ev.target.checked;
});
} else {
console.warn(`${useMSAASelector} is not found, useMSAA is ${useMSAA}`);
}
let usePerspective = true;
const usePerspectiveSelector = "[data-perspective-toggle]";
const perspectiveToggle = document.querySelector(usePerspectiveSelector);
if (perspectiveToggle !== null) {
perspectiveToggle.checked = usePerspective;
perspectiveToggle.addEventListener("change", (ev) => {
usePerspective = ev.target.checked;
});
} else {
console.warn(
`${usePerspectiveSelector} is not found, usePerspective is ${usePerspective}`,
);
}
function drawArray() {
const model_world = plzScale(1, 1, 2);
const world_view = plzMany(
plzScale(
orthogonal_projection_W / 3,
orthogonal_projection_H / 3,
orthogonal_projection_D / 3,
),
plzTranslate(
-orthogonal_projection_W / 2,
-orthogonal_projection_W / 2,
-orthogonal_projection_D / 2,
),
plzMoveCamera(
[-panHorizontal * pDim, panVertical * pDim, -3],
-axisVertical,
-axisHorizontal,
0,
),
);
let projection;
if (usePerspective) {
projection = plzPerspective();
} else {
projection = plzOrthogonal();
}
const vertexShader = makeBasicVertexShader(
plzMany(model_world, world_view, projection),
);
const vertices_clip_space = vertices_model_space.map(vertexShader);
const vertices_screen_space = vertices_clip_space.map((v) =>
plzApplyManyMat4(
v,
plzMany(
plzTranslate(1, 1, 0),
plzScale(1 / 2, 1 / 2, 1),
plzScale(1, -1, 1),
plzTranslate(0, 1, 0),
plzScale(screenW - 1, screenH - 1, 1),
),
),
);
const gpuCtx = new GpuCtx(screenW, screenH);
let shader;
if (useMSAA) {
shader = MSAA(gpuCtx);
} else {
shader = defaultShader;
}
drawTriangles(
gpuCtx,
vertices_screen_space,
elements,
element_attributes,
shader,
);
}