Skip to content

Commit de1de3d

Browse files
authored
Merge pull request #5 from KBLLR/codex/implement-stage-and-lighting-manager
Implement stage and lighting managers
2 parents 182a77c + a0ff31e commit de1de3d

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { LightHandler } from './handlers/LightHandler';
2+
3+
// LightingManager manages creation and lifecycle of lights within a scene.
4+
export class LightingManager {
5+
constructor(scene = null) {
6+
this.scene = scene;
7+
this.handler = new LightHandler();
8+
this.lights = new Map();
9+
}
10+
11+
setScene(scene) {
12+
this.scene = scene;
13+
}
14+
15+
/**
16+
* Add a light of a registered type to the current scene.
17+
* @param {string} name - Unique identifier for the light instance.
18+
* @param {string} type - Registered light type handled by LightHandler.
19+
* @param {object} params - Optional debug parameters to override defaults.
20+
*/
21+
addLight(name, type, params = {}) {
22+
const template = this.handler.get(type);
23+
if (!template) throw new Error(`Unknown light type: ${type}`);
24+
25+
const light = template.clone ? template.clone() : template;
26+
if (light.debugObject) {
27+
Object.assign(light.debugObject, params);
28+
if (light.updateFromDebug) {
29+
light.updateFromDebug();
30+
}
31+
}
32+
33+
this.lights.set(name, light);
34+
if (this.scene) {
35+
this.scene.add(light);
36+
}
37+
return light;
38+
}
39+
40+
removeLight(name) {
41+
const light = this.lights.get(name);
42+
if (!light) return;
43+
if (this.scene) {
44+
this.scene.remove(light);
45+
}
46+
if (light.dispose) {
47+
light.dispose();
48+
}
49+
this.lights.delete(name);
50+
}
51+
52+
update() {
53+
this.lights.forEach((light) => {
54+
light.updateFromDebug?.();
55+
});
56+
}
57+
58+
dispose() {
59+
this.lights.forEach((light) => {
60+
if (light.dispose) light.dispose();
61+
});
62+
this.lights.clear();
63+
this.handler.dispose();
64+
}
65+
}

src/core/managers/StageManager.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { SceneManager } from './SceneManager';
2+
import { CameraManager } from './CameraManager';
3+
import { ComponentManager } from './ComponentManager';
4+
import { EnvironmentManager } from './EnvironmentManager';
5+
import { LightingManager } from './LightingManager';
6+
7+
// StageManager ties together the lower level managers to provide
8+
// a simple interface for creating and controlling a complete stage.
9+
export class StageManager {
10+
constructor(renderer, container) {
11+
this.renderer = renderer;
12+
this.container = container;
13+
14+
this.sceneManager = new SceneManager(renderer);
15+
this.cameraManager = new CameraManager(renderer, container);
16+
this.componentManager = new ComponentManager();
17+
18+
this.environmentManager = null;
19+
this.lightingManager = null;
20+
}
21+
22+
/**
23+
* Create a new stage (scene) and set up environment and lighting managers.
24+
*/
25+
createStage(name, options = {}) {
26+
const scene = this.sceneManager.createScene(name, options);
27+
28+
this.environmentManager = new EnvironmentManager(this.renderer, scene);
29+
this.lightingManager = new LightingManager(scene);
30+
31+
this.sceneManager.setManagers(
32+
this.environmentManager,
33+
this.cameraManager,
34+
this.componentManager,
35+
);
36+
37+
return scene;
38+
}
39+
40+
/**
41+
* Set the active stage for rendering.
42+
*/
43+
setActiveStage(name) {
44+
this.sceneManager.setActiveScene(name);
45+
if (this.lightingManager) {
46+
this.lightingManager.setScene(this.sceneManager.activeScene);
47+
}
48+
}
49+
50+
/** Add an object to the active stage. */
51+
addToStage(object) {
52+
if (this.sceneManager.activeScene) {
53+
this.sceneManager.activeScene.add(object);
54+
}
55+
}
56+
57+
/** Update all managers each frame. */
58+
update(deltaTime) {
59+
this.sceneManager.update(deltaTime);
60+
this.lightingManager?.update();
61+
}
62+
63+
/** Handle resize events. */
64+
resize(width, height) {
65+
this.cameraManager.updateAspect(width, height);
66+
this.renderer.setSize(width, height);
67+
}
68+
69+
/** Dispose of all resources. */
70+
dispose() {
71+
this.lightingManager?.dispose();
72+
this.environmentManager?.dispose();
73+
this.componentManager.dispose();
74+
this.sceneManager.dispose();
75+
}
76+
}

0 commit comments

Comments
 (0)