Lobby 3d
This commit is contained in:
1 parent
fd5e92d0f5
commit
9f898c04dd
415 files changed
+6241617
-633464
No files matched your search
@@ -0,0 +1,95 @@
|
||||
// Простой рендер моделей из кубов в PNG (без Blockbench): ортографическая камера спереди-справа-сверху,
|
||||
// плоское освещение, z-буфер, сглаживание 2×. Повороты кубов как в lib (Rz·Ry·Rx вокруг pivot).
|
||||
const fs = require('fs');
|
||||
const zlib = require('zlib');
|
||||
|
||||
function corners(c) {
|
||||
const d2r = Math.PI / 180, [ax, ay, az] = c.rot.map(a => a * d2r), p0 = c.pivot, out = [];
|
||||
for (let i = 0; i < 8; i++) {
|
||||
let x = (i & 4 ? c.to[0] : c.from[0]) - p0[0], y = (i & 2 ? c.to[1] : c.from[1]) - p0[1], z = (i & 1 ? c.to[2] : c.from[2]) - p0[2];
|
||||
[y, z] = [y * Math.cos(ax) - z * Math.sin(ax), y * Math.sin(ax) + z * Math.cos(ax)];
|
||||
[x, z] = [x * Math.cos(ay) + z * Math.sin(ay), -x * Math.sin(ay) + z * Math.cos(ay)];
|
||||
[x, y] = [x * Math.cos(az) - y * Math.sin(az), x * Math.sin(az) + y * Math.cos(az)];
|
||||
out.push([x + p0[0], y + p0[1], z + p0[2]]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
const FACES = [[0, 1, 3, 2], [4, 6, 7, 5], [0, 4, 5, 1], [2, 3, 7, 6], [0, 2, 6, 4], [1, 5, 7, 3]];
|
||||
const sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]], dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
||||
const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
|
||||
const norm = a => { const l = Math.hypot(...a) || 1; return a.map(v => v / l); };
|
||||
|
||||
function encodePng(W, H, rgb) {
|
||||
const raw = Buffer.alloc((W * 3 + 1) * H);
|
||||
for (let y = 0; y < H; y++) { raw[y * (W * 3 + 1)] = 0; rgb.copy(raw, y * (W * 3 + 1) + 1, y * W * 3, (y + 1) * W * 3); }
|
||||
const crcT = []; for (let n = 0; n < 256; n++) { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; crcT[n] = c >>> 0; }
|
||||
const crc = b => { let c = 0xffffffff; for (const x of b) c = crcT[(c ^ x) & 255] ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; };
|
||||
const chunk = (t, d) => { const l = Buffer.alloc(4); l.writeUInt32BE(d.length); const td = Buffer.concat([Buffer.from(t), d]); const cb = Buffer.alloc(4); cb.writeUInt32BE(crc(td)); return Buffer.concat([l, td, cb]); };
|
||||
const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(W, 0); ihdr.writeUInt32BE(H, 4); ihdr[8] = 8; ihdr[9] = 2;
|
||||
return Buffer.concat([Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk('IHDR', ihdr), chunk('IDAT', zlib.deflateSync(raw)), chunk('IEND', Buffer.alloc(0))]);
|
||||
}
|
||||
|
||||
// items: [{ model, offset:[x,y,z] }]; opts: { yaw, pitch, width, height, ground }
|
||||
function render(items, file, opts = {}) {
|
||||
const yaw = (opts.yaw ?? 30) * Math.PI / 180, pitch = (opts.pitch ?? 25) * Math.PI / 180, SS = 2;
|
||||
const W = (opts.width || 1600), H = (opts.height || 1000), w = W * SS, h = H * SS;
|
||||
const f = norm([-Math.sin(yaw) * Math.cos(pitch), -Math.sin(pitch), Math.cos(yaw) * Math.cos(pitch)]);
|
||||
const r = norm(cross(f, [0, 1, 0])), u = cross(r, f), L = norm([-0.45, 1, -0.6]);
|
||||
const tris = [];
|
||||
for (const { model, offset = [0, 0, 0] } of items) {
|
||||
for (const c of model.cubes) {
|
||||
const P = corners(c).map(p => [p[0] + offset[0], p[1] + offset[1], p[2] + offset[2]]);
|
||||
const ctr = P.reduce((a, p) => [a[0] + p[0] / 8, a[1] + p[1] / 8, a[2] + p[2] / 8], [0, 0, 0]);
|
||||
const rgb = model.colors[model.colorIdx[c.col]];
|
||||
for (const F of FACES) {
|
||||
const q = F.map(i => P[i]);
|
||||
let n = norm(cross(sub(q[1], q[0]), sub(q[2], q[0])));
|
||||
const fc = q.reduce((a, p) => [a[0] + p[0] / 4, a[1] + p[1] / 4, a[2] + p[2] / 4], [0, 0, 0]);
|
||||
if (dot(n, sub(fc, ctr)) < 0) n = n.map(v => -v);
|
||||
if (dot(n, f) >= 0) continue;
|
||||
const s = 0.58 + 0.42 * Math.max(0, dot(n, L)) + (n[1] > 0.5 ? 0.04 : 0);
|
||||
const col = rgb.map(v => Math.min(255, v * s));
|
||||
const pr = q.map(p => [dot(p, r), dot(p, u), dot(p, f)]);
|
||||
tris.push([pr[0], pr[1], pr[2], col], [pr[0], pr[2], pr[3], col]);
|
||||
}
|
||||
}
|
||||
}
|
||||
// вписать в кадр
|
||||
let mnx = 1e9, mxx = -1e9, mny = 1e9, mxy = -1e9;
|
||||
for (const t of tris) for (let k = 0; k < 3; k++) { mnx = Math.min(mnx, t[k][0]); mxx = Math.max(mxx, t[k][0]); mny = Math.min(mny, t[k][1]); mxy = Math.max(mxy, t[k][1]); }
|
||||
const sc = Math.min((w * 0.92) / (mxx - mnx), (h * 0.9) / (mxy - mny)), ox = w / 2 - sc * (mnx + mxx) / 2, oy = h / 2 + sc * (mny + mxy) / 2;
|
||||
const zb = new Float32Array(w * h).fill(1e9), img = Buffer.alloc(w * h * 3);
|
||||
for (let y = 0; y < h; y++) { const g = 236 - 30 * y / h; for (let x = 0; x < w; x++) { const i = (y * w + x) * 3; img[i] = g; img[i + 1] = g + 2; img[i + 2] = g + 6; } }
|
||||
for (const [a, b, c, col] of tris) {
|
||||
const A = [ox + a[0] * sc, oy - a[1] * sc, a[2]], B = [ox + b[0] * sc, oy - b[1] * sc, b[2]], Cc = [ox + c[0] * sc, oy - c[1] * sc, c[2]];
|
||||
const area = (B[0] - A[0]) * (Cc[1] - A[1]) - (B[1] - A[1]) * (Cc[0] - A[0]);
|
||||
if (Math.abs(area) < 1e-9) continue;
|
||||
const x0 = Math.max(0, Math.floor(Math.min(A[0], B[0], Cc[0]))), x1 = Math.min(w - 1, Math.ceil(Math.max(A[0], B[0], Cc[0])));
|
||||
const y0 = Math.max(0, Math.floor(Math.min(A[1], B[1], Cc[1]))), y1 = Math.min(h - 1, Math.ceil(Math.max(A[1], B[1], Cc[1])));
|
||||
for (let y = y0; y <= y1; y++) for (let x = x0; x <= x1; x++) {
|
||||
const px = x + 0.5, py = y + 0.5;
|
||||
const w0 = ((B[0] - px) * (Cc[1] - py) - (B[1] - py) * (Cc[0] - px)) / area;
|
||||
const w1 = ((Cc[0] - px) * (A[1] - py) - (Cc[1] - py) * (A[0] - px)) / area;
|
||||
const w2 = 1 - w0 - w1;
|
||||
if (w0 < -1e-6 || w1 < -1e-6 || w2 < -1e-6) continue;
|
||||
const z = w0 * A[2] + w1 * B[2] + w2 * Cc[2], i = y * w + x;
|
||||
if (z < zb[i]) { zb[i] = z; img[i * 3] = col[0]; img[i * 3 + 1] = col[1]; img[i * 3 + 2] = col[2]; }
|
||||
}
|
||||
}
|
||||
// контур по скачку глубины — читаемость силуэтов
|
||||
const out = Buffer.alloc(W * H * 3);
|
||||
for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) {
|
||||
let s0 = 0, s1 = 0, s2 = 0, edge = false;
|
||||
for (let dy = 0; dy < SS; dy++) for (let dx = 0; dx < SS; dx++) {
|
||||
const X = x * SS + dx, Y = y * SS + dy, i = Y * w + X;
|
||||
s0 += img[i * 3]; s1 += img[i * 3 + 1]; s2 += img[i * 3 + 2];
|
||||
if (X + 1 < w && Math.abs(zb[i] - zb[i + 1]) > 0.35) edge = true;
|
||||
if (Y + 1 < h && Math.abs(zb[i] - zb[i + w]) > 0.35) edge = true;
|
||||
}
|
||||
const k = edge ? 0.55 : 1, o = (y * W + x) * 3;
|
||||
out[o] = s0 / 4 * k; out[o + 1] = s1 / 4 * k; out[o + 2] = s2 / 4 * k;
|
||||
}
|
||||
fs.writeFileSync(file, encodePng(W, H, out));
|
||||
}
|
||||
|
||||
module.exports = { render };
|
||||
Reference in new issue
Block a user