-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimitives.js
More file actions
178 lines (156 loc) · 5.37 KB
/
Copy pathprimitives.js
File metadata and controls
178 lines (156 loc) · 5.37 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
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
// @ts-check
const WIDTH = 800;
const HEIGHT = 420;
// ── Shared vector utilities ────────────────────────────────────
function cross(a, b) {
return [
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0],
];
}
function normalize(v) {
const len = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
if (len === 0) return [0, 0, 0];
return [v[0] / len, v[1] / len, v[2] / len];
}
// ── Shared camera & lighting globals ───────────────────────────
const camZ = -500;
const fov = 500;
const lightDir = normalize([-0.5, -0.5, -1]);
const ambient = 0.4;
let rotX = 0;
let rotY = 0;
let rotZ = 0;
/** @type {HTMLCanvasElement} */
const canvas = document.getElementById('screen');
const ctx = canvas.getContext('2d');
// double buffering
const frontData = ctx.createImageData(WIDTH, HEIGHT);
const backBuf = new Uint8ClampedArray(WIDTH * HEIGHT * 4);
const zBuf = new Float32Array(WIDTH * HEIGHT);
/**
* @param {number} x
* @param {number} y
* @param {number} z - depth (smaller = closer)
* @param {number} r
* @param {number} g
* @param {number} b
* @param {number} a
*/
function putpixel(x, y, z, r, g, b, a) {
x = x | 0;
y = y | 0;
if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) return;
const zi = y * WIDTH + x;
if (z >= zBuf[zi]) return;
const i = zi << 2;
if (a >= 255) {
zBuf[zi] = z;
backBuf[i] = r & 0xff;
backBuf[i + 1] = g & 0xff;
backBuf[i + 2] = b & 0xff;
backBuf[i + 3] = 255;
} else {
// semi-transparent: blend over existing, don't claim depth
const sa = (a & 0xff) / 255;
const da = 1 - sa;
backBuf[i] = (r * sa + backBuf[i] * da) & 0xff;
backBuf[i + 1] = (g * sa + backBuf[i + 1] * da) & 0xff;
backBuf[i + 2] = (b * sa + backBuf[i + 2] * da) & 0xff;
backBuf[i + 3] = Math.min(255, backBuf[i + 3] + a) & 0xff;
}
}
/** Fill a horizontal span with interpolated Blinn-Phong lighting (smooth) or constant normal (flat). */
function fillSpan(y, xL, zL, nxL, nyL, nzL, xR, zR, nxR, nyR, nzR, r, g, b, a) {
const xStart = Math.ceil(xL);
const xEnd = Math.floor(xR);
const span = xR - xL;
const halfW = WIDTH / 2;
const halfH = HEIGHT / 2;
for (let x = xStart; x <= xEnd; x++) {
const t = span > 0 ? (x - xL) / span : 0;
const z = zL + t * (zR - zL);
// interpolate and re-normalize the normal
let nx = nxL + t * (nxR - nxL);
let ny = nyL + t * (nyR - nyL);
let nz = nzL + t * (nzR - nzL);
const nlen = Math.sqrt(nx * nx + ny * ny + nz * nz);
if (nlen > 0) { nx /= nlen; ny /= nlen; nz /= nlen; }
// diffuse
const diff = Math.max(0, nx * lightDir[0] + ny * lightDir[1] + nz * lightDir[2]);
const brightness = ambient + diff;
// Blinn-Phong specular: half-vector between light and view
const vlen = Math.sqrt((x - halfW) * (x - halfW) + (y - halfH) * (y - halfH) + fov * fov);
const hx = lightDir[0] + (halfW - x) / vlen;
const hy = lightDir[1] + (halfH - y) / vlen;
const hz = lightDir[2] + (-fov) / vlen;
const hlen = Math.sqrt(hx * hx + hy * hy + hz * hz);
const ndoth = hlen > 0 ? Math.max(0, (nx * hx + ny * hy + nz * hz) / hlen) : 0;
const spec = Math.pow(ndoth, RT_SPECULAR_EXP) * RT_SPECULAR_STR;
putpixel(x, y, z,
Math.min(255, r * brightness + spec * 255),
Math.min(255, g * brightness + spec * 255),
Math.min(255, b * brightness + spec * 255), a);
}
}
/** Get the min and max Y of a polygon, clamped to screen bounds. */
function polyYBounds(pts) {
let ymin = HEIGHT, ymax = 0;
for (let i = 0; i < pts.length; i++) {
const y = pts[i][1] | 0;
if (y < ymin) ymin = y;
if (y > ymax) ymax = y;
}
return [Math.max(0, ymin), Math.min(HEIGHT - 1, ymax)];
}
/**
* Find where a scanline at y crosses the polygon edges.
* Returns sorted array of [x, z, nx, ny, nz] intersection points.
*/
function scanlineHits(pts, y) {
const hits = [];
const n = pts.length;
for (let i = 0; i < n; i++) {
const j = (i + 1) % n;
const y0 = pts[i][1], y1 = pts[j][1];
if ((y0 <= y && y1 > y) || (y1 <= y && y0 > y)) {
const t = (y - y0) / (y1 - y0);
hits.push([
pts[i][0] + t * (pts[j][0] - pts[i][0]), // interpolated x
pts[i][2] + t * (pts[j][2] - pts[i][2]), // interpolated z
pts[i][3] + t * (pts[j][3] - pts[i][3]), // interpolated nx
pts[i][4] + t * (pts[j][4] - pts[i][4]), // interpolated ny
pts[i][5] + t * (pts[j][5] - pts[i][5]), // interpolated nz
]);
}
}
hits.sort((a, b) => a[0] - b[0]);
return hits;
}
/**
* Scanline-fill a 2D polygon. Points are [x, y, z, nx, ny, nz].
* Walks each scanline top to bottom, finds edge crossings,
* and fills horizontal spans with per-pixel Phong lighting.
*/
function fillpoly(pts, r, g, b, a) {
if (pts.length < 3) return;
const [ymin, ymax] = polyYBounds(pts);
for (let y = ymin; y <= ymax; y++) {
const hits = scanlineHits(pts, y);
for (let i = 0; i < hits.length - 1; i += 2) {
fillSpan(y,
hits[i][0], hits[i][1], hits[i][2], hits[i][3], hits[i][4],
hits[i+1][0], hits[i+1][1], hits[i+1][2], hits[i+1][3], hits[i+1][4],
r, g, b, a);
}
}
}
function clear() {
backBuf.fill(0);
zBuf.fill(Infinity);
}
function flip() {
frontData.data.set(backBuf);
ctx.putImageData(frontData, 0, 0);
}