载入中...
搜索中...
未找到
RenderSystem3D.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5#include "common/ECS.h"
6#include "common/Result.h"
8#include "graphics/Material.h"
10#include "zeroerr/assert.h"
11
12#include <array>
13#include <cstdint>
14#include <functional>
15#include <glm/mat4x4.hpp>
16#include <optional>
17#include <string>
18#include <vector>
19
20namespace eve::graphics {
21
22class Canvas;
23class Graphics;
24class Mesh;
25class Shader;
26class Texture;
27
29class EVENGINE_API_BACKENDS Camera3D : public ecs::Entity {
30public:
32 ENTITY(Camera3D, ecs::Entity)
33
34
35 void release() override {}
36
38 struct Data {
39 static constexpr int kMaxReflectionProbes = 8;
42 Texture* cubemap = nullptr;
43 glm::vec3 center{0.f};
44 glm::vec3 extent{0.f};
45 float intensity = 0.f;
46 float blendDistance = 0.f;
47 int priority = 0;
48 bool enabled = false;
49 };
50 float eyeX = 0.f, eyeY = 0.f, eyeZ = 3.f;
51 float targetX = 0.f, targetY = 0.f, targetZ = 0.f;
52 float upX = 0.f, upY = 1.f, upZ = 0.f;
53 float fovYDeg = 60.f, nearZ = 0.1f, farZ = 100.f;
54 float aperture = 19.f, focalLength = 50.f;
55 float lodBias = 1.f;
57 std::array<float, 32> layerCullDistances{};
59 std::array<float, 32> shadowLayerCullDistances{};
60 bool orthographic = false;
61 float orthoHeight = 20.f;
62 float ambientR = 0.12f, ambientG = 0.12f, ambientB = 0.14f;
63 Texture* envMap = nullptr; // cubemap; nullptr → no IBL
64 float envIntensity = 1.f;
65 glm::vec3 envProbeCenter{0.f};
66 glm::vec3 envProbeExtent{0.f}; // any zero axis disables box projection
67 std::array<ReflectionProbe, kMaxReflectionProbes> reflectionProbes{};
68 float exposureEV = 0.f; // exposure compensation in stops; 0 preserves legacy brightness
69 bool autoExposure = false;
70 float autoExposureMinEV = -8.f;
71 float autoExposureMaxEV = 8.f;
72 float bloomIntensity = 0.f;
73 float bloomThreshold = 1.f;
74 float dofFocusDistance = 0.f;
75 float dofMaxBlurPx = 0.f; // <= 0 disables DOF
76 float dofFocusRange = 8.f;
77 bool active = false;
78 Camera3D *entity = nullptr;
79 // Last screenToRay() result (origin = eye, dir normalized).
80 float screenRayOx = 0.f, screenRayOy = 0.f, screenRayOz = 0.f;
81 float screenRayDx = 0.f, screenRayDy = 0.f, screenRayDz = -1.f;
82 };
83
84 COMPONENT(Data, data)
85
86
87 static Camera3D *createCamera() {
88 Camera3D* c = Camera3D::create();
89 ASSERT(c != nullptr);
90 c->data()->entity = c;
91 c->data()->active = true;
92 return c;
93 }
94
95 void setEye(float x, float y, float z);
97 float getEyeX();
98 float getEyeY();
99 float getEyeZ();
100 void setTarget(float x, float y, float z);
102 float getTargetX();
103 float getTargetY();
104 float getTargetZ();
105 void setUp(float x, float y, float z);
106 void setFov(float fovYDeg);
108 float getFov();
110 void setOrthographic(float height);
112 void setPerspective();
114 void setClipPlanes(float nearZ, float farZ);
float getNearClip(); float getFarClip(); void setPhysicalLens(float aperture,float focalLength); float getAperture(); float getFocalLength();
121 void setLayerCullDistance(int layer, float distance);
123 float getLayerCullDistance(int layer);
125 void setShadowLayerCullDistance(int layer, float distance);
127 float getShadowLayerCullDistance(int layer);
128 void setActive(bool active);
129 void setAmbient(float r, float g, float b);
131 void setEnvMap(Texture* cube);
132 void setEnvIntensity(float intensity);
134 void setExposure(float ev);
136 float getExposure();
138 void setAutoExposure(bool enabled, float minEV = -8.f, float maxEV = 8.f);
140 bool isAutoExposure();
142 void setBloom(float intensity, float threshold = 1.f);
144 float getBloomIntensity();
146 float getBloomThreshold();
153 void setDepthOfField(float focusDistance, float maxBlurPx, float focusRange = 8.f);
155 void clearDepthOfField();
157 float getDofFocusDistance();
159 float getDofMaxBlur();
161 float getDofFocusRange();
163 void setEnvProbe(float centerX, float centerY, float centerZ, float extentX, float extentY, float extentZ);
165 void clearEnvProbe();
167 bool hasEnvProbe();
168 float getEnvProbeCenterX();
169 float getEnvProbeCenterY();
170 float getEnvProbeCenterZ();
171 float getEnvProbeExtentX();
172 float getEnvProbeExtentY();
173 float getEnvProbeExtentZ();
181 void setReflectionProbe(int slot, Texture* cubemap, float centerX, float centerY, float centerZ, float extentX,
182 float extentY, float extentZ, float intensity = 1.f, float blendDistance = 1.f,
183 int priority = 0);
185 void clearReflectionProbe(int slot);
187 int getReflectionProbeCount();
188
194 void screenToRay(float screenX, float screenY, float viewW, float viewH);
195 float getScreenRayOriginX();
196 float getScreenRayOriginY();
197 float getScreenRayOriginZ();
198 float getScreenRayDirX();
199 float getScreenRayDirY();
200 float getScreenRayDirZ();
201};
202
204class EVENGINE_API_BACKENDS Renderable3D : public ecs::Entity {
205public:
207 ENTITY(Renderable3D, ecs::Entity)
208
209
210 void release() override {}
211
213 struct Transform3D {
214 float x = 0, y = 0, z = 0;
215 float yaw = 0, pitch = 0, roll = 0; // radians
216 float sx = 1, sy = 1, sz = 1;
217 };
218
220 static constexpr int kMaxLodLevels = 4;
221 static constexpr int kMaxParts = Material::kMaxPartsHint;
222
223 Mesh* mesh = nullptr;
224 Texture* texture = nullptr;
225 Texture* normalTexture = nullptr; // nullptr → flat normal in default PBR path
226 bool packedNormalMask = false; // RG normal XY, B AO, A smoothness
228 Texture *heightTexture = nullptr; // nullptr → no parallax height (R = height)
229 Shader *shader = nullptr; // nullptr → default mesh3d PBR pipeline
231 std::optional<MeshInstanceRange> instances;
233 Material* material = nullptr;
241 Shader* xrayShader = nullptr;
242 bool xrayHighlight = false;
243 float r = 1, g = 1, b = 1, a = 1;
244 float metallic = 0.f;
245 float roughness = 0.45f;
246 float texBombScale = 4.f; // cells per UV unit
247 float texBombStrength = 0.f; // 0 = off (default, preserves tiling)
248 float texBombRot = 1.f; // 0..1 per-cell rotation amount
249 float parallaxScale = 0.f; // 0 = off (default)
250 float parallaxMinLayers = 8.f;
251 float parallaxMaxLayers = 32.f;
252 bool visible = true;
253 int layer = 0;
254 uint32_t reflectionCaptureMask = 1u;
255 bool receiveLight = true;
256 bool castShadow = true;
257 bool receiveShadow = true;
258 bool castOcclusion = true; // volumetric occlusion (screen-space shafts)
259 bool customLightProbe = false;
260 std::array<glm::vec4, 9> customLightProbeSh{};
262 bool isHair = false;
263 Camera3D* camera = nullptr;
264
269 int partCount = 0;
270 ModelPart parts[kMaxParts] = {};
271
277 int lodCount = 0;
278 Mesh* lodMeshes[kMaxLodLevels] = {};
279 float lodDistances[kMaxLodLevels - 1] = {25.f, 60.f, 120.f};
281 float lodCullDistance = 0.f;
284 bool configured = false;
285 int skinQuality = 0;
286 int shadowCastingMode = 1;
287 bool receiveShadows = true;
288 int motionVectorMode = 1;
289 bool skinnedMotionVectors = true;
291 int reflectionProbeUsage = 1;
292 };
293 LodRendererState lodRendererStates[kMaxLodLevels] = {};
294 float lodFadeWidths[kMaxLodLevels] = {};
295 int lodFadeMode = 0;
296 bool lodAnimateCrossFading = false;
297 float lodCrossFadeDuration = 0.5f;
298 int lodAnimatedCurrent = -2;
299 int lodAnimatedPrevious = -2;
300 float lodAnimatedProgress = 1.f;
301
306 if (lodCullDistance > 0.f && distance >= lodCullDistance) return nullptr;
307 if (lodCount <= 0) return mesh;
308 int level = 0;
309 while (level + 1 < lodCount && distance >= lodDistances[level]) ++level;
310 Mesh* picked = lodMeshes[level];
311 return picked ? picked : mesh;
312 }
313
315 int lodLevelForDistance(float distance) const {
316 if (lodCullDistance > 0.f && distance >= lodCullDistance) return -1;
317 if (lodCount <= 0) return 0;
318 int level = 0;
319 while (level + 1 < lodCount && distance >= lodDistances[level]) ++level;
320 return level;
321 }
322
324 void lodBlendForDistance(float distance, int& primary, int& secondary, float& secondaryWeight) const;
325
327 bool usesParts() const { return partCount > 0; }
328
330 bool effectiveHair() const {
331 if (material) return material->isTransparentHair();
332 return isHair;
333 }
334
336 bool effectiveCastShadow() const {
337 if (material) return material->getCastShadow();
338 return castShadow;
339 }
340 };
341
342 COMPONENT(Transform3D, transform)
343 COMPONENT(MeshRenderer, meshRenderer)
344
345 void setPosition(float x, float y, float z);
347 uint32_t getEntityId() const { return id; }
349 uint32_t getEntityGeneration() const { return generation; }
350 void setRotation(float yaw, float pitch, float roll);
351 void setYaw(float yaw);
352 float getYaw();
353 void setScale(float sx, float sy, float sz);
354 void setMesh(Mesh *mesh);
366 [[nodiscard]] Result<void> setInstanceRange(const MeshInstanceRange &range);
368 void clearInstanceRange();
371 Mesh *getMesh();
372 void setTexture(Texture* texture);
373 void setNormalTexture(Texture* texture);
375 void setPackedNormalMask(bool enabled);
377 void setHeightTexture(Texture* texture);
378 void setShader(Shader* shader);
380 void setMaterial(Material* material);
382 Material *getMaterial();
384 void setXRayShader(Shader* shader);
386 Shader *getXRayShader();
392 void setXRayHighlight(bool on);
393 bool getXRayHighlight();
398 void setPart(int index, const std::string& name, Mesh* mesh, Material* material);
400 void setPartSortPriority(int index, int priority);
402 void clearPartSortPriority(int index);
404 int getPartSortPriority(int index);
405 void clearParts();
406 int getPartCount();
407 std::string getPartName(int index);
408 Mesh* getPartMesh(int index);
409 Material* getPartMaterial(int index);
410 void setHair(bool hair);
411 bool getHair();
412 void setTint(float r, float g, float b, float a = 1.f);
414 float getTintR() { return meshRenderer()->r; }
416 float getTintG() { return meshRenderer()->g; }
418 float getTintB() { return meshRenderer()->b; }
420 float getRoughness() { return meshRenderer()->roughness; }
421 void setMetallic(float metallic);
422 void setRoughness(float roughness);
427 void setTexCellBomb(float cellScale, float strength, float rotAmount = 1.f);
428 float getTexCellBombScale();
429 float getTexCellBombStrength();
430 float getTexCellBombRotation();
435 void setParallax(float scale, float minLayers = 8.f, float maxLayers = 32.f);
436 float getParallaxScale();
437 float getParallaxMinLayers();
438 float getParallaxMaxLayers();
439 void setVisible(bool visible);
441 bool getVisible() { return meshRenderer()->visible; }
443 void setLayer(int layer);
445 int getLayer();
447 void setReflectionCaptureMask(int mask);
449 int getReflectionCaptureMask();
450 void setReceiveLight(bool receive);
452 [[nodiscard]] Result<void> setCustomLightProbe(float r, float g, float b);
454 [[nodiscard]] Result<void> setCustomLightProbeCoefficient(int coefficient, float r, float g, float b);
456 void clearCustomLightProbe();
457 void setCastShadow(bool cast);
458 void setReceiveShadow(bool receive);
459 void setCastOcclusion(bool cast);
460 bool getCastOcclusion();
461 void setCamera(Camera3D* camera);
462
468 void setMeshLod(int index, Mesh* mesh, float switchDistance = 0.f);
470 [[nodiscard]] Result<void> setMeshLodRendererState(int index, int skinQuality, int shadowCastingMode,
471 bool receiveShadows, int motionVectorMode,
472 bool skinnedMotionVectors, int lightProbeUsage,
473 int reflectionProbeUsage);
475 int getMeshLodRendererState(int index, int field);
477 [[nodiscard]] Result<void> setMeshLodFadeWidth(int index, float width);
479 [[nodiscard]] Result<void> setMeshLodFadePolicy(int mode, bool animate, float duration);
481 [[nodiscard]] Result<void> advanceMeshLodTransition(float distance, float dt);
483 int getMeshLodSecondaryLevelAtDistance(float distance);
485 float getMeshLodSecondaryWeightAtDistance(float distance);
487 void setMeshLodCullDistance(float distance);
489 float getMeshLodCullDistance();
490 void clearMeshLod();
491 int getMeshLodCount();
492 int getMeshLodLevelAtDistance(float distance);
493};
494
497public:
499 static void render(Graphics& gfx);
500
513 static void renderToCanvas(Graphics& gfx, Canvas* target, Camera3D* camera,
514 uint32_t reflectionCaptureMask = 0xffffffffu, float lodDistanceScale = 1.f,
515 bool includeTransparent = true, bool useClusteredLighting = true,
516 Texture* skyFaceTexture = nullptr, Mesh* skyQuad = nullptr,
517 float skyFaceTextureScale = 1.f);
518
527 using CaptureExtraDrawer = std::function<void(Graphics& gfx, const Camera3D::Data& cam, const glm::mat4& viewProj,
528 float aspect, uint32_t reflectionCaptureMask)>;
529
536 static uint64_t addCaptureExtraDrawer(uint32_t reflectionCaptureMask, CaptureExtraDrawer drawer);
537
542 static void removeCaptureExtraDrawer(uint64_t token);
543
551 std::function<void(Graphics& gfx, const Camera3D::Data& cam, const glm::mat4& viewProj, float aspect)>;
552
554 [[nodiscard]] static uint64_t addForwardExtraDrawer(ForwardExtraDrawer drawer);
555
557 static void removeForwardExtraDrawer(uint64_t token);
558
571 std::function<void(Graphics& gfx, const Camera3D::Data& cam, const glm::mat4& viewProj, float aspect,
572 std::vector<GpuInstance>& instances)>;
573
575 [[nodiscard]] static uint64_t addGpuOpaqueCollector(GpuOpaqueCollector collector);
576
578 static void removeGpuOpaqueCollector(uint64_t token);
579
588 std::function<void(Graphics& gfx, const Camera3D::Data& cam, const glm::mat4& viewProj, float aspect)>;
590 static void addGBufferExtraDrawer(GBufferExtraDrawer drawer);
591
600 std::function<void(Graphics& gfx, const Camera3D::Data& cam, const glm::mat4& viewProj, float aspect)>;
602 static void addDecalExtraDrawer(DecalExtraDrawer drawer);
603
626 std::function<void(Graphics &gfx, const glm::mat4 &lightVP,
627 const Camera3D::Data &cam)>;
629 [[nodiscard]] static uint64_t addShadowExtraDrawer(ShadowExtraDrawer drawer);
630
638 static void removeShadowExtraDrawer(uint64_t token);
639
641 static void setDirectionalLight(float dx, float dy, float dz, float r, float g, float b);
642};
643
644} // namespace eve::graphics
LogicalId target
bool & active
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
float duration
std::vector< eve::artifact::PartView > parts
int mask
std::vector< BuildingInstanceSnapshot > instances
int priority
#define EVENGINE_API_BACKENDS
Definition Export.h:108
tensor::Graph g
Definition GpuGraph.cpp:7
float camera[2]
double r
std::int32_t c
int secondary
std::uint32_t height
std::uint32_t width
Range range
std::array< float, 3 > scale
std::string name
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
float roughness
float metallic
int level
TileLayer * layer
std::string id
Definition PlayHost.cpp:108
std::uint32_t generation
std::function< eve::Result< void >()> release
Definition Procgen.cpp:86
RenderSystem3D::ShadowExtraDrawer drawer
RenderSystem3D::GpuOpaqueCollector collector
Mesh * mesh
int lightProbeUsage
bool receiveShadow
glm::mat4 viewProj
Shader * shader
bool hair
uint64_t token
Material * material
float ambientG
float ambientR
float ambientB
Move-only, checked operation results for the common layer.
float dz
glm::mat4 lightVP
float dy
float dx
float targetX
bool visible
uint32_t index
Move-only operation result carrying either a value or Status.
Definition Result.h:155
EVENGINE_API_BACKENDS public API.
Canvas public API.
Definition Canvas.h:17
Packages shading method + surface parameters into one attachable asset.
Definition Material.h:35
GPU mesh handle (+ optional CPU morph targets).
Definition Mesh.h:25
EVENGINE_API_BACKENDS public API.
std::function< void(Graphics &gfx, const Camera3D::Data &cam, const glm::mat4 &viewProj, float aspect, std::vector< GpuInstance > &instances)> GpuOpaqueCollector
Collect non-ECS geometry for the normal GPU-driven opaque submission.
std::function< void(Graphics &gfx, const Camera3D::Data &cam, const glm::mat4 &viewProj, float aspect, uint32_t reflectionCaptureMask)> CaptureExtraDrawer
Draw custom forward geometry into reflection-probe and preview captures.
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)> ForwardExtraDrawer
Draw custom opaque geometry after the main scene pass is open. @thread Register, unregister and invok...
std::function< void(Graphics &gfx, const Camera3D::Data &cam, const glm::mat4 &viewProj, float aspect)> DecalExtraDrawer
Register a callback that draws screen-space decals (box-projected volumes writing the DecalLayer) aft...
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...
EVENGINE_API_BACKENDS public API.
float getRoughness()
Return the field-backed material roughness.
float getTintG()
Return the field-backed material tint green channel.
float getTintR()
Return the field-backed material tint red channel.
float getTintB()
Return the field-backed material tint blue channel.
uint32_t getEntityId() const
Return the table-local entity id used with getEntityGeneration().
bool getVisible()
Return whether this renderable participates in scene rendering.
uint32_t getEntityGeneration() const
Return the generation required to reject stale entity ids.
Custom GPU program.
Definition Shader.h:39
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:18
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
bool enabled
Value-owned subset of a resource shader's static instances.
One mesh + material slot on a multi-part model (Assimp mesh / body region). When Material* is null,...
Definition Material.h:330
std::optional< MeshInstanceRange > instances
Optional value-owned instance range; forward custom shader path only.
int lodLevelForDistance(float distance) const
Lod level for distance.
bool effectiveCastShadow() const
Effective cast shadow.
bool effectiveHair() const
Effective hair.
Mesh * meshForDistance(float distance) const
Pick LOD mesh for a camera distance; falls back to mesh when LOD disabled.