载入中...
搜索中...
未找到
Renderable3D.cpp
浏览该文件的文档.
1#include <algorithm>
2#include <cmath>
3#include "common/Assert.h"
4#include "common/Exception.h"
5#include "graphics/Material.h"
6#include "graphics/Mesh.h"
8
9namespace eve::graphics {
10void Renderable3D::setPosition(float x, float y, float z) {
11 auto t = transform();
12 t->hasLinkedWorld = false;
13 t->x = x;
14 t->y = y;
15 t->z = z;
16}
17
18void Renderable3D::setRotation(float yaw, float pitch, float roll) {
19 auto t = transform();
20 t->hasLinkedWorld = false;
21 t->yaw = yaw;
22 t->pitch = pitch;
23 t->roll = roll;
24}
25
27 transform()->hasLinkedWorld = false;
28 transform()->yaw = yaw;
29}
30
31float Renderable3D::getYaw() { return transform()->yaw; }
32
33void Renderable3D::setScale(float sx, float sy, float sz) {
34 auto t = transform();
35 t->hasLinkedWorld = false;
36 t->sx = sx;
37 t->sy = sy;
38 t->sz = sz;
39}
40
41void Renderable3D::setMesh(Mesh* mesh) { meshRenderer()->mesh = mesh; }
42
43Mesh* Renderable3D::getMesh() { return meshRenderer()->mesh; }
44
45void Renderable3D::setTexture(Texture* texture) { meshRenderer()->texture = texture; }
46
47void Renderable3D::setNormalTexture(Texture* texture) { meshRenderer()->normalTexture = texture; }
48
49void Renderable3D::setPackedNormalMask(bool enabled) { meshRenderer()->packedNormalMask = enabled; }
50
51void Renderable3D::setHeightTexture(Texture* texture) { meshRenderer()->heightTexture = texture; }
52
53void Renderable3D::setShader(Shader* shader) { meshRenderer()->shader = shader; }
54
55void Renderable3D::setMaterial(Material* material) { meshRenderer()->material = material; }
56
57Material* Renderable3D::getMaterial() { return meshRenderer()->material; }
58
59void Renderable3D::setXRayShader(Shader* shader) { meshRenderer()->xrayShader = shader; }
60
61Shader* Renderable3D::getXRayShader() { return meshRenderer()->xrayShader; }
62
63void Renderable3D::setXRayHighlight(bool on) { meshRenderer()->xrayHighlight = on; }
64
65bool Renderable3D::getXRayHighlight() { return meshRenderer()->xrayHighlight; }
66
67void Renderable3D::setPart(int index, const std::string& name, Mesh* mesh, Material* material) {
68 auto mr = meshRenderer();
69 if (index < 0 || index >= MeshRenderer::kMaxParts) return;
70 mr->parts[index].name = name;
71 mr->parts[index].mesh = mesh;
72 mr->parts[index].material = material;
73 if (mesh) {
74 if (mr->partCount < index + 1) mr->partCount = index + 1;
75 } else if (index + 1 == mr->partCount) {
76 while (mr->partCount > 0 && !mr->parts[mr->partCount - 1].mesh) --mr->partCount;
77 }
78}
79
81 auto mr = meshRenderer();
82 if (index < 0 || index >= MeshRenderer::kMaxParts) return;
83 mr->parts[index].sortPriority = priority;
84 mr->parts[index].hasSortPriority = true;
85}
86
88 auto mr = meshRenderer();
89 if (index < 0 || index >= MeshRenderer::kMaxParts) return;
90 mr->parts[index].sortPriority = 0;
91 mr->parts[index].hasSortPriority = false;
92}
93
95 auto mr = meshRenderer();
96 if (index < 0 || index >= mr->partCount) return 0;
97 const auto& part = mr->parts[index];
98 if (part.hasSortPriority) return part.sortPriority;
99 return part.material ? part.material->getSortPriority() : 0;
100}
101
103 auto mr = meshRenderer();
104 mr->partCount = 0;
105 for (int i = 0; i < MeshRenderer::kMaxParts; ++i) {
106 mr->parts[i] = ModelPart{};
107 }
108}
109
110int Renderable3D::getPartCount() { return meshRenderer()->partCount; }
111
113 auto mr = meshRenderer();
114 if (index < 0 || index >= mr->partCount) return {};
115 return mr->parts[index].name;
116}
117
119 auto mr = meshRenderer();
120 if (index < 0 || index >= mr->partCount) return nullptr;
121 return mr->parts[index].mesh;
122}
123
125 auto mr = meshRenderer();
126 if (index < 0 || index >= mr->partCount) return nullptr;
127 return mr->parts[index].material;
128}
129
130void Renderable3D::setHair(bool hair) { meshRenderer()->isHair = hair; }
131
132bool Renderable3D::getHair() { return meshRenderer()->isHair; }
133
134void Renderable3D::setTint(float r, float g, float b, float a) {
135 auto mr = meshRenderer();
136 mr->r = r;
137 mr->g = g;
138 mr->b = b;
139 mr->a = a;
140}
141
142void Renderable3D::setMetallic(float metallic) { meshRenderer()->metallic = metallic; }
143
144void Renderable3D::setRoughness(float roughness) { meshRenderer()->roughness = roughness; }
145
146void Renderable3D::setTexCellBomb(float cellScale, float strength, float rotAmount) {
147 auto mr = meshRenderer();
148 mr->texBombScale = cellScale > 1e-3f ? cellScale : 1e-3f;
149 mr->texBombStrength = strength < 0.f ? 0.f : (strength > 1.f ? 1.f : strength);
150 mr->texBombRot = rotAmount < 0.f ? 0.f : (rotAmount > 1.f ? 1.f : rotAmount);
151}
152
153float Renderable3D::getTexCellBombScale() { return meshRenderer()->texBombScale; }
154
155float Renderable3D::getTexCellBombStrength() { return meshRenderer()->texBombStrength; }
156
157float Renderable3D::getTexCellBombRotation() { return meshRenderer()->texBombRot; }
158
159void Renderable3D::setParallax(float scale, float minLayers, float maxLayers) {
160 auto mr = meshRenderer();
161 mr->parallaxScale = scale < 0.f ? 0.f : (scale > 0.25f ? 0.25f : scale);
162 float minL = minLayers < 1.f ? 1.f : minLayers;
163 float maxL = maxLayers < minL ? minL : maxLayers;
164 if (maxL > 64.f) maxL = 64.f;
165 mr->parallaxMinLayers = minL;
166 mr->parallaxMaxLayers = maxL;
167}
168
169float Renderable3D::getParallaxScale() { return meshRenderer()->parallaxScale; }
170
171float Renderable3D::getParallaxMinLayers() { return meshRenderer()->parallaxMinLayers; }
172
173float Renderable3D::getParallaxMaxLayers() { return meshRenderer()->parallaxMaxLayers; }
174
175void Renderable3D::setVisible(bool visible) { meshRenderer()->visible = visible; }
176
178 meshRenderer()->reflectionCaptureMask = static_cast<uint32_t>(mask);
179}
180
181int Renderable3D::getReflectionCaptureMask() { return static_cast<int>(meshRenderer()->reflectionCaptureMask); }
182
183void Renderable3D::setReceiveLight(bool receive) { meshRenderer()->receiveLight = receive; }
184
185void Renderable3D::setCastShadow(bool cast) { meshRenderer()->castShadow = cast; }
186
187void Renderable3D::setReceiveShadow(bool receive) { meshRenderer()->receiveShadow = receive; }
188
189void Renderable3D::setCastOcclusion(bool cast) { meshRenderer()->castOcclusion = cast; }
190
191bool Renderable3D::getCastOcclusion() { return meshRenderer()->castOcclusion; }
192
193void Renderable3D::setCamera(Camera3D* camera) { meshRenderer()->camera = camera; }
194
195void Renderable3D::setMeshLod(int index, Mesh* mesh, float switchDistance) {
196 auto mr = meshRenderer();
197 if (index < 0 || index >= MeshRenderer::kMaxLodLevels) return;
198 mr->lodMeshes[index] = mesh;
199 if (index > 0) mr->lodDistances[index - 1] = switchDistance;
200 if (mesh) {
201 if (mr->lodCount < index + 1) mr->lodCount = index + 1;
202 } else if (index + 1 == mr->lodCount) {
203 while (mr->lodCount > 0 && !mr->lodMeshes[mr->lodCount - 1]) --mr->lodCount;
204 }
205 // Keep primary mesh in sync with LOD0 when set.
206 if (index == 0 && mesh) mr->mesh = mesh;
207}
208
210 float& secondaryWeight) const {
211 primary = lodLevelForDistance(distance);
212 secondary = -1;
213 secondaryWeight = 0.f;
214 if (lodFadeMode == 0 || lodCount <= 0) return;
218 secondaryWeight = std::clamp(lodAnimatedProgress, 0.f, 1.f);
219 if (primary == secondary || secondaryWeight >= 1.f) {
220 primary = secondary;
221 secondary = -1;
222 secondaryWeight = 0.f;
223 }
224 return;
225 }
226 for (int level = 0; level < lodCount; ++level) {
227 const float boundary = level + 1 < lodCount ? lodDistances[level] : lodCullDistance;
228 if (!(boundary > 0.f)) continue;
229 const float start = level == 0 ? 0.f : lodDistances[level - 1];
230 const float width = (boundary - start) * std::clamp(lodFadeWidths[level], 0.f, 1.f);
231 if (width > 0.f && distance >= boundary - width && distance < boundary) {
232 primary = level;
233 secondary = level + 1 < lodCount ? level + 1 : -1;
234 secondaryWeight = std::clamp((distance - (boundary - width)) / width, 0.f, 1.f);
235 return;
236 }
237 }
238}
239
241 meshRenderer()->lodCullDistance = std::isfinite(distance) && distance > 0.f ? distance : 0.f;
242}
243
245 auto mr = meshRenderer();
246 mr->lodCount = 0;
247 for (int i = 0; i < MeshRenderer::kMaxLodLevels; ++i) mr->lodMeshes[i] = nullptr;
248}
249
250int Renderable3D::getMeshLodCount() { return meshRenderer()->lodCount; }
251
252int Renderable3D::getMeshLodLevelAtDistance(float distance) { return meshRenderer()->lodLevelForDistance(distance); }
253
254
255Result<void> Renderable3D::setMeshLodRendererState(int index, int skinQuality, int shadowCastingMode,
256 bool receiveShadows, int motionVectorMode,
257 bool skinnedMotionVectors, int lightProbeUsage,
258 int reflectionProbeUsage) {
259 const bool validSkin = skinQuality == 0 || skinQuality == 1 || skinQuality == 2 || skinQuality == 4;
260 const bool validLight = lightProbeUsage == 0 || lightProbeUsage == 1 || lightProbeUsage == 2 ||
261 lightProbeUsage == 4;
262 if (index < 0 || index >= MeshRenderer::kMaxLodLevels || !validSkin || shadowCastingMode < 0 ||
263 shadowCastingMode > 3 || motionVectorMode < 0 || motionVectorMode > 2 || !validLight ||
264 reflectionProbeUsage < 0 || reflectionProbeUsage > 2)
266 "mesh LOD renderer state is invalid", "graphics.renderable3d.lod"));
267 auto& state = meshRenderer()->lodRendererStates[index];
268 state.configured = true;
269 state.skinQuality = skinQuality;
270 state.shadowCastingMode = shadowCastingMode;
271 state.receiveShadows = receiveShadows;
272 state.motionVectorMode = motionVectorMode;
273 state.skinnedMotionVectors = skinnedMotionVectors;
274 state.lightProbeUsage = lightProbeUsage;
275 state.reflectionProbeUsage = reflectionProbeUsage;
276 return Result<void>::success();
277}
278
280 if (index < 0 || index >= MeshRenderer::kMaxLodLevels || !std::isfinite(width) || width < 0.f || width > 1.f)
282 "mesh LOD fade width is invalid", "graphics.renderable3d.lod"));
283 meshRenderer()->lodFadeWidths[index] = width;
284 return Result<void>::success();
285}
286
288 if (mode < 0 || mode > 2 || !std::isfinite(duration) || duration <= 0.f || (animate && mode == 0))
290 "mesh LOD fade policy is invalid", "graphics.renderable3d.lod"));
291 auto mr = meshRenderer();
292 mr->lodFadeMode = mode;mr->lodAnimateCrossFading = animate;mr->lodCrossFadeDuration = duration;
293 mr->lodAnimatedCurrent = -2;mr->lodAnimatedPrevious = -2;mr->lodAnimatedProgress = 1.f;
294 return Result<void>::success();
295}
296
298 auto mr = meshRenderer();
299 if (index < 0 || index >= mr->lodCount || field < 0 || field > 6) return -1;
300 const auto& state = mr->lodRendererStates[index];
301 switch (field) {
302 case 0: return state.skinQuality;
303 case 1: return state.shadowCastingMode;
304 case 2: return state.receiveShadows ? 1 : 0;
305 case 3: return state.motionVectorMode;
306 case 4: return state.skinnedMotionVectors ? 1 : 0;
307 case 5: return state.lightProbeUsage;
308 case 6: return state.reflectionProbeUsage;
309 default: return -1;
310 }
311}
312
314 if (!std::isfinite(distance) || distance < 0.f || !std::isfinite(dt) || dt < 0.f)
316 "mesh LOD transition inputs are invalid", "graphics.renderable3d.lod"));
317 auto mr = meshRenderer();const int target = mr->lodLevelForDistance(distance);
318 if (mr->lodAnimatedCurrent == -2) {mr->lodAnimatedCurrent=target;mr->lodAnimatedProgress=1.f;return Result<void>::success();}
319 if (target != mr->lodAnimatedCurrent) {
320 mr->lodAnimatedPrevious=mr->lodAnimatedCurrent;mr->lodAnimatedCurrent=target;mr->lodAnimatedProgress=0.f;
321 }
322 if (mr->lodAnimatedPrevious != -2 && mr->lodAnimatedPrevious != mr->lodAnimatedCurrent) {
323 mr->lodAnimatedProgress=std::min(1.f,mr->lodAnimatedProgress+dt/mr->lodCrossFadeDuration);
324 if (mr->lodAnimatedProgress>=1.f)mr->lodAnimatedPrevious=-2;
325 }
326 return Result<void>::success();
327}
328
330 int primary=-1,secondary=-1;float weight=0.f;meshRenderer()->lodBlendForDistance(distance,primary,secondary,weight);
331 return secondary;
332}
333
335 int primary=-1,secondary=-1;float weight=0.f;meshRenderer()->lodBlendForDistance(distance,primary,secondary,weight);
336 return weight;
337}
338
339float Renderable3D::getMeshLodCullDistance() { return meshRenderer()->lodCullDistance; }
340
342 EV_PARAM_CHECK(layer >= 0, "layer must be in [0,31]");
343 EV_PARAM_CHECK(layer < 32, "layer must be in [0,31]");
344 meshRenderer()->layer = layer;
345}
346
347int Renderable3D::getLayer() { return meshRenderer()->layer; }
348
350 if (!std::isfinite(r) || !std::isfinite(g) || !std::isfinite(b) || r < 0.f || g < 0.f || b < 0.f)
352 "custom light probe irradiance must be finite and nonnegative", "graphics.renderable3d.lightProbe"));
353 meshRenderer()->customLightProbe = true;
354 meshRenderer()->customLightProbeSh = {};
355 constexpr float kY00 = 0.2820947918f;
356 meshRenderer()->customLightProbeSh[0] = glm::vec4(r / kY00, g / kY00, b / kY00, 0.f);
357 return Result<void>::success();
358}
359
360Result<void> Renderable3D::setCustomLightProbeCoefficient(int coefficient, float r, float g, float b) {
361 if (coefficient < 0 || coefficient >= 9 || !std::isfinite(r) || !std::isfinite(g) || !std::isfinite(b))
363 "custom light probe coefficient requires index 0..8 and finite RGB", "graphics.renderable3d.lightProbe"));
364 meshRenderer()->customLightProbe = true;
365 meshRenderer()->customLightProbeSh[static_cast<size_t>(coefficient)] = glm::vec4(r, g, b, 0.f);
366 return Result<void>::success();
367}
368
370 meshRenderer()->customLightProbe = false;
371 meshRenderer()->customLightProbeSh = {};
372}
373
374} // namespace eve::graphics
double z
double y
double x
LogicalId target
Duration start
float duration
EVEngine assertion entry point, backed by zeroerr.
#define EV_PARAM_CHECK(cond,...)
Validate a function parameter / public API precondition.
Definition Assert.h:30
int mask
float yaw
Definition BushMesh.cpp:37
int priority
std::string name
Definition ECS.cpp:154
float strength
tensor::Graph g
Definition GpuGraph.cpp:7
float camera[2]
double r
int secondary
std::uint32_t width
std::array< float, 3 > scale
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
float roughness
float metallic
int level
OceanMeshSettings mesh
double dt
std::array< uint32_t, 16 > material
Texture * texture
TileLayer * layer
float t
Renderable3D::MeshRenderer * mr
int lightProbeUsage
Shader * shader
bool hair
CommandLogBoundary boundary
Anchor rule, see above.
bool visible
double weight
uint32_t index
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
EVENGINE_API_BACKENDS public API.
Packages shading method + surface parameters into one attachable asset.
Definition Material.h:38
GPU mesh handle (+ optional CPU morph targets).
Definition Mesh.h:26
void setNormalTexture(Texture *texture)
void setCamera(Camera3D *camera)
void setReflectionCaptureMask(int mask)
Set the bit mask used to include this object in reflection-probe captures.
int getMeshLodLevelAtDistance(float distance)
void setPartSortPriority(int index, int priority)
Override transparent sorting for one part without mutating its shared Material.
std::string getPartName(int index)
Material * getPartMaterial(int index)
Result< void > advanceMeshLodTransition(float distance, float dt)
Advance an animated transition using caller-injected distance and dt.
void setRotation(float yaw, float pitch, float roll)
void setLayer(int layer)
Assign this renderable to a camera-culling layer in [0,31].
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...
Mesh * getMesh()
Main (non-part) mesh attached via setMesh; nullptr when unset.
void setTint(float r, float g, float b, float a=1.f)
void clearCustomLightProbe()
Clear the custom diffuse probe and restore the camera ambient value.
void setMetallic(float metallic)
void setTexture(Texture *texture)
void setMeshLodCullDistance(float distance)
Set the distance that culls the whole mesh LOD chain; zero disables culling.
void setReceiveLight(bool receive)
float getMeshLodSecondaryWeightAtDistance(float distance)
Return secondary LOD weight in [0,1].
void setShader(Shader *shader)
int getReflectionCaptureMask()
Return the reflection-probe capture bit mask.
Result< void > setMeshLodFadePolicy(int mode, bool animate, float duration)
Configure Unity LODFadeMode and deterministic time-driven transitions.
void setMaterial(Material *material)
Attach a Material that packages shading method + surface params.
void setPackedNormalMask(bool enabled)
Enable RG normal XY, B AO and A smoothness interpretation for the normal texture.
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 clearPartSortPriority(int index)
Clear a part's per-instance sort override and use its Material priority.
void setXRayShader(Shader *shader)
Attach an X-ray mesh shader (see Shader::setXray) for occluded silhouettes.
Result< void > setCustomLightProbe(float r, float g, float b)
Supply baked diffuse irradiance for LOD levels using CustomProvided light probes.
int getMeshLodRendererState(int index, int field)
Return one LOD renderer policy field, or -1 for an invalid level/field.
float getMeshLodCullDistance()
Return the mesh LOD culling distance; zero means disabled.
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...
int getPartSortPriority(int index)
Return the effective per-part or Material transparent sort priority.
int getMeshLodSecondaryLevelAtDistance(float distance)
Return secondary LOD selected for a fade, or -1 for cull/no secondary.
Mesh * getPartMesh(int index)
Result< void > setMeshLodRendererState(int index, int skinQuality, int shadowCastingMode, bool receiveShadows, int motionVectorMode, bool skinnedMotionVectors, int lightProbeUsage, int reflectionProbeUsage)
Set one LOD's Pcg-compatible renderer policy using Unity enum numeric values.
int getLayer()
Return this renderable's camera-culling layer.
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)
Result< void > setMeshLodFadeWidth(int index, float width)
Configure per-level distance fade width.
void setScale(float sx, float sy, float sz)
void setReceiveShadow(bool receive)
void setVisible(bool visible)
Result< void > setCustomLightProbeCoefficient(int coefficient, float r, float g, float b)
Set one L0..L2 RGB spherical-harmonic coefficient for CustomProvided mode.
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
One mesh + material slot on a multi-part model (Assimp mesh / body region). When Material* is null,...
Definition Material.h:404
void lodBlendForDistance(float distance, int &primary, int &secondary, float &secondaryWeight) const
Select primary/secondary levels and secondary weight for the active fade policy.
int lodLevelForDistance(float distance) const
Lod level for distance.
int lodCount
Optional geometric LOD. When lodCount > 0, lodMeshes[0..lodCount) are used instead of mesh based on c...
float lodCullDistance
Distance beyond which the complete LOD chain is culled; zero disables.