map viewer
This commit is contained in:
parent
93bc6e7a07
commit
2b1f1a0191
15 changed files with 2355 additions and 40 deletions
|
|
@ -139,12 +139,68 @@ function blitRgba(canvas, canvasWidth, canvasHeight, sprite, dstX, dstY, flipped
|
|||
}
|
||||
}
|
||||
|
||||
// Magenta diamond marker for placeholder (non-renderable) entities such as
|
||||
// spawners, triggers, NPCs, and UI panels. Drawn in place of a sprite blit so
|
||||
// the viewer still surfaces the entity for inspection without guessing wrong
|
||||
// art. The diamond is 12x12 by default (caller passes the placeholder width).
|
||||
function drawPlaceholderMarker(canvas, canvasWidth, canvasHeight, dstX, dstY, w, h) {
|
||||
const half = Math.max(2, Math.floor(Math.min(w, h) / 2));
|
||||
const cx = Math.round(dstX + w / 2);
|
||||
const cy = Math.round(dstY + h / 2);
|
||||
// Filled diamond by scanline.
|
||||
for (let dy = -half; dy <= half; dy += 1) {
|
||||
const span = half - Math.abs(dy);
|
||||
const y = cy + dy;
|
||||
if (y < 0 || y >= canvasHeight) continue;
|
||||
for (let dx = -span; dx <= span; dx += 1) {
|
||||
const x = cx + dx;
|
||||
if (x < 0 || x >= canvasWidth) continue;
|
||||
// Magenta interior with darker outline on the diamond edge.
|
||||
const onEdge = Math.abs(dx) + Math.abs(dy) === half;
|
||||
const target = ((y * canvasWidth) + x) * 4;
|
||||
canvas[target + 0] = onEdge ? 90 : 220;
|
||||
canvas[target + 1] = onEdge ? 0 : 60;
|
||||
canvas[target + 2] = onEdge ? 110 : 200;
|
||||
canvas[target + 3] = 255;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function renderMap(items, options = {}) {
|
||||
if (items.length === 0) {
|
||||
throw new Error('No renderable scene items were produced.');
|
||||
}
|
||||
|
||||
const bounds = items.reduce(
|
||||
// Guard against a single bad item (e.g. corrupt sprite origin) inflating the
|
||||
// canvas to multiple gigabytes. We compute the median centroid of all items
|
||||
// and drop anything farther than MAX_EXTENT px from it before computing
|
||||
// bounds. This keeps render output sane even if the parser produces a few
|
||||
// outliers, while preserving every real authored item (the playfield is
|
||||
// never larger than a few thousand pixels in either axis).
|
||||
const MAX_EXTENT = 4096;
|
||||
const centroids = items.map((it) => ({ x: it.drawX + (it.width ?? 0) / 2, y: it.drawY + (it.height ?? 0) / 2 }));
|
||||
const sortedX = centroids.map((c) => c.x).sort((a, b) => a - b);
|
||||
const sortedY = centroids.map((c) => c.y).sort((a, b) => a - b);
|
||||
const medianX = sortedX[sortedX.length >> 1];
|
||||
const medianY = sortedY[sortedY.length >> 1];
|
||||
const droppedOutliers = [];
|
||||
const visibleItems = items.filter((it, idx) => {
|
||||
const dx = Math.abs(centroids[idx].x - medianX);
|
||||
const dy = Math.abs(centroids[idx].y - medianY);
|
||||
if (dx > MAX_EXTENT || dy > MAX_EXTENT) {
|
||||
droppedOutliers.push({ recordIndex: it.recordIndex, typeWord: it.typeWord, drawX: it.drawX, drawY: it.drawY, dx, dy });
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
if (droppedOutliers.length > 0 && process.env.PSX_TRACE) {
|
||||
console.error('[renderMap] dropped ' + droppedOutliers.length + ' outlier item(s) beyond ' + MAX_EXTENT + 'px from median; first: ' + JSON.stringify(droppedOutliers[0]));
|
||||
}
|
||||
if (visibleItems.length === 0) {
|
||||
throw new Error('All scene items were classified as outliers; nothing to render.');
|
||||
}
|
||||
|
||||
const bounds = visibleItems.reduce(
|
||||
(state, item) => ({
|
||||
minX: Math.min(state.minX, item.drawX),
|
||||
minY: Math.min(state.minY, item.drawY),
|
||||
|
|
@ -157,9 +213,27 @@ export function renderMap(items, options = {}) {
|
|||
const padding = 16;
|
||||
const width = Math.max(1, (bounds.maxX - bounds.minX) + (padding * 2));
|
||||
const height = Math.max(1, (bounds.maxY - bounds.minY) + (padding * 2));
|
||||
if (process.env.PSX_TRACE) {
|
||||
console.error('[renderMap] items=' + visibleItems.length + ' bounds=' + JSON.stringify(bounds) + ' size=' + width + 'x' + height);
|
||||
}
|
||||
const canvas = clearCanvas(width, height, options.background ?? DEFAULT_BACKGROUND);
|
||||
|
||||
for (const item of items) {
|
||||
for (const item of visibleItems) {
|
||||
if (item.placeholder || !item.sprite) {
|
||||
// Draw a small magenta diamond marker for non-renderable entities
|
||||
// (spawners, triggers, NPCs). Keeps them visible and inspectable in
|
||||
// the viewer overlay without guessing wrong art.
|
||||
drawPlaceholderMarker(
|
||||
canvas,
|
||||
width,
|
||||
height,
|
||||
item.drawX - bounds.minX + padding,
|
||||
item.drawY - bounds.minY + padding,
|
||||
item.width,
|
||||
item.height
|
||||
);
|
||||
continue;
|
||||
}
|
||||
blitRgba(
|
||||
canvas,
|
||||
width,
|
||||
|
|
@ -179,7 +253,7 @@ export function renderMap(items, options = {}) {
|
|||
// on every tile and obscure surrounding art.
|
||||
const labelBudgetPerBundle = 10;
|
||||
const labelTally = new Map();
|
||||
for (const item of items) {
|
||||
for (const item of visibleItems) {
|
||||
const tallyKey = `${item.typeWord ?? '?'}|${item.bundleAbsoluteOffset ?? '?'}`;
|
||||
const used = labelTally.get(tallyKey) ?? 0;
|
||||
if (used >= labelBudgetPerBundle) {
|
||||
|
|
@ -210,6 +284,7 @@ export function renderMap(items, options = {}) {
|
|||
width,
|
||||
height,
|
||||
bounds,
|
||||
droppedOutlierCount: droppedOutliers.length,
|
||||
png: encodePng(canvas, width, height),
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue