VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
lightComponent.h
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.09.2025.
5//
6
7#pragma once
8
9#include <algorithm>
10#include <memory>
11#include <vector>
12
13#include "core/math/color.h"
14#include "core/math/vector3.h"
16#include "scene/constants.h"
17
18namespace visutwin::canvas
19{
20 class Light;
21
22 /*
23 * The LightComponent enables an Entity to light the scene.
24 */
26 {
27 public:
29 ~LightComponent() override;
30
31 void initializeComponentData() override {}
32
33 static const std::vector<LightComponent*>& instances() { return _instances; }
34
35 // Scene-graph Light object owned by this component. Created lazily on first access.
36 Light* light() const;
37
38 const Color& color() const { return _color; }
39 void setColor(const Color& color) { _color = color; }
40
41 float intensity() const { return _intensity; }
42 void setIntensity(const float intensity) { _intensity = intensity; }
43
44 LightType type() const { return _type; }
45 void setType(const LightType type) { _type = type; }
46
47 float range() const { return _range; }
48 void setRange(const float range) { _range = range; }
49
50 float innerConeAngle() const { return _innerConeAngle; }
51 void setInnerConeAngle(const float angleDegrees) { _innerConeAngle = angleDegrees; }
52
53 float outerConeAngle() const { return _outerConeAngle; }
54 void setOuterConeAngle(const float angleDegrees) { _outerConeAngle = angleDegrees; }
55
56 LightFalloff falloffMode() const { return _falloffMode; }
57 void setFalloffMode(const LightFalloff mode) { _falloffMode = mode; }
58
59 uint32_t mask() const { return _mask; }
60 void setMask(const uint32_t value) { _mask = value; }
61
62 bool castShadows() const { return _castShadows; }
63 void setCastShadows(const bool castShadows) { _castShadows = castShadows; }
64
65 float shadowBias() const { return _shadowBias; }
66 void setShadowBias(const float value) { _shadowBias = value; }
67
68 float shadowNormalBias() const { return _shadowNormalBias; }
69 void setShadowNormalBias(const float value) { _shadowNormalBias = value; }
70
71 float shadowStrength() const { return _shadowStrength; }
72 void setShadowStrength(const float value) { _shadowStrength = value; }
73
74 float shadowDistance() const { return _shadowDistance; }
75 void setShadowDistance(const float value) { _shadowDistance = value; }
76
77 int shadowResolution() const { return _shadowResolution; }
78 void setShadowResolution(const int value) { _shadowResolution = value; }
79
80 int numCascades() const { return _numCascades; }
81 void setNumCascades(const int value) { _numCascades = value; }
82
83 float cascadeDistribution() const { return _cascadeDistribution; }
84 void setCascadeDistribution(const float value) { _cascadeDistribution = value; }
85
86 float cascadeBlend() const { return _cascadeBlend; }
87 void setCascadeBlend(const float value) { _cascadeBlend = value; }
88
89 // --- Area Light ---
90 float areaWidth() const { return _areaWidth; }
91 void setAreaWidth(const float value) { _areaWidth = value; }
92 float areaHeight() const { return _areaHeight; }
93 void setAreaHeight(const float value) { _areaHeight = value; }
94
95 // Directional lights use the node's -Y axis as emission direction.
96 Vector3 direction() const;
97 Vector3 position() const;
98
99 const std::vector<int>& layers() const { return _layers; }
100 void setLayers(const std::vector<int>& layers) { _layers = layers; }
101 bool rendersLayer(const int layerId) const
102 {
103 if (_layers.empty()) {
104 return true;
105 }
106 return std::find(_layers.begin(), _layers.end(), layerId) != _layers.end();
107 }
108
113 void cloneFrom(const Component* source) override;
114
115 private:
116 void syncToLight() const;
117
118 inline static std::vector<LightComponent*> _instances;
119
120 mutable std::unique_ptr<Light> _light;
121
123 Color _color = Color(1.0f, 1.0f, 1.0f, 1.0f);
124 float _intensity = 1.0f;
125 float _range = 10.0f;
126 float _innerConeAngle = 30.0f;
127 float _outerConeAngle = 45.0f;
128 // programmatic lights default to linear falloff.
129 // GLB-loaded lights use inverse-squared.
131 uint32_t _mask = MASK_AFFECT_DYNAMIC;
132 bool _castShadows = false;
133 float _shadowBias = 0.001f;
134 float _shadowNormalBias = 0.0f;
135 float _shadowStrength = 1.0f;
136 float _shadowDistance = 40.0f;
137 int _shadowResolution = 2048;
138 int _numCascades = 4;
139 float _cascadeDistribution = 0.5f;
140 float _cascadeBlend = 0.0f;
141 float _areaWidth = 1.0f;
142 float _areaHeight = 1.0f;
143 std::vector<int> _layers = {LAYERID_WORLD};
144 };
145}
Base class for ECS components that attach functionality to entities.
Definition component.h:40
Entity * entity() const
Definition component.cpp:16
Component(IComponentSystem *system, Entity *entity)
Definition component.cpp:12
IComponentSystem * system() const
Definition component.h:47
ECS entity — a GraphNode that hosts components defining its behavior.
Definition entity.h:32
void setAreaHeight(const float value)
void setNumCascades(const int value)
LightComponent(IComponentSystem *system, Entity *entity)
void setShadowDistance(const float value)
void setOuterConeAngle(const float angleDegrees)
const std::vector< int > & layers() const
void setIntensity(const float intensity)
void setLayers(const std::vector< int > &layers)
void cloneFrom(const Component *source) override
static const std::vector< LightComponent * > & instances()
void setShadowResolution(const int value)
void setAreaWidth(const float value)
bool rendersLayer(const int layerId) const
void setType(const LightType type)
void setFalloffMode(const LightFalloff mode)
void setCascadeBlend(const float value)
void setShadowStrength(const float value)
void setCascadeDistribution(const float value)
void setInnerConeAngle(const float angleDegrees)
void setShadowNormalBias(const float value)
LightFalloff falloffMode() const
void setColor(const Color &color)
void setMask(const uint32_t value)
void setShadowBias(const float value)
void setCastShadows(const bool castShadows)
void setRange(const float range)
Directional, point, spot, or area light with shadow mapping and cookie projection.
Definition light.h:54
constexpr int LAYERID_WORLD
Definition constants.h:15
constexpr uint32_t MASK_AFFECT_DYNAMIC
Definition constants.h:31
RGBA color with floating-point components in [0, 1].
Definition color.h:18
3D vector for positions, directions, and normals with multi-backend SIMD acceleration.
Definition vector3.h:29