VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
lightComponent.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 10.02.2026.
5//
6#include "lightComponent.h"
7
8#include <cmath>
9
10#include "framework/entity.h"
11#include "scene/light.h"
12
13namespace visutwin::canvas
14{
19
21 {
22 const auto it = std::find(_instances.begin(), _instances.end(), this);
23 if (it != _instances.end()) {
24 _instances.erase(it);
25 }
26 }
27
29 {
30 if (!_light) {
31 // Lazy creation — the Light needs a GraphicsDevice pointer, but we can pass nullptr
32 // since the Light object here is used only for shadow camera/render-data management,
33 // not for device resource allocation (ShadowMap::create takes the device directly).
34 _light = std::make_unique<Light>(nullptr, false);
35 }
36 syncToLight();
37 return _light.get();
38 }
39
40 void LightComponent::syncToLight() const
41 {
42 if (!_light) {
43 return;
44 }
45 _light->setType(_type);
46 _light->setEnabled(enabled());
47 _light->setVisibleThisFrame(true);
48 _light->setCastShadows(_castShadows);
49 _light->setMask(static_cast<MaskType>(_mask));
50 _light->setShadowDistance(_shadowDistance);
51 _light->setShadowResolution(_shadowResolution);
52 _light->setNumCascades(_numCascades);
53 _light->setCascadeDistribution(_cascadeDistribution);
54 _light->setCascadeBlend(_cascadeBlend);
55 _light->setShadowBias(_shadowBias);
56 _light->setNormalBias(_shadowNormalBias);
57 _light->setShadowIntensity(_shadowStrength);
58 _light->setRange(_range);
59 _light->setOuterConeAngle(_outerConeAngle);
60 _light->setNode(_entity);
61
62 if (_castShadows) {
63 _light->setShadowUpdateMode(ShadowUpdateType::SHADOWUPDATE_REALTIME);
64 }
65 }
66
68 {
69 const auto* src = dynamic_cast<const LightComponent*>(source);
70 if (!src) {
71 return;
72 }
73 //copies all properties.
74 _type = src->_type;
75 _color = src->_color;
76 _intensity = src->_intensity;
77 _range = src->_range;
78 _innerConeAngle = src->_innerConeAngle;
79 _outerConeAngle = src->_outerConeAngle;
80 _falloffMode = src->_falloffMode;
81 _mask = src->_mask;
82 _castShadows = src->_castShadows;
83 _shadowBias = src->_shadowBias;
84 _shadowNormalBias = src->_shadowNormalBias;
85 _shadowStrength = src->_shadowStrength;
86 _shadowDistance = src->_shadowDistance;
87 _shadowResolution = src->_shadowResolution;
88 _numCascades = src->_numCascades;
89 _cascadeDistribution = src->_cascadeDistribution;
90 _cascadeBlend = src->_cascadeBlend;
91 _areaWidth = src->_areaWidth;
92 _areaHeight = src->_areaHeight;
93 _layers = src->_layers;
94 setEnabled(src->enabled());
95 }
96
98 {
99 if (!_entity) {
100 return Vector3(0.0f, -1.0f, 0.0f);
101 }
102
103 const auto& world = _entity->worldTransform();
104 Vector3 up = Vector3(world.getColumn(1));
105 up = up * -1.0f;
106
107 if (up.lengthSquared() < 1e-8f) {
108 return Vector3(0.0f, -1.0f, 0.0f);
109 }
110
111 return up.normalized();
112 }
113
115 {
116 if (!_entity) {
117 return Vector3(0.0f);
118 }
119 return _entity->position();
120 }
121}
Entity * entity() const
Definition component.cpp:16
virtual bool enabled() const
Definition component.h:49
Component(IComponentSystem *system, Entity *entity)
Definition component.cpp:12
virtual void setEnabled(bool value)
Definition component.cpp:21
IComponentSystem * system() const
Definition component.h:47
ECS entity — a GraphNode that hosts components defining its behavior.
Definition entity.h:32
LightComponent(IComponentSystem *system, Entity *entity)
void cloneFrom(const Component *source) override
Directional, point, spot, or area light with shadow mapping and cookie projection.
Definition light.h:54
3D vector for positions, directions, and normals with multi-backend SIMD acceleration.
Definition vector3.h:29
Vector3 normalized() const
float lengthSquared() const
Definition vector3.h:233