Skip to content

Commit

Permalink
feat(map): make daynight private to map classes
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Jan 10, 2024
1 parent a0c157d commit 528027f
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './controls/MapControls.js';
export * from './controls/OrbitControls.js';
export * from './light/SceneLight.js';
export * from './map/MapManager.js';
export * from './model/ModelManager.js';
export * from './texture/TextureManager.js';
Expand Down
28 changes: 28 additions & 0 deletions src/lib/light/SceneLight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as THREE from 'three';

class SceneLight {
#sunDir = new THREE.Vector3();
#sunDirView = new THREE.Vector3();

#uniforms = {
sunDir: {
value: this.#sunDirView,
},
};

get uniforms() {
return this.#uniforms;
}

setSunDir(dir: THREE.Vector3) {
this.#sunDir.copy(dir);
}

update(camera: THREE.Camera) {
const viewMatrix = camera.matrixWorldInverse;
this.#sunDirView.copy(this.#sunDir).transformDirection(viewMatrix).normalize();
}
}

export default SceneLight;
export { SceneLight };
6 changes: 3 additions & 3 deletions src/lib/map/DoodadManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import ModelManager from '../model/ModelManager.js';
import TextureManager from '../texture/TextureManager.js';
import { AssetHost } from '../asset.js';
import { MapAreaSpec } from './loader/types.js';
import DayNight from '../daynight/DayNight.js';
import MapLight from './MapLight.js';

type DoodadManagerOptions = {
host: AssetHost;
textureManager?: TextureManager;
dayNight: DayNight;
mapLight: MapLight;
};

class DoodadManager {
Expand All @@ -21,7 +21,7 @@ class DoodadManager {
this.#modelManager = new ModelManager({
host: options.host,
textureManager: options.textureManager,
dayNight: options.dayNight,
sceneLight: options.mapLight,
});
}

Expand Down
18 changes: 18 additions & 0 deletions src/lib/map/MapLight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as THREE from 'three';
import DayNight from './daynight/DayNight.js';
import SceneLight from '../light/SceneLight.js';

class MapLight extends SceneLight {
#dayNight = new DayNight();

update(camera: THREE.Camera) {
this.#dayNight.update();

this.setSunDir(this.#dayNight.sunDir);

super.update(camera);
}
}

export default MapLight;
export { MapLight };
12 changes: 7 additions & 5 deletions src/lib/map/MapManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import DoodadManager from './DoodadManager.js';
import { AssetHost } from '../asset.js';
import MapLoader from './loader/MapLoader.js';
import { MapAreaSpec, MapSpec } from './loader/types.js';
import DayNight from '../daynight/DayNight.js';
import MapLight from './MapLight.js';

const DEFAULT_VIEW_DISTANCE = 1277.0;

Expand All @@ -33,7 +33,7 @@ class MapManager {
#terrainManager: TerrainManager;
#doodadManager: DoodadManager;

#dayNight = new DayNight();
#mapLight: MapLight;

#target = new THREE.Vector2();
#targetAreaX: number;
Expand All @@ -55,15 +55,17 @@ class MapManager {
this.#textureManager = options.textureManager ?? new TextureManager({ host: options.host });
this.#loader = new MapLoader({ host: options.host });

this.#mapLight = new MapLight();

this.#terrainManager = new TerrainManager({
host: options.host,
textureManager: this.#textureManager,
dayNight: this.#dayNight,
mapLight: this.#mapLight,
});
this.#doodadManager = new DoodadManager({
host: options.host,
textureManager: this.#textureManager,
dayNight: this.#dayNight,
mapLight: this.#mapLight,
});

this.#root = new THREE.Group();
Expand Down Expand Up @@ -111,7 +113,7 @@ class MapManager {
}

update(deltaTime: number, camera: THREE.Camera) {
this.#dayNight.update(camera);
this.#mapLight.update(camera);

this.#projScreenMatrix.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
this.#cameraFrustum.setFromProjectionMatrix(this.#projScreenMatrix);
Expand Down
18 changes: 5 additions & 13 deletions src/lib/daynight/DayNight.ts → src/lib/map/daynight/DayNight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ class DayNight {
#timeProgression = 0.0;

#sunDir = new THREE.Vector3();
#sunDirView = new THREE.Vector3();

#uniforms = {
sunDir: { value: this.#sunDirView },
};

constructor() {}

get sunDir() {
return this.#sunDir;
}

get time() {
return this.#time;
}
Expand All @@ -33,10 +32,6 @@ class DayNight {
return this.#timeProgression;
}

get uniforms() {
return this.#uniforms;
}

setTimeOverride(override: number) {
this.#timeOverride = override;
this.#updateTime();
Expand All @@ -47,12 +42,9 @@ class DayNight {
this.#updateTime();
}

update(camera: THREE.Camera) {
update() {
this.#updateTime();
this.#updateSunDirection();

const viewMatrix = camera.matrixWorldInverse;
this.#sunDirView.copy(this.#sunDir).transformDirection(viewMatrix).normalize();
}

#updateTime() {
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions src/lib/map/terrain/TerrainManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ import TerrainMaterial from './TerrainMaterial.js';
import TerrainMesh from './TerrainMesh.js';
import { AssetHost } from '../../asset.js';
import { MapAreaSpec, TerrainSpec } from '../loader/types.js';
import DayNight from '../../daynight/DayNight.js';
import MapLight from '../MapLight.js';

const SPLAT_TEXTURE_PLACEHOLDER = new THREE.Texture();

type TerrainManagerOptions = {
host: AssetHost;
textureManager?: TextureManager;
dayNight: DayNight;
mapLight: MapLight;
};

class TerrainManager {
#textureManager: TextureManager;
#dayNight: DayNight;
#mapLight: MapLight;

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

constructor(options: TerrainManagerOptions) {
this.#textureManager = options.textureManager ?? new TextureManager({ host: options.host });
this.#dayNight = options.dayNight;
this.#mapLight = options.mapLight;
}

getArea(areaId: number, area: MapAreaSpec): Promise<THREE.Group> {
Expand Down Expand Up @@ -104,7 +104,7 @@ class TerrainManager {
const layerTextures = await Promise.all(
spec.material.layers.map((layer) => this.#textureManager.get(layer.texturePath)),
);
const uniforms = { ...this.#dayNight.uniforms };
const uniforms = { ...this.#mapLight.uniforms };

return new TerrainMaterial(spec.material.layers.length, layerTextures, splatTexture, uniforms);
}
Expand Down
10 changes: 5 additions & 5 deletions src/lib/model/ModelManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getFragmentShader } from './shader/fragment.js';
import { M2_MATERIAL_PASS } from './const.js';
import ModelLoader from './loader/ModelLoader.js';
import { MaterialSpec, ModelSpec, TextureSpec } from './loader/types.js';
import DayNight from '../daynight/DayNight.js';
import SceneLight from '../light/SceneLight.js';

type ModelResources = {
name: string;
Expand All @@ -20,13 +20,13 @@ type ModelResources = {
type ModelManagerOptions = {
host: AssetHost;
textureManager?: TextureManager;
dayNight?: DayNight;
sceneLight?: SceneLight;
};

class ModelManager {
#host: AssetHost;
#textureManager: TextureManager;
#dayNight: DayNight;
#sceneLight: SceneLight;

#loader: ModelLoader;
#loaded = new globalThis.Map<string, ModelResources>();
Expand All @@ -38,7 +38,7 @@ class ModelManager {
this.#textureManager = options.textureManager ?? new TextureManager({ host: options.host });
this.#loader = new ModelLoader({ host: options.host });

this.#dayNight = options.dayNight ?? new DayNight();
this.#sceneLight = options.sceneLight ?? new SceneLight();
}

async get(path: string) {
Expand Down Expand Up @@ -139,7 +139,7 @@ class ModelManager {
const textures = await Promise.all(
spec.textures.map((textureSpec) => this.#createTexture(textureSpec)),
);
const uniforms = { ...this.#dayNight.uniforms };
const uniforms = { ...this.#sceneLight.uniforms };

return new ModelMaterial(
vertexShader,
Expand Down

0 comments on commit 528027f

Please sign in to comment.