Merge branch 'main' into dev

This commit is contained in:
MihailRis 2026-10-16 01:30:13 +03:00
commit 35e3d80f35
111 changed files with 935 additions and 664 deletions

View file

@ -5,6 +5,9 @@
#include "graphics/render/WorldRenderer.hpp"
#include "graphics/render/NamedSkeletons.hpp"
#include <algorithm>
#include <glm/gtc/matrix_transform.hpp>
namespace scripting {
extern WorldRenderer* renderer;
}
@ -196,6 +199,174 @@ static int l_exists(lua::State* L) {
return lua::pushboolean(L, get_skeleton(L));
}
namespace {
struct NativeTrackBone {
std::string name;
bool translate = false;
int translation[3] {};
std::vector<std::pair<int, int>> rotations;
bool scale = false;
int scaling[3] {};
};
struct NativeTrack {
std::string name;
std::vector<NativeTrackBone> bones;
int valuesCount = 0;
std::weak_ptr<const rigging::SkeletonConfig> config;
std::vector<int> indices;
};
std::vector<NativeTrack> native_tracks;
constexpr int MAX_TRACK_VALUES = 256;
}
static void read_indices(lua::State* L, int idx, const char* field, int* dst) {
if (!lua::istable(L, -1)) {
return;
}
for (int i = 0; i < 3; i++) {
lua::rawgeti(L, i + 1);
dst[i] = static_cast<int>(lua::tointeger(L, -1));
lua::pop(L);
}
}
static int l_register_track(lua::State* L) {
NativeTrack track;
track.name = lua::require_string(L, 1);
if (!lua::istable(L, 2)) {
throw std::runtime_error("layout table expected");
}
int maxValue = 0;
size_t count = lua::objlen(L, 2);
for (size_t i = 1; i <= count; i++) {
lua::rawgeti(L, static_cast<int>(i), 2);
int bone = lua::gettop(L);
NativeTrackBone entry;
lua::requirefield(L, "name", bone);
entry.name = lua::require_string(L, -1);
lua::pop(L);
if (lua::getfield(L, "t", bone)) {
entry.translate = lua::istable(L, -1);
read_indices(L, bone, "t", entry.translation);
lua::pop(L);
}
if (lua::getfield(L, "s", bone)) {
entry.scale = lua::istable(L, -1);
read_indices(L, bone, "s", entry.scaling);
lua::pop(L);
}
if (lua::getfield(L, "r", bone)) {
if (lua::istable(L, -1)) {
size_t n = lua::objlen(L, -1);
for (size_t j = 1; j + 1 <= n; j += 2) {
lua::rawgeti(L, static_cast<int>(j), -1);
int axis = static_cast<int>(lua::tointeger(L, -1));
lua::rawgeti(L, static_cast<int>(j + 1), -2);
int value = static_cast<int>(lua::tointeger(L, -1));
lua::pop(L, 2);
if (axis < 0 || axis > 2) {
throw std::runtime_error("invalid rotation axis");
}
entry.rotations.emplace_back(axis, value);
}
}
lua::pop(L);
}
for (int v : entry.translation) {
maxValue = std::max(maxValue, v);
}
for (int v : entry.scaling) {
maxValue = std::max(maxValue, v);
}
for (auto [_, v] : entry.rotations) {
maxValue = std::max(maxValue, v);
}
track.bones.push_back(std::move(entry));
lua::pop(L);
}
track.valuesCount = maxValue;
for (size_t i = 0; i < native_tracks.size(); i++) {
if (native_tracks[i].name == track.name) {
native_tracks[i] = std::move(track);
return lua::pushinteger(L, static_cast<lua::Integer>(i));
}
}
native_tracks.push_back(std::move(track));
return lua::pushinteger(L, native_tracks.size() - 1);
}
static int l_apply_track(lua::State* L) {
auto skeleton = get_skeleton(L);
if (skeleton == nullptr || skeleton->config == nullptr) {
return 0;
}
auto id = lua::tointeger(L, 2);
if (id < 0 || static_cast<size_t>(id) >= native_tracks.size()) {
throw std::runtime_error("invalid track id");
}
auto& track = native_tracks[id];
const auto& config = skeleton->config;
if (track.config.owner_before(config) || config.owner_before(track.config) ||
track.config.expired() || track.indices.size() != track.bones.size()) {
track.config = config;
track.indices.clear();
for (const auto& bone : track.bones) {
auto found = config->find(bone.name);
track.indices.push_back(found ? static_cast<int>(found->getIndex()) : -1);
}
}
float values[MAX_TRACK_VALUES + 1] {};
int top = lua::gettop(L);
int count = std::min({track.valuesCount, top - 2, MAX_TRACK_VALUES});
for (int i = 1; i <= count; i++) {
values[i] = static_cast<float>(lua::tonumber(L, i + 2));
}
static const glm::vec3 AXES[] {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
auto value = [&values](int index, float fallback) {
return index > 0 && index <= MAX_TRACK_VALUES ? values[index] : fallback;
};
auto& matrices = skeleton->pose.matrices;
for (size_t i = 0; i < track.bones.size(); i++) {
int index = track.indices[i];
if (index < 0 || static_cast<size_t>(index) >= matrices.size()) {
continue;
}
const auto& bone = track.bones[i];
auto& matrix = matrices[index];
if (bone.translate) {
matrix = glm::translate(matrix, glm::vec3(
value(bone.translation[0], 0.0f),
value(bone.translation[1], 0.0f),
value(bone.translation[2], 0.0f)
));
}
for (const auto& [axis, v] : bone.rotations) {
matrix = glm::rotate(matrix, glm::radians(value(v, 0.0f)), AXES[axis]);
}
if (bone.scale) {
matrix = glm::scale(matrix, glm::vec3(
value(bone.scaling[0], 1.0f),
value(bone.scaling[1], 1.0f),
value(bone.scaling[2], 1.0f)
));
}
}
return 0;
}
static int l_is_culled(lua::State* L) {
if (auto skeleton = get_skeleton(L)) {
return lua::pushboolean(L, skeleton->culled);
}
return lua::pushboolean(L, false);
}
const luaL_Reg skeletonlib[] = {
{"get_model", lua::wrap<l_get_model>},
{"set_model", lua::wrap<l_set_model>},
@ -212,5 +383,8 @@ const luaL_Reg skeletonlib[] = {
{"is_interpolated", lua::wrap<l_is_interpolated>},
{"set_interpolated", lua::wrap<l_set_interpolated>},
{"exists", lua::wrap<l_exists>},
{"is_culled", lua::wrap<l_is_culled>},
{"register_track", lua::wrap<l_register_track>},
{"apply_track", lua::wrap<l_apply_track>},
{nullptr, nullptr}
};

View file

@ -70,7 +70,7 @@ static int l_get_def(lua::State* L) {
static int l_spawn(lua::State* L) {
auto level = controller->getLevel();
auto defname = lua::tostring(L, 1);
auto defname = lua::require_string(L, 1);
auto& def = content->entities.require(defname);
auto pos = lua::tovec3(L, 2);
dv::value args = nullptr;

View file

@ -211,59 +211,36 @@ static int l_tostring(lua::State* L) {
return lua::pushstring(L, ss.str());
}
#define VECLIB_BASE(n) \
{"add", lua::wrap<l_binop<n, std::plus>>}, \
{"sub", lua::wrap<l_binop<n, std::minus>>}, \
{"mul", lua::wrap<l_binop<n, std::multiplies>>}, \
{"div", lua::wrap<l_binop<n, std::divides>>}, \
{"distance", lua::wrap<l_distance<n>>}, \
{"normalize", lua::wrap<l_unaryop<n, glm::normalize>>}, \
{"length", lua::wrap<l_scalar_op<n, glm::length>>}, \
{"tostring", lua::wrap<l_tostring<n>>}, \
{"abs", lua::wrap<l_unaryop<n, glm::abs>>}, \
{"round", lua::wrap<l_unaryop<n, glm::round>>}, \
{"inverse", lua::wrap<l_inverse<n>>}, \
{"pow", lua::wrap<l_pow<n>>}, \
{"dot", lua::wrap<l_dot<n>>}, \
{"mix", lua::wrap<l_mix<n>>},
const luaL_Reg vec2lib[] = {
{"add", lua::wrap<l_binop<2, std::plus>>},
{"sub", lua::wrap<l_binop<2, std::minus>>},
{"mul", lua::wrap<l_binop<2, std::multiplies>>},
{"div", lua::wrap<l_binop<2, std::divides>>},
{"distance", lua::wrap<l_distance<2>>},
{"normalize", lua::wrap<l_unaryop<2, glm::normalize>>},
{"length", lua::wrap<l_scalar_op<2, glm::length>>},
{"tostring", lua::wrap<l_tostring<2>>},
{"abs", lua::wrap<l_unaryop<2, glm::abs>>},
{"round", lua::wrap<l_unaryop<2, glm::round>>},
{"inverse", lua::wrap<l_inverse<2>>},
{"pow", lua::wrap<l_pow<2>>},
{"dot", lua::wrap<l_dot<2>>},
VECLIB_BASE(2)
{"angle", lua::wrap<l_vec2_angle>},
{"mix", lua::wrap<l_mix<2>>},
{"rotate", lua::wrap<l_vec2_rotate>},
{nullptr, nullptr}
};
const luaL_Reg vec3lib[] = {
{"add", lua::wrap<l_binop<3, std::plus>>},
{"sub", lua::wrap<l_binop<3, std::minus>>},
{"mul", lua::wrap<l_binop<3, std::multiplies>>},
{"div", lua::wrap<l_binop<3, std::divides>>},
{"distance", lua::wrap<l_distance<3>>},
{"normalize", lua::wrap<l_unaryop<3, glm::normalize>>},
{"length", lua::wrap<l_scalar_op<3, glm::length>>},
{"tostring", lua::wrap<l_tostring<3>>},
{"abs", lua::wrap<l_unaryop<3, glm::abs>>},
{"round", lua::wrap<l_unaryop<3, glm::round>>},
{"inverse", lua::wrap<l_inverse<3>>},
{"pow", lua::wrap<l_pow<3>>},
{"dot", lua::wrap<l_dot<3>>},
VECLIB_BASE(3)
{"spherical_rand", lua::wrap<l_spherical_rand>},
{"mix", lua::wrap<l_mix<3>>},
{nullptr, nullptr}
};
const luaL_Reg vec4lib[] = {
{"add", lua::wrap<l_binop<4, std::plus>>},
{"sub", lua::wrap<l_binop<4, std::minus>>},
{"mul", lua::wrap<l_binop<4, std::multiplies>>},
{"div", lua::wrap<l_binop<4, std::divides>>},
{"distance", lua::wrap<l_distance<4>>},
{"normalize", lua::wrap<l_unaryop<4, glm::normalize>>},
{"length", lua::wrap<l_scalar_op<4, glm::length>>},
{"tostring", lua::wrap<l_tostring<4>>},
{"abs", lua::wrap<l_unaryop<4, glm::abs>>},
{"round", lua::wrap<l_unaryop<4, glm::round>>},
{"inverse", lua::wrap<l_inverse<4>>},
{"pow", lua::wrap<l_pow<4>>},
{"dot", lua::wrap<l_dot<4>>},
{"mix", lua::wrap<l_mix<4>>},
VECLIB_BASE(4)
{nullptr, nullptr}
};