|
| 1 | +// eslint-disable-next-line max-classes-per-file |
| 2 | +import * as THREE from "three"; |
| 3 | +import { SimpleScene } from "./simple-scene"; |
| 4 | +import { |
| 5 | + ColorSettingsControl, |
| 6 | + ConfigManager, |
| 7 | + NumberSettingControl, |
| 8 | + VectorSettingControl, |
| 9 | +} from "../../Types"; |
| 10 | + |
| 11 | +type SimpleSceneConfigType = { |
| 12 | + backgroundColor: ColorSettingsControl; |
| 13 | + ambientLight: { |
| 14 | + color: ColorSettingsControl; |
| 15 | + intensity: NumberSettingControl; |
| 16 | + }; |
| 17 | + directionalLight: { |
| 18 | + color: ColorSettingsControl; |
| 19 | + intensity: NumberSettingControl; |
| 20 | + position: VectorSettingControl; |
| 21 | + }; |
| 22 | +}; |
| 23 | + |
| 24 | +class DirectionalLightConfig { |
| 25 | + private _list: SimpleSceneConfigType; |
| 26 | + private _scene: SimpleScene; |
| 27 | + |
| 28 | + constructor(list: SimpleSceneConfigType, scene: SimpleScene) { |
| 29 | + this._list = list; |
| 30 | + this._scene = scene; |
| 31 | + } |
| 32 | + |
| 33 | + get color() { |
| 34 | + return this._list.directionalLight.color.value; |
| 35 | + } |
| 36 | + |
| 37 | + set color(value: number) { |
| 38 | + this._list.directionalLight.color.value = value; |
| 39 | + for (const [, light] of this._scene.directionalLights) { |
| 40 | + light.color = new THREE.Color(value); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + get intensity() { |
| 45 | + return this._list.directionalLight.intensity.value; |
| 46 | + } |
| 47 | + |
| 48 | + set intensity(value: number) { |
| 49 | + this._list.directionalLight.intensity.value = value; |
| 50 | + for (const [, light] of this._scene.directionalLights) { |
| 51 | + light.intensity = value; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + get position() { |
| 56 | + return this._list.directionalLight.position.value; |
| 57 | + } |
| 58 | + |
| 59 | + set position(value: number[]) { |
| 60 | + this._list.directionalLight.position.value = value; |
| 61 | + const [x, y, z] = value; |
| 62 | + for (const [, light] of this._scene.directionalLights) { |
| 63 | + light.position.set(x, y, z); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +class AmbientLightConfig { |
| 69 | + private _list: SimpleSceneConfigType; |
| 70 | + private _scene: SimpleScene; |
| 71 | + |
| 72 | + constructor(list: SimpleSceneConfigType, scene: SimpleScene) { |
| 73 | + this._list = list; |
| 74 | + this._scene = scene; |
| 75 | + } |
| 76 | + |
| 77 | + get color() { |
| 78 | + return this._list.ambientLight.color.value; |
| 79 | + } |
| 80 | + |
| 81 | + set color(value: number) { |
| 82 | + this._list.ambientLight.color.value = value; |
| 83 | + for (const [, light] of this._scene.ambientLights) { |
| 84 | + light.color = new THREE.Color(value); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + get intensity() { |
| 89 | + return this._list.ambientLight.intensity.value; |
| 90 | + } |
| 91 | + |
| 92 | + set intensity(value: number) { |
| 93 | + this._list.ambientLight.intensity.value = value; |
| 94 | + for (const [, light] of this._scene.ambientLights) { |
| 95 | + light.intensity = value; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Configuration interface for the {@link SimpleScene}. |
| 102 | + */ |
| 103 | +export interface SimpleSceneConfig { |
| 104 | + directionalLight: { |
| 105 | + color: THREE.Color; |
| 106 | + intensity: number; |
| 107 | + position: THREE.Vector3; |
| 108 | + }; |
| 109 | + ambientLight: { |
| 110 | + color: THREE.Color; |
| 111 | + intensity: number; |
| 112 | + }; |
| 113 | +} |
| 114 | + |
| 115 | +export class SimpleSceneConfigManager extends ConfigManager< |
| 116 | + SimpleScene, |
| 117 | + SimpleSceneConfigType |
| 118 | +> { |
| 119 | + protected _list = { |
| 120 | + backgroundColor: { |
| 121 | + value: 0, |
| 122 | + opacity: 1, |
| 123 | + type: "Color" as const, |
| 124 | + }, |
| 125 | + ambientLight: { |
| 126 | + color: { |
| 127 | + type: "Color" as const, |
| 128 | + opacity: 1, |
| 129 | + value: 1, |
| 130 | + }, |
| 131 | + intensity: { |
| 132 | + type: "Number" as const, |
| 133 | + interpolable: false, |
| 134 | + value: 2, |
| 135 | + }, |
| 136 | + }, |
| 137 | + directionalLight: { |
| 138 | + color: { |
| 139 | + type: "Color" as const, |
| 140 | + opacity: 1, |
| 141 | + value: 1, |
| 142 | + }, |
| 143 | + intensity: { |
| 144 | + type: "Number" as const, |
| 145 | + interpolable: false, |
| 146 | + value: 2, |
| 147 | + }, |
| 148 | + position: { |
| 149 | + type: "Vector" as const, |
| 150 | + value: [], |
| 151 | + }, |
| 152 | + }, |
| 153 | + }; |
| 154 | + |
| 155 | + ambientLight = new AmbientLightConfig(this._list, this._component); |
| 156 | + |
| 157 | + directionalLight = new DirectionalLightConfig(this._list, this._component); |
| 158 | + |
| 159 | + get backgroundColor() { |
| 160 | + return this._list.backgroundColor.value; |
| 161 | + } |
| 162 | + |
| 163 | + set backgroundColor(value: number) { |
| 164 | + this._list.backgroundColor.value = value; |
| 165 | + this._component.three.background = new THREE.Color(value); |
| 166 | + } |
| 167 | +} |
0 commit comments