Skip to content

Commit

Permalink
feat(map): reduce view distance based on fog end
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Jan 12, 2024
1 parent 6b1e55a commit 07996e5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib/controls/MapControls.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as THREE from 'three';
import BaseOrbitControls from './BaseOrbitControls.js';

const TARGET_OFFSET = 100.0;
const TARGET_OFFSET = 30.0;

class MapControls extends BaseOrbitControls {
#raycaster = new THREE.Raycaster();
Expand Down
8 changes: 8 additions & 0 deletions src/lib/light/SceneLight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ class SceneLight {
return this.#params[this.#location].fogParams;
}

get fogStart() {
return this.#params[this.#location].fogParams.x;
}

get fogEnd() {
return this.#params[this.#location].fogParams.y;
}

get fogColor() {
return this.#params[this.#location].fogColor;
}
Expand Down
11 changes: 10 additions & 1 deletion src/lib/map/MapManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import MapLight from './light/MapLight.js';
import DbManager from '../db/DbManager.js';

const DEFAULT_VIEW_DISTANCE = 1277.0;
const EFFECTIVE_VIEW_DISTANCE_EXTENSION = 100.0;

type MapManagerOptions = {
host: AssetHost;
Expand Down Expand Up @@ -45,6 +46,7 @@ class MapManager {
#targetChunkY: number;

#viewDistance = DEFAULT_VIEW_DISTANCE;
#effectiveViewDistance = this.#viewDistance;
#projScreenMatrix = new THREE.Matrix4();
#cameraFrustum = new THREE.Frustum();

Expand Down Expand Up @@ -125,6 +127,13 @@ class MapManager {
update(deltaTime: number, camera: THREE.Camera) {
this.#mapLight.update(camera);

// If fog end is closer than the configured view distance, use the fog end plus extension as
// the effective view distance
this.#effectiveViewDistance = Math.min(
this.#mapLight.fogEnd + EFFECTIVE_VIEW_DISTANCE_EXTENSION,
this.#viewDistance,
);

this.#projScreenMatrix.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
this.#cameraFrustum.setFromProjectionMatrix(this.#projScreenMatrix);

Expand Down Expand Up @@ -228,7 +237,7 @@ class MapManager {
}

#getChunkRadius() {
return this.#viewDistance / MAP_CHUNK_HEIGHT;
return this.#effectiveViewDistance / MAP_CHUNK_HEIGHT;
}

#getAreaId(areaX: number, areaY: number) {
Expand Down

0 comments on commit 07996e5

Please sign in to comment.