载入中...
搜索中...
未找到
MeshInstanceRange.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include <glm/geometric.hpp>
5#include <glm/mat4x4.hpp>
6#include <glm/vec3.hpp>
7#include <glm/vec4.hpp>
8#include <new>
9#include "graphics/Mesh.h"
11
12// MeshInstanceRange.cpp
13namespace eve::graphics {
15 if (range.first > 1048576 || range.count > 1048576 - range.first ||
16 !std::isfinite(range.maximumHorizontalDistance) || range.maximumHorizontalDistance < 0)
18 "Invalid instance range or maximum horizontal distance",
19 "graphics.instances"));
20 for (int i = 0; i < 3; ++i)
21 if (!std::isfinite(range.minimum[i]) || !std::isfinite(range.maximum[i]) || range.minimum[i] > range.maximum[i])
23 Diagnostic::error(DiagnosticCode::InvalidArgument, "Invalid instance bounds", "graphics.instances"));
24 return Result<void>::success();
25}
26bool meshInstanceRangeVisible(const MeshInstanceRange& range, const glm::mat4& model, const glm::mat4& vp,
27 const glm::vec3& eye) {
28 if (!range.count) return false;
29 glm::vec3 localCenter, half;
30 for (int i = 0; i < 3; ++i) {
31 localCenter[i] = range.minimum[i] * .5f + range.maximum[i] * .5f;
32 half[i] = range.maximum[i] * .5f - range.minimum[i] * .5f;
33 }
34 const glm::vec3 center(model * glm::vec4(localCenter, 1));
35 const glm::vec3 axes[]{glm::vec3(model[0]) * half.x, glm::vec3(model[1]) * half.y, glm::vec3(model[2]) * half.z};
36 const auto row = [&](int i) { return glm::vec4(vp[0][i], vp[1][i], vp[2][i], vp[3][i]); };
37 const glm::vec4 planes[]{row(3) + row(0), row(3) - row(0), row(3) + row(1),
38 row(3) - row(1), row(2), row(3) - row(2)};
39 for (const auto& p : planes) {
40 const glm::vec3 n(p);
41 const float radius =
42 std::abs(glm::dot(n, axes[0])) + std::abs(glm::dot(n, axes[1])) + std::abs(glm::dot(n, axes[2]));
43 if (glm::dot(n, center) + p.w < -radius) return false;
44 }
45 if (range.maximumHorizontalDistance > 0) {
46 const glm::vec3 extent = glm::abs(axes[0]) + glm::abs(axes[1]) + glm::abs(axes[2]);
47 const double limit = double(range.maximumHorizontalDistance) + std::max(extent.x, extent.z);
48 const double dx = double(center.x) - eye.x, dz = double(center.z) - eye.z;
49 if (dx * dx + dz * dz > limit * limit) return false;
50 }
51 return true;
52}
53} // namespace eve::graphics
54
55// MeshShaderRasterState.cpp
56namespace eve::graphics {
58 if ((state.depthCompare != MeshDepthCompare::Less && state.depthCompare != MeshDepthCompare::LessEqual &&
59 state.depthCompare != MeshDepthCompare::Always) ||
60 state.colorWriteMask > 15 || !std::isfinite(state.depthBiasConstant) || !std::isfinite(state.depthBiasSlope))
62 "Invalid custom mesh depth comparison, bias or color mask",
63 "shader.raster"));
64 return Result<void>::success();
65}
66} // namespace eve::graphics
67
68// MeshTangent.cpp
69namespace eve::graphics {
71 if (!values.empty()) {
72 if (gpuVertexCount <= 0 || values.size() != size_t(gpuVertexCount) * 5)
74 DiagnosticCode::InvalidArgument, "vegetation factor stream must contain five values per mesh vertex"));
75 for (size_t i = 0; i < values.size(); ++i)
76 if (!std::isfinite(values[i]) || (i % 5 < 3 && (values[i] < 0 || values[i] > 1)))
79 "vegetation factors must be unit values and detail UVs must be finite"));
80 }
81 return Result<void>::success();
82}
85 if (!valid) return valid;
86 vegetationFactors_.swap(values);
87 return Result<void>::success();
88}
90 if (values.empty()) return Result<void>::success();
91 if (gpuVertexCount <= 0 || values.size() != size_t(gpuVertexCount) * 9)
93 DiagnosticCode::InvalidArgument, "vegetation deformation stream must contain nine values per mesh vertex"));
94 for (size_t i = 0; i < values.size(); i += 9) {
95 for (size_t component = 0; component < 9; ++component)
96 if (!std::isfinite(values[i + component]))
98 "vegetation deformation factors must be finite"));
99 for (size_t component = 3; component <= 6; ++component)
100 if (values[i + component] < 0 || values[i + component] > 1)
102 "vegetation deformation masks must be unit values"));
103 if (values[i + 7] < 0 || values[i + 8] < 0)
105 "vegetation deformation bounds must be nonnegative"));
106 }
107 return Result<void>::success();
108}
111 if (!valid) return valid;
112 vegetationDeformationFactors_.swap(values);
113 return Result<void>::success();
114}
116 if (!values.empty()) {
117 if (gpuVertexCount <= 0 || values.size() != size_t(gpuVertexCount))
119 "motion highlight stream must match mesh vertex count"));
120 for (float value : values)
121 if (!std::isfinite(value) || value < 0)
123 "motion highlights must be finite and nonnegative"));
124 }
125 return Result<void>::success();
126}
129 if (!valid) return valid;
130 motionHighlights_.swap(values);
131 return Result<void>::success();
132}
133Result<void> Mesh::validateTangentFrame(std::span<const float> tangents, std::span<const float> bitangents) const {
134 auto invalid = [] {
137 "tangent frame must contain finite independent XYZ vectors for every mesh vertex"));
138 };
139 if (tangents.empty() && bitangents.empty()) {
140 return Result<void>::success();
141 }
142 if (gpuVertexCount <= 0 || tangents.size() != size_t(gpuVertexCount) * 3 || bitangents.size() != tangents.size())
143 return invalid();
144 for (size_t i = 0; i < tangents.size(); i += 3) {
145 double tt = 0, bb = 0, tb = 0;
146 for (unsigned c = 0; c < 3; ++c) {
147 const double t = tangents[i + c], b = bitangents[i + c];
148 if (!std::isfinite(t) || !std::isfinite(b)) return invalid();
149 tt += t * t;
150 bb += b * b;
151 tb += t * b;
152 }
153 if (tt <= 1e-20 || bb <= 1e-20 || tt * bb - tb * tb <= tt * bb * 1e-12) return invalid();
154 }
155 return Result<void>::success();
156}
157Result<void> Mesh::adoptTangentFrame(std::vector<float>&& tangents, std::vector<float>&& bitangents) {
158 if (&tangents == &bitangents)
160 Diagnostic::error(DiagnosticCode::InvalidArgument, "tangent frame buffers must be distinct"));
161 auto valid = validateTangentFrame(tangents, bitangents);
162 if (!valid) return valid;
163 importedTangents_.swap(tangents);
164 importedBitangents_.swap(bitangents);
165 markTangentFrameChanged();
166 return Result<void>::success();
167}
168Result<void> Mesh::setTangentFrame(std::span<const float> tangents, std::span<const float> bitangents) {
169 auto valid = validateTangentFrame(tangents, bitangents);
170 if (!valid) return valid;
171 try {
172 std::vector<float> t(tangents.begin(), tangents.end()), b(bitangents.begin(), bitangents.end());
173 importedTangents_.swap(t);
174 importedBitangents_.swap(b);
175 markTangentFrameChanged();
176 return Result<void>::success();
177 } catch (const std::bad_alloc&) {
178 return Result<void>::failure(Diagnostic::error(DiagnosticCode::Failed, "tangent frame allocation failed"));
179 }
180}
181} // namespace eve::graphics
float planes[6][4]
glm::vec4 p[6]
glm::vec3 n
Definition Grass.cpp:63
std::int32_t c
Range range
bool valid
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
float tb
glm::dvec2 center
glm::dvec3 dx
glm::dvec3 dz
std::set< std::string > values
Object::Ptr value
float t
glm::vec3 eye
glm::mat4 model
std::array< std::array< double, 3 >, 3 > axes
int limit
Definition TreeMesh.cpp:166
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
Result< void > adoptVegetationFactors(std::vector< float > &&values)
Validate and take ownership of interleaved vegetation variation/occlusion pairs.
int gpuVertexCount
Vertex count of the GPU buffer (morph CPU base may be empty).
Definition Mesh.h:31
Result< void > validateMotionHighlights(std::span< const float > values) const
Validate an optional per-vertex motion-highlight stream without mutation.
Result< void > adoptVegetationDeformationFactors(std::vector< float > &&values)
Validate and atomically take ownership of a TVE rest-deformation stream. Failure preserves the mesh a...
Result< void > validateVegetationDeformationFactors(std::span< const float > values) const
Validate optional TVE rest-deformation data without mutation. Nine floats per vertex are pivot XYZ,...
Result< void > adoptTangentFrame(std::vector< float > &&tangents, std::vector< float > &&bitangents)
Validate then take owning frame buffers without allocating on success.
Result< void > validateVegetationFactors(std::span< const float > values) const
Validate optional interleaved vegetation variation/occlusion pairs without mutation.
Result< void > adoptMotionHighlights(std::vector< float > &&values)
Validate and transfer an owning motion-highlight stream without allocating on success.
Result< void > setTangentFrame(std::span< const float > tangents, std::span< const float > bitangents)
Atomically copy authored tangent and bitangent XYZ streams, or clear both with empty spans.
Result< void > validateTangentFrame(std::span< const float > tangents, std::span< const float > bitangents) const
Validate a frame against current vertex count without mutation or allocation on success.
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
bool meshInstanceRangeVisible(const MeshInstanceRange &range, const glm::mat4 &model, const glm::mat4 &vp, const glm::vec3 &eye)
Test transformed bounds against Vulkan clip planes and horizontal distance.
Result< void > validateMeshInstanceRange(const MeshInstanceRange &range)
Worker-safe validation of value-owned range and culling metadata.
Result< void > validateMeshShaderRasterState(const MeshShaderRasterState &state)
Validate a borrowed value without touching graphics state; safe on any thread.
Value-owned subset of a resource shader's static instances.
Value snapshot of custom mesh rasterization state, independent of blending/culling.