Line data Source code
1 : import * as THREE from 'three'
2 :
3 : import type {
4 : EventManagerInterface,
5 : LightingManagerInterface
6 : } from './interfaces'
7 :
8 24 : export class LightingManager implements LightingManagerInterface {
9 : lights: THREE.Light[] = []
10 : currentIntensity: number = 3
11 : private scene: THREE.Scene
12 : private eventManager: EventManagerInterface
13 : private lightMultipliers = new Map<THREE.Light, number>()
14 :
15 : constructor(scene: THREE.Scene, eventManager: EventManagerInterface) {
16 27 : this.scene = scene
17 27 : this.eventManager = eventManager
18 : }
19 :
20 : init(): void {
21 27 : this.setupLights()
22 : }
23 :
24 : dispose(): void {
25 1 : this.lights.forEach((light) => {
26 6 : this.scene.remove(light)
27 : })
28 1 : this.lights = []
29 1 : this.lightMultipliers.clear()
30 : }
31 :
32 : setupLights(): void {
33 27 : const addLight = (light: THREE.Light, multiplier: number) => {
34 162 : this.scene.add(light)
35 162 : this.lights.push(light)
36 162 : this.lightMultipliers.set(light, multiplier)
37 : }
38 :
39 27 : addLight(new THREE.AmbientLight(0xffffff, 0.5), 0.5)
40 :
41 27 : const mainLight = new THREE.DirectionalLight(0xffffff, 0.8)
42 27 : mainLight.position.set(0, 10, 10)
43 27 : addLight(mainLight, 0.8)
44 :
45 27 : const backLight = new THREE.DirectionalLight(0xffffff, 0.5)
46 27 : backLight.position.set(0, 10, -10)
47 27 : addLight(backLight, 0.5)
48 :
49 27 : const leftFillLight = new THREE.DirectionalLight(0xffffff, 0.3)
50 27 : leftFillLight.position.set(-10, 0, 0)
51 27 : addLight(leftFillLight, 0.3)
52 :
53 27 : const rightFillLight = new THREE.DirectionalLight(0xffffff, 0.3)
54 27 : rightFillLight.position.set(10, 0, 0)
55 27 : addLight(rightFillLight, 0.3)
56 :
57 27 : const bottomLight = new THREE.DirectionalLight(0xffffff, 0.2)
58 27 : bottomLight.position.set(0, -10, 0)
59 27 : addLight(bottomLight, 0.2)
60 : }
61 :
62 : setLightIntensity(intensity: number): void {
63 79 : this.currentIntensity = intensity
64 79 : this.lights.forEach((light) => {
65 474 : light.intensity = intensity * (this.lightMultipliers.get(light) ?? 1)
66 : })
67 :
68 79 : this.eventManager.emitEvent('lightIntensityChange', intensity)
69 : }
70 :
71 : setHDRIMode(hdriActive: boolean): void {
72 25 : this.lights.forEach((light) => {
73 150 : light.visible = !hdriActive
74 : })
75 : }
76 :
77 : reset(): void {}
78 : }
|