载入中...
搜索中...
未找到
AvatarInstance.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
4
5
8
9#include <array>
10#include <functional>
11#include <memory>
12#include <string>
13#include <unordered_map>
14#include <utility>
15#include <vector>
16
17namespace eve::graphics {
18struct DrawItem2D;
19class Mesh;
20class Material;
21class Renderable2D;
22class Renderable3D;
23class Texture;
24} // namespace eve::graphics
25
26namespace eve::image {
27class ImageData;
28}
29
30namespace eve::model3d {
31class ModelData;
32}
33
34namespace eve::scene {
35class Scene;
36}
37
38namespace eve::inventory {
39class EquipmentSet;
40}
41
42namespace eve::animation {
43class AnimClip;
44class AnimPlayer;
45class AnimLayerMixer;
46class AnimConstraintStack;
47class AnimPose;
48class AnimSkin;
49class AnimSkeleton;
50class AnimStateMachine;
51class DynamicBoneSolver;
52class FootIKSolver;
53class Tween;
54}
55
56namespace eve::avatar {
57
58class VrmRuntime;
59
62 Applied,
64 Removed,
66};
67
70 Unavailable = 0,
71 Gpu = 1,
72 Cpu = 2,
73};
74
77public:
79 virtual ~ILive2DBackend() = default;
81 virtual std::string getName() const = 0;
83 virtual bool isRuntimeAvailable() const { return true; }
85 virtual bool loadModel(const std::string &path) = 0;
87 virtual void update(float dt) = 0;
89 virtual void setParameter(const std::string &name, float value) = 0;
91 virtual float getParameter(const std::string &name) const = 0;
93 virtual bool setExpression(const std::string &name) = 0;
95 virtual bool setMotion(const std::string &name) = 0;
97 virtual void setTransform(float /*x*/, float /*y*/, float /*sx*/, float /*sy*/) {}
99 virtual void setVisible(bool /*visible*/) {}
101 virtual void setLayer(int /*layer*/) {}
103 virtual void collectDrawItems(std::vector<graphics::DrawItem2D> & /*out*/) {}
104};
105
107
114public:
116 using DestroyHook = std::function<void(AvatarInstance *)>;
117
119 explicit AvatarInstance(std::string kind);
122
125
127 std::string getKind() const { return kind_; }
128
130 void setPosition(float x, float y);
132 float getX() const { return x_; }
134 float getY() const { return y_; }
135
137 void setScale(float sx, float sy);
139 float getScaleX() const { return sx_; }
141 float getScaleY() const { return sy_; }
142
144 void setVisible(bool visible);
146 bool isVisible() const { return visible_; }
147
149 void setLayer(int layer);
151 int getLayer() const { return layer_; }
152
154 void setExpression(const std::string &name);
156 std::string getExpression() const { return expression_; }
157
159 void setMotion(const std::string &name);
161 std::string getMotion() const { return motion_; }
162
164 void setParameter(const std::string &name, float value);
166 float getParameter(const std::string &name) const;
168 bool hasParameter(const std::string &name) const;
170 int getParameterCount() const;
172 std::string getParameterName(int index) const;
174 bool defineParameter(const std::string& name, float defaultValue, float minimum, float maximum);
176 float getParameterDefault(const std::string& name) const;
178 float getParameterMinimum(const std::string& name) const;
180 float getParameterMaximum(const std::string& name) const;
181
183 void update(float dt);
185 void sync();
186
188 void release();
189
191 size_t addDestroyHook(DestroyHook hook);
193 void removeDestroyHook(size_t id);
194
195 // ---- image kind ----
197 bool addLayer(const std::string &name, graphics::Texture *texture, int zIndex);
199 bool setLayerTexture(const std::string &name, graphics::Texture *texture);
201 bool setLayerVisible(const std::string &name, bool visible);
203 bool setLayerOffset(const std::string &name, float ox, float oy);
205 bool setLayerColor(const std::string &name, float r, float g, float b, float a);
207 bool setLayerZ(const std::string &name, int zIndex);
209 bool setLayerSize(const std::string &name, float w, float h);
211 int getLayerCount() const;
213 std::string getLayerName(int index) const;
215 bool hasLayer(const std::string &name) const;
217 graphics::Renderable2D* getLayerRenderable(const std::string& name);
219 bool defineExpression(const std::string &name, const std::string &spec);
221 bool removeExpression(const std::string& name);
223 int getExpressionCount() const;
225 std::string getExpressionName(int index) const;
227 bool applyExpression(const std::string &name);
229 bool transitionExpression(const std::string& name, float duration);
230
231 // ---- live2d kind ----
233 bool loadLive2DModel(const std::string &path);
235 std::string getLive2DBackendName() const;
237 bool hasLive2DBackend() const;
239 void collectLive2DDrawItems(std::vector<graphics::DrawItem2D>& out);
240
241 // ---- vroid kind ----
250 [[nodiscard]] eve::Result<void> loadVroidModel(std::string_view path);
252 bool loadVroidModelPath(const std::string &path);
254 int getVroidMeshCount() const;
256 int getVroidSpringCount() const;
258 std::string getVroidVersion() const;
269 [[nodiscard]] eve::Result<void> setHumanoidBoneRotation(std::string_view semantic, float yaw, float pitch,
270 float roll);
272 bool bindVroidModelData(model3d::ModelData *data);
274 int loadMorphNamesFromModel(int meshIndex = 0);
276 void setMesh(graphics::Mesh *mesh);
278 void setTexture(graphics::Texture *texture);
280 void setPosition3D(float x, float y, float z);
282 void setRotation3D(float yaw, float pitch, float roll);
284 void setScale3D(float sx, float sy, float sz);
290 graphics::Renderable3D* getRenderable3D() const;
297 graphics::Mesh *getBoundMesh() const;
299 std::string getVroidModelPath() const { return vroidPath_; }
301 bool bakeMorphs();
302
303 // ---- skeletal animation ----
305 bool bindAnimPlayer(animation::AnimPlayer* player);
307 bool bindAnimStateMachine(animation::AnimStateMachine* machine);
309 bool bindAnimLayerMixer(animation::AnimLayerMixer* mixer);
311 void setAnimConstraintStack(animation::AnimConstraintStack* stack);
317 animation::AnimConstraintStack* getAnimConstraintStack() const { return animConstraintStack_.get(); }
319 void setFootIKSolver(animation::FootIKSolver* solver);
325 animation::FootIKSolver* getFootIKSolver() const { return footIKSolver_.get(); }
331 void setDynamicBoneSolver(animation::DynamicBoneSolver* solver);
337 animation::DynamicBoneSolver* getDynamicBoneSolver() const { return dynamicBoneSolver_.get(); }
339 bool bindAnimSkin(animation::AnimSkin* skin);
345 EquipmentVisualChange bindSkinnedPart(int partIndex, const std::string& partName,
347 animation::AnimSkin* skin);
349 EquipmentVisualChange unbindSkinnedPart(int partIndex);
351 SkinnedPartUpdateMode getSkinnedPartUpdateMode(int partIndex) const;
353 bool registerMotion(const std::string& name, animation::AnimClip* clip);
355 bool unregisterMotion(const std::string& name);
357 int getMotionCount() const;
359 std::string getMotionName(int index) const;
361 animation::AnimClip* getMotionClip(const std::string& name) const;
363 void setMotionBlendTime(float seconds);
365 void setApplyRootMotion(bool enabled);
367 bool getApplyRootMotion() const { return applyRootMotion_; }
369 float getRootMotionDeltaX() const { return rootMotionDeltaX_; }
371 float getRootMotionDeltaZ() const { return rootMotionDeltaZ_; }
373 int getAnimationEventCount() const;
375 std::string getAnimationEventLayer(int index) const;
377 std::string getAnimationEventName(int index) const;
379 std::string getAnimationEventPayload(int index) const;
381 [[nodiscard]] std::size_t animationEventCount() const noexcept override;
383 [[nodiscard]] std::string animationEventName(std::size_t index) const override;
385 bool mapHumanoidBone(const std::string& semantic, const std::string& boneName);
387 int autoMapHumanoidBones();
389 std::string getHumanoidBoneName(const std::string& semantic) const;
391 bool mapViseme(const std::string& viseme, const std::string& morphName);
393 bool setViseme(const std::string& viseme, float weight);
395 bool setLookAtTarget(float x, float y, float z);
397 void setLookAtWeight(float weight);
399 void clearLookAtTarget();
413 bool attachToBone(const std::string& name, const std::string& boneSemanticOrName,
414 graphics::Renderable3D* renderable, float ox = 0.f, float oy = 0.f, float oz = 0.f);
416 bool detachAttachment(const std::string& name);
418 int getAttachmentCount() const { return static_cast<int>(attachments_.size()); }
420 [[nodiscard]] eve::Result<eve::AttachmentPoint> sampleAttachmentPoint(
421 std::string_view name, eve::AttachmentPoint localOffset = {}) const override;
423 bool linkSceneNode(scene::Scene* scene, const std::string& nodeId);
425 bool isSceneLinked() const { return sceneLinked_; }
426
427 // ---- equipment appearance projection ----
434 EquipmentVisualChange bindEquipment(inventory::EquipmentSet* equipment);
436 EquipmentVisualChange unbindEquipment();
441 EquipmentVisualChange defineEquipmentVisual2D(const std::string& itemId,
442 const std::string& equipmentSlot,
443 const std::string& layerName,
444 graphics::Texture* texture, int zIndex);
451 EquipmentVisualChange defineEquipmentVisual3D(const std::string& itemId,
452 const std::string& equipmentSlot,
453 graphics::Renderable3D* renderable,
454 const std::string& boneSemanticOrName,
455 float ox = 0.f, float oy = 0.f, float oz = 0.f);
462 EquipmentVisualChange defineEquipmentSkinnedVisual3D(
463 const std::string& itemId, const std::string& equipmentSlot, int partIndex,
464 const std::string& partName, graphics::Mesh* mesh, graphics::Material* material,
465 animation::AnimSkin* skin);
467 EquipmentVisualChange defineEquipmentLayer(const std::string& name, int order,
468 const std::string& parent = {});
470 EquipmentVisualChange addEquipmentLayerOcclusion(const std::string& outerLayer,
471 const std::string& innerLayer);
473 EquipmentVisualChange setEquipmentVisualLayer(const std::string& itemId,
474 const std::string& equipmentSlot,
475 const std::string& wearLayer,
476 int withinLayerOrder = 0);
478 EquipmentVisualChange syncEquipmentAppearance();
480 std::string getEquipmentVisualItem(const std::string& equipmentSlot) const;
482 int getEquipmentRenderStackCount() const;
484 std::string getEquipmentRenderStackItem(int index) const;
486 std::string getEquipmentRenderStackLayer(int index) const;
487
488 // ---- animation tween binding ----
490 void bindTween(animation::Tween *tween);
492 void unbindTween();
494 animation::Tween *getBoundTween() const { return tween_; }
495
496private:
497 struct Layer {
498 std::string name;
500 int zIndex = 0;
501 bool visible = true;
502 bool autoSize = true;
503 float ox = 0.f, oy = 0.f;
504 float appliedParentRotation = 0.f;
505 };
506
507 Layer *findLayer(const std::string &name);
508 const Layer *findLayer(const std::string &name) const;
509 void syncImageLayers();
510 void syncVroid();
511 void destroyLayers();
512 void destroyVroid();
513 bool applyExpressionSpec(const std::string &spec);
514
515 std::string kind_;
516 float x_ = 0.f, y_ = 0.f;
517 float sx_ = 1.f, sy_ = 1.f;
518 bool visible_ = true;
519 int layer_ = 100;
520 std::string expression_;
521 std::string motion_;
522 std::unordered_map<std::string, float> parameters_;
523 std::vector<std::string> parameterOrder_;
524 struct ParameterMetadata {
525 float defaultValue = 0.f;
526 float minimum = 0.f;
527 float maximum = 1.f;
528 };
529 std::unordered_map<std::string, ParameterMetadata> parameterMetadata_;
530
531 // image
532 std::vector<Layer> layers_;
533 std::unordered_map<std::string, std::string> expressionDefs_;
534 std::unordered_map<std::string, float> expressionBlendFrom_;
535 std::unordered_map<std::string, float> expressionBlendTo_;
536 float expressionBlendElapsed_ = 0.f;
537 float expressionBlendDuration_ = 0.f;
538 std::vector<std::pair<size_t, DestroyHook>> destroyHooks_;
539 size_t nextDestroyHookId_ = 0;
540
541 // live2d
542 ILive2DBackend *live2d_ = nullptr;
543
544 // vroid
545 std::unique_ptr<VrmRuntime> vrm_;
546 std::string vroidPath_;
547 model3d::ModelData *vroidData_ = nullptr;
548 graphics::Renderable3D *renderable3d_ = nullptr;
549 graphics::Renderable2D* sceneAnchor2d_ = nullptr;
550 graphics::Mesh *boundMesh_ = nullptr;
551 float x3_ = 0.f, y3_ = 0.f, z3_ = 0.f;
552 float yaw_ = 0.f, pitch_ = 0.f, roll_ = 0.f;
553 float sx3_ = 1.f, sy3_ = 1.f, sz3_ = 1.f;
554
555 animation::Tween *tween_ = nullptr;
556 animation::AnimPlayer* animPlayer_ = nullptr;
557 animation::AnimLayerMixer* animLayerMixer_ = nullptr;
558 animation::AnimStateMachine* animStateMachine_ = nullptr;
559 std::unique_ptr<animation::AnimConstraintStack> animConstraintStack_;
560 std::unique_ptr<animation::FootIKSolver> footIKSolver_;
561 std::unique_ptr<animation::DynamicBoneSolver> dynamicBoneSolver_;
562 animation::AnimSkin* animSkin_ = nullptr;
563 animation::AnimSkeleton* animSkeleton_ = nullptr;
564 std::vector<std::array<float, 16>> attachmentPoseMatrices_;
565 std::unordered_map<std::string, animation::AnimClip*> motions_;
566 std::unordered_map<std::string, std::string> humanoidBones_;
567 std::unordered_map<std::string, std::string> visemeMorphs_;
568 std::string activeViseme_;
569 struct Attachment {
570 std::string name;
571 std::string boneName;
572 graphics::Renderable3D* renderable = nullptr;
573 float ox = 0.f, oy = 0.f, oz = 0.f;
574 };
575 std::vector<Attachment> attachments_;
576 struct SkinnedPart {
577 int partIndex = -1;
578 std::string partName;
579 graphics::Mesh* mesh = nullptr;
580 graphics::Material* material = nullptr;
581 animation::AnimSkin* skin = nullptr;
582 std::vector<float> cpuPositions;
583 bool active = true;
584 SkinnedPartUpdateMode updateMode = SkinnedPartUpdateMode::Unavailable;
585 };
586 std::vector<SkinnedPart> skinnedParts_;
587 struct EquipmentVisual {
588 std::string itemId;
589 std::string equipmentSlot;
590 std::string layerName;
591 graphics::Texture* texture = nullptr;
592 graphics::Renderable3D* renderable = nullptr;
593 graphics::Mesh* skinnedMesh = nullptr;
594 graphics::Material* skinnedMaterial = nullptr;
595 animation::AnimSkin* skinnedSkin = nullptr;
596 int partIndex = -1;
597 std::vector<float> cpuPositions;
598 SkinnedPartUpdateMode updateMode = SkinnedPartUpdateMode::Unavailable;
599 std::string boneName;
600 std::string wearLayer;
601 int zIndex = 0;
602 int withinLayerOrder = 0;
603 float ox = 0.f, oy = 0.f, oz = 0.f;
604 };
605 inventory::EquipmentSet* equipment_ = nullptr;
606 std::vector<EquipmentVisual> equipmentVisuals_;
607 std::unordered_map<std::string, std::string> projectedEquipment_;
608 std::string equipmentSignature_;
609 struct EquipmentLayer {
610 std::string parent;
611 int order = 0;
612 std::vector<std::string> occludes;
613 };
614 struct EquipmentRenderEntry {
615 std::string itemId;
616 std::string wearLayer;
617 int order = 0;
618 };
619 std::unordered_map<std::string, EquipmentLayer> equipmentLayers_;
620 std::vector<EquipmentRenderEntry> equipmentRenderStack_;
621 float lookAtX_ = 0.f, lookAtY_ = 0.f, lookAtZ_ = 1.f;
622 float lookAtWeight_ = 1.f;
623 bool lookAtEnabled_ = false;
624 bool lookAtApplied_ = false;
625 float lookAtBaseQx_ = 0.f, lookAtBaseQy_ = 0.f, lookAtBaseQz_ = 0.f, lookAtBaseQw_ = 1.f;
626 float lookAtPostQx_ = 0.f, lookAtPostQy_ = 0.f, lookAtPostQz_ = 0.f, lookAtPostQw_ = 1.f;
627 std::vector<float> skinnedPositions_;
628 float motionBlendTime_ = 0.2f;
629 float rootMotionDeltaX_ = 0.f, rootMotionDeltaZ_ = 0.f;
630 float previousRootX_ = 0.f, previousRootZ_ = 0.f;
631 bool applyRootMotion_ = false;
632 bool hasPreviousRoot_ = false;
633 int rootMotionLoopCount_ = 0;
634 bool sceneLinked_ = false;
635
636 void ensureParameter(const std::string &name, float value = 0.f);
637 void applyTweenTracks();
638 void syncMorphWeightsToMesh();
639 animation::AnimPose* updateSkeletalAnimation(float dt);
640 void updateRootMotion(animation::AnimPose* pose);
641 void updateSkin(animation::AnimPose* pose);
642 SkinnedPartUpdateMode updateSkinnedMesh(animation::AnimSkin* skin, graphics::Mesh* mesh,
643 animation::AnimPose* pose,
644 std::vector<float>& cpuPositions);
645 void updateAttachments(animation::AnimPose* pose);
646 void applyLookAt(animation::AnimPose* pose);
647 void updateExpressionTransition(float dt);
648 void hideEquipmentVisuals();
649 int equipmentLayerOrder(const std::string& name) const;
650
651 bool released_ = false;
652};
653
654} // namespace eve::avatar
double z
double y
double x
LogicalId itemId
bool & active
float w
Definition AnimClip.cpp:738
float duration
eve::EntitySpatialPose pose
float yaw
Definition BushMesh.cpp:37
std::string nodeId
std::string name
Definition ECS.cpp:154
std::uint64_t localOffset
#define EVENGINE_API_DOMAINS
Definition Export.h:110
float maximum[3]
float minimum[3]
tensor::Graph g
Definition GpuGraph.cpp:7
glm::vec4 clip
double r
int h
const ColourScope * parent
double defaultValue
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::vector< std::int32_t > order
OceanMeshSettings mesh
double dt
std::array< uint32_t, 16 > material
Texture * texture
TileLayer * layer
float f
std::string path
Definition PlayHost.cpp:111
std::weak_ptr< PrimitiveScene > scene
Object::Ptr value
const EntityFact * entity
uint32_t semantic
Graphics * graphics
ScriptStructure out
std::string kind
Share string attribute map operations through the Windows Foundation DLL.
bool visible
double weight
uint32_t index
double oy
double ox
Read-only cross-module view of animation events emitted by the latest simulation update....
Read-only cross-module contract for sampling named animated attachment points. @ownership Implementat...
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Keyframed skeletal animation clip (local TRS tracks per bone). Script type: AnimClip.
Definition AnimClip.h:145
Ordered main-thread post-process stack with aim, limb IK and foot planting.
Combines a borrowed base pose source with ordered override/additive layers.
Single-clip (or cross-fading) 3D animation player. Script type: AnimPlayer.
Definition AnimPlayer.h:33
CPU linear-blend skinning binding for one mesh against an AnimSkeleton.
Definition AnimSkin.h:47
Clip-driven animation state machine with float/bool/trigger parameters and cross-fade transitions....
World-space spring-chain solver that writes results as local bone rotations.
Paired-foot IK with contact smoothing and pelvis compensation.
Property tween — interpolates named float properties from → to over time.
Definition Tween.h:25
Unified avatar instance. Kind is a string: "image" | "live2d" | "vroid". Script-facing API avoids ove...
float getScaleX() const
Returns the scale x.
bool isVisible() const
True when visible.
std::string getMotion() const
Returns the motion.
float getX() const
Returns the x.
AvatarInstance & operator=(const AvatarInstance &)=delete
std::string getVroidModelPath() const
Returns the vroid model path.
std::string getExpression() const
Returns the expression.
animation::DynamicBoneSolver * getDynamicBoneSolver() const
Return the Avatar-owned solver, or null. @ownership Borrowed from this Avatar. @lifetime Valid until ...
std::function< void(AvatarInstance *)> DestroyHook
Callback fired exactly once, when the instance is destroyed.
animation::AnimConstraintStack * getAnimConstraintStack() const
Return the Avatar-owned constraint stack, or null. @ownership Borrowed from this Avatar....
float getRootMotionDeltaZ() const
Last evaluated root-motion Z delta.
std::string getKind() const
Returns the kind.
bool getApplyRootMotion() const
Return whether root motion is applied to the Avatar transform.
float getY() const
Returns the y.
float getScaleY() const
Returns the scale y.
float getRootMotionDeltaX() const
Last evaluated root-motion X delta.
AvatarInstance(const AvatarInstance &)=delete
animation::Tween * getBoundTween() const
Returns the bound tween.
bool isSceneLinked() const
Return whether this avatar is currently scene-driven.
animation::FootIKSolver * getFootIKSolver() const
Return the Avatar-owned paired-foot IK solver, or null. @ownership Borrowed from this Avatar....
int getLayer() const
Returns the layer.
Optional Live2D runtime backend (Cubism etc.). Registered from C++ / plugins.
virtual void setParameter(const std::string &name, float value)=0
Sets the parameter.
virtual void update(float dt)=0
Updates .
virtual void collectDrawItems(std::vector< graphics::DrawItem2D > &)
Optional: push drawables into the 2D queue. Default no-op.
virtual std::string getName() const =0
Returns the name.
virtual ~ILive2DBackend()=default
Releases ILive2DBackend resources.
virtual bool setMotion(const std::string &name)=0
Sets the motion.
virtual bool isRuntimeAvailable() const
True only for a backend that can render a real Live2D model.
virtual float getParameter(const std::string &name) const =0
Returns the parameter.
virtual bool loadModel(const std::string &path)=0
Loads model.
virtual void setVisible(bool)
Optional: receive the Avatar's effective visibility.
virtual void setLayer(int)
Optional: receive the Avatar's render sorting layer.
virtual bool setExpression(const std::string &name)=0
Sets the expression.
virtual void setTransform(float, float, float, float)
Optional: receive the Avatar's 2D transform.
Packages shading method + surface parameters into one attachable asset.
Definition Material.h:38
GPU mesh handle (+ optional CPU morph targets).
Definition Mesh.h:26
Default renderable entity for declarative 2D sprites / solid quads.
EVENGINE_API_BACKENDS public API.
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:15
Represents raw pixel data.
Definition ImageData.h:38
EVENGINE_API_FOUNDATION public API.
Definition Equipment.h:19
CPU-side decoded 3D model (Assimp scene owned via medialoader::ModelScene). Does not upload to GPU — ...
Definition ModelData.h:39
Declarative scene module (eve.Scene).
Definition Scene.h:52
ILive2DBackend *(*)() Live2DBackendFactory
EquipmentVisualChange
Outcome of changing or rebuilding the equipment appearance projection.
SkinnedPartUpdateMode
Observable backend selected for a shared-pose skinned part.
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
运行时容器:固定格数的背包 / 箱子 / 商店栏等。 行为由 InventorySystem 提供;本类暴露便于脚本绑定的薄封装方法。
bool enabled
Plain world-space point shared by animation consumers without a math-library dependency.