UNPKG

mylingo3d

Version:

Lingo3D is a React/Vue 3d game development framework that ships with a complete visual editor

129 lines 4.99 kB
import { Cancellable } from "@lincode/promiselikes"; import { Reactive } from "@lincode/reactivity"; import { Color, Group } from "three"; import mainCamera from "../../engine/mainCamera"; import scene from "../../engine/scene"; import { onBeforeRender } from "../../events/onBeforeRender"; import { emitSelectionTarget, onSelectionTarget } from "../../events/onSelectionTarget"; import { SHADOW_BIAS } from "../../globals"; import { getCameraRendered } from "../../states/useCameraRendered"; import { getShadowBias } from "../../states/useShadowBias"; import { getShadowResolution } from "../../states/useShadowResolution"; import ObjectManager from "./ObjectManager"; import makeLightSprite from "./utils/makeLightSprite"; export default class LightBase extends ObjectManager { lightState = new Reactive(undefined); defaultShadowResolution = 256; defaultShadowBias = SHADOW_BIAS; shadowResolutionComputedState = new Reactive(undefined); shadowBiasComputedState = new Reactive(undefined); constructor(Light, Helper) { const group = new Group(); super(group); this.createEffect(() => { const light = new Light(); this.lightState.set(light); group.add(light); if (light.shadow && this.castShadowState.get()) { const shadowResolution = this.shadowResolutionState.get() ?? getShadowResolution() ?? this.defaultShadowResolution; const shadowBias = this.shadowBiasState.get() ?? getShadowBias() ?? this.defaultShadowBias; this.shadowBiasComputedState.set(shadowBias); this.shadowResolutionComputedState.set(shadowResolution); light.castShadow = true; light.shadow.bias = shadowBias; light.shadow.mapSize.width = shadowResolution; light.shadow.mapSize.height = shadowResolution; } return () => { group.remove(light); light.dispose(); }; }, [ this.castShadowState.get, this.shadowResolutionState.get, this.shadowBiasState.get, getShadowResolution, getShadowBias ]); this.createEffect(() => { const light = this.lightState.get(); if (getCameraRendered() !== mainCamera || !this.helperState.get() || !light) return; const handle = new Cancellable(); const sprite = makeLightSprite(); handle.watch(onSelectionTarget(({ target }) => { target === sprite && emitSelectionTarget(this); })); if (Helper) { const helper = new Helper(light); scene.add(helper); helper.add(sprite.outerObject3d); if ("update" in helper) handle.watch(onBeforeRender(() => helper.update())); handle.then(() => { helper.dispose(); scene.remove(helper); }); } else this.outerObject3d.add(sprite.outerObject3d); return () => { sprite.dispose(); handle.cancel(); }; }, [getCameraRendered, this.helperState.get, this.lightState.get]); } helperState = new Reactive(true); get helper() { return this.helperState.get(); } set helper(val) { this.helperState.set(val); } castShadowState = new Reactive(true); get castShadow() { return this.castShadowState.get(); } set castShadow(val) { this.castShadowState.set(val); } shadowResolutionState = new Reactive(undefined); get shadowResolution() { return this.shadowResolutionState.get(); } set shadowResolution(val) { this.shadowResolutionState.set(val); } shadowBiasState = new Reactive(undefined); get shadowBias() { return this.shadowBiasState.get(); } set shadowBias(val) { this.shadowBiasState.set(val); } get color() { const light = this.lightState.get(); if (!light) return "#ffffff"; return "#" + light.color.getHexString(); } set color(val) { this.cancelHandle("color", () => this.lightState.get((light) => light && (light.color = new Color(val)))); } get intensity() { const light = this.lightState.get(); if (!light) return 1; return light.intensity; } set intensity(val) { this.cancelHandle("intensity", () => this.lightState.get((light) => light && (light.intensity = val))); } } //# sourceMappingURL=LightBase.js.map