载入中...
搜索中...
未找到
SceneNodeRef.cpp
浏览该文件的文档.
2
3#include "scene/Scene.h"
4#include "scene/SceneHost.h"
5
6#include <glm/glm.hpp>
7
8#include <cmath>
9#include <utility>
10
11namespace eve::scene {
12namespace {
13
14template <class T>
15T *borrowSceneResult(eve::Result<T *> result) {
16 if (!result.ok()) return nullptr;
17 return std::move(result).takeValue();
18}
19
20SceneHost *resolveHost(const std::string &hostName) {
21 Scene *s = Scene::create();
22 if (!s) return nullptr;
23 if (hostName.empty()) return s->current();
24 return borrowSceneResult(s->findHost(hostName));
25}
26
27SceneNode *resolveNode(const std::string &hostName, const std::string &nodeId) {
28 if (nodeId.empty()) return nullptr;
29 SceneHost *h = resolveHost(hostName);
30 return h ? borrowSceneResult(h->findById(nodeId)) : nullptr;
31}
32
33glm::vec3 normalizedColumn(const glm::mat4 &m, int col) {
34 glm::vec3 v(m[col]);
35 const float len = glm::length(v);
36 if (len > 1e-8f) v /= len;
37 return v;
38}
39
40} // namespace
41
42bool SceneNodeRef::isValid() const { return resolveNode(hostName_, nodeId_) != nullptr; }
43
45 const SceneNode *node = resolveNode(hostName_, nodeId_);
46 return node ? node->persistentId : eve::SceneObjectId::nil();
47}
48
49Scene *SceneNodeRef::getScene() const { return Scene::create(); }
50
51bool SceneNodeRef::setPosition(float x, float y, float z) {
52 SceneHost *h = resolveHost(hostName_);
53 SceneNode *n = resolveNode(hostName_, nodeId_);
54 if (!h || !n) return false;
55 n->x = x;
56 n->y = y;
57 n->z = z;
58 h->markSubtreeDirtyById(nodeId_);
59 return true;
60}
61
63 const SceneNode *n = resolveNode(hostName_, nodeId_);
64 return n ? n->x : 0.f;
65}
66
68 const SceneNode *n = resolveNode(hostName_, nodeId_);
69 return n ? n->y : 0.f;
70}
71
73 const SceneNode *n = resolveNode(hostName_, nodeId_);
74 return n ? n->z : 0.f;
75}
76
77std::vector<float> SceneNodeRef::getPosition() const {
78 const SceneNode *n = resolveNode(hostName_, nodeId_);
79 if (!n) return {0.f, 0.f, 0.f};
80 return {n->x, n->y, n->z};
81}
82
83bool SceneNodeRef::setRotation(float yaw, float pitch, float roll) {
84 SceneHost *h = resolveHost(hostName_);
85 SceneNode *n = resolveNode(hostName_, nodeId_);
86 if (!h || !n) return false;
87 n->yaw = yaw;
88 n->pitch = pitch;
89 n->roll = roll;
90 h->markSubtreeDirtyById(nodeId_);
91 return true;
92}
93
95 const SceneNode *n = resolveNode(hostName_, nodeId_);
96 return n ? n->yaw : 0.f;
97}
98
100 const SceneNode *n = resolveNode(hostName_, nodeId_);
101 return n ? n->pitch : 0.f;
102}
103
105 const SceneNode *n = resolveNode(hostName_, nodeId_);
106 return n ? n->roll : 0.f;
107}
108
109std::vector<float> SceneNodeRef::getRotation() const {
110 const SceneNode *n = resolveNode(hostName_, nodeId_);
111 if (!n) return {0.f, 0.f, 0.f};
112 return {n->yaw, n->pitch, n->roll};
113}
114
115bool SceneNodeRef::setScale(float sx, float sy, float sz) {
116 SceneHost *h = resolveHost(hostName_);
117 SceneNode *n = resolveNode(hostName_, nodeId_);
118 if (!h || !n) return false;
119 n->sx = sx;
120 n->sy = sy;
121 n->sz = sz;
122 h->markSubtreeDirtyById(nodeId_);
123 return true;
124}
125
127 const SceneNode *n = resolveNode(hostName_, nodeId_);
128 return n ? n->sx : 0.f;
129}
130
132 const SceneNode *n = resolveNode(hostName_, nodeId_);
133 return n ? n->sy : 0.f;
134}
135
137 const SceneNode *n = resolveNode(hostName_, nodeId_);
138 return n ? n->sz : 0.f;
139}
140
141std::vector<float> SceneNodeRef::getScale() const {
142 const SceneNode *n = resolveNode(hostName_, nodeId_);
143 if (!n) return {0.f, 0.f, 0.f};
144 return {n->sx, n->sy, n->sz};
145}
146
148 SceneNode *n = resolveNode(hostName_, nodeId_);
149 if (!n) return false;
150 n->visible = v;
151 SceneHost *h = resolveHost(hostName_);
152 if (h) h->markSubtreeDirtyById(nodeId_);
153 return true;
154}
155
157 const SceneNode *n = resolveNode(hostName_, nodeId_);
158 return n ? n->visible : false;
159}
160
162 const SceneNode *n = resolveNode(hostName_, nodeId_);
163 return n ? n->world[3][0] : 0.f;
164}
165
167 const SceneNode *n = resolveNode(hostName_, nodeId_);
168 return n ? n->world[3][1] : 0.f;
169}
170
172 const SceneNode *n = resolveNode(hostName_, nodeId_);
173 return n ? n->world[3][2] : 0.f;
174}
175
176std::vector<float> SceneNodeRef::getWorldPosition() const {
177 const SceneNode *n = resolveNode(hostName_, nodeId_);
178 if (!n) return {0.f, 0.f, 0.f};
179 return {n->world[3][0], n->world[3][1], n->world[3][2]};
180}
181
182std::vector<float> SceneNodeRef::getWorldMatrix() const {
183 const SceneNode *n = resolveNode(hostName_, nodeId_);
184 if (!n) return std::vector<float>(16, 0.f);
185 std::vector<float> out;
186 out.reserve(16);
187 for (int c = 0; c < 4; ++c)
188 for (int r = 0; r < 4; ++r) out.push_back(n->world[c][r]);
189 return out;
190}
191
192std::vector<float> SceneNodeRef::getForward() const {
193 const SceneNode *n = resolveNode(hostName_, nodeId_);
194 if (!n) return {0.f, 0.f, 1.f};
195 glm::vec3 v = normalizedColumn(n->world, 2);
196 return {v.x, v.y, v.z};
197}
198
199std::vector<float> SceneNodeRef::getRight() const {
200 const SceneNode *n = resolveNode(hostName_, nodeId_);
201 if (!n) return {1.f, 0.f, 0.f};
202 glm::vec3 v = normalizedColumn(n->world, 0);
203 return {v.x, v.y, v.z};
204}
205
206std::vector<float> SceneNodeRef::getUp() const {
207 const SceneNode *n = resolveNode(hostName_, nodeId_);
208 if (!n) return {0.f, 1.f, 0.f};
209 glm::vec3 v = normalizedColumn(n->world, 1);
210 return {v.x, v.y, v.z};
211}
212
213std::string SceneNodeRef::getParentId() const {
214 SceneHost *h = resolveHost(hostName_);
215 if (!h) return {};
216 SceneNode *p = h->getParentById(nodeId_);
217 return p ? p->id : std::string{};
218}
219
221 SceneHost *h = resolveHost(hostName_);
222 return h ? h->getChildCountById(nodeId_) : 0;
223}
224
225std::string SceneNodeRef::getChildIdAt(int ordinal) const {
226 SceneHost *h = resolveHost(hostName_);
227 if (!h) return {};
228 SceneNode *c = h->getChildAtById(nodeId_, ordinal);
229 return c ? c->id : std::string{};
230}
231
232std::string SceneNodeRef::getPath() const {
233 SceneHost *h = resolveHost(hostName_);
234 return h ? h->getPathById(nodeId_) : std::string{};
235}
236
237} // namespace eve::scene
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
const std::string & s
glm::vec4 p[6]
std::string nodeId
glm::vec3 n
Definition Grass.cpp:63
double r
float v
std::int32_t c
int h
const RoadNode * node
float m[16]
Move-only operation result carrying either a value or Status.
Definition Result.h:155
bool ok() const noexcept
Whether this result represents a non-failure outcome.
Definition Result.h:255
Id128 public API.
Definition Identity.h:112
static constexpr Id128 nil() noexcept
Returns the all-zero nil value.
Definition Identity.h:142
ECS mount point for one scene graph (full scene or nested subtree root). Isomorphic to eve::ui::UIHos...
Definition SceneHost.h:89
float getPositionX() const
Returns the position x.
float getWorldPositionZ() const
Returns the world position z.
std::vector< float > getForward() const
Normalized local axes in world space (forward = +Z).
bool isValid() const
True when host + node currently resolve.
float getWorldPositionX() const
Returns the world position x.
float getRotationYaw() const
Returns the rotation yaw.
float getWorldPositionY() const
Returns the world position y.
float getRotationPitch() const
Returns the rotation pitch.
bool setVisible(bool v)
Sets the visible.
float getScaleZ() const
Returns the scale z.
float getScaleX() const
Returns the scale x.
Scene * getScene() const
Returns the eve.Scene module instance for entity-binding forwarding.
std::string getPath() const
Returns the path.
std::string getChildIdAt(int ordinal) const
Returns the child id at.
std::vector< float > getPosition() const
Returns the position.
eve::SceneObjectId persistentId() const
Return the persisted UUID of the resolved node, or nil if absent.
std::vector< float > getWorldPosition() const
Returns the world position.
std::vector< float > getRotation() const
Returns the rotation.
bool setPosition(float x, float y, float z)
Sets the position.
float getPositionY() const
Returns the position y.
bool setScale(float sx, float sy, float sz)
Sets the scale.
float getRotationRoll() const
Returns the rotation roll.
std::vector< float > getScale() const
Returns the scale.
int getChildCount() const
Returns the child count.
bool isVisible() const
True when visible.
float getScaleY() const
Returns the scale y.
std::string getParentId() const
Returns the parent id.
std::vector< float > getUp() const
Returns the up.
bool setRotation(float yaw, float pitch, float roll)
Sets the rotation.
std::vector< float > getRight() const
Returns the right.
float getPositionZ() const
Returns the position z.
std::vector< float > getWorldMatrix() const
Column-major 4x4 world matrix, 16 floats.
Declarative scene module (eve.Scene).
Definition Scene.h:51
Retained scene node (arena). Conceptual GameObject; isomorphic to eve::ui::UINode.
Definition SceneHost.h:46