Skip to content

Commit

Permalink
Lua: Quaternion bindings, allow vectors as rotator args
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Oct 6, 2024
1 parent b17be34 commit 81ae896
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,15 @@ list(APPEND luavrlib_SOURCES
"lua-api/lib/src/ScriptContext.cpp"
"lua-api/lib/src/ScriptState.cpp"
"lua-api/lib/src/ScriptUtility.cpp"
"lua-api/lib/src/datatypes/Quaternion.cpp"
"lua-api/lib/src/datatypes/StructObject.cpp"
"lua-api/lib/src/datatypes/Vector.cpp"
"lua-api/lib/src/datatypes/XInput.cpp"
"lua-api/lib/include/ScriptContext.hpp"
"lua-api/lib/include/ScriptState.hpp"
"lua-api/lib/include/ScriptUtility.hpp"
"lua-api/lib/include/datatypes/FFrame.hpp"
"lua-api/lib/include/datatypes/Quaternion.hpp"
"lua-api/lib/include/datatypes/StructObject.hpp"
"lua-api/lib/include/datatypes/Vector.hpp"
"lua-api/lib/include/datatypes/XInput.hpp"
Expand Down
16 changes: 16 additions & 0 deletions lua-api/lib/include/datatypes/Quaternion.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#define GLM_ENABLE_EXPERIMENTAL

#include <glm/ext/quaternion_float.hpp>
#include <glm/ext/quaternion_double.hpp>

#include <sol/sol.hpp>
#include <uevr/API.hpp>

namespace lua::datatypes {
using Quaternionf = glm::quat;
using Quaterniond = glm::dquat;

void bind_quaternions(sol::state_view& lua);
}
4 changes: 3 additions & 1 deletion lua-api/lib/src/ScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "datatypes/XInput.hpp"
#include "datatypes/Vector.hpp"
#include "datatypes/Quaternion.hpp"
#include "datatypes/StructObject.hpp"
#include "datatypes/FFrame.hpp"

Expand Down Expand Up @@ -182,6 +183,7 @@ int ScriptContext::setup_bindings() {

lua::datatypes::bind_xinput(m_lua);
lua::datatypes::bind_vectors(m_lua);
lua::datatypes::bind_quaternions(m_lua);
lua::datatypes::bind_struct_object(m_lua);

m_lua.new_usertype<UEVR_PluginInitializeParam>("UEVR_PluginInitializeParam",
Expand Down Expand Up @@ -876,7 +878,7 @@ void ScriptContext::on_early_calculate_stereo_view_offset(UEVR_StereoRenderingDe
const auto ue5_position = (lua::datatypes::Vector3d*)position;
const auto ue4_position = (lua::datatypes::Vector3f*)position;
const auto ue5_rotation = (lua::datatypes::Vector3d*)rotation;
const auto ue4_rotation = (lua::datatypes::Vector3f*)rotation;
const auto ue4_rotation = (lua::datatypes::Vector3f*)rotation;
const auto is_ue5 = lua::utility::is_ue5();

for (auto& fn : ctx->m_on_early_calculate_stereo_view_offset_callbacks) try {
Expand Down
13 changes: 12 additions & 1 deletion lua-api/lib/src/ScriptUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ uevr::API::UScriptStruct* get_vector_struct() {
return vector_struct;
}

uevr::API::UScriptStruct* get_rotator_struct() {
static auto rotator_struct = []() {
const auto modern_class = uevr::API::get()->find_uobject<uevr::API::UScriptStruct>(L"ScriptStruct /Script/CoreUObject.Rotator");
const auto old_class = modern_class == nullptr ? uevr::API::get()->find_uobject<uevr::API::UScriptStruct>(L"ScriptStruct /Script/CoreUObject.Object.Rotator") : nullptr;

return modern_class != nullptr ? modern_class : old_class;
}();

return rotator_struct;
}

bool is_ue5() {
static auto cached_result = []() {
const auto c = get_vector_struct();
Expand Down Expand Up @@ -388,7 +399,7 @@ void set_property(sol::this_state s, void* self, uevr::API::UStruct* owner_c, ue
}

memcpy((void*)((uintptr_t)self + offset), arg.object, struct_desc->get_struct_size());
} else if (struct_desc == get_vector_struct()) {
} else if (struct_desc == get_vector_struct() || struct_desc == get_rotator_struct()) { // Same layout
if (value.is<lua::datatypes::Vector3f>()) {
const auto arg = value.as<lua::datatypes::Vector3f>();

Expand Down
37 changes: 37 additions & 0 deletions lua-api/lib/src/datatypes/Quaternion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <datatypes/Quaternion.hpp>

namespace lua::datatypes {
void bind_quaternions(sol::state_view& lua) {
#define BIND_QUATERNION_LIKE(name, datatype) \
lua.new_usertype<name>(#name, \
"x", &name::x, \
"y", &name::y, \
"z", &name::z, \
"w", &name::w, \
"X", &name::x, \
"Y", &name::y, \
"Z", &name::z, \
"W", &name::w, \
"length", [](name& v) { return glm::length(v); }, \
"normalize", [](name& v) { v = glm::normalize(v); }, \
"normalized", [](name& v) { return glm::normalize(v); }, \
"conjugate", [](name& v) { return glm::conjugate(v); }, \
"inverse", [](name& v) { return glm::inverse(v); }, \
"dot", [](name& v1, name& v2) { return glm::dot(v1, v2); }, \
"slerp", [](name& v1, name& v2, datatype t) { return glm::slerp(v1, v2, t); }, \
sol::meta_function::addition, [](name& lhs, name& rhs) { return lhs + rhs; }, \
sol::meta_function::subtraction, [](name& lhs, name& rhs) { return lhs - rhs; }, \
sol::meta_function::multiplication, [](name& lhs, datatype scalar) { return lhs * scalar; }

#define BIND_QUATERNION_LIKE_END() \
);

BIND_QUATERNION_LIKE(Quaternionf, float),
sol::meta_function::construct, sol::constructors<Quaternionf(float, float, float, float)>()
BIND_QUATERNION_LIKE_END();

BIND_QUATERNION_LIKE(Quaterniond, double),
sol::meta_function::construct, sol::constructors<Quaterniond(double, double, double, double)>()
BIND_QUATERNION_LIKE_END();
}
}

0 comments on commit 81ae896

Please sign in to comment.