3#include <assimp/mesh.h>
13#include <glm/mat4x4.hpp>
14#include <glm/vec3.hpp>
19 std::any_of(
values.begin(),
values.end(), [](
float x) { return !std::isfinite(x); }))
23 texcoords_.insert_or_assign(set, std::move(
owned));
28 auto found = texcoords_.find(set);
29 return found == texcoords_.end() ? std::span<const float>{} : std::span<const float>(
found->second);
34const std::vector<float> kEmptyAttribute;
38 markTangentFrameChanged();
40 for (
unsigned channel = 0; channel <
mesh.GetNumUVChannels(); ++channel) {
41 if (!
mesh.HasTextureCoords(channel))
continue;
43 values.reserve(
size_t(
mesh.mNumVertices) * 2u);
44 for (
unsigned i = 0; i <
mesh.mNumVertices; ++i) {
45 values.push_back(
mesh.mTextureCoords[channel][i].x);
46 values.push_back(
mesh.mTextureCoords[channel][i].y);
48 importedUvs_.push_back(std::move(
values));
50 importedColors_.clear();
51 for (
unsigned channel = 0; channel <
mesh.GetNumColorChannels(); ++channel) {
52 if (!
mesh.HasVertexColors(channel))
continue;
54 values.reserve(
size_t(
mesh.mNumVertices) * 4u);
55 for (
unsigned i = 0; i <
mesh.mNumVertices; ++i) {
56 const aiColor4D &
c =
mesh.mColors[channel][i];
59 importedColors_.push_back(std::move(
values));
61 importedTangents_.clear();
62 importedBitangents_.clear();
63 if (
mesh.HasTangentsAndBitangents()) {
64 importedTangents_.reserve(
size_t(
mesh.mNumVertices) * 3u);
65 importedBitangents_.reserve(
size_t(
mesh.mNumVertices) * 3u);
66 for (
unsigned i = 0; i <
mesh.mNumVertices; ++i) {
67 const aiVector3D &
t =
mesh.mTangents[i];
68 const aiVector3D &
b =
mesh.mBitangents[i];
69 importedTangents_.insert(importedTangents_.end(), {t.x, t.y, t.z});
70 importedBitangents_.insert(importedBitangents_.end(), {b.x, b.y, b.z});
76 return channel >= 0 &&
static_cast<size_t>(channel) < importedUvs_.size()
77 ? importedUvs_[
static_cast<size_t>(channel)]
82 return channel >= 0 &&
static_cast<size_t>(channel) < importedColors_.size()
83 ? importedColors_[
static_cast<size_t>(channel)]
88 if (!matrices || matrixCount <= 0 ||
static_cast<size_t>(matrixCount) > skinPalette_.max_size() / 16u)
90 const size_t count =
static_cast<size_t>(matrixCount) * 16u;
91 for (
size_t i = 0; i <
count; ++i)
92 if (!std::isfinite(matrices[i]))
97 if (
count <= skinPalette_.size()) {
98 std::memmove(skinPalette_.data(), matrices,
count *
sizeof(
float));
99 skinPalette_.resize(
count);
102 skinPalette_.resize(
count);
103 std::memcpy(skinPalette_.data(), matrices,
count *
sizeof(
float));
111 hasLocalBounds_ =
false;
120 localBounds_[
axis] = std::numeric_limits<float>::max();
121 localBounds_[
axis + 3] = std::numeric_limits<float>::lowest();
124 for (
size_t i = 0; i <
n; ++i) {
126 const float value = posXYZ[i * 3u +
static_cast<std::size_t
>(
axis)];
127 if (!std::isfinite(
value))
return;
129 localBounds_[
axis + 3] = std::max(localBounds_[
axis + 3],
value);
131 c.x += posXYZ[i * 3u + 0
u];
132 c.y += posXYZ[i * 3u + 1u];
133 c.z += posXYZ[i * 3u + 2u];
136 if (!std::isfinite(
c.x) || !std::isfinite(
c.y) || !std::isfinite(
c.z))
return;
142 for (
size_t i = 0; i <
n; ++i) {
143 const float dx = posXYZ[i * 3u + 0
u] -
c.x;
144 const float dy = posXYZ[i * 3u + 1u] -
c.y;
145 const float dz = posXYZ[i * 3u + 2u] -
c.z;
151 if (std::isfinite(
r)) {
153 hasLocalBounds_ =
true;
158 if (!hasLocalBounds_)
170 if (
w <= 0.f ||
h <= 0.f)
return;
179 morphWeights_.clear();
188 basePos_.assign(posXYZ, posXYZ +
n * 3);
190 baseNrm_.assign(nrmXYZ, nrmXYZ +
n * 3);
192 baseNrm_.assign(
n * 3, 0.f);
194 baseUv_.assign(uvST, uvST +
n * 2);
196 baseUv_.assign(
n * 2, 0.f);
200 if (
name.empty() || !deltaPosXYZ || basePos_.empty())
return false;
204 t.deltaPos.assign(deltaPosXYZ, deltaPosXYZ + basePos_.size());
205 morphs_.push_back(std::move(
t));
206 morphWeights_[
name] = 0.f;
212 if (
name.empty() || !absPosXYZ || basePos_.empty())
return false;
216 t.deltaPos.resize(basePos_.size());
217 for (
size_t i = 0; i < basePos_.size(); ++i)
218 t.deltaPos[i] = absPosXYZ[i] - basePos_[i];
219 morphs_.push_back(std::move(
t));
220 morphWeights_[
name] = 0.f;
226 if (!basePos_.empty())
return int(basePos_.size() / 3);
232 std::find_if(morphs_.begin(), morphs_.end(), [&](
const auto &morph) { return morph.name == name; });
233 if (
target == morphs_.end())
236 if ((!deltas.empty() && deltas.size() != baseNrm_.size()) ||
237 std::any_of(deltas.begin(), deltas.end(), [](
float x) { return !std::isfinite(x); }))
239 "Expected finite normal deltas matching the morph base"));
240 std::vector<float>
owned(deltas.begin(), deltas.end());
249 if (
index < 0 ||
size_t(
index) >= morphs_.size())
return {};
250 return morphs_[size_t(
index)].name;
254 return morphWeights_.find(
name) != morphWeights_.end();
258 auto it = morphWeights_.find(
name);
259 if (it == morphWeights_.end())
return false;
262 if (std::fabs(it->second -
weight) > 1e-6f) {
270 auto it = morphWeights_.find(
name);
271 return it == morphWeights_.end() ? 0.f : it->second;
275 for (
auto &kv : morphWeights_) {
276 if (kv.second != 0.f) {
286 if (outPos.empty())
return;
287 bool normalsChanged =
false;
288 for (
const MorphTarget &
t : morphs_) {
289 auto it = morphWeights_.find(
t.name);
290 const float w = (it == morphWeights_.end()) ? 0.f : it->second;
291 if (std::fabs(
w) < 1e-8f)
continue;
292 for (
size_t i = 0; i < outPos.size() && i <
t.deltaPos.size(); ++i)
293 outPos[i] +=
t.deltaPos[i] *
w;
294 if (!
t.deltaNrm.empty()) {
295 normalsChanged =
true;
296 for (
size_t i = 0; i < outNrm.size(); ++i) outNrm[i] +=
t.deltaNrm[i] *
w;
300 for (
size_t i = 0; i + 2 < outNrm.size(); i += 3) {
301 const float length = std::hypot(outNrm[i], outNrm[i + 1], outNrm[i + 2]);
307 outNrm[i] = baseNrm_[i];
308 outNrm[i + 1] = baseNrm_[i + 1];
309 outNrm[i + 2] = baseNrm_[i + 2];
std::uint32_t vertexCount
std::set< std::string > values
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.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
bool getCastOcclusion() const
When false, skipped by Graphics::drawOcclusion / volumetric occluder passes.
virtual void drawOcclusionSolid(float x, float y, float w, float h)
Draws occlusion solid.
void initMorphBase(int vertexCount, const float *posXYZ, const float *nrmXYZ=nullptr, const float *uvST=nullptr)
Capture base pose (xyz packed). Optional normals/uvs (same vertex count).
const std::vector< float > & importedUv(int channel) const
Imported uv.
void clearMorphData()
Clears morph data.
void computeBounds(const float *posXYZ, int vertexCount)
Compute the bounding sphere (centroid + max radius) from positions.
int gpuVertexCount
Vertex count of the GPU buffer (morph CPU base may be empty).
bool setMorphWeight(const std::string &name, float weight)
Sets the morph weight.
void captureImportedAttributes(const ::aiMesh &mesh)
Retain imported UV/color/tangent streams for custom pipelines and baking.
bool addMorphTarget(const std::string &name, const float *deltaPosXYZ)
Adds morph target.
bool hasMorph(const std::string &name) const
True when morph.
float boundsCx
Model-space bounding sphere used for view/cascade frustum culling. Computed from vertex positions at ...
Result< std::array< float, 6 > > localBounds() const
Copy CPU-uploaded mesh-local minimum XYZ and maximum XYZ.
void drawOcclusion(Graphics *gfx, const glm::mat4 &matrix) const override
Screen-space black proxy for volumetric occlusion. Interprets matrix as 2D affine (translation + scal...
Result< void > setMorphNormalDeltas(std::string_view name, std::span< const float > deltas)
Copy normal deltas for an existing morph, or clear them with an empty span. Input is borrowed only du...
float getMorphWeight(const std::string &name) const
Returns the morph weight.
bool addMorphTargetAbsolute(const std::string &name, const float *absPosXYZ)
Absolute morph (Assimp aiAnimMesh style): stored as delta from base.
void clearMorphWeights()
Clears morph weights.
int getMorphCount() const
Returns the morph count.
Result< void > setTexcoordSet(uint32_t set, std::span< const float > values)
Copy one packed ST channel; finite values/count are checked atomically. Render-thread only,...
std::string getMorphName(int index) const
Returns the morph name.
std::span< const float > texcoordSet(uint32_t set) const
Borrow a channel until mutation/destruction; empty means absent. Render-thread only.
int getVertexCount() const
Returns the vertex count.
const std::vector< float > & importedColor(int channel) const
Imported color.
Result< void > setSkinPalette(const float *matrices, int matrixCount)
Copy a dynamically sized column-major palette on the render thread. Invalid/non-finite input leaves t...
void computeMorphedPositions(std::vector< float > &outPos, std::vector< float > &outNrm) const
Bake current weights into outPos / outNrm (xyz packed).
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...