UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

135 lines (99 loc) 3.32 kB
import { max3 } from "../../../../../core/math/max3.js"; import { LightType } from "../LightType.js"; export class LightBinding { constructor() { /** * * @type {Transform} * @protected */ this.__c_transform = null; /** * * @type {Light} * @protected */ this.__c_light = null; } get scaled_distance() { const scale = this.__c_transform.scale; const scale_magnitude = max3(scale.x, scale.y, scale.z); const light_radius = this.__c_light.distance.getValue(); return light_radius * scale_magnitude; } set component_transform(v) { this.__c_transform = v; } set component_light(v) { this.__c_light = v; } /** * * @param {Object} settings */ applySettings(settings) { } /** * * @param {LightContext} ctx */ link(ctx) { this.__c_transform.position.onChanged.add(this.__apply_position, this); this.__c_transform.rotation.onChanged.add(this.__apply_rotation, this); this.__c_transform.rotation.onChanged.add(this.__apply_scale, this); const l = this.__c_light; l.intensity.onChanged.add(this.__apply_intensity, this); l.color.onChanged.add(this.__apply_color, this); l.distance.onChanged.add(this.__apply_distance, this); l.angle.onChanged.add(this.__apply_angle, this); l.penumbra.onChanged.add(this.__apply_penumbra, this); l.castShadow.onChanged.add(this.__apply_castShadow, this); const type = l.type.getValue(); if (type === LightType.POINT) { this.__apply_distance(); } else if (type === LightType.SPOT) { this.__apply_angle(); this.__apply_penumbra(); } this.__apply_intensity(); this.__apply_color(); this.__apply_castShadow(); this.__apply_position(); this.__apply_rotation(); this.__apply_scale(); this.applySettings(ctx.system.settings); } /** * * @param {LightContext} ctx */ unlink(ctx) { this.__c_transform.position.onChanged.remove(this.__apply_position, this); this.__c_transform.rotation.onChanged.remove(this.__apply_rotation, this); this.__c_transform.rotation.onChanged.remove(this.__apply_scale, this); this.__c_light.intensity.onChanged.remove(this.__apply_intensity, this); this.__c_light.color.onChanged.remove(this.__apply_color, this); this.__c_light.distance.onChanged.remove(this.__apply_distance, this); this.__c_light.angle.onChanged.remove(this.__apply_angle, this); this.__c_light.penumbra.onChanged.remove(this.__apply_penumbra, this); this.__c_light.castShadow.onChanged.remove(this.__apply_castShadow, this); } __apply_position() { } __apply_rotation() { } __apply_scale(){ } __apply_intensity() { } __apply_color() { } __apply_distance() { } __apply_angle() { } __apply_penumbra() { } __apply_castShadow() { } }