载入中...
搜索中...
未找到
Light.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5#include "common/ECS.h"
6#include "zeroerr/assert.h"
7
8#include <algorithm>
9#include <array>
10#include <cmath>
11#include <cstdint>
12#include <string>
13#include <string_view>
14#include <glm/glm.hpp>
15
16namespace eve::graphics {
17
18class Canvas;
19
28inline void lightSpotCosines(float angleDeg, float softness, float &cosOuter, float &cosInner) {
29 constexpr float kPi = 3.14159265358979323846f;
30 const float outerDeg = std::clamp(angleDeg, 0.1f, 89.f);
31 const float soft = std::clamp(softness, 0.f, 1.f);
32 const float outerRad = outerDeg * (kPi / 180.f);
33 const float innerRad = outerRad * (1.f - soft);
34 cosOuter = std::cos(outerRad);
35 cosInner = std::cos(innerRad);
36 if (cosInner < cosOuter + 1e-4f) cosInner = cosOuter + 1e-4f;
37}
38
40inline void light2dSpotCosines(float angleDeg, float softness, float &cosOuter, float &cosInner) {
41 lightSpotCosines(angleDeg, softness, cosOuter, cosInner);
42}
43
45struct Light2DGpu {
46 glm::vec4 posRadius{0.f}; // xy = point/spot pos or dir; w = radius (0 => directional)
47 glm::vec4 color{0.f}; // rgb * intensity
49 glm::vec4 spot{0.f, 0.f, -2.f, -2.f};
50};
51
54 static constexpr int kMaxLights = 8;
55 glm::vec4 ambient{0.15f, 0.15f, 0.18f, 0.f};
56 glm::vec4 meta{0.f}; // x = count, y = viewW, z = viewH
58};
59
64class EVENGINE_API_BACKENDS Light2D : public ecs::Entity {
65public:
67 ENTITY(Light2D, ecs::Entity)
68
69
70 void release() override {}
71
73 struct Data {
74 std::string type = "point";
75 float x = 0.f;
76 float y = 0.f;
77 float dx = 0.f;
78 float dy = -1.f;
79 float r = 1.f, g = 1.f, b = 1.f;
80 float intensity = 1.f;
81 float radius = 200.f;
83 float spotAngleDeg = 30.f;
85 float spotSoftness = 0.35f;
86 bool enabled = true;
88 bool volumetric = false;
89 float volumetricIntensity = 1.f;
94 bool volumetricOnly = false;
95 Canvas *canvas = nullptr;
96 Light2D *entity = nullptr;
97 };
98
99 COMPONENT(Data, data)
100
101 static Light2D *createLight(const std::string &type = "point");
102
114 static Light2D *createEmissiveProxy(float x, float y, float r, float g, float b,
115 float intensity = 1.f, float radius = 200.f);
116
117 void setType(const std::string &type);
118 std::string getType();
119
120 void setPosition(float x, float y);
121 float getX();
122 float getY();
123
124 void setDirection(float dx, float dy);
125 float getDirX();
126 float getDirY();
127
128 void setColor(float r, float g, float b, float intensity = 1.f);
129 void setRadius(float radius);
130 float getRadius();
131
136 void setSpotAngle(float degrees);
138 float getSpotAngle();
143 void setSpotSoftness(float softness);
145 float getSpotSoftness();
146
147 void setEnabled(bool enabled);
148 bool isEnabled();
149
150 void setVolumetric(bool enabled);
151 bool getVolumetric();
152 void setVolumetricIntensity(float intensity);
153 float getVolumetricIntensity();
154 void setVolumetricOnly(bool enabled);
155 bool getVolumetricOnly();
156
157 void setCanvas(Canvas *canvas);
158};
159
162 glm::vec4 posRadius{0.f}; // xyz = point/spot pos or dir; w = radius (0 => directional)
163 glm::vec4 color{0.f}; // rgb * intensity; a = spotBias when spot else 1
168 glm::vec4 spot{0.f, 0.f, 0.f, -1.f};
169};
170
173 static constexpr int kMaxLights = 8;
174 glm::vec4 ambient{0.12f, 0.12f, 0.14f, 0.f};
176 int count = 0;
177 std::array<glm::vec4, 9> diffuseProbeSh{}; // xyz = RGB coefficient
179 static constexpr int kMaxDiffuseVolumeProbes = 8;
180 std::array<glm::vec4, kMaxDiffuseVolumeProbes> diffuseVolumePosition{};
181 std::array<glm::vec4, kMaxDiffuseVolumeProbes> diffuseVolumeExtent{};
182 std::array<glm::vec4, kMaxDiffuseVolumeProbes * 9> diffuseVolumeSh{};
185};
186
191class EVENGINE_API_BACKENDS Light3D : public ecs::Entity {
192public:
194 ENTITY(Light3D, ecs::Entity)
195
196
197 void release() override {}
198
200 struct Data {
201 std::string type = "point";
202 float x = 0.f, y = 0.f, z = 0.f;
203 float dx = 0.4f, dy = 1.f, dz = 0.3f;
204 float r = 1.f, g = 1.f, b = 1.f;
205 float intensity = 1.f;
206 float radius = 8.f;
208 float spotAngleDeg = 30.f;
210 float spotSoftness = 0.35f;
211 bool enabled = true;
212 bool castShadow = false;
218 std::uint8_t shadowMethod = 0;
224 int shadowLocalSlot = -1;
225 float shadowBias = 0.f; // 0 = auto NDC from cascade texel / Z range
226 float shadowStrength = 1.f;
227 bool volumetric = false;
228 float volumetricIntensity = 1.f;
233 bool volumetricOnly = false;
234 Light3D *entity = nullptr;
235 };
236
237 COMPONENT(Data, data)
238
239 static Light3D *createLight(const std::string &type = "point");
240
247 static Light3D *createEmissiveProxy(float x, float y, float z, float r, float g, float b,
248 float intensity = 1.f, float radius = 8.f);
249
250 void setType(const std::string &type);
251 std::string getType();
252
253 void setPosition(float x, float y, float z);
254 float getX();
255 float getY();
256 float getZ();
257
258 void setDirection(float dx, float dy, float dz);
259 float getDirX();
260 float getDirY();
261 float getDirZ();
262
263 void setColor(float r, float g, float b, float intensity = 1.f);
264 void setRadius(float radius);
265 float getRadius();
266
271 void setSpotAngle(float degrees);
273 float getSpotAngle();
278 void setSpotSoftness(float softness);
280 float getSpotSoftness();
281
282 void setEnabled(bool enabled);
283 bool isEnabled();
284
285 void setCastShadow(bool cast);
286 bool getCastShadow();
291 void setShadowMethod(const std::string &method);
293 std::string getShadowMethod();
294 void setShadowBias(float bias);
295 float getShadowBias();
296 void setShadowStrength(float strength);
297 float getShadowStrength();
298
299 void setVolumetric(bool enabled);
300 bool getVolumetric();
301 void setVolumetricIntensity(float intensity);
302 float getVolumetricIntensity();
303 void setVolumetricOnly(bool enabled);
304 bool getVolumetricOnly();
305};
306
307} // namespace eve::graphics
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
float degrees
Definition CardTypes.cpp:35
std::uint16_t method
#define EVENGINE_API_BACKENDS
Definition Export.h:108
tensor::Graph g
Definition GpuGraph.cpp:7
float soft[4]
double r
MeleePoint3 b
Definition MeleeHit.cpp:41
float radius
std::function< eve::Result< void >()> release
Definition Procgen.cpp:86
float shadowStrength
float dz
float spotAngleDeg
float dy
float dx
float bias
Canvas public API.
Definition Canvas.h:17
Declarative 2D light. Collected by RenderSystem (max 8 per canvas/frame). type: "point" | "dir" | "sp...
Definition Light.h:64
Declarative 3D light. Collected by RenderSystem3D (max 8 per frame). type: "point" | "dir" | "spot" (...
Definition Light.h:191
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
void light2dSpotCosines(float angleDeg, float softness, float &cosOuter, float &cosInner)
Backward-compatible alias for lightSpotCosines.
Definition Light.h:40
void lightSpotCosines(float angleDeg, float softness, float &cosOuter, float &cosInner)
Convert spot angle/softness into GPU cosines (outer < inner). Shared by Light2D and Light3D flashligh...
Definition Light.h:28
bool enabled
GPU light packing for lit2d (std140-friendly).
Definition Light.h:45
glm::vec4 spot
Spot cone: xy = beam dir; z/w = cos(outer/inner half-angle). z <= -1.5 => off.
Definition Light.h:49
Data public API.
Definition Light.h:73
GPU light packing for mesh3d / PBR (std140-friendly).
Definition Light.h:161
glm::vec4 spot
Spot cone: xyz = world beam direction; w = spotScale (1/(cosInner-cosOuter)). w <= 0 means the light ...
Definition Light.h:168
Data public API.
Definition Light.h:200
Lighting2DUBO public API.
Definition Light.h:53
static constexpr int kMaxLights
Definition Light.h:54
Light2DGpu lights[kMaxLights]
Definition Light.h:57
Lighting3DPack public API.
Definition Light.h:172
std::array< glm::vec4, kMaxDiffuseVolumeProbes > diffuseVolumePosition
Definition Light.h:180
std::array< glm::vec4, kMaxDiffuseVolumeProbes > diffuseVolumeExtent
Definition Light.h:181
std::array< glm::vec4, kMaxDiffuseVolumeProbes *9 > diffuseVolumeSh
Definition Light.h:182
static constexpr int kMaxLights
Definition Light.h:173
static constexpr int kMaxDiffuseVolumeProbes
Definition Light.h:179
Light3DGpu lights[kMaxLights]
Definition Light.h:175
std::array< glm::vec4, 9 > diffuseProbeSh
Definition Light.h:177