add vecn.mix

This commit is contained in:
MihailRis 2025-09-06 11:37:06 +03:00
parent 88641e032a
commit 11a92a54b2
3 changed files with 52 additions and 7 deletions

View File

@ -110,6 +110,21 @@ function vec3.dot(a, b)
return a[1] * b[1] + a[2] * b[2] + a[3] * b[3] return a[1] * b[1] + a[2] * b[2] + a[3] * b[3]
end end
function vec3.mix(a, b, t, dest)
if dest then
dest[1] = a[1] * (1.0 - t) + b[1] * t
dest[2] = a[2] * (1.0 - t) + b[2] * t
dest[3] = a[3] * (1.0 - t) + b[3] * t
return dest
else
return {
a[1] * (1.0 - t) + b[1] * t,
a[2] * (1.0 - t) + b[2] * t,
a[3] * (1.0 - t) + b[3] * t,
}
end
end
-- =================================================== -- -- =================================================== --
-- ====================== vec2 ======================= -- -- ====================== vec2 ======================= --
-- =================================================== -- -- =================================================== --
@ -210,3 +225,16 @@ end
function vec2.dot(a, b) function vec2.dot(a, b)
return a[1] * b[1] + a[2] * b[2] return a[1] * b[1] + a[2] * b[2]
end end
function vec2.mix(a, b, t, dest)
if dest then
dest[1] = a[1] * (1.0 - t) + b[1] * t
dest[2] = a[2] * (1.0 - t) + b[2] * t
return dest
else
return {
a[1] * (1.0 - t) + b[1] * t,
a[2] * (1.0 - t) + b[2] * t,
}
end
end

View File

@ -15,6 +15,20 @@ inline T angle(glm::vec<2, T> vec) {
return val; return val;
} }
template <int n>
static int l_mix(lua::State* L) {
uint argc = lua::check_argc(L, 3, 4);
auto a = lua::tovec<n, number_t>(L, 1);
auto b = lua::tovec<n, number_t>(L, 2);
auto t = lua::tonumber(L, 3);
if (argc == 3) {
return lua::pushvec(L, a * (1.0 - t) + b * t);
} else {
return lua::setvec(L, 4, a * (1.0 - t) + b * t);
}
}
template <int n, template <class> class Op> template <int n, template <class> class Op>
static int l_binop(lua::State* L) { static int l_binop(lua::State* L) {
uint argc = lua::check_argc(L, 2, 3); uint argc = lua::check_argc(L, 2, 3);
@ -200,6 +214,7 @@ const luaL_Reg vec2lib[] = {
{"pow", lua::wrap<l_pow<2>>}, {"pow", lua::wrap<l_pow<2>>},
{"dot", lua::wrap<l_dot<2>>}, {"dot", lua::wrap<l_dot<2>>},
{"angle", lua::wrap<l_vec2_angle>}, {"angle", lua::wrap<l_vec2_angle>},
{"mix", lua::wrap<l_mix<2>>},
{NULL, NULL}}; {NULL, NULL}};
const luaL_Reg vec3lib[] = { const luaL_Reg vec3lib[] = {
@ -217,6 +232,7 @@ const luaL_Reg vec3lib[] = {
{"pow", lua::wrap<l_pow<3>>}, {"pow", lua::wrap<l_pow<3>>},
{"dot", lua::wrap<l_dot<3>>}, {"dot", lua::wrap<l_dot<3>>},
{"spherical_rand", lua::wrap<l_spherical_rand>}, {"spherical_rand", lua::wrap<l_spherical_rand>},
{"mix", lua::wrap<l_mix<3>>},
{NULL, NULL}}; {NULL, NULL}};
const luaL_Reg vec4lib[] = { const luaL_Reg vec4lib[] = {
@ -233,4 +249,5 @@ const luaL_Reg vec4lib[] = {
{"inverse", lua::wrap<l_inverse<4>>}, {"inverse", lua::wrap<l_inverse<4>>},
{"pow", lua::wrap<l_pow<4>>}, {"pow", lua::wrap<l_pow<4>>},
{"dot", lua::wrap<l_dot<4>>}, {"dot", lua::wrap<l_dot<4>>},
{"mix", lua::wrap<l_mix<4>>},
{NULL, NULL}}; {NULL, NULL}};

View File

@ -48,8 +48,8 @@ namespace lua {
return true; return true;
} }
template <int n> template <int n, typename T = float>
inline int pushvec(lua::State* L, const glm::vec<n, float>& vec) { inline int pushvec(lua::State* L, const glm::vec<n, T>& vec) {
createtable(L, n, 0); createtable(L, n, 0);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
pushnumber(L, vec[i]); pushnumber(L, vec[i]);
@ -161,8 +161,8 @@ namespace lua {
} }
return 1; return 1;
} }
template <int n> template <int n, typename T = float>
inline int setvec(lua::State* L, int idx, glm::vec<n, float> vec) { inline int setvec(lua::State* L, int idx, glm::vec<n, T> vec) {
pushvalue(L, idx); pushvalue(L, idx);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
pushnumber(L, vec[i]); pushnumber(L, vec[i]);
@ -305,15 +305,15 @@ namespace lua {
setglobal(L, name); setglobal(L, name);
} }
template <int n> template <int n, typename T = float>
inline glm::vec<n, float> tovec(lua::State* L, int idx) { inline glm::vec<n, T> tovec(lua::State* L, int idx) {
pushvalue(L, idx); pushvalue(L, idx);
if (!istable(L, idx) || objlen(L, idx) < n) { if (!istable(L, idx) || objlen(L, idx) < n) {
throw std::runtime_error( throw std::runtime_error(
"value must be an array of " + std::to_string(n) + " numbers" "value must be an array of " + std::to_string(n) + " numbers"
); );
} }
glm::vec<n, float> vec; glm::vec<n, T> vec;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
rawgeti(L, i + 1); rawgeti(L, i + 1);
vec[i] = tonumber(L, -1); vec[i] = tonumber(L, -1);