Skip to content

Commit

Permalink
feat: add usage oriented aliases to 'Arrays' such as 'TriAreasArray'
Browse files Browse the repository at this point in the history
  • Loading branch information
isaac-mason committed Dec 20, 2023
1 parent 173e738 commit 467747f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 21 deletions.
7 changes: 7 additions & 0 deletions .changeset/strange-crabs-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@recast-navigation/generators": patch
"@recast-navigation/core": patch
"recast-navigation": patch
---

feat: add usage oriented aliases to 'Arrays' such as 'TriAreasArray'
23 changes: 21 additions & 2 deletions packages/recast-navigation-core/src/raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ const arrays = [
'FloatArray',
] as const satisfies readonly ModuleKey[];

const arrayAliases = {
VertsArray: 'FloatArray',
TrisArray: 'IntArray',
TriAreasArray: 'UnsignedCharArray',
ChunkIdsArray: 'IntArray',
TileCacheData: 'UnsignedCharArray',
} satisfies Record<string, (typeof arrays)[number]>;

type RawApi = Pretty<
{
Module: typeof RawModule;
Expand All @@ -53,9 +61,15 @@ type RawApi = Pretty<
}
>;

type ArraysApi = Pretty<{
type Arrays = {
[K in (typeof arrays)[number]]: (typeof RawModule)[K];
}>;
};

type ArrayAliases = {
[K in keyof typeof arrayAliases]: (typeof RawModule)[(typeof arrayAliases)[K]];
};

type ArraysApi = Pretty<Arrays & ArrayAliases>;

export const Arrays = {} as ArraysApi;

Expand Down Expand Up @@ -88,4 +102,9 @@ export const init = async () => {
for (const array of arrays) {
Arrays[array] = Raw.Module[array];
}

for (const [arrayAlias, target] of Object.entries(arrayAliases)) {
Arrays[arrayAlias as keyof ArrayAliases] =
Raw.Module[target as keyof Arrays];
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ export const generateSoloNavMesh = (

const verts = positions as number[];
const nVerts = indices.length;
const vertsArray = new Arrays.FloatArray();
const vertsArray = new Arrays.VertsArray();
vertsArray.copy(verts, verts.length);

const tris = indices as number[];
const nTris = indices.length / 3;
const trisArray = new Arrays.IntArray();
const trisArray = new Arrays.TrisArray();
trisArray.copy(tris, tris.length);

const { bbMin, bbMax } = getBoundingBox(positions, indices);
Expand Down Expand Up @@ -176,7 +176,7 @@ export const generateSoloNavMesh = (
// Find triangles which are walkable based on their slope and rasterize them.
// If your input data is multiple meshes, you can transform them here, calculate
// the are type for each of the meshes and rasterize them.
const triAreasArray = new Arrays.UnsignedCharArray();
const triAreasArray = new Arrays.TriAreasArray();
triAreasArray.resize(nTris);

markWalkableTriangles(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export const generateTileCache = (
): TileCacheGeneratorResult => {
const buildContext = new RecastBuildContext();

let intermediates: TileCacheGeneratorIntermediates = {
const intermediates: TileCacheGeneratorIntermediates = {
type: 'tilecache',
buildContext,
chunkyTriMesh: undefined,
Expand Down Expand Up @@ -179,12 +179,12 @@ export const generateTileCache = (

const verts = positions as number[];
const nVerts = indices.length;
const vertsArray = new Arrays.FloatArray();
const vertsArray = new Arrays.VertsArray();
vertsArray.copy(verts, verts.length);

const tris = indices as number[];
const nTris = indices.length / 3;
const trisArray = new Arrays.IntArray();
const trisArray = new Arrays.TrisArray();
trisArray.copy(tris, tris.length);

const { bbMin, bbMax } = getBoundingBox(positions, indices);
Expand Down Expand Up @@ -349,7 +349,7 @@ export const generateTileCache = (

// TODO: Make grow when returning too many items.
const maxChunkIds = 512;
const chunkIdsArray = new Arrays.IntArray();
const chunkIdsArray = new Arrays.ChunkIdsArray();
chunkIdsArray.resize(maxChunkIds);

const nChunksOverlapping = chunkyTriMesh.getChunksOverlappingRect(
Expand All @@ -370,7 +370,7 @@ export const generateTileCache = (

const nodeTrisArray = chunkyTriMesh.getNodeTris(nodeId);

const triAreasArray = new Arrays.UnsignedCharArray();
const triAreasArray = new Arrays.TriAreasArray();
triAreasArray.resize(nNodeTris);

// Find triangles which are walkable based on their slope and rasterize them.
Expand Down Expand Up @@ -474,7 +474,7 @@ export const generateTileCache = (
const tiles: R.UnsignedCharArray[] = [];

for (let i = 0; i < heightfieldLayerSet.nlayers(); i++) {
const tile = new Arrays.UnsignedCharArray();
const tile = new Arrays.TileCacheData();
const heightfieldLayer = heightfieldLayerSet.layers(i);

// Store header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,13 @@ export const generateTiledNavMesh = (
/* verts */
const verts = positions as number[];
const nVerts = indices.length;
const vertsArray = new Arrays.FloatArray();
const vertsArray = new Arrays.VertsArray();
vertsArray.copy(verts, verts.length);

/* tris */
const tris = indices as number[];
const nTris = indices.length / 3;
const trisArray = new Arrays.IntArray();
const trisArray = new Arrays.TrisArray();
trisArray.copy(tris, tris.length);

/* Create dtNavMeshParams, initialise nav mesh for tiled use */
Expand Down Expand Up @@ -336,7 +336,7 @@ export const generateTiledNavMesh = (
// Allocate array that can hold triangle flags.
// If you have multiple meshes you need to process, allocate
// and array which can hold the max number of triangles you need to process.
const triAreas = new Arrays.UnsignedCharArray();
const triAreas = new Arrays.TriAreasArray();
triAreas.resize(chunkyTriMesh.maxTrisPerChunk());

const tbmin: Vector2Tuple = [
Expand All @@ -350,7 +350,7 @@ export const generateTiledNavMesh = (

// TODO: Make grow when returning too many items.
const maxChunkIds = 512;
const chunkIdsArray = new Arrays.IntArray();
const chunkIdsArray = new Arrays.ChunkIdsArray();
chunkIdsArray.resize(maxChunkIds);

const nChunksOverlapping = chunkyTriMesh.getChunksOverlappingRect(
Expand All @@ -371,7 +371,7 @@ export const generateTiledNavMesh = (

const nodeTrisArray = chunkyTriMesh.getNodeTris(nodeId);

const triAreasArray = new Arrays.UnsignedCharArray();
const triAreasArray = new Arrays.TriAreasArray();
triAreasArray.resize(nNodeTris);

// Find triangles which are walkable based on their slope and rasterize them.
Expand Down Expand Up @@ -588,8 +588,8 @@ export const generateTiledNavMesh = (

buildContext.startTimer(Raw.Module.RC_TIMER_TEMP);

let lastBuiltTileBmin: Vector3Tuple = [0, 0, 0];
let lastBuiltTileBmax: Vector3Tuple = [0, 0, 0];
const lastBuiltTileBmin: Vector3Tuple = [0, 0, 0];
const lastBuiltTileBmax: Vector3Tuple = [0, 0, 0];

for (let y = 0; y < tileHeight; y++) {
for (let x = 0; x < tileWidth; x++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ export const generateNavMesh = (

const verts = positions as number[];
const nVerts = indices.length;
const vertsArray = new Arrays.FloatArray();
const vertsArray = new Arrays.VertsArray();
vertsArray.copy(verts, verts.length);

const tris = indices as number[];
const nTris = indices.length / 3;
const trisArray = new Arrays.IntArray();
const trisArray = new Arrays.TrisArray();
trisArray.copy(tris, tris.length);

const { bbMin, bbMax } = getBoundingBox(positions, indices);
Expand Down Expand Up @@ -186,7 +186,7 @@ export const generateNavMesh = (
return fail('Could not create heightfield');
}

const triAreasArray = new Arrays.UnsignedCharArray();
const triAreasArray = new Arrays.TriAreasArray();
triAreasArray.resize(nTris);

markWalkableTriangles(
Expand Down

0 comments on commit 467747f

Please sign in to comment.