Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
SpotLightComponent.cpp
Go to the documentation of this file.
1
2
4
10
11namespace Divide {
12
13SpotLightComponent::SpotLightComponent(SceneGraphNode* sgn, PlatformContext& context)
14 : BaseComponentType<SpotLightComponent, ComponentType::SPOT_LIGHT>(sgn, context),
15 Light(sgn, 30.0f, LightType::SPOT, *sgn->sceneGraph()->parentScene().lightPool())
16{
17 _shadowProperties._lightDetails.z = 0.0001f;
18
19 EditorComponentField cutoffAngle = {};
20 cutoffAngle._name = "Cutoff Angle";
21 cutoffAngle._data = &_coneCutoffAngle;
22 cutoffAngle._type = EditorComponentFieldType::PUSH_TYPE;
23 cutoffAngle._readOnly = false;
24 cutoffAngle._range = { EPSILON_F32, 179.99f };
25 cutoffAngle._basicType = PushConstantType::FLOAT;
26 editorComponent().registerField(MOV(cutoffAngle));
27
28 EditorComponentField outerCutoffAngle = {};
29 outerCutoffAngle._name = "Outer Cutoff Angle";
30 outerCutoffAngle._data = &_outerConeCutoffAngle;
31 outerCutoffAngle._type = EditorComponentFieldType::PUSH_TYPE;
32 outerCutoffAngle._readOnly = false;
33 outerCutoffAngle._range = { EPSILON_F32, 180.0f };
34 outerCutoffAngle._basicType = PushConstantType::FLOAT;
35 editorComponent().registerField(MOV(outerCutoffAngle));
36
37 EditorComponentField directionField = {};
38 directionField._name = "Direction";
39 directionField._dataGetter = [this](void* dataOut) noexcept { static_cast<vec3<F32>*>(dataOut)->set(directionCache()); };
40 directionField._dataSetter = [this](const void* data) { setDirection(*static_cast<const vec3<F32>*>(data)); };
41 directionField._type = EditorComponentFieldType::PUSH_TYPE;
42 directionField._readOnly = true;
43 directionField._basicType = PushConstantType::VEC3;
44
45 editorComponent().registerField(MOV(directionField));
46
47 EditorComponentField showConeField = {};
48 showConeField._name = "Show direction cone";
49 showConeField._data = &_showDirectionCone;
50 showConeField._type = EditorComponentFieldType::PUSH_TYPE;
51 showConeField._readOnly = false;
52 showConeField._basicType = PushConstantType::BOOL;
53
54 editorComponent().registerField(MOV(showConeField));
55
56 registerFields(editorComponent());
57
58 editorComponent().onChangedCbk([this](std::string_view) noexcept {
59 if (coneCutoffAngle() > outerConeCutoffAngle()) {
60 coneCutoffAngle(outerConeCutoffAngle());
61 }
62 });
63
64 BoundingBox bb = {};
65 bb.setMin(-1.0f);
66 bb.setMax(1.0f);
67 Attorney::SceneNodeLightComponent::setBounds(sgn->getNode(), bb);
68}
69
70F32 SpotLightComponent::outerConeRadius() const noexcept {
71 return range() * std::tan(Angle::DegreesToRadians(outerConeCutoffAngle()));
72}
73
74F32 SpotLightComponent::innerConeRadius() const noexcept {
75 return range() * std::tan(Angle::DegreesToRadians(coneCutoffAngle()));
76}
77
78F32 SpotLightComponent::coneSlantHeight() const noexcept {
79 const F32 coneRadius = outerConeRadius();
80 const F32 coneHeight = range();
81
82 return Sqrt(SQUARED(coneRadius) + SQUARED(coneHeight));
83}
84
85void SpotLightComponent::OnData(const ECS::CustomEvent& data) {
86 SGNComponent::OnData(data);
87
89 updateCache(data);
91 const SceneGraphNode::Flags flag = static_cast<SceneGraphNode::Flags>(data._flag);
92 if (flag == SceneGraphNode::Flags::SELECTED) {
93 _drawImpostor = data._dataPair._first == 1u;
94 }
95 }
96}
97
98void SpotLightComponent::setDirection(const vec3<F32>& direction) const {
99 TransformComponent* tComp = _parentSGN->get<TransformComponent>();
100 if (tComp != nullptr) {
101 tComp->setDirection(direction);
102 }
103}
104
105} //namespace Divide
#define MOV(...)
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
constexpr T SQUARED(T input) noexcept
Definition: MathHelper.inl:156
T Sqrt(T input) noexcept
Definition: MathHelper.inl:252
constexpr F32 EPSILON_F32
SGNComponent::Registrar< T, C > BaseComponentType
Definition: SGNComponent.h:207
LightType
The different types of lights supported.
void setDirection(const vec3< F32 > &direction)
F32 outerConeRadius() const noexcept
DataPair _dataPair
Definition: SGNComponent.h:77
Divide::U32 _flag
Definition: SGNComponent.h:67