载入中...
搜索中...
未找到
AvatarVroid.cpp
浏览该文件的文档.
1#include <glm/gtc/matrix_transform.hpp>
12#include "animation/Tween.h"
13#include "avatar/Avatar.h"
15#include "avatar/VrmRuntime.h"
16#include "common/ECS.h"
17#include "common/Module.h"
19#include "graphics/DrawItem2D.h"
20#include "graphics/Graphics.h"
21#include "graphics/Mesh.h"
24#include "graphics/Texture.h"
25#include "image/ImageData.h"
26#include "inventory/Equipment.h"
27#include "model3d/ModelData.h"
28#include "scene/Scene.h"
29
30#include <algorithm>
31#include <cctype>
32#include <cstdlib>
33#include <functional>
34#include <sstream>
35
36namespace eve::avatar {
37// ---- vroid ----
38
40 if (kind_ != "vroid" || released_ || path.empty())
42 eve::DiagnosticCode::InvalidArgument, "Expected a live VRoid Avatar and a nonempty path", "path"));
43 auto prepared = VrmRuntime::prepare(path);
44 if (!prepared.ok()) return eve::Result<void>::failure(prepared.status());
45 auto candidate = std::move(prepared).takeValue();
46 std::unordered_map<std::string, std::string> humanoid;
47 for (const auto &[name, node] : candidate->document.humanoid) humanoid[name] = candidate->document.nodes[node];
48 decltype(parameters_) parameters;
49 decltype(parameterOrder_) order;
50 decltype(parameterMetadata_) metadata;
51 auto add = [&](const std::string &name) {
52 if (name.empty() || parameters.contains(name)) return;
53 parameters.emplace(name, 0.f);
54 order.push_back(name);
55 metadata.emplace(name, ParameterMetadata{});
56 };
57 for (const auto &expression : candidate->document.expressions) add(expression.name);
58 for (int mesh = 0; mesh < candidate->model->getMeshCount(); ++mesh)
59 for (int morph = 0; morph < candidate->model->getMorphTargetCount(mesh); ++morph)
60 add(candidate->model->getMorphTargetName(mesh, morph));
61 std::string importedPath(path);
62 animPlayer_ = nullptr;
63 animSkeleton_ = nullptr;
64 animSkin_ = nullptr;
65 animStateMachine_ = nullptr;
66 animLayerMixer_ = nullptr;
67 animConstraintStack_.reset();
68 footIKSolver_.reset();
69 dynamicBoneSolver_.reset();
70 skinnedParts_.clear();
71 motions_.clear();
72 attachmentPoseMatrices_.clear();
73 humanoidBones_ = std::move(humanoid);
74 visemeMorphs_.clear();
75 activeViseme_.clear();
76 parameters_.swap(parameters);
77 parameterOrder_.swap(order);
78 parameterMetadata_.swap(metadata);
79 expressionDefs_.clear();
80 expressionBlendFrom_.clear();
81 expressionBlendTo_.clear();
82 expression_.clear();
83 motion_.clear();
84 lookAtApplied_ = false;
85 hasPreviousRoot_ = false;
86 vrm_ = std::move(candidate);
87 vroidPath_ = std::move(importedPath);
88 vroidData_ = vrm_->model.get();
89 animPlayer_ = vrm_->player.get();
90 animSkeleton_ = vrm_->skeleton.get();
92}
94 float roll) {
95 if (!vrm_ || !std::isfinite(yaw) || !std::isfinite(pitch) || !std::isfinite(roll))
97 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "Expected imported VRM and finite angles"));
98 auto found = vrm_->document.humanoid.find(std::string(semantic));
99 if (found == vrm_->document.humanoid.end())
101 eve::DiagnosticCode::NotFound, "Humanoid semantic is not mapped", std::string(semantic)));
102 vrm_->rotations[vrm_->nodeBones[found->second]] = {yaw, pitch, roll};
104}
105bool AvatarInstance::loadVroidModelPath(const std::string &path) { return loadVroidModel(path).ok(); }
106int AvatarInstance::getVroidMeshCount() const { return vrm_ ? static_cast<int>(vrm_->parts.size()) : 0; }
107int AvatarInstance::getVroidSpringCount() const { return vrm_ ? static_cast<int>(vrm_->document.springs.size()) : 0; }
108std::string AvatarInstance::getVroidVersion() const { return vrm_ ? vrm_->document.version : std::string{}; }
110 if (vrm_ && !vrm_->parts.empty())
111 return dynamic_cast<graphics::Renderable3D *>(ecs::try_get(vrm_->parts.front().entity));
112 return renderable3d_;
113}
114
116 if (kind_ != "vroid" || vrm_) return false;
117 vroidData_ = data;
118 if (data) loadMorphNamesFromModel(0);
119 return data != nullptr;
120}
121
123 if (kind_ != "vroid" || !vroidData_) return 0;
124 const int n = vroidData_->getMorphTargetCount(meshIndex);
125 int added = 0;
126 for (int i = 0; i < n; ++i) {
127 const std::string name = vroidData_->getMorphTargetName(meshIndex, i);
128 if (name.empty()) continue;
129 if (!hasParameter(name)) {
130 ensureParameter(name, 0.f);
131 ++added;
132 }
133 }
134 return added;
135}
136
138 if (kind_ != "vroid" || vrm_) return;
139 boundMesh_ = mesh;
140 if (!renderable3d_) renderable3d_ = graphics::Renderable3D::create();
141 renderable3d_->setMesh(mesh);
142 if (mesh && mesh->hasMorphData()) {
143 for (int i = 0; i < mesh->getMorphCount(); ++i) {
144 const std::string name = mesh->getMorphName(i);
145 ensureParameter(name, mesh->getMorphWeight(name));
146 }
147 }
148}
149
151 if (kind_ != "vroid" || vrm_) return;
152 if (!renderable3d_) renderable3d_ = graphics::Renderable3D::create();
153 renderable3d_->setTexture(texture);
154}
155
156void AvatarInstance::setPosition3D(float x, float y, float z) {
157 x3_ = x;
158 y3_ = y;
159 z3_ = z;
160}
161
162void AvatarInstance::setRotation3D(float yaw, float pitch, float roll) {
163 yaw_ = yaw;
164 pitch_ = pitch;
165 roll_ = roll;
166}
167
168void AvatarInstance::setScale3D(float sx, float sy, float sz) {
169 sx3_ = sx;
170 sy3_ = sy;
171 sz3_ = sz;
172}
173
175 return vrm_ ? (!vrm_->providerLifetime.expired() && !vrm_->parts.empty() ? vrm_->parts.front().mesh : nullptr)
176 : boundMesh_;
177}
178
179void AvatarInstance::syncMorphWeightsToMesh() {
180 if (!boundMesh_ || !boundMesh_->hasMorphData()) return;
181 for (int i = 0; i < boundMesh_->getMorphCount(); ++i) {
182 const std::string name = boundMesh_->getMorphName(i);
183 boundMesh_->setMorphWeight(name, getParameter(name));
184 }
185}
186
188 if (vrm_) {
189 vrm_->morphs(*this);
190 return true;
191 }
192 syncMorphWeightsToMesh();
193 if (!boundMesh_ || !boundMesh_->isMorphDirty()) return false;
194 auto *gfx = ModuleManager::getInstance<graphics::Graphics>("Graphics");
195 if (!gfx) return false;
196 return gfx->bakeMeshMorph(boundMesh_);
197}
198
199void AvatarInstance::syncVroid() {
200 if (vrm_) {
201 glm::mat4 world = glm::translate(glm::mat4(1), glm::vec3(x3_, y3_, z3_));
202 world = glm::rotate(world, yaw_, glm::vec3(0, 1, 0));
203 world = glm::rotate(world, pitch_, glm::vec3(1, 0, 0));
204 world = glm::rotate(world, roll_, glm::vec3(0, 0, 1));
205 world = glm::scale(world, glm::vec3(sx3_ * sx_, sy3_ * sy_, sz3_));
206 vrm_->sync(*this, world, visible_);
207 vrm_->morphs(*this);
208 return;
209 }
210 if (!renderable3d_) renderable3d_ = graphics::Renderable3D::create();
211 if (!sceneLinked_) {
212 renderable3d_->setPosition(x3_, y3_, z3_);
213 renderable3d_->setRotation(yaw_, pitch_, roll_);
214 renderable3d_->setScale(sx3_ * sx_, sy3_ * sy_, sz3_);
215 renderable3d_->setVisible(visible_);
216 }
217 for (auto &visual : equipmentVisuals_) {
218 const auto active = projectedEquipment_.find(visual.equipmentSlot);
219 if (!visual.renderable || !visual.boneName.empty() || active == projectedEquipment_.end() ||
220 active->second != visual.itemId)
221 continue;
222 visual.renderable->setPosition(x3_ + visual.ox, y3_ + visual.oy, z3_ + visual.oz);
223 visual.renderable->setRotation(yaw_, pitch_, roll_);
224 visual.renderable->setScale(sx3_ * sx_, sy3_ * sy_, sz3_);
225 visual.renderable->setVisible(visible_);
226 }
227 bakeMorphs();
228}
229
230void AvatarInstance::destroyVroid() {
231 vrm_.reset();
232 if (renderable3d_) {
233 ecs::DestroyEntity(renderable3d_);
234 renderable3d_ = nullptr;
235 }
236 boundMesh_ = nullptr;
237 vroidData_ = nullptr;
238 vroidPath_.clear();
239}
240
241} // namespace eve::avatar
double z
double y
double x
bool & active
float yaw
Definition BushMesh.cpp:37
std::string name
Definition ECS.cpp:154
glm::vec3 n
Definition Grass.cpp:63
std::vector< std::int32_t > order
OceanMeshSettings mesh
std::array< uint32_t, 9 > parameters
Texture * texture
std::string path
Definition PlayHost.cpp:111
const RoadNode * node
uint32_t semantic
bool found
Share string attribute map operations through the Windows Foundation DLL.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
bool ok() const noexcept
Whether this result represents a non-failure outcome.
Definition Result.h:514
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
eve::Result< void > setHumanoidBoneRotation(std::string_view semantic, float yaw, float pitch, float roll)
Set an imported humanoid bone's local rotation offset in radians.
int getVroidSpringCount() const
Number of imported VRM spring chains.
float getParameter(const std::string &name) const
Returns the parameter.
graphics::Mesh * getBoundMesh() const
Borrow the first imported mesh, or the manually bound mesh.
bool loadVroidModelPath(const std::string &path)
Compatibility-only bool projection of loadVroidModel; performs the real import.
std::string getVroidVersion() const
Imported VRM specification version, or empty before import.
bool bindVroidModelData(model3d::ModelData *data)
Binds vroid model data.
void setMesh(graphics::Mesh *mesh)
Sets the mesh.
int loadMorphNamesFromModel(int meshIndex=0)
Register morph target names from ModelData as parameters (weights default 0).
bool bakeMorphs()
Push parameter weights onto Mesh morphs and bake GPU verts when possible.
void setTexture(graphics::Texture *texture)
Sets the texture.
void setRotation3D(float yaw, float pitch, float roll)
Sets the rotation 3 d.
void setScale3D(float sx, float sy, float sz)
Sets the scale 3 d.
int getVroidMeshCount() const
Number of imported mesh primitives; zero before successful import.
bool hasParameter(const std::string &name) const
True when parameter.
eve::Result< void > loadVroidModel(std::string_view path)
Import and atomically replace a complete VRM avatar from a VFS path.
graphics::Renderable3D * getRenderable3D() const
Borrow the first imported render projection, or the manual projection.
void setPosition3D(float x, float y, float z)
Sets the position 3 d.
static eve::Result< std::unique_ptr< VrmRuntime > > prepare(std::string_view path)
Prepare.
GPU mesh handle (+ optional CPU morph targets).
Definition Mesh.h:26
bool hasMorphData() const
True when morph data.
Definition Mesh.h:133
bool isMorphDirty() const
True when morph dirty.
Definition Mesh.h:129
bool setMorphWeight(const std::string &name, float weight)
Sets the morph weight.
Definition Mesh.cpp:257
int getMorphCount() const
Returns the morph count.
Definition Mesh.cpp:246
std::string getMorphName(int index) const
Returns the morph name.
Definition Mesh.cpp:248
EVENGINE_API_BACKENDS public API.
void setRotation(float yaw, float pitch, float roll)
void setTexture(Texture *texture)
void setPosition(float x, float y, float z)
void setScale(float sx, float sy, float sz)
void setVisible(bool visible)
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:15
CPU-side decoded 3D model (Assimp scene owned via medialoader::ModelScene). Does not upload to GPU — ...
Definition ModelData.h:39
std::string getMorphTargetName(int meshIndex, int morphIndex) const
Returns the morph target name.
int getMorphTargetCount(int meshIndex) const
Assimp morph / blend-shape targets on a mesh (aiAnimMesh).