@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
54 lines (53 loc) • 1.65 kB
JavaScript
;
import { mod } from "../math/_Module";
import { EMPTY_TILE_ID } from "./WFCConstant";
export const WFC_ALL_HORIZONTAL_SIDES = "snwe";
export const ALL_HORIZONTAL_SIDES = "snwe";
export const ALL_SIDES = ["s", "n", "w", "e", "b", "t"];
export const CLOCK_WISE_TILE_SIDES = ["n", "e", "s", "w"];
export function rotatedSide(side, rotation) {
if (side == "t" || side == "b") {
return side;
}
const index = CLOCK_WISE_TILE_SIDES.indexOf(side);
return CLOCK_WISE_TILE_SIDES[mod(index + rotation, 4)];
}
export function configTilesStats(tileConfigs, target) {
target.solid = 0;
target.empty = 0;
for (const config of tileConfigs) {
if (config.tileId == EMPTY_TILE_ID) {
target.empty++;
} else {
target.solid++;
}
}
}
export function solidTilesStats(tileConfigs) {
return tileConfigs.filter((config) => config.tileId != EMPTY_TILE_ID);
}
export function sortTileIds(id0, id1, target, invert = false) {
if (invert) {
if (id0 > id1) {
target.first = id0;
target.second = id1;
} else {
target.first = id1;
target.second = id0;
}
} else {
if (id0 < id1) {
target.first = id0;
target.second = id1;
} else {
target.first = id1;
target.second = id0;
}
}
}
export function tileSideUnrotated(xOffset, yOffset, zOffset) {
return xOffset < 0 ? "s" : xOffset > 0 ? "n" : zOffset < 0 ? "w" : zOffset > 0 ? "e" : yOffset < 0 ? "b" : "t";
}
export function neighbourTileSideUnrotated(xOffset, yOffset, zOffset) {
return xOffset < 0 ? "n" : xOffset > 0 ? "s" : zOffset < 0 ? "e" : zOffset > 0 ? "w" : yOffset < 0 ? "t" : "b";
}