95 if (!destroyConn_.isConnected()) {
97 states_.erase(entity);
103 for (
auto it = states_.begin(); it != states_.end();) {
104 const Entity root = it->first;
106 if (sb && sb->enabled && sb->weight > 0.0f) { ++it;
continue; }
107 restoreBones(registry, it->second);
108 it = states_.erase(it);
113 for (
auto entity : view) { (void)entity; any =
true;
break; }
119 for (
auto entity : view) {
121 if (!sb.enabled || sb.weight <= 0.0f)
continue;
123 collectBones(registry, entity, found_);
124 const size_t bones = found_.size();
125 if (bones < 2 && !(bones == 1 && sb.endLength > 0.0f))
continue;
127 State& st = states_[entity];
131 childCount_.assign(bones, 0);
132 parentOf_.assign(bones, -1);
133 auto& childCount = childCount_;
134 auto& parentOf = parentOf_;
135 for (
size_t i = 1; i < bones; ++i) {
139 for (
size_t j = i; j-- > 0;)
if (found_[j] == p->entity) { parent =
static_cast<i32>(j);
break; }
141 parentOf[i] = parent;
142 childCount[
static_cast<size_t>(parent)]++;
144 for (
size_t i = 0; i < bones; ++i)
if (childCount[i] == 0) ++leaves;
145 const size_t tips = sb.endLength > 0.0f ? leaves : 0;
147 if (st.entities != found_ || st.particles.size() != bones + tips) {
148 st.entities = found_;
149 st.particles.assign(bones + tips,
Particle{});
150 st.nodes.assign(bones + tips, Node{});
151 for (
size_t i = 0; i < bones; ++i) {
152 st.nodes[i].entity = found_[i];
153 st.particles[i].parent = parentOf[i];
155 for (
size_t i = 1; i < bones; ++i) {
156 Node& parent = st.nodes[
static_cast<size_t>(parentOf[i])];
157 if (parent.firstChild < 0) parent.firstChild =
static_cast<i32>(i);
161 for (
size_t i = 0; i < bones; ++i) {
162 if (childCount[i] != 0)
continue;
163 st.nodes[i].firstChild =
static_cast<i32>(next);
164 st.particles[next].parent =
static_cast<i32>(i);
169 st.animated.assign(bones, glm::quat(1.0f, 0.0f, 0.0f, 0.0f));
170 st.written.assign(bones, glm::quat(1.0f, 0.0f, 0.0f, 0.0f));
171 st.hasWritten.assign(bones,
false);
176 poses_.resize(bones);
177 tipOffsets_.assign(bones + tips, glm::vec3(0.0f));
178 for (
size_t i = 0; i < bones; ++i) {
181 Pose& pose = poses_[i];
182 pose.animScale = t.worldScale;
183 pose.localPosition = t.position;
185 if (st.hasWritten[i] && t.rotation == st.written[i]) {
186 pose.localRotation = st.animated[i];
188 pose.localRotation = t.rotation;
189 st.animated[i] = t.rotation;
190 st.hasWritten[i] =
false;
194 const Pose& parent = poses_[
static_cast<size_t>(parentOf[i])];
195 pose.animRot = parent.animRot * pose.localRotation;
196 pose.animPos = parent.animPos + parent.animRot * (pose.localPosition * parent.animScale);
198 pose.localRotation = t.rotation;
199 pose.animPos = t.worldPosition;
200 pose.animRot = t.worldRotation;
205 f32 totalRest = 0.0f;
206 for (
size_t i = 0; i < bones; ++i) {
208 p.
target = poses_[i].animPos;
214 for (
size_t i = 0; i < bones; ++i) {
215 const i32 tip = st.nodes[i].firstChild;
216 if (tip <
static_cast<i32>(bones) || tip < 0)
continue;
217 glm::vec3 direction = poses_[i].animRot * glm::vec3(0.0f, 1.0f, 0.0f);
218 if (parentOf[i] >= 0) {
219 const glm::vec3 along = poses_[i].animPos - poses_[
static_cast<size_t>(parentOf[i])].animPos;
220 if (glm::dot(along, along) > 1e-12f) direction = glm::normalize(along);
222 Particle& p = st.particles[
static_cast<size_t>(tip)];
223 p.
target = poses_[i].animPos + direction * sb.endLength;
225 totalRest += sb.endLength;
227 tipOffsets_[
static_cast<size_t>(tip)] = glm::inverse(poses_[i].animRot) * (p.
target - poses_[i].animPos);
231 const glm::vec3 rootPosition = poses_[0].animPos;
232 const bool teleported = st.primed && glm::length(rootPosition - st.rootPosition) > std::max(totalRest, 1e-3f) * 4.0f;
233 if (!st.primed || teleported || dt > 0.5f) {
237 st.rootPosition = rootPosition;
239 gatherColliders(registry, sb.colliders, colliders_);
242 params.
drag = sb.drag;
244 params.
radius = sb.radius;
245 params.
wind = sb.wind;
248 params.
time = st.clock;
249 step(st.particles, params, colliders_, dt);
255 fkPosition_.resize(bones);
256 fkRotation_.resize(bones);
257 const f32 weight = std::clamp(sb.weight, 0.0f, 1.0f);
259 for (
size_t i = 0; i < bones; ++i) {
262 fkPosition_[0] = poses_[0].animPos;
263 fkRotation_[0] = poses_[0].animRot;
264 unaimed = fkRotation_[0];
266 const size_t b =
static_cast<size_t>(parentOf[i]);
267 fkPosition_[i] = fkPosition_[b] + fkRotation_[b] * (poses_[i].localPosition * poses_[b].animScale);
268 unaimed = fkRotation_[b] * poses_[i].localRotation;
269 fkRotation_[i] = unaimed;
271 const i32 c = st.nodes[i].firstChild;
272 if (i == 0 || c < 0)
continue;
273 const glm::vec3 offset =
static_cast<size_t>(c) < bones
274 ? poses_[
static_cast<size_t>(c)].localPosition * poses_[i].animScale
275 : tipOffsets_[
static_cast<size_t>(c)];
276 const glm::vec3 current = fkRotation_[i] * offset;
277 const glm::vec3 wanted = st.particles[
static_cast<size_t>(c)].position - fkPosition_[i];
278 glm::quat aimed =
shortestArc(current, wanted) * fkRotation_[i];
279 if (weight < 1.0f) aimed = glm::slerp(unaimed, aimed, weight);
280 fkRotation_[i] = aimed;
282 const size_t b =
static_cast<size_t>(parentOf[i]);
283 const glm::quat local = glm::normalize(glm::inverse(fkRotation_[b]) * aimed);
285 if (t.rotation != local) {
289 st.written[i] = local;
290 st.hasWritten[i] =
true;
i32 parent
Index of the parent particle, -1 for the root, which the animation drives.