载入中...
搜索中...
未找到
AvatarInstance.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
7
8#include <array>
9#include <functional>
10#include <memory>
11#include <string>
12#include <unordered_map>
13#include <utility>
14#include <vector>
15
16namespace eve::graphics {
17struct DrawItem2D;
18class Mesh;
19class Material;
20class Renderable2D;
21class Renderable3D;
22class Texture;
23} // namespace eve::graphics
24
25namespace eve::image {
26class ImageData;
27}
28
29namespace eve::model3d {
30class ModelData;
31}
32
33namespace eve::scene {
34class Scene;
35}
36
37namespace eve::inventory {
38class EquipmentSet;
39}
40
41namespace eve::animation {
42class AnimClip;
43class AnimPlayer;
44class AnimLayerMixer;
45class AnimConstraintStack;
46class AnimPose;
47class AnimSkin;
48class AnimSkeleton;
49class AnimStateMachine;
50class DynamicBoneSolver;
51class FootIKSolver;
52class Tween;
53}
54
55namespace eve::avatar {
56
57class VrmRuntime;
58
61 Applied,
63 Removed,
65};
66
69 Unavailable = 0,
70 Gpu = 1,
71 Cpu = 2,
72};
73
76public:
78 virtual ~ILive2DBackend() = default;
80 virtual std::string getName() const = 0;
82 virtual bool isRuntimeAvailable() const { return true; }
84 virtual bool loadModel(const std::string &path) = 0;
86 virtual void update(float dt) = 0;
88 virtual void setParameter(const std::string &name, float value) = 0;
90 virtual float getParameter(const std::string &name) const = 0;
92 virtual bool setExpression(const std::string &name) = 0;
94 virtual bool setMotion(const std::string &name) = 0;
96 virtual void setTransform(float /*x*/, float /*y*/, float /*sx*/, float /*sy*/) {}
98 virtual void setVisible(bool /*visible*/) {}
100 virtual void setLayer(int /*layer*/) {}
102 virtual void collectDrawItems(std::vector<graphics::DrawItem2D> & /*out*/) {}
103};
104
106
113public:
115 using DestroyHook = std::function<void(AvatarInstance *)>;
116
118 explicit AvatarInstance(std::string kind);
121
124
126 std::string getKind() const { return kind_; }
127
129 void setPosition(float x, float y);
131 float getX() const { return x_; }
133 float getY() const { return y_; }
134
136 void setScale(float sx, float sy);
138 float getScaleX() const { return sx_; }
140 float getScaleY() const { return sy_; }
141
143 void setVisible(bool visible);
145 bool isVisible() const { return visible_; }
146
148 void setLayer(int layer);
150 int getLayer() const { return layer_; }
151
153 void setExpression(const std::string &name);
155 std::string getExpression() const { return expression_; }
156
158 void setMotion(const std::string &name);
160 std::string getMotion() const { return motion_; }
161
163 void setParameter(const std::string &name, float value);
165 float getParameter(const std::string &name) const;
167 bool hasParameter(const std::string &name) const;
169 int getParameterCount() const;
171 std::string getParameterName(int index) const;
173 bool defineParameter(const std::string& name, float defaultValue, float minimum, float maximum);
175 float getParameterDefault(const std::string& name) const;
177 float getParameterMinimum(const std::string& name) const;
179 float getParameterMaximum(const std::string& name) const;
180
182 void update(float dt);
184 void sync();
185
187 void release();
188
190 size_t addDestroyHook(DestroyHook hook);
192 void removeDestroyHook(size_t id);
193
194 // ---- image kind ----
196 bool addLayer(const std::string &name, graphics::Texture *texture, int zIndex);
198 bool setLayerTexture(const std::string &name, graphics::Texture *texture);
200 bool setLayerVisible(const std::string &name, bool visible);
202 bool setLayerOffset(const std::string &name, float ox, float oy);
204 bool setLayerColor(const std::string &name, float r, float g, float b, float a);
206 bool setLayerZ(const std::string &name, int zIndex);
208 bool setLayerSize(const std::string &name, float w, float h);
210 int getLayerCount() const;
212 std::string getLayerName(int index) const;
214 bool hasLayer(const std::string &name) const;
216 graphics::Renderable2D* getLayerRenderable(const std::string& name);
218 bool defineExpression(const std::string &name, const std::string &spec);
220 bool removeExpression(const std::string& name);
222 int getExpressionCount() const;
224 std::string getExpressionName(int index) const;
226 bool applyExpression(const std::string &name);
228 bool transitionExpression(const std::string& name, float duration);
229
230 // ---- live2d kind ----
232 bool loadLive2DModel(const std::string &path);
234 std::string getLive2DBackendName() const;
236 bool hasLive2DBackend() const;
238 void collectLive2DDrawItems(std::vector<graphics::DrawItem2D>& out);
239
240 // ---- vroid kind ----
249 [[nodiscard]] eve::Result<void> loadVroidModel(std::string_view path);
251 bool loadVroidModelPath(const std::string &path);
253 int getVroidMeshCount() const;
255 int getVroidSpringCount() const;
257 std::string getVroidVersion() const;
268 [[nodiscard]] eve::Result<void> setHumanoidBoneRotation(std::string_view semantic, float yaw, float pitch,
269 float roll);
271 bool bindVroidModelData(model3d::ModelData *data);
273 int loadMorphNamesFromModel(int meshIndex = 0);
275 void setMesh(graphics::Mesh *mesh);
277 void setTexture(graphics::Texture *texture);
279 void setPosition3D(float x, float y, float z);
281 void setRotation3D(float yaw, float pitch, float roll);
283 void setScale3D(float sx, float sy, float sz);
289 graphics::Renderable3D* getRenderable3D() const;
296 graphics::Mesh *getBoundMesh() const;
298 std::string getVroidModelPath() const { return vroidPath_; }
300 bool bakeMorphs();
301
302 // ---- skeletal animation ----
304 bool bindAnimPlayer(animation::AnimPlayer* player);
306 bool bindAnimStateMachine(animation::AnimStateMachine* machine);
308 bool bindAnimLayerMixer(animation::AnimLayerMixer* mixer);
310 void setAnimConstraintStack(animation::AnimConstraintStack* stack);
316 animation::AnimConstraintStack* getAnimConstraintStack() const { return animConstraintStack_.get(); }
318 void setFootIKSolver(animation::FootIKSolver* solver);
324 animation::FootIKSolver* getFootIKSolver() const { return footIKSolver_.get(); }
330 void setDynamicBoneSolver(animation::DynamicBoneSolver* solver);
336 animation::DynamicBoneSolver* getDynamicBoneSolver() const { return dynamicBoneSolver_.get(); }
338 bool bindAnimSkin(animation::AnimSkin* skin);
344 EquipmentVisualChange bindSkinnedPart(int partIndex, const std::string& partName,
346 animation::AnimSkin* skin);
348 EquipmentVisualChange unbindSkinnedPart(int partIndex);
350 SkinnedPartUpdateMode getSkinnedPartUpdateMode(int partIndex) const;
352 bool registerMotion(const std::string& name, animation::AnimClip* clip);
354 bool unregisterMotion(const std::string& name);
356 int getMotionCount() const;
358 std::string getMotionName(int index) const;
360 animation::AnimClip* getMotionClip(const std::string& name) const;
362 void setMotionBlendTime(float seconds);
364 void setApplyRootMotion(bool enabled);
366 bool getApplyRootMotion() const { return applyRootMotion_; }
368 float getRootMotionDeltaX() const { return rootMotionDeltaX_; }
370 float getRootMotionDeltaZ() const { return rootMotionDeltaZ_; }
372 int getAnimationEventCount() const;
374 std::string getAnimationEventLayer(int index) const;
376 std::string getAnimationEventName(int index) const;
378 std::string getAnimationEventPayload(int index) const;
380 [[nodiscard]] std::size_t animationEventCount() const noexcept override;
382 [[nodiscard]] std::string animationEventName(std::size_t index) const override;
384 bool mapHumanoidBone(const std::string& semantic, const std::string& boneName);
386 int autoMapHumanoidBones();
388 std::string getHumanoidBoneName(const std::string& semantic) const;
390 bool mapViseme(const std::string& viseme, const std::string& morphName);
392 bool setViseme(const std::string& viseme, float weight);
394 bool setLookAtTarget(float x, float y, float z);
396 void setLookAtWeight(float weight);
398 void clearLookAtTarget();
412 bool attachToBone(const std::string& name, const std::string& boneSemanticOrName,
413 graphics::Renderable3D* renderable, float ox = 0.f, float oy = 0.f, float oz = 0.f);
415 bool detachAttachment(const std::string& name);
417 int getAttachmentCount() const { return static_cast<int>(attachments_.size()); }
419 [[nodiscard]] eve::Result<eve::AttachmentPoint> sampleAttachmentPoint(
420 std::string_view name, eve::AttachmentPoint localOffset = {}) const override;
422 bool linkSceneNode(scene::Scene* scene, const std::string& nodeId);
424 bool isSceneLinked() const { return sceneLinked_; }
425
426 // ---- equipment appearance projection ----
433 EquipmentVisualChange bindEquipment(inventory::EquipmentSet* equipment);
435 EquipmentVisualChange unbindEquipment();
440 EquipmentVisualChange defineEquipmentVisual2D(const std::string& itemId,
441 const std::string& equipmentSlot,
442 const std::string& layerName,
443 graphics::Texture* texture, int zIndex);
450 EquipmentVisualChange defineEquipmentVisual3D(const std::string& itemId,
451 const std::string& equipmentSlot,
452 graphics::Renderable3D* renderable,
453 const std::string& boneSemanticOrName,
454 float ox = 0.f, float oy = 0.f, float oz = 0.f);
461 EquipmentVisualChange defineEquipmentSkinnedVisual3D(
462 const std::string& itemId, const std::string& equipmentSlot, int partIndex,
463 const std::string& partName, graphics::Mesh* mesh, graphics::Material* material,
464 animation::AnimSkin* skin);
466 EquipmentVisualChange defineEquipmentLayer(const std::string& name, int order,
467 const std::string& parent = {});
469 EquipmentVisualChange addEquipmentLayerOcclusion(const std::string& outerLayer,
470 const std::string& innerLayer);
472 EquipmentVisualChange setEquipmentVisualLayer(const std::string& itemId,
473 const std::string& equipmentSlot,
474 const std::string& wearLayer,
475 int withinLayerOrder = 0);
477 EquipmentVisualChange syncEquipmentAppearance();
479 std::string getEquipmentVisualItem(const std::string& equipmentSlot) const;
481 int getEquipmentRenderStackCount() const;
483 std::string getEquipmentRenderStackItem(int index) const;
485 std::string getEquipmentRenderStackLayer(int index) const;
486
487 // ---- animation tween binding ----
489 void bindTween(animation::Tween *tween);
491 void unbindTween();
493 animation::Tween *getBoundTween() const { return tween_; }
494
495private:
496 struct Layer {
497 std::string name;
498 graphics::Renderable2D *entity = nullptr;
499 int zIndex = 0;
500 bool visible = true;
501 bool autoSize = true;
502 float ox = 0.f, oy = 0.f;
503 float appliedParentRotation = 0.f;
504 };
505
506 Layer *findLayer(const std::string &name);
507 const Layer *findLayer(const std::string &name) const;
508 void syncImageLayers();
509 void syncVroid();
510 void destroyLayers();
511 void destroyVroid();
512 bool applyExpressionSpec(const std::string &spec);
513
514 std::string kind_;
515 float x_ = 0.f, y_ = 0.f;
516 float sx_ = 1.f, sy_ = 1.f;
517 bool visible_ = true;
518 int layer_ = 100;
519 std::string expression_;
520 std::string motion_;
521 std::unordered_map<std::string, float> parameters_;
522 std::vector<std::string> parameterOrder_;
523 struct ParameterMetadata {
524 float defaultValue = 0.f;
525 float minimum = 0.f;
526 float maximum = 1.f;
527 };
528 std::unordered_map<std::string, ParameterMetadata> parameterMetadata_;
529
530 // image
531 std::vector<Layer> layers_;
532 std::unordered_map<std::string, std::string> expressionDefs_;
533 std::unordered_map<std::string, float> expressionBlendFrom_;
534 std::unordered_map<std::string, float> expressionBlendTo_;
535 float expressionBlendElapsed_ = 0.f;
536 float expressionBlendDuration_ = 0.f;
537 std::vector<std::pair<size_t, DestroyHook>> destroyHooks_;
538 size_t nextDestroyHookId_ = 0;
539
540 // live2d
541 ILive2DBackend *live2d_ = nullptr;
542
543 // vroid
544 std::unique_ptr<VrmRuntime> vrm_;
545 std::string vroidPath_;
546 model3d::ModelData *vroidData_ = nullptr;
547 graphics::Renderable3D *renderable3d_ = nullptr;
548 graphics::Renderable2D* sceneAnchor2d_ = nullptr;
549 graphics::Mesh *boundMesh_ = nullptr;
550 float x3_ = 0.f, y3_ = 0.f, z3_ = 0.f;
551 float yaw_ = 0.f, pitch_ = 0.f, roll_ = 0.f;
552 float sx3_ = 1.f, sy3_ = 1.f, sz3_ = 1.f;
553
554 animation::Tween *tween_ = nullptr;
555 animation::AnimPlayer* animPlayer_ = nullptr;
556 animation::AnimLayerMixer* animLayerMixer_ = nullptr;
557 animation::AnimStateMachine* animStateMachine_ = nullptr;
558 std::unique_ptr<animation::AnimConstraintStack> animConstraintStack_;
559 std::unique_ptr<animation::FootIKSolver> footIKSolver_;
560 std::unique_ptr<animation::DynamicBoneSolver> dynamicBoneSolver_;
561 animation::AnimSkin* animSkin_ = nullptr;
562 animation::AnimSkeleton* animSkeleton_ = nullptr;
563 std::vector<std::array<float, 16>> attachmentPoseMatrices_;
564 std::unordered_map<std::string, animation::AnimClip*> motions_;
565 std::unordered_map<std::string, std::string> humanoidBones_;
566 std::unordered_map<std::string, std::string> visemeMorphs_;
567 std::string activeViseme_;
568 struct Attachment {
569 std::string name;
570 std::string boneName;
571 graphics::Renderable3D* renderable = nullptr;
572 float ox = 0.f, oy = 0.f, oz = 0.f;
573 };
574 std::vector<Attachment> attachments_;
575 struct SkinnedPart {
576 int partIndex = -1;
577 std::string partName;
578 graphics::Mesh* mesh = nullptr;
579 graphics::Material* material = nullptr;
580 animation::AnimSkin* skin = nullptr;
581 std::vector<float> cpuPositions;
582 bool active = true;
583 SkinnedPartUpdateMode updateMode = SkinnedPartUpdateMode::Unavailable;
584 };
585 std::vector<SkinnedPart> skinnedParts_;
586 struct EquipmentVisual {
587 std::string itemId;
588 std::string equipmentSlot;
589 std::string layerName;
590 graphics::Texture* texture = nullptr;
591 graphics::Renderable3D* renderable = nullptr;
592 graphics::Mesh* skinnedMesh = nullptr;
593 graphics::Material* skinnedMaterial = nullptr;
594 animation::AnimSkin* skinnedSkin = nullptr;
595 int partIndex = -1;
596 std::vector<float> cpuPositions;
597 SkinnedPartUpdateMode updateMode = SkinnedPartUpdateMode::Unavailable;
598 std::string boneName;
599 std::string wearLayer;
600 int zIndex = 0;
601 int withinLayerOrder = 0;
602 float ox = 0.f, oy = 0.f, oz = 0.f;
603 };
604 inventory::EquipmentSet* equipment_ = nullptr;
605 std::vector<EquipmentVisual> equipmentVisuals_;
606 std::unordered_map<std::string, std::string> projectedEquipment_;
607 std::string equipmentSignature_;
608 struct EquipmentLayer {
609 std::string parent;
610 int order = 0;
611 std::vector<std::string> occludes;
612 };
613 struct EquipmentRenderEntry {
614 std::string itemId;
615 std::string wearLayer;
616 int order = 0;
617 };
618 std::unordered_map<std::string, EquipmentLayer> equipmentLayers_;
619 std::vector<EquipmentRenderEntry> equipmentRenderStack_;
620 float lookAtX_ = 0.f, lookAtY_ = 0.f, lookAtZ_ = 1.f;
621 float lookAtWeight_ = 1.f;
622 bool lookAtEnabled_ = false;
623 bool lookAtApplied_ = false;
624 float lookAtBaseQx_ = 0.f, lookAtBaseQy_ = 0.f, lookAtBaseQz_ = 0.f, lookAtBaseQw_ = 1.f;
625 float lookAtPostQx_ = 0.f, lookAtPostQy_ = 0.f, lookAtPostQz_ = 0.f, lookAtPostQw_ = 1.f;
626 std::vector<float> skinnedPositions_;
627 float motionBlendTime_ = 0.2f;
628 float rootMotionDeltaX_ = 0.f, rootMotionDeltaZ_ = 0.f;
629 float previousRootX_ = 0.f, previousRootZ_ = 0.f;
630 bool applyRootMotion_ = false;
631 bool hasPreviousRoot_ = false;
632 int rootMotionLoopCount_ = 0;
633 bool sceneLinked_ = false;
634
635 void ensureParameter(const std::string &name, float value = 0.f);
636 void applyTweenTracks();
637 void syncMorphWeightsToMesh();
638 animation::AnimPose* updateSkeletalAnimation(float dt);
639 void updateRootMotion(animation::AnimPose* pose);
640 void updateSkin(animation::AnimPose* pose);
641 SkinnedPartUpdateMode updateSkinnedMesh(animation::AnimSkin* skin, graphics::Mesh* mesh,
642 animation::AnimPose* pose,
643 std::vector<float>& cpuPositions);
644 void updateAttachments(animation::AnimPose* pose);
645 void applyLookAt(animation::AnimPose* pose);
646 void updateExpressionTransition(float dt);
647 void hideEquipmentVisuals();
648 int equipmentLayerOrder(const std::string& name) const;
649
650 bool released_ = false;
651};
652
653} // namespace eve::avatar
double value
LogicalId itemId
bool & active
float w
Definition AnimClip.cpp:738
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
float duration
eve::EntitySpatialPose pose
std::string nodeId
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
TokenKind kind
std::int32_t parent
std::string name
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::vector< std::int32_t > order
TileLayer * layer
float f
std::string path
Definition PlayHost.cpp:110
std::weak_ptr< PrimitiveScene > scene
std::function< eve::Result< void >()> release
Definition Procgen.cpp:86
Mesh * mesh
Material * material
bool visible
uint32_t index
double oy
double ox
uint32_t semantic
Definition WfcSimple.cpp:15
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:155
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:30
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:35
GPU mesh handle (+ optional CPU morph targets).
Definition Mesh.h:25
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:18
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:51
const char * defaultValue
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.