@spearwolf/twopoint5d
Version:
Create 2.5D realtime graphics and pixelart with WebGL and three.js
89 lines • 3.24 kB
JavaScript
import { Vector2, Vector3 } from 'three/webgpu';
import { AABB2 } from './AABB2.js';
import { Map2DTileCoords } from './Map2DTileCoords.js';
export class RectangularVisibilityArea {
#width;
#height;
#tileCreated;
constructor(width = 320, height = 240) {
this.#width = 0;
this.#height = 0;
this.needsUpdate = true;
this.width = width;
this.height = height;
}
get width() {
return this.#width;
}
set width(width) {
if (this.#width !== width) {
this.#width = width;
this.needsUpdate = true;
}
}
get height() {
return this.#height;
}
set height(height) {
if (this.#height !== height) {
this.#height = height;
this.needsUpdate = true;
}
}
computeVisibleTiles(previousTiles, [centerX, centerY], map2dTileCoords, matrixWorld) {
if (this.width === 0 || this.height === 0) {
return undefined;
}
const { width, height } = this;
const halfWidth = width / 2;
const halfHeight = height / 2;
const left = centerX - halfWidth;
const top = centerY - halfHeight;
const tileCoords = map2dTileCoords.computeTilesWithinCoords(left, top, width, height);
const fullViewArea = AABB2.from(tileCoords);
const removeTiles = [];
const reuseTiles = [];
const tilesLength = tileCoords.rows * tileCoords.columns;
let tileCreated = this.#tileCreated;
if (tileCreated == null || tileCreated.length < tilesLength) {
this.#tileCreated = new Uint8Array(tilesLength);
tileCreated = this.#tileCreated;
}
else {
tileCreated.fill(0);
}
previousTiles.forEach((tile) => {
if (fullViewArea.isIntersecting(tile.view)) {
reuseTiles.push(tile);
const tx = tile.x - tileCoords.tileLeft;
const ty = tile.y - tileCoords.tileTop;
tileCreated[ty * tileCoords.columns + tx] = 1;
}
else {
removeTiles.push(tile);
}
});
const createTiles = [];
for (let ty = 0; ty < tileCoords.rows; ty++) {
for (let tx = 0; tx < tileCoords.columns; tx++) {
if (tileCreated[ty * tileCoords.columns + tx] === 0) {
const tileX = tx + tileCoords.tileLeft;
const tileY = ty + tileCoords.tileTop;
const tile = new Map2DTileCoords(tileX, tileY, new AABB2(tileX * tileCoords.tileWidth, tileY * tileCoords.tileHeight, tileCoords.tileWidth, tileCoords.tileHeight));
createTiles.push(tile);
}
}
}
const offset = new Vector2(map2dTileCoords.xOffset - centerX, map2dTileCoords.yOffset - centerY);
const translate = new Vector3().setFromMatrixPosition(matrixWorld);
return {
tiles: reuseTiles.concat(createTiles),
offset,
translate,
removeTiles,
createTiles,
reuseTiles,
};
}
}
//# sourceMappingURL=RectangularVisibilityArea.js.map