mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-20 09:29:44 +00:00
Merge pull request #1062 from MihailRis/entity-rotation-interpolation
entity rotation interpolation
This commit is contained in:
commit
5eae83be33
10 changed files with 84 additions and 17 deletions
|
|
@ -118,8 +118,8 @@ local env = {
|
|||
if target.set_rot then
|
||||
target:set_rot(rot)
|
||||
end
|
||||
if target.set_scale then
|
||||
target:set_scale(scale)
|
||||
if target.set_size then
|
||||
target:set_size(scale)
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
|
|
|||
|
|
@ -160,17 +160,17 @@ void SkeletonConfig::update(
|
|||
const glm::vec3& position,
|
||||
const glm::vec3& scale
|
||||
) const {
|
||||
if (skeleton.interpolation.isEnabled()) {
|
||||
const auto& interpolation = skeleton.interpolation;
|
||||
update(
|
||||
0,
|
||||
skeleton,
|
||||
root.get(),
|
||||
build_matrix(rotation, interpolation.getCurrent(), scale)
|
||||
);
|
||||
} else {
|
||||
update(0, skeleton, root.get(), build_matrix(rotation, position, scale));
|
||||
glm::vec3 pos = position;
|
||||
const auto& interpolation = skeleton.interpolation;
|
||||
if (interpolation.isEnabled() && interpolation.isRefreshed()) {
|
||||
pos = interpolation.getCurrent();
|
||||
}
|
||||
glm::mat3 rot = rotation;
|
||||
const auto& rotationInterpolation = skeleton.rotationInterpolation;
|
||||
if (rotationInterpolation.isEnabled() && rotationInterpolation.isRefreshed()) {
|
||||
rot = glm::mat3_cast(rotationInterpolation.getCurrent());
|
||||
}
|
||||
update(0, skeleton, root.get(), build_matrix(rot, pos, scale));
|
||||
}
|
||||
|
||||
void SkeletonConfig::render(
|
||||
|
|
|
|||
|
|
@ -101,6 +101,7 @@ namespace rigging {
|
|||
std::vector<glm::vec4> boneTints;
|
||||
|
||||
util::VecInterpolation<3, float> interpolation {false};
|
||||
util::RotationInterpolation rotationInterpolation {false};
|
||||
|
||||
Skeleton(std::shared_ptr<const SkeletonConfig> config);
|
||||
|
||||
|
|
|
|||
|
|
@ -201,7 +201,9 @@ static int l_is_interpolated(lua::State* L) {
|
|||
|
||||
static int l_set_interpolated(lua::State* L) {
|
||||
if (auto skeleton = get_skeleton(L)) {
|
||||
skeleton->interpolation.setEnabled(lua::toboolean(L, 2));
|
||||
bool enabled = lua::toboolean(L, 2);
|
||||
skeleton->interpolation.setEnabled(enabled);
|
||||
skeleton->rotationInterpolation.setEnabled(enabled);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,9 +61,11 @@ static int l_get_rot(lua::State* L) {
|
|||
|
||||
static int l_set_rot(lua::State* L) {
|
||||
if (auto entity = get_entity(L, 1)) {
|
||||
auto matrix = lua::tomat4(L, 2);
|
||||
glm::mat3 matrix(lua::tomat4(L, 2));
|
||||
check_valid(matrix);
|
||||
entity->getTransform().setRot(matrix);
|
||||
auto rotation = glm::normalize(glm::quat_cast(matrix));
|
||||
entity->getTransform().setRot(glm::mat3_cast(rotation));
|
||||
entity->setInterpolatedRotation(rotation);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -403,6 +403,9 @@ void Entities::update(float delta) {
|
|||
if (skeleton.interpolation.isEnabled()) {
|
||||
skeleton.interpolation.updateTimer(delta);
|
||||
}
|
||||
if (skeleton.rotationInterpolation.isEnabled()) {
|
||||
skeleton.rotationInterpolation.updateTimer(delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,13 +20,20 @@ void Entity::setInterpolatedPosition(const glm::vec3& position) {
|
|||
|
||||
glm::vec3 Entity::getInterpolatedPosition() const {
|
||||
if (auto skeleton = getSkeleton()) {
|
||||
if (skeleton->interpolation.isEnabled()) {
|
||||
return skeleton->interpolation.getCurrent();
|
||||
const auto& interpolation = skeleton->interpolation;
|
||||
if (interpolation.isEnabled() && interpolation.isRefreshed()) {
|
||||
return interpolation.getCurrent();
|
||||
}
|
||||
}
|
||||
return getTransform().pos;
|
||||
}
|
||||
|
||||
void Entity::setInterpolatedRotation(const glm::quat& rotation) {
|
||||
if (auto skeleton = getSkeleton()) {
|
||||
skeleton->rotationInterpolation.refresh(rotation);
|
||||
}
|
||||
}
|
||||
|
||||
void Entity::destroy() {
|
||||
if (isValid()) {
|
||||
entities.despawn(id);
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
#include <string>
|
||||
#include <entt/fwd.hpp>
|
||||
#include <glm/vec3.hpp>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
|
||||
class Entities;
|
||||
struct EntityDef;
|
||||
|
|
@ -72,6 +73,8 @@ public:
|
|||
|
||||
glm::vec3 getInterpolatedPosition() const;
|
||||
|
||||
void setInterpolatedRotation(const glm::quat& rotation);
|
||||
|
||||
entt::entity getHandler() const {
|
||||
return entity;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
#include "maths/util.hpp"
|
||||
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
|
||||
static debug::Logger logger("entity-transform");
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
|
||||
const float INTERPOLATION_DURATION = 0.15f; // seconds
|
||||
|
||||
|
|
@ -9,6 +11,7 @@ namespace util {
|
|||
template<int N, typename T, bool angular=false>
|
||||
class VecInterpolation {
|
||||
bool enabled;
|
||||
bool refreshed = false;
|
||||
glm::vec<N, T> prevPos {std::numeric_limits<T>::quiet_NaN()};
|
||||
glm::vec<N, T> nextPos {};
|
||||
mutable glm::vec<N, T> interpolationStep {};
|
||||
|
|
@ -37,6 +40,7 @@ namespace util {
|
|||
} else {
|
||||
interpolationStep = (nextPos - prevPos) / currentDuration.x;
|
||||
}
|
||||
refreshed = true;
|
||||
timer = 0.0;
|
||||
}
|
||||
|
||||
|
|
@ -57,6 +61,50 @@ namespace util {
|
|||
return interpolated;
|
||||
}
|
||||
|
||||
bool isRefreshed() const {
|
||||
return refreshed;
|
||||
}
|
||||
|
||||
bool isEnabled() const {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
void setEnabled(bool enabled) {
|
||||
this->enabled = enabled;
|
||||
}
|
||||
};
|
||||
|
||||
class RotationInterpolation {
|
||||
bool enabled;
|
||||
bool refreshed = false;
|
||||
glm::quat prevRot {1.0f, 0.0f, 0.0f, 0.0f};
|
||||
glm::quat nextRot {1.0f, 0.0f, 0.0f, 0.0f};
|
||||
float timer = 0.0f;
|
||||
public:
|
||||
RotationInterpolation(bool enabled) : enabled(enabled) {}
|
||||
|
||||
void refresh(const glm::quat& rotation) {
|
||||
prevRot = refreshed ? getCurrent() : rotation;
|
||||
nextRot = rotation;
|
||||
refreshed = true;
|
||||
timer = 0.0f;
|
||||
}
|
||||
|
||||
void updateTimer(float delta) {
|
||||
timer += delta;
|
||||
}
|
||||
|
||||
glm::quat getCurrent() const {
|
||||
if (timer >= INTERPOLATION_DURATION) {
|
||||
return nextRot;
|
||||
}
|
||||
return glm::slerp(prevRot, nextRot, timer / INTERPOLATION_DURATION);
|
||||
}
|
||||
|
||||
bool isRefreshed() const {
|
||||
return refreshed;
|
||||
}
|
||||
|
||||
bool isEnabled() const {
|
||||
return enabled;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue