载入中...
搜索中...
未找到
Avatar.cpp
浏览该文件的文档.
1#include "avatar/Avatar.h"
3#include "graphics/Graphics.h"
5
6#include <algorithm>
7#include <simplesquirrel/simplesquirrel.hpp>
8
9namespace eve::avatar {
10
11Live2DBackendFactory Avatar::live2dFactory_ = &createNullLive2DBackend;
12
14
16 // Instances may outlive the module if script GC still holds them; detach.
17 for (AvatarInstance *a : avatars_) {
18 (void)a;
19 }
20 avatars_.clear();
21}
22
23void Avatar::registerInstance(AvatarInstance *a) {
24 if (!a) return;
25 if (std::find(avatars_.begin(), avatars_.end(), a) == avatars_.end())
26 avatars_.push_back(a);
27}
28
29void Avatar::unregisterInstance(AvatarInstance *a) {
30 if (!a) return;
31 auto it = std::find(avatars_.begin(), avatars_.end(), a);
32 if (it != avatars_.end()) avatars_.erase(it);
33}
34
36
38
40
41void Avatar::update(float dt) {
42 std::vector<AvatarInstance *> snapshot = avatars_;
43 for (AvatarInstance *a : snapshot) {
44 if (a) a->update(dt);
45 }
46}
47
49 std::vector<AvatarInstance *> snapshot = avatars_;
50 for (AvatarInstance *a : snapshot) {
51 if (a) a->sync();
52 }
53}
54
56 sync();
57 if (!gfx) return;
58 std::vector<graphics::DrawItem2D> items;
60 graphics::RenderSystem::drawItems(*gfx, items, false);
61}
62
63int Avatar::getAvatarCount() const { return int(avatars_.size()); }
64
66 live2dFactory_ = factory ? factory : &createNullLive2DBackend;
67}
68
70
72 Live2DBackendFactory f = live2dFactory_ ? live2dFactory_ : &createNullLive2DBackend;
73 return f();
74}
75
78 if (!tmp) return "none";
79 std::string n = tmp->getName();
80 delete tmp;
81 return n;
82}
83
84void Avatar::expose(ssq::Table &table) {
85 auto cls = table.addClass(name, Avatar::create, false);
86 expose(cls);
87
88 auto av = table.addClass<AvatarInstance>(
89 "AvatarInstance",
90 std::function<AvatarInstance *()>([]() -> AvatarInstance * { return nullptr; }), true);
91
92 av.addFunc("getKind", &AvatarInstance::getKind);
93 av.addFunc("setPosition", &AvatarInstance::setPosition);
94 av.addFunc("getX", &AvatarInstance::getX);
95 av.addFunc("getY", &AvatarInstance::getY);
96 av.addFunc("setScale", &AvatarInstance::setScale);
97 av.addFunc("getScaleX", &AvatarInstance::getScaleX);
98 av.addFunc("getScaleY", &AvatarInstance::getScaleY);
99 av.addFunc("setVisible", &AvatarInstance::setVisible);
100 av.addFunc("isVisible", &AvatarInstance::isVisible);
101 av.addFunc("setLayer", &AvatarInstance::setLayer);
102 av.addFunc("getLayer", &AvatarInstance::getLayer);
103 av.addFunc("setExpression", &AvatarInstance::setExpression);
104 av.addFunc("getExpression", &AvatarInstance::getExpression);
105 av.addFunc("setMotion", &AvatarInstance::setMotion);
106 av.addFunc("getMotion", &AvatarInstance::getMotion);
107 av.addFunc("setParameter", &AvatarInstance::setParameter);
108 av.addFunc("getParameter", &AvatarInstance::getParameter);
109 av.addFunc("hasParameter", &AvatarInstance::hasParameter);
110 av.addFunc("getParameterCount", &AvatarInstance::getParameterCount);
111 av.addFunc("getParameterName", &AvatarInstance::getParameterName);
112 av.addFunc("update", &AvatarInstance::update);
113 av.addFunc("sync", &AvatarInstance::sync);
114 av.addFunc("release", &AvatarInstance::release);
115
116 // addLayer / setLayerTexture accept a nullable texture (colored placeholder
117 // layers). ssq's default pointer binding rejects Squirrel `null`, so accept
118 // ssq::Object and translate null -> nullptr.
119 av.addFunc("addLayer", [](AvatarInstance *a, const std::string &name, ssq::Object textureObj,
120 int zIndex) -> bool {
121 graphics::Texture *tex =
122 textureObj.isNull() ? nullptr : textureObj.toPtrUnsafe<graphics::Texture *>();
123 return a->addLayer(name, tex, zIndex);
124 });
125 av.addFunc("setLayerTexture", [](AvatarInstance *a, const std::string &name,
126 ssq::Object textureObj) -> bool {
127 graphics::Texture *tex =
128 textureObj.isNull() ? nullptr : textureObj.toPtrUnsafe<graphics::Texture *>();
129 return a->setLayerTexture(name, tex);
130 });
131 av.addFunc("setLayerVisible", &AvatarInstance::setLayerVisible);
132 av.addFunc("setLayerOffset", &AvatarInstance::setLayerOffset);
133 av.addFunc("setLayerColor", &AvatarInstance::setLayerColor);
134 av.addFunc("setLayerZ", &AvatarInstance::setLayerZ);
135 av.addFunc("setLayerSize", &AvatarInstance::setLayerSize);
136 av.addFunc("getLayerCount", &AvatarInstance::getLayerCount);
137 av.addFunc("getLayerName", &AvatarInstance::getLayerName);
138 av.addFunc("hasLayer", &AvatarInstance::hasLayer);
139 av.addFunc("defineExpression", &AvatarInstance::defineExpression);
140 av.addFunc("applyExpression", &AvatarInstance::applyExpression);
141
142 av.addFunc("loadLive2DModel", &AvatarInstance::loadLive2DModel);
143 av.addFunc("getLive2DBackendName", &AvatarInstance::getLive2DBackendName);
144 av.addFunc("hasLive2DBackend", &AvatarInstance::hasLive2DBackend);
145
146 av.addFunc("loadVroidModelPath", &AvatarInstance::loadVroidModelPath);
147 av.addFunc("bindVroidModelData", &AvatarInstance::bindVroidModelData);
148 av.addFunc("loadMorphNamesFromModel", &AvatarInstance::loadMorphNamesFromModel);
149 av.addFunc("setMesh", &AvatarInstance::setMesh);
150 av.addFunc("setTexture", &AvatarInstance::setTexture);
151 av.addFunc("setPosition3D", &AvatarInstance::setPosition3D);
152 av.addFunc("setRotation3D", &AvatarInstance::setRotation3D);
153 av.addFunc("setScale3D", &AvatarInstance::setScale3D);
154 av.addFunc("getRenderable3D", &AvatarInstance::getRenderable3D);
155 av.addFunc("getBoundMesh", &AvatarInstance::getBoundMesh);
156 av.addFunc("getVroidModelPath", &AvatarInstance::getVroidModelPath);
157 av.addFunc("bakeMorphs", &AvatarInstance::bakeMorphs);
158 av.addFunc("bindTween", &AvatarInstance::bindTween);
159 av.addFunc("unbindTween", &AvatarInstance::unbindTween);
160 av.addFunc("getBoundTween", &AvatarInstance::getBoundTween);
161}
162
163void Avatar::expose(ssq::Class &cls) {
164 cls.addFunc("getName", &Avatar::getName);
165 cls.addFunc("newImageAvatar", &Avatar::newImageAvatar);
166 cls.addFunc("newLive2DAvatar", &Avatar::newLive2DAvatar);
167 cls.addFunc("newVroidAvatar", &Avatar::newVroidAvatar);
168 cls.addFunc("update", &Avatar::update);
169 cls.addFunc("sync", &Avatar::sync);
170 cls.addFunc("render", &Avatar::render);
171 cls.addFunc("getAvatarCount", &Avatar::getAvatarCount);
172 // Static method: wrap so SSQ gets an instance-method signature (raw static
173 // function pointers make Class::addFunc recurse until stack overflow).
174 cls.addFunc("getLive2DBackendName",
175 [](Avatar *) -> std::string { return Avatar::getLive2DBackendName(); });
176}
177
178} // namespace eve::avatar
HSQOBJECT cls
Definition ECS.cpp:21
glm::vec3 n
Definition Grass.cpp:64
uint32_t a
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:24
float f
const char * name
Definition RockMesh.cpp:21
virtual std::string getName() const =0
Unified avatar instance. Kind is a string: "image" | "live2d" | "vroid". Script-facing API avoids ove...
void setScale(float sx, float sy)
void setExpression(const std::string &name)
bool setLayerColor(const std::string &name, float r, float g, float b, float a)
bool setLayerOffset(const std::string &name, float ox, float oy)
void sync()
Push image / live2d layers into ECS or draw queues.
float getParameter(const std::string &name) const
std::string getMotion() const
std::string getVroidModelPath() const
std::string getExpression() const
void bindTween(animation::Tween *tween)
Drive x/y/sx/sy and matching parameters from a Tween each update().
bool hasLayer(const std::string &name) const
bool applyExpression(const std::string &name)
std::string getParameterName(int index) const
std::string getKind() const
graphics::Mesh * getBoundMesh() const
void setVisible(bool visible)
bool loadVroidModelPath(const std::string &path)
bool bindVroidModelData(model3d::ModelData *data)
void setMesh(graphics::Mesh *mesh)
int loadMorphNamesFromModel(int meshIndex=0)
Register morph target names from ModelData as parameters (weights default 0).
void setParameter(const std::string &name, float value)
std::string getLayerName(int index) const
bool bakeMorphs()
Push parameter weights onto Mesh morphs and bake GPU verts when possible.
void setTexture(graphics::Texture *texture)
void setPosition(float x, float y)
void setMotion(const std::string &name)
void setRotation3D(float yaw, float pitch, float roll)
animation::Tween * getBoundTween() const
bool loadLive2DModel(const std::string &path)
bool setLayerZ(const std::string &name, int zIndex)
std::string getLive2DBackendName() const
bool setLayerSize(const std::string &name, float w, float h)
void setScale3D(float sx, float sy, float sz)
bool setLayerVisible(const std::string &name, bool visible)
bool hasParameter(const std::string &name) const
bool defineExpression(const std::string &name, const std::string &spec)
Spec: "eyes=1;mouth=smile;blush=0" (semicolon-separated name=value).
graphics::Renderable3D * getRenderable3D() const
void setPosition3D(float x, float y, float z)
Avatar module — layered character rendering for VN / portrait use. Script: avatar <- eve....
Definition Avatar.h:23
static void registerLive2DBackend(Live2DBackendFactory factory)
C++ / plugin: replace Live2D backend factory. Pass nullptr to restore the built-in NullLive2DBackend ...
Definition Avatar.cpp:65
static Live2DBackendFactory live2DBackendFactory()
Definition Avatar.cpp:69
void render(graphics::Graphics *gfx)
Sync then draw Image/Live2D avatars via the shared 2D sprite queue (does not present)....
Definition Avatar.cpp:55
void update(float dt)
Definition Avatar.cpp:41
AvatarInstance * newLive2DAvatar()
Definition Avatar.cpp:37
~Avatar() override
Definition Avatar.cpp:15
AvatarInstance * newImageAvatar()
Definition Avatar.cpp:35
int getAvatarCount() const
Definition Avatar.cpp:63
static std::string getLive2DBackendName()
Backend name currently in effect ("null" when using the built-in stub).
Definition Avatar.cpp:76
friend class AvatarInstance
Definition Avatar.h:55
AvatarInstance * newVroidAvatar()
Definition Avatar.cpp:39
static ILive2DBackend * createLive2DBackend()
Always returns a backend: custom factory, else NullLive2DBackend.
Definition Avatar.cpp:71
void sync()
Sync all live avatars into renderables.
Definition Avatar.cpp:48
Optional Live2D runtime backend (Cubism etc.). Registered from C++ / plugins.
virtual std::string getName() const =0
static void collectSprites(std::vector< DrawItem2D > &out)
Append visible Renderable2D sprites into a shared queue.
static void drawItems(Graphics &gfx, std::vector< DrawItem2D > &items, bool present)
Sort and draw items. If present=true, calls gfx.present() at the end. Map::render uses present=false ...
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:17
ILive2DBackend *(*)() Live2DBackendFactory
ILive2DBackend * createNullLive2DBackend()