lingo3d
Version:
Lingo3D is a React/Vue 3d game development framework that ships with a complete visual editor
96 lines • 3.29 kB
JavaScript
import { skyLightDefaults, skyLightSchema } from "../../interface/ISkyLight";
import { Reactive } from "@lincode/reactivity";
import { CSM } from "three/examples/jsm/csm/CSM";
import scene from "../../engine/scene";
import { getCameraRendered } from "../../states/useCameraRendered";
import DirectionalLight from "./DirectionalLight";
import AmbientLight from "./AmbientLight";
import { cameraRenderedPtr } from "../../pointers/cameraRenderedPtr";
import MeshAppendable from "../core/MeshAppendable";
import { skyLightSystem } from "../../systems/skyLightSystem";
class SkyLight extends MeshAppendable {
static componentName = "skyLight";
static defaults = skyLightDefaults;
static schema = skyLightSchema;
$backLight;
$csm;
_ambientLight;
constructor() {
super();
skyLightSystem.add(this);
const backLight = (this.$backLight = new DirectionalLight());
backLight.$ghost();
const ambientLight = (this._ambientLight = new AmbientLight());
ambientLight.$ghost();
this.createEffect(() => {
const intensity = this.intensityState.get();
const color = this.colorState.get();
backLight.intensity = intensity * 0.25;
ambientLight.intensity = intensity * 0.25;
backLight.color = color;
ambientLight.color = color;
if (!this.shadowsState.get()) {
const light = new DirectionalLight();
light.$ghost();
light.intensity = intensity;
light.color = color;
this.append(light);
return () => {
light.dispose();
};
}
const csm = (this.$csm = new CSM({
maxFar: 50,
shadowMapSize: 2048,
shadowBias: -0.0002,
cascades: 1,
parent: scene,
camera: cameraRenderedPtr[0],
lightIntensity: intensity
}));
for (const light of csm.lights)
light.color.set(color);
const handle = getCameraRendered((val) => (csm.camera = val));
return () => {
handle.cancel();
csm.dispose();
for (const light of csm.lights) {
light.dispose();
scene.remove(light);
}
};
}, [
this.intensityState.get,
this.colorState.get,
this.shadowsState.get
]);
}
disposeNode() {
super.disposeNode();
this.$backLight.dispose();
this._ambientLight.dispose();
}
intensityState = new Reactive(1);
get intensity() {
return this.intensityState.get();
}
set intensity(val) {
this.intensityState.set(Math.max(val, 0.1));
}
colorState = new Reactive("#ffffff");
get color() {
return this.colorState.get();
}
set color(val) {
this.colorState.set(val);
}
shadowsState = new Reactive(true);
get shadows() {
return this.shadowsState.get();
}
set shadows(val) {
this.shadowsState.set(val);
}
}
export default SkyLight;
//# sourceMappingURL=SkyLight.js.map