用途
本技能用于在游戏引擎或自定义渲染器中实现俯视角(top-down)15 格 SpriteCook 图集。它聚焦于“如何使用”已生成的图集;若需要生成图集,请使用 spritecook-generate-tilesets。
核心概念
双网格图集将地形存储为逻辑绘制的单元格,然后在这些单元格之间渲染可见瓦片。每个渲染瓦片由围绕它的四个逻辑单元格决定:
- 左上逻辑单元格
- 右上逻辑单元格
- 左下逻辑单元格
- 右下逻辑单元格
这四个单元格的填充状态组成一个 4 位掩码,掩码用于从 4x4 的 15 格图集中选择一帧。
掩码位顺序
使用 SpriteCook 当前的掩码位顺序:
let mask = 0
if (filled(x - 1, y - 1)) mask |= 1 // top-left
if (filled(x, y - 1)) mask |= 2 // top-right
if (filled(x - 1, y )) mask |= 4 // bottom-left
if (filled(x, y )) mask |= 8 // bottom-right
掩码 0 不渲染任何内容。其他掩码通过以下查找表映射到 4x4 图集:
const frameByMask = [
-1,
15,
8,
9,
0,
11,
14,
7,
13,
4,
1,
10,
3,
2,
5,
6,
]
图集单元格计算方式:
const frame = frameByMask[mask]
if (frame < 0) return null
const atlasColumn = frame % 4
const atlasRow = Math.floor(frame / 4)
渲染器伪代码
type CellMap = boolean[] // width * height logical terrain cells
function cellIndex(x: number, y: number, columns: number) {
return y * columns + x
}
function isFilled(cells: CellMap, x: number, y: number, columns: number, rows: number) {
return x >= 0 && y >= 0 && x < columns && y < rows && cells[cellIndex(x, y, columns)] === true
}
function dualGridMask(cells: CellMap, x: number, y: number, columns: number, rows: number) {
let mask = 0
if (isFilled(cells, x - 1, y - 1, columns, rows)) mask |= 1
if (isFilled(cells, x, y - 1, columns, rows)) mask |= 2
if (isFilled(cells, x - 1, y, columns, rows)) mask |= 4
if (isFilled(cells, x, y, columns, rows)) mask |= 8
return mask
}
function atlasCellForMask(mask: number) {
const frameByMask = [-1, 15, 8, 9, 0, 11, 14, 7, 13, 4, 1, 10, 3, 2, 5, 6]
const frame = frameByMask[Math.max(0, Math.min(15, Math.floor(mask)))]
if (frame < 0) return null
return { column: frame % 4, row: Math.floor(frame / 4) }
}
function renderDualGrid(ctx, atlasImage, cells: CellMap, columns: number, rows: number, tileSize: number) {
for (let y = 0; y <= rows; y += 1) {
for (let x = 0; x <= columns; x += 1) {
const mask = dualGridMask(cells, x, y, columns, rows)
const atlasCell = atlasCellForMask(mask)
if (!atlasCell) continue
ctx.drawImage(
atlasImage,
atlasCell.column * tileSize,
atlasCell.row * tileSize,
tileSize,
tileSize,
x * tileSize,
y * tileSize,
tileSize,
tileSize,
)
}
}
}
实用注意事项
- 渲染的瓦片列数和行数应比逻辑地图尺寸多一,因为渲染瓦片位于已绘制的逻辑单元格周围。
- 对于像素艺术,应禁用平滑并使用最近邻缩放。
- 将掩码 15 视为填充的内部瓦片,掩码 0 视为透明/不绘制。
- 将生成的图集严格视为等大瓦片单元格组成的 4x4 网格。
- 不要将该图集重新解释为普通的 8 邻域 blob 图集;此映射专用于 SpriteCook 的双网格 15 格布局。
适用场景与限制
适用于在游戏引擎或自定义渲染器中实现俯视角 15 格 SpriteCook 图集,尤其是需要解释或编写 4x4 图集到双网格 15 格布局映射代码的场景。本技能仅涉及图集的使用方式,不涉及图集生成;其他功能、收费、价格、平台、效果或使用步骤暂未提供。