载入中...
搜索中...
未找到
RenderSystem3D.h
浏览该文件的文档.
1#pragma once
2
3#include "common/ECS.h"
4#include "graphics/Material.h"
5#include "graphics/Mesh.h"
6#include "graphics/Shader.h"
7#include "graphics/Texture.h"
8#include "zeroerr/assert.h"
9
10#include <functional>
11#include <string>
12
13namespace eve::graphics {
14
15class Canvas;
16class Graphics;
17
18class Camera3D : public ecs::Entity {
19public:
20 ENTITY(Camera3D, ecs::Entity)
21
22 void release() override {}
23
24 struct Data {
25 float eyeX = 0.f, eyeY = 0.f, eyeZ = 3.f;
26 float targetX = 0.f, targetY = 0.f, targetZ = 0.f;
27 float upX = 0.f, upY = 1.f, upZ = 0.f;
28 float fovYDeg = 60.f, nearZ = 0.1f, farZ = 100.f;
29 float ambientR = 0.12f, ambientG = 0.12f, ambientB = 0.14f;
30 Texture *envMap = nullptr; // cubemap; nullptr → no IBL
31 float envIntensity = 1.f;
32 bool active = false;
33 Camera3D *entity = nullptr;
34 // Last screenToRay() result (origin = eye, dir normalized).
35 float screenRayOx = 0.f, screenRayOy = 0.f, screenRayOz = 0.f;
36 float screenRayDx = 0.f, screenRayDy = 0.f, screenRayDz = -1.f;
37 };
38
39 COMPONENT(Data, data)
40
42 Camera3D *c = Camera3D::create();
43 ASSERT(c != nullptr);
44 c->data()->entity = c;
45 c->data()->active = true;
46 return c;
47 }
48
49 void setEye(float x, float y, float z);
50 void setTarget(float x, float y, float z);
51 void setUp(float x, float y, float z);
52 void setFov(float fovYDeg);
53 void setActive(bool active);
54 void setAmbient(float r, float g, float b);
56 void setEnvMap(Texture *cube);
57 void setEnvIntensity(float intensity);
58
64 void screenToRay(float screenX, float screenY, float viewW, float viewH);
65 float getScreenRayOriginX();
66 float getScreenRayOriginY();
67 float getScreenRayOriginZ();
68 float getScreenRayDirX();
69 float getScreenRayDirY();
70 float getScreenRayDirZ();
71};
72
73class Renderable3D : public ecs::Entity {
74public:
75 ENTITY(Renderable3D, ecs::Entity)
76
77 void release() override {}
78
79 struct Transform3D {
80 float x = 0, y = 0, z = 0;
81 float yaw = 0, pitch = 0, roll = 0; // radians
82 float sx = 1, sy = 1, sz = 1;
83 };
84
85 struct MeshRenderer {
86 static constexpr int kMaxLodLevels = 4;
87 static constexpr int kMaxParts = Material::kMaxPartsHint;
88
89 Mesh *mesh = nullptr;
90 Texture *texture = nullptr;
91 Texture *normalTexture = nullptr; // nullptr → flat normal in default PBR path
92 Texture *heightTexture = nullptr; // nullptr → no parallax height (R = height)
93 Shader *shader = nullptr; // nullptr → default mesh3d PBR pipeline
95 Material *material = nullptr;
103 Shader *xrayShader = nullptr;
104 bool xrayHighlight = false;
105 float r = 1, g = 1, b = 1, a = 1;
106 float metallic = 0.f;
107 float roughness = 0.45f;
108 float texBombScale = 4.f; // cells per UV unit
109 float texBombStrength = 0.f; // 0 = off (default, preserves tiling)
110 float texBombRot = 1.f; // 0..1 per-cell rotation amount
111 float parallaxScale = 0.f; // 0 = off (default)
112 float parallaxMinLayers = 8.f;
113 float parallaxMaxLayers = 32.f;
114 bool visible = true;
115 bool receiveLight = true;
116 bool castShadow = true;
117 bool receiveShadow = true;
118 bool castOcclusion = true; // volumetric occlusion (screen-space shafts)
120 bool isHair = false;
121 Camera3D *camera = nullptr;
122
127 int partCount = 0;
129
135 int lodCount = 0;
137 float lodDistances[kMaxLodLevels - 1] = {25.f, 60.f, 120.f};
138
140 Mesh *meshForDistance(float distance) const {
141 if (lodCount <= 0) return mesh;
142 int level = 0;
143 while (level + 1 < lodCount && distance >= lodDistances[level]) ++level;
144 Mesh *picked = lodMeshes[level];
145 return picked ? picked : mesh;
146 }
147
148 int lodLevelForDistance(float distance) const {
149 if (lodCount <= 0) return 0;
150 int level = 0;
151 while (level + 1 < lodCount && distance >= lodDistances[level]) ++level;
152 return level;
153 }
154
155 bool usesParts() const { return partCount > 0; }
156
157 bool effectiveHair() const {
158 if (material) return material->isTransparentHair();
159 return isHair;
160 }
161
162 bool effectiveCastShadow() const {
163 if (material) return material->getCastShadow();
164 return castShadow;
165 }
166 };
167
168 COMPONENT(Transform3D, transform)
169 COMPONENT(MeshRenderer, meshRenderer)
170
171 void setPosition(float x, float y, float z);
172 void setRotation(float yaw, float pitch, float roll);
173 void setYaw(float yaw);
174 float getYaw();
175 void setScale(float sx, float sy, float sz);
176 void setMesh(Mesh *mesh);
177 void setTexture(Texture *texture);
178 void setNormalTexture(Texture *texture);
180 void setHeightTexture(Texture *texture);
181 void setShader(Shader *shader);
193 void setXRayHighlight(bool on);
194 bool getXRayHighlight();
199 void setPart(int index, const std::string &name, Mesh *mesh, Material *material);
200 void clearParts();
201 int getPartCount();
202 std::string getPartName(int index);
203 Mesh *getPartMesh(int index);
204 Material *getPartMaterial(int index);
205 void setHair(bool hair);
206 bool getHair();
207 void setTint(float r, float g, float b, float a = 1.f);
208 void setMetallic(float metallic);
209 void setRoughness(float roughness);
214 void setTexCellBomb(float cellScale, float strength, float rotAmount = 1.f);
215 float getTexCellBombScale();
222 void setParallax(float scale, float minLayers = 8.f, float maxLayers = 32.f);
223 float getParallaxScale();
224 float getParallaxMinLayers();
225 float getParallaxMaxLayers();
226 void setVisible(bool visible);
227 void setReceiveLight(bool receive);
228 void setCastShadow(bool cast);
229 void setReceiveShadow(bool receive);
230 void setCastOcclusion(bool cast);
231 bool getCastOcclusion();
232 void setCamera(Camera3D *camera);
233
239 void setMeshLod(int index, Mesh *mesh, float switchDistance = 0.f);
240 void clearMeshLod();
241 int getMeshLodCount();
242 int getMeshLodLevelAtDistance(float distance);
243};
244
246public:
247 static void render(Graphics &gfx);
248
254 static void renderToCanvas(Graphics &gfx, Canvas *target, Camera3D *camera);
255
263 std::function<void(Graphics &gfx, const Camera3D::Data &cam,
264 const glm::mat4 &viewProj, float aspect)>;
265 static void addGBufferExtraDrawer(GBufferExtraDrawer drawer);
266
276 std::function<void(Graphics &gfx, const glm::mat4 &lightVP,
277 const Camera3D::Data &cam)>;
278 static void addShadowExtraDrawer(ShadowExtraDrawer drawer);
279
281 static void setDirectionalLight(float dx, float dy, float dz, float r, float g, float b);
282};
283
284} // namespace eve::graphics
bool active
Definition CardTypes.cpp:34
int y
Definition Grass.cpp:135
int z
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
uint32_t a
uint32_t b
uint32_t c
float roughness
float metallic
Mesh * mesh
glm::mat4 viewProj
Shader * shader
Material * material
const char * name
Definition RockMesh.cpp:21
float scale
Definition TreeMesh.cpp:122
void release() override
void setUp(float x, float y, float z)
void setFov(float fovYDeg)
void screenToRay(float screenX, float screenY, float viewW, float viewH)
Build a world-space picking ray from a screen pixel. Stores origin (camera eye) and normalized direct...
void setEnvIntensity(float intensity)
static Camera3D * createCamera()
void setTarget(float x, float y, float z)
void setAmbient(float r, float g, float b)
void setActive(bool active)
void setEye(float x, float y, float z)
void setEnvMap(Texture *cube)
Specular IBL cubemap (Graphics::newCubemap). nullptr disables IBL.
Packages shading method + surface parameters into one attachable asset.
Definition Material.h:26
static constexpr int kMaxPartsHint
Definition Material.h:28
bool isTransparentHair() const
True when this material should go through the hair transparent pass.
Definition Material.cpp:68
bool getCastShadow() const
Definition Material.h:79
GPU mesh handle (+ optional CPU morph targets).
Definition Mesh.h:18
static void addShadowExtraDrawer(ShadowExtraDrawer drawer)
static void setDirectionalLight(float dx, float dy, float dz, float r, float g, float b)
Legacy single directional light used when no enabled Light3D exists.
static void addGBufferExtraDrawer(GBufferExtraDrawer drawer)
static void renderToCanvas(Graphics &gfx, Canvas *target, Camera3D *camera)
static void render(Graphics &gfx)
std::function< void(Graphics &gfx, const glm::mat4 &lightVP, const Camera3D::Data &cam)> ShadowExtraDrawer
Register a callback that casts shadows for geometry outside the Renderable3D ECS (e....
std::function< void(Graphics &gfx, const Camera3D::Data &cam, const glm::mat4 &viewProj, float aspect)> GBufferExtraDrawer
Register a callback that fills the G-buffer (depth/normal/albedo) for geometry outside the Renderable...
void setNormalTexture(Texture *texture)
void setCamera(Camera3D *camera)
int getMeshLodLevelAtDistance(float distance)
std::string getPartName(int index)
Material * getPartMaterial(int index)
void setRotation(float yaw, float pitch, float roll)
void setHeightTexture(Texture *texture)
Height map for parallax (R channel; white = raised). nullptr disables sampling.
void setPart(int index, const std::string &name, Mesh *mesh, Material *material)
Bind a named mesh+material part (e.g. Assimp submesh / body region). index 0..kMaxParts-1....
void setTexCellBomb(float cellScale, float strength, float rotAmount=1.f)
Texture cell bombing — random per-cell UV offset/rotation blended across a 2×2 neighborhood to hide t...
void setTint(float r, float g, float b, float a=1.f)
void setMetallic(float metallic)
void setTexture(Texture *texture)
void setReceiveLight(bool receive)
void setShader(Shader *shader)
void setMaterial(Material *material)
Attach a Material that packages shading method + surface params.
void setParallax(float scale, float minLayers=8.f, float maxLayers=32.f)
Parallax occlusion mapping. scale 0 disables (default). Typical scale 0.02..0.08. Requires a height t...
void setPosition(float x, float y, float z)
void setXRayShader(Shader *shader)
Attach an X-ray mesh shader (see Shader::setXray) for occluded silhouettes.
void setXRayHighlight(bool on)
When true, this entity is an X-ray target: its G-buffer pixels are replaced by occluders and it is re...
void setMeshLod(int index, Mesh *mesh, float switchDistance=0.f)
Configure geometric LOD. index 0 = highest detail. For index > 0, switchDistance is the camera distan...
void setRoughness(float roughness)
void setScale(float sx, float sy, float sz)
void setReceiveShadow(bool receive)
Custom GPU program.
Definition Shader.h:30
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:17
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition AnimTrail.h:6
One mesh + material slot on a multi-part model (Assimp mesh / body region). When Material* is null,...
Definition Material.h:134
int partCount
Multi-part model slots (one mesh + material per Assimp mesh / body region). When partCount > 0,...
int lodLevelForDistance(float distance) const
Shader * xrayShader
Optional X-ray shader used to paint this entity's occluded silhouette (see Shader::setXray)....
int lodCount
Optional geometric LOD. When lodCount > 0, lodMeshes[0..lodCount) are used instead of mesh based on c...
bool isHair
Alpha-blended hair/fur card pass (drawn after opaque meshes, back-to-front).
Mesh * meshForDistance(float distance) const
Pick LOD mesh for a camera distance; falls back to mesh when LOD disabled.
Material * material
Optional packed material; when set, overrides texture/shader/PBR fields below.