Skip to content

Commit

Permalink
chore(map): move doodad culling into doodad manager
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Jan 29, 2024
1 parent ba7d5ae commit 678e36e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
29 changes: 23 additions & 6 deletions src/lib/map/DoodadManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ class DoodadManager {
#host: AssetHost;
#modelManager: ModelManager;

#loadedAreas = new Map<number, THREE.Group>();
#loadingAreas = new Map<number, Promise<THREE.Group>>();
#loadedAreas = new Map<number, THREE.Group>();
#areaBounds = new Map<number, THREE.Sphere>();

#doodads = new Map<number, Model>();
#doodadDefs = new Map<number, MapDoodadDefSpec[]>();
Expand All @@ -33,6 +34,15 @@ class DoodadManager {
});
}

cull(cullingFrustum: THREE.Frustum) {
for (const [areaId, areaGroup] of this.#loadedAreas.entries()) {
const areaBounds = this.#areaBounds.get(areaId);

const visible = cullingFrustum.intersectsSphere(areaBounds);
areaGroup.visible = visible;
}
}

getArea(areaId: number, area: MapAreaSpec): Promise<THREE.Group> {
const loaded = this.#loadedAreas.get(areaId);
if (loaded) {
Expand All @@ -51,6 +61,7 @@ class DoodadManager {
}

removeArea(areaId: number) {
this.#areaBounds.delete(areaId);
this.#loadedAreas.delete(areaId);

// Dereference doodads
Expand Down Expand Up @@ -111,8 +122,10 @@ class DoodadManager {
// Only load newly referenced doodad defs (defs can be shared across multiple areas)
const doodadDefs = area.doodadDefs.filter((doodadDef) => this.#refDoodad(doodadDef.id) === 1);

const group = new THREE.Group();
group.name = 'doodads';
const areaGroup = new THREE.Group();
areaGroup.name = 'doodads';

const areaBoundingBox = new THREE.Box3();

const doodadModels = await Promise.all(
doodadDefs.map((doodadDef) => this.#modelManager.get(doodadDef.name)),
Expand All @@ -130,15 +143,19 @@ class DoodadManager {

model.updateMatrixWorld();

group.add(model);
areaGroup.add(model);
areaBoundingBox.expandByObject(model);

this.#doodads.set(def.id, model);
}

this.#loadedAreas.set(areaId, group);
const areaBounds = areaBoundingBox.getBoundingSphere(new THREE.Sphere());
this.#areaBounds.set(areaId, areaBounds);

this.#loadedAreas.set(areaId, areaGroup);
this.#loadingAreas.delete(areaId);

return group;
return areaGroup;
}
}

Expand Down
14 changes: 2 additions & 12 deletions src/lib/map/MapManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ class MapManager extends EventTarget {

// Cull entire groups to save on frustum intersection cost
this.#cullGroups();

this.#doodadManager.cull(this.#cullingFrustum);
}

dispose() {
Expand All @@ -210,13 +212,6 @@ class MapManager extends EventTarget {
terrainGroup.userData.boundingSphere,
);
}

// Doodad groups
for (const doodadGroup of this.#doodadGroups.values()) {
doodadGroup.visible = this.#cullingFrustum.intersectsSphere(
doodadGroup.userData.boundingSphere,
);
}
}

#handleAreaTableChange() {
Expand Down Expand Up @@ -325,11 +320,6 @@ class MapManager extends EventTarget {
this.#terrainGroups.set(areaId, terrainGroup);
this.#root.add(terrainGroup);

const doodadBoundingSphere = new THREE.Box3()
.setFromObject(doodadGroup)
.getBoundingSphere(new THREE.Sphere());
doodadGroup.userData.boundingSphere = doodadBoundingSphere;

this.#doodadGroups.set(areaId, doodadGroup);
this.#root.add(doodadGroup);
}
Expand Down

0 comments on commit 678e36e

Please sign in to comment.