载入中...
搜索中...
未找到
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
11#include <array>
12#include <cstdint>
13#include <functional>
14#include <glm/mat4x4.hpp>
15#include <optional>
16#include <string>
17#include <vector>
18
19namespace eve::graphics {
20
21class Canvas;
22class Graphics;
23class Mesh;
24class Shader;
25class Texture;
26
28class EVENGINE_API_BACKENDS Camera3D : public ecs::Entity {
29public:
31 ENTITY(Camera3D, ecs::Entity)
32
33
34 void release() override {}
35
37 struct Data {
38 static constexpr int kMaxReflectionProbes = 8;
41 Texture* cubemap = nullptr;
42 glm::vec3 center{0.f};
43 glm::vec3 extent{0.f};
44 float intensity = 0.f;
45 float blendDistance = 0.f;
46 int priority = 0;
47 bool enabled = false;
48 };
49 float eyeX = 0.f, eyeY = 0.f, eyeZ = 3.f;
50 float targetX = 0.f, targetY = 0.f, targetZ = 0.f;
51 float upX = 0.f, upY = 1.f, upZ = 0.f;
52 float fovYDeg = 60.f, nearZ = 0.1f, farZ = 100.f;
53 float aperture = 19.f, focalLength = 50.f;
54 float lodBias = 1.f;
56 std::array<float, 32> layerCullDistances{};
58 std::array<float, 32> shadowLayerCullDistances{};
59 bool orthographic = false;
60 float orthoHeight = 20.f;
62 float pixelsPerUnit = 0.f;
64 int pixelRefWidth = 0;
66 int pixelRefHeight = 0;
68 int pixelScale = 1;
70 bool pixelSnap = true;
71 float ambientR = 0.12f, ambientG = 0.12f, ambientB = 0.14f;
72 Texture* envMap = nullptr; // cubemap; nullptr → no IBL
73 float envIntensity = 1.f;
74 glm::vec3 envProbeCenter{0.f};
75 glm::vec3 envProbeExtent{0.f}; // any zero axis disables box projection
76 std::array<ReflectionProbe, kMaxReflectionProbes> reflectionProbes{};
77 float exposureEV = 0.f; // exposure compensation in stops; 0 preserves legacy brightness
78 bool autoExposure = false;
79 float autoExposureMinEV = -8.f;
80 float autoExposureMaxEV = 8.f;
81 float bloomIntensity = 0.f;
82 float bloomThreshold = 1.f;
83 float dofFocusDistance = 0.f;
84 float dofMaxBlurPx = 0.f; // <= 0 disables DOF
85 float dofFocusRange = 8.f;
86 bool active = false;
87 Camera3D *entity = nullptr;
88 // Last screenToRay() result (origin = eye, dir normalized).
89 float screenRayOx = 0.f, screenRayOy = 0.f, screenRayOz = 0.f;
90 float screenRayDx = 0.f, screenRayDy = 0.f, screenRayDz = -1.f;
91 };
92
93 COMPONENT(Data, data)
94
95
103 static Camera3D* createCamera(ecs::Table& table = *ecs::current());
104
105 void setEye(float x, float y, float z);
107 float getEyeX();
108 float getEyeY();
109 float getEyeZ();
110 void setTarget(float x, float y, float z);
112 float getTargetX();
113 float getTargetY();
114 float getTargetZ();
115 void setUp(float x, float y, float z);
116 void setFov(float fovYDeg);
118 float getFov();
120 void setOrthographic(float height);
122 void setPerspective();
124 bool isOrthographic();
126 float getOrthoHeight();
127
135 [[nodiscard]] Result<void> setPixelConfig(float pixelsPerUnit, int refWidth = 0, int refHeight = 0);
137 void clearPixelConfig();
139 bool hasPixelConfig();
141 void setPixelSnap(bool enabled);
143 bool getPixelSnap();
145 float getPixelsPerUnit();
147 int getPixelRefWidth();
149 int getPixelRefHeight();
151 int getPixelScale();
159 [[nodiscard]] Result<void> applyPixelPerfect(int viewWidth, int viewHeight);
168 [[nodiscard]] Result<void> setPixelOrthographic(float pixelsPerUnit, int viewWidth, int viewHeight,
169 int refWidth = 0, int refHeight = 0);
175 void snapToPixelGrid(int viewWidth, int viewHeight);
176
178 void setClipPlanes(float nearZ, float farZ);
float getNearClip(); float getFarClip(); void setPhysicalLens(float aperture,float focalLength); float getAperture(); float getFocalLength();
185 void setLayerCullDistance(int layer, float distance);
187 float getLayerCullDistance(int layer);
189 void setShadowLayerCullDistance(int layer, float distance);
191 float getShadowLayerCullDistance(int layer);
192 void setActive(bool active);
193 void setAmbient(float r, float g, float b);
195 void setEnvMap(Texture* cube);
196 void setEnvIntensity(float intensity);
198 void setExposure(float ev);
200 float getExposure();
202 void setAutoExposure(bool enabled, float minEV = -8.f, float maxEV = 8.f);
204 bool isAutoExposure();
206 void setBloom(float intensity, float threshold = 1.f);
208 float getBloomIntensity();
210 float getBloomThreshold();
217 void setDepthOfField(float focusDistance, float maxBlurPx, float focusRange = 8.f);
219 void clearDepthOfField();
221 float getDofFocusDistance();
223 float getDofMaxBlur();
225 float getDofFocusRange();
227 void setEnvProbe(float centerX, float centerY, float centerZ, float extentX, float extentY, float extentZ);
229 void clearEnvProbe();
231 bool hasEnvProbe();
232 float getEnvProbeCenterX();
233 float getEnvProbeCenterY();
234 float getEnvProbeCenterZ();
235 float getEnvProbeExtentX();
236 float getEnvProbeExtentY();
237 float getEnvProbeExtentZ();
245 void setReflectionProbe(int slot, Texture* cubemap, float centerX, float centerY, float centerZ, float extentX,
246 float extentY, float extentZ, float intensity = 1.f, float blendDistance = 1.f,
247 int priority = 0);
249 void clearReflectionProbe(int slot);
251 int getReflectionProbeCount();
252
258 void screenToRay(float screenX, float screenY, float viewW, float viewH);
259 float getScreenRayOriginX();
260 float getScreenRayOriginY();
261 float getScreenRayOriginZ();
262 float getScreenRayDirX();
263 float getScreenRayDirY();
264 float getScreenRayDirZ();
265};
266
268class EVENGINE_API_BACKENDS Renderable3D : public ecs::Entity {
269public:
271 ENTITY(Renderable3D, ecs::Entity)
272
273
274 void release() override {}
275
277 struct Transform3D {
278 float x = 0, y = 0, z = 0;
279 float yaw = 0, pitch = 0, roll = 0; // radians
280 float sx = 1, sy = 1, sz = 1;
285 glm::mat4 linkedWorld{1.f};
286 bool hasLinkedWorld = false;
287 };
288
290 static constexpr int kMaxLodLevels = 4;
291 static constexpr int kMaxParts = Material::kMaxPartsHint;
292
293 Mesh* mesh = nullptr;
294 Texture* texture = nullptr;
295 Texture* normalTexture = nullptr; // nullptr → flat normal in default PBR path
296 bool packedNormalMask = false; // RG normal XY, B AO, A smoothness
298 Texture *heightTexture = nullptr; // nullptr → no parallax height (R = height)
299 Shader *shader = nullptr; // nullptr → default mesh3d PBR pipeline
301 std::optional<MeshInstanceRange> instances;
303 Material* material = nullptr;
311 Shader* xrayShader = nullptr;
312 bool xrayHighlight = false;
313 float r = 1, g = 1, b = 1, a = 1;
314 float metallic = 0.f;
315 float roughness = 0.45f;
316 float texBombScale = 4.f; // cells per UV unit
317 float texBombStrength = 0.f; // 0 = off (default, preserves tiling)
318 float texBombRot = 1.f; // 0..1 per-cell rotation amount
319 float parallaxScale = 0.f; // 0 = off (default)
320 float parallaxMinLayers = 8.f;
321 float parallaxMaxLayers = 32.f;
322 bool visible = true;
323 int layer = 0;
324 uint32_t reflectionCaptureMask = 1u;
325 bool receiveLight = true;
326 bool castShadow = true;
327 bool receiveShadow = true;
328 bool castOcclusion = true; // volumetric occlusion (screen-space shafts)
329 bool customLightProbe = false;
330 std::array<glm::vec4, 9> customLightProbeSh{};
332 bool isHair = false;
333 Camera3D* camera = nullptr;
334
339 int partCount = 0;
340 ModelPart parts[kMaxParts] = {};
341
347 int lodCount = 0;
348 Mesh* lodMeshes[kMaxLodLevels] = {};
349 float lodDistances[kMaxLodLevels - 1] = {25.f, 60.f, 120.f};
351 float lodCullDistance = 0.f;
354 bool configured = false;
355 int skinQuality = 0;
356 int shadowCastingMode = 1;
357 bool receiveShadows = true;
358 int motionVectorMode = 1;
359 bool skinnedMotionVectors = true;
361 int reflectionProbeUsage = 1;
362 };
363 LodRendererState lodRendererStates[kMaxLodLevels] = {};
364 float lodFadeWidths[kMaxLodLevels] = {};
365 int lodFadeMode = 0;
366 bool lodAnimateCrossFading = false;
367 float lodCrossFadeDuration = 0.5f;
368 int lodAnimatedCurrent = -2;
369 int lodAnimatedPrevious = -2;
370 float lodAnimatedProgress = 1.f;
371
376 if (lodCullDistance > 0.f && distance >= lodCullDistance) return nullptr;
377 if (lodCount <= 0) return mesh;
378 int level = 0;
379 while (level + 1 < lodCount && distance >= lodDistances[level]) ++level;
380 Mesh* picked = lodMeshes[level];
381 return picked ? picked : mesh;
382 }
383
385 int lodLevelForDistance(float distance) const {
386 if (lodCullDistance > 0.f && distance >= lodCullDistance) return -1;
387 if (lodCount <= 0) return 0;
388 int level = 0;
389 while (level + 1 < lodCount && distance >= lodDistances[level]) ++level;
390 return level;
391 }
392
394 void lodBlendForDistance(float distance, int& primary, int& secondary, float& secondaryWeight) const;
395
397 bool usesParts() const { return partCount > 0; }
398
400 bool effectiveHair() const {
401 if (material) return material->isTransparentHair();
402 return isHair;
403 }
404
406 bool effectiveCastShadow() const {
407 if (material) return material->getCastShadow();
408 return castShadow;
409 }
410 };
411
412 COMPONENT(Transform3D, transform)
413 COMPONENT(MeshRenderer, meshRenderer)
414
415 void setPosition(float x, float y, float z);
417 uint32_t getEntityId() const { return id; }
419 uint32_t getEntityGeneration() const { return generation; }
420 void setRotation(float yaw, float pitch, float roll);
421 void setYaw(float yaw);
422 float getYaw();
423 void setScale(float sx, float sy, float sz);
424 void setMesh(Mesh *mesh);
436 [[nodiscard]] Result<void> setInstanceRange(const MeshInstanceRange &range);
438 void clearInstanceRange();
441 Mesh *getMesh();
442 void setTexture(Texture* texture);
443 void setNormalTexture(Texture* texture);
445 void setPackedNormalMask(bool enabled);
447 void setHeightTexture(Texture* texture);
448 void setShader(Shader* shader);
450 void setMaterial(Material* material);
452 Material *getMaterial();
454 void setXRayShader(Shader* shader);
456 Shader *getXRayShader();
462 void setXRayHighlight(bool on);
463 bool getXRayHighlight();
468 void setPart(int index, const std::string& name, Mesh* mesh, Material* material);
470 void setPartSortPriority(int index, int priority);
472 void clearPartSortPriority(int index);
474 int getPartSortPriority(int index);
475 void clearParts();
476 int getPartCount();
477 std::string getPartName(int index);
478 Mesh* getPartMesh(int index);
479 Material* getPartMaterial(int index);
480 void setHair(bool hair);
481 bool getHair();
482 void setTint(float r, float g, float b, float a = 1.f);
484 float getTintR() { return meshRenderer()->r; }
486 float getTintG() { return meshRenderer()->g; }
488 float getTintB() { return meshRenderer()->b; }
490 float getRoughness() { return meshRenderer()->roughness; }
491 void setMetallic(float metallic);
493 float getMetallic() { return meshRenderer()->metallic; }
494 void setRoughness(float roughness);
499 void setTexCellBomb(float cellScale, float strength, float rotAmount = 1.f);
500 float getTexCellBombScale();
501 float getTexCellBombStrength();
502 float getTexCellBombRotation();
507 void setParallax(float scale, float minLayers = 8.f, float maxLayers = 32.f);
508 float getParallaxScale();
509 float getParallaxMinLayers();
510 float getParallaxMaxLayers();
511 void setVisible(bool visible);
513 bool getVisible() { return meshRenderer()->visible; }
515 void setLayer(int layer);
517 int getLayer();
519 void setReflectionCaptureMask(int mask);
521 int getReflectionCaptureMask();
522 void setReceiveLight(bool receive);
524 [[nodiscard]] Result<void> setCustomLightProbe(float r, float g, float b);
526 [[nodiscard]] Result<void> setCustomLightProbeCoefficient(int coefficient, float r, float g, float b);
528 void clearCustomLightProbe();
529 void setCastShadow(bool cast);
530 void setReceiveShadow(bool receive);
531 void setCastOcclusion(bool cast);
532 bool getCastOcclusion();
533 void setCamera(Camera3D* camera);
534
540 void setMeshLod(int index, Mesh* mesh, float switchDistance = 0.f);
542 [[nodiscard]] Result<void> setMeshLodRendererState(int index, int skinQuality, int shadowCastingMode,
543 bool receiveShadows, int motionVectorMode,
544 bool skinnedMotionVectors, int lightProbeUsage,
545 int reflectionProbeUsage);
547 int getMeshLodRendererState(int index, int field);
549 [[nodiscard]] Result<void> setMeshLodFadeWidth(int index, float width);
551 [[nodiscard]] Result<void> setMeshLodFadePolicy(int mode, bool animate, float duration);
553 [[nodiscard]] Result<void> advanceMeshLodTransition(float distance, float dt);
555 int getMeshLodSecondaryLevelAtDistance(float distance);
557 float getMeshLodSecondaryWeightAtDistance(float distance);
559 void setMeshLodCullDistance(float distance);
561 float getMeshLodCullDistance();
562 void clearMeshLod();
563 int getMeshLodCount();
564 int getMeshLodLevelAtDistance(float distance);
565};
566
569public:
571 static void render(Graphics& gfx);
572
587 static void renderToCanvas(Graphics& gfx, Canvas* target, Camera3D* camera,
588 uint32_t reflectionCaptureMask = 0xffffffffu, float lodDistanceScale = 1.f,
589 bool includeTransparent = true, bool useClusteredLighting = true,
590 Texture* skyFaceTexture = nullptr, Mesh* skyQuad = nullptr,
591 float skyFaceTextureScale = 1.f, bool includePrimitives = false);
592
599 static void renderWithShadowsToCanvas(Graphics& gfx, Canvas& target, Camera3D& camera);
600
609 using CaptureExtraDrawer = std::function<void(Graphics& gfx, const Camera3D::Data& cam, const glm::mat4& viewProj,
610 float aspect, uint32_t reflectionCaptureMask)>;
611
618 static uint64_t addCaptureExtraDrawer(uint32_t reflectionCaptureMask, CaptureExtraDrawer drawer);
619
624 static void removeCaptureExtraDrawer(uint64_t token);
625
633 std::function<void(Graphics& gfx, const Camera3D::Data& cam, const glm::mat4& viewProj, float aspect)>;
634
636 [[nodiscard]] static uint64_t addForwardExtraDrawer(ForwardExtraDrawer drawer);
637
639 static void removeForwardExtraDrawer(uint64_t token);
640
653 std::function<void(Graphics& gfx, const Camera3D::Data& cam, const glm::mat4& viewProj, float aspect,
654 std::vector<GpuInstance>& instances)>;
655
657 [[nodiscard]] static uint64_t addGpuOpaqueCollector(GpuOpaqueCollector collector);
658
660 static void removeGpuOpaqueCollector(uint64_t token);
661
670 std::function<void(Graphics& gfx, const Camera3D::Data& cam, const glm::mat4& viewProj, float aspect)>;
672 static void addGBufferExtraDrawer(GBufferExtraDrawer drawer);
673
682 std::function<void(Graphics& gfx, const Camera3D::Data& cam, const glm::mat4& viewProj, float aspect)>;
684 static void addDecalExtraDrawer(DecalExtraDrawer drawer);
685
708 std::function<void(Graphics &gfx, const glm::mat4 &lightVP,
709 const Camera3D::Data &cam)>;
711 [[nodiscard]] static uint64_t addShadowExtraDrawer(ShadowExtraDrawer drawer);
712
720 static void removeShadowExtraDrawer(uint64_t token);
721
723 static void setDirectionalLight(float dx, float dy, float dz, float r, float g, float b);
724
725private:
726 enum class CanvasShadows { Disabled, Directional };
727 static void renderToCanvasImpl(Graphics& gfx, Canvas* target, Camera3D* camera, uint32_t reflectionCaptureMask,
728 float lodDistanceScale, bool includeTransparent, bool useClusteredLighting,
729 Texture* skyFaceTexture, Mesh* skyQuad, float skyFaceTextureScale,
730 CanvasShadows shadows, bool includePrimitives);
731};
732
733} // namespace eve::graphics
double z
double y
double x
LogicalId target
bool & active
float duration
std::vector< eve::artifact::PartView > parts
int mask
float yaw
Definition BushMesh.cpp:37
int priority
Generation generation
Definition ECS.cpp:40
std::string name
Definition ECS.cpp:154
#define EVENGINE_API_BACKENDS
Definition Export.h:108
float strength
tensor::Graph g
Definition GpuGraph.cpp:7
float camera[2]
double r
int secondary
std::uint32_t height
std::uint32_t width
Range range
std::array< float, 3 > scale
std::string id
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
float roughness
float metallic
int level
OceanGpuFrameToken token
OceanMeshSettings mesh
glm::dvec2 center
glm::dvec3 dx
glm::dvec3 dz
double dt
std::array< uint32_t, 16 > material
Texture * texture
TileLayer * layer
const EntityFact * entity
std::string slot
RenderSystem3D::ShadowExtraDrawer drawer
RenderSystem3D::GpuOpaqueCollector collector
int lightProbeUsage
bool receiveShadow
glm::mat4 viewProj
Shader * shader
bool hair
float ambientG
float ambientR
float ambientB
Move-only, checked operation results for the common layer.
glm::mat4 lightVP
Definition Shadow.cpp:148
float dy
Definition Shadow.cpp:151
float targetX
bool visible
uint32_t index
Move-only operation result carrying either a value or Status.
Definition Result.h:176
EVENGINE_API_BACKENDS public API.
Canvas public API.
Definition Canvas.h:17
Packages shading method + surface parameters into one attachable asset.
Definition Material.h:38
GPU mesh handle (+ optional CPU morph targets).
Definition Mesh.h:26
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.
float getMetallic()
Read the current material metallic scalar on the render thread.
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:41
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:15
卡牌游戏 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:404
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.