Skip to content

Commit

Permalink
feat(lua): add panima.Channel:DebugPrint, :PopulateRandom, :Decimate
Browse files Browse the repository at this point in the history
  • Loading branch information
Silverlan committed Sep 30, 2023
1 parent 3fee072 commit ce7e0ee
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build_scripts/scripts/external_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
get_submodule("materialsystem","https://github.com/Silverlan/materialsystem.git","6214d38")
get_submodule("mathutil","https://github.com/Silverlan/mathutil.git","5aa9ec46b104b086ddfd090d1be0030c9f2788b5")
get_submodule("networkmanager","https://github.com/Silverlan/networkmanager.git","68d2f5c")
get_submodule("panima","https://github.com/Silverlan/panima.git","26e90c1bf1cc2181d3d4ac2ec63766292dc54fe9")
get_submodule("panima","https://github.com/Silverlan/panima.git","2002d2dae8faf3a24a8a658ffadf1cd42b87fbe0")
get_submodule("prosper","https://github.com/Silverlan/prosper.git","1062716010d53b5c350cad431fcc30b218ef3541")
get_submodule("sharedutils","https://github.com/Silverlan/sharedutils.git","ea6e60be7f59c300484addba83ba41359671df9d")
get_submodule("util_bsp","https://github.com/Silverlan/util_bsp.git","3c11053")
Expand Down
60 changes: 56 additions & 4 deletions core/shared/src/lua/libraries/lanimation_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,21 @@ DEFINE_OSTREAM_OPERATOR_NAMESPACE_ALIAS(panima, Slice);
DEFINE_OSTREAM_OPERATOR_NAMESPACE_ALIAS(panima, AnimationManager);
DEFINE_OSTREAM_OPERATOR_NAMESPACE_ALIAS(panima, Animation);

static uint32_t insert_channel_values(lua_State *l, panima::Channel &channel, const std::vector<float> &times, luabind::tableT<void> tValues, float offset)
static uint32_t insert_channel_values(lua_State *l, panima::Channel &channel, const std::vector<float> &times, luabind::tableT<void> tValues, float offset, panima::Channel::InsertFlags flags)
{
auto numTimes = times.size();
auto numValues = Lua::GetObjectLength(l, tValues);
if(numTimes != numValues)
throw std::runtime_error {"Number of elements in times array (" + std::to_string(numTimes) + ") doesn't match number of values in values array (" + std::to_string(numValues) + ")! This is not allowed."};
auto insertIndex = ::udm::visit(channel.GetValueType(), [l, &tValues, &channel, &times, numValues, offset](auto tag) {
auto insertIndex = ::udm::visit(channel.GetValueType(), [l, &tValues, &channel, &times, numValues, offset, flags](auto tag) {
using T = typename decltype(tag)::type;
using TValue = std::conditional_t<std::is_same_v<T, bool>, uint8_t, T>;
if constexpr(pragma::is_animatable_type_v<TValue>) {
if constexpr(!std::is_same_v<T, bool>) {
auto values = luabind::object_cast<std::vector<TValue>>(tValues);
if(values.size() != times.size())
throw std::runtime_error {"Number of data values does not match number of time values!"};
return channel.InsertValues<TValue>(times.size(), times.data(), values.data(), offset);
return channel.InsertValues<TValue>(times.size(), times.data(), values.data(), offset, flags);
}
else {
std::vector<TValue> values;
Expand All @@ -208,7 +208,7 @@ static uint32_t insert_channel_values(lua_State *l, panima::Channel &channel, co
}
if(values.size() != times.size())
throw std::runtime_error {"Number of data values does not match number of time values!"};
return channel.InsertValues<TValue>(times.size(), times.data(), values.data(), offset);
return channel.InsertValues<TValue>(times.size(), times.data(), values.data(), offset, flags);
}
}
else
Expand All @@ -218,6 +218,7 @@ static uint32_t insert_channel_values(lua_State *l, panima::Channel &channel, co
channel.Update();
return insertIndex;
}
static uint32_t insert_channel_values(lua_State *l, panima::Channel &channel, const std::vector<float> &times, luabind::tableT<void> tValues, float offset) { return insert_channel_values(l, channel, times, tValues, offset, panima::Channel::InsertFlags::ClearExistingDataInRange); }
static uint32_t insert_channel_values(lua_State *l, panima::Channel &channel, const std::vector<float> &times, luabind::tableT<void> tValues) { return insert_channel_values(l, channel, times, tValues, 0.f); }

void Lua::animation::register_library(Lua::Interface &lua)
Expand Down Expand Up @@ -261,6 +262,9 @@ void Lua::animation::register_library(Lua::Interface &lua)
return luabind::object {l, std::pair<std::vector<float>, std::vector<TValue>> {std::move(times), std::move(values)}};
});
})];
cdChannel.add_static_constant("INSERT_FLAG_NONE", umath::to_integral(panima::Channel::InsertFlags::None));
cdChannel.add_static_constant("INSERT_FLAG_BIT_CLEAR_EXISTING_DATA_IN_RANGE", umath::to_integral(panima::Channel::InsertFlags::ClearExistingDataInRange));
cdChannel.add_static_constant("INSERT_FLAG_BIT_DECIMATE_INSERTED_DATA", umath::to_integral(panima::Channel::InsertFlags::DecimateInsertedData));

auto cdPath = luabind::class_<panima::ChannelPath>("Path");
cdPath.def(luabind::constructor<>());
Expand Down Expand Up @@ -330,6 +334,44 @@ void Lua::animation::register_library(Lua::Interface &lua)
return r;
});
cdChannel.def("Validate", &panima::Channel::Validate);
cdChannel.def(
"DebugPrint", +[](panima::Channel &channel) {
auto data = ::udm::Data::Create();
auto udmData = data->GetAssetData().GetData();
channel.Save(udmData);
std::stringstream ss;
data->ToAscii(ss, ::udm::AsciiSaveFlags::DontCompressLz4Arrays);
Con::cout << ss.str() << Con::endl;
});
cdChannel.def(
"PopulateRandom", +[](panima::Channel &channel, uint32_t numValues, float minTime, float maxTime, float minVal, float maxVal) {
return ::udm::visit_ng(channel.GetValueType(), [&channel, numValues, minTime, maxTime, minVal, maxVal](auto tag) {
using T = typename decltype(tag)::type;
using TValue = std::conditional_t<std::is_same_v<T, bool>, uint8_t, T>;
if constexpr(panima::is_animatable_type(::udm::type_to_enum<TValue>())) {
std::vector<float> times;
std::vector<TValue> values;
times.reserve(numValues);
values.reserve(numValues);
auto nc = ::udm::get_numeric_component_count(::udm::type_to_enum<TValue>());
for(auto i = decltype(numValues) {0u}; i < numValues; ++i) {
times.push_back(umath::random(minTime, maxTime));

using TBase = std::conditional_t<std::is_same_v<TValue, ::udm::Quaternion>, ::udm::EulerAngles, TValue>;
TBase value {};
for(auto i = decltype(nc) {0u}; i < nc; ++i)
::udm::get_numeric_component(value, i) = umath::random(minVal, maxVal);
if constexpr(std::is_same_v<TValue, ::udm::Quaternion>)
values.push_back(uquat::create(value));
else
values.push_back(value);
}
std::sort(times.begin(), times.end());
channel.InsertValues<TValue>(numValues, times.data(), values.data());
}
});
});
cdChannel.def("InsertValues", static_cast<uint32_t (*)(lua_State *, panima::Channel &, const std::vector<float> &, luabind::tableT<void>, float, panima::Channel::InsertFlags)>(&insert_channel_values));
cdChannel.def("InsertValues", static_cast<uint32_t (*)(lua_State *, panima::Channel &, const std::vector<float> &, luabind::tableT<void>, float)>(&insert_channel_values));
cdChannel.def("InsertValues", static_cast<uint32_t (*)(lua_State *, panima::Channel &, const std::vector<float> &, luabind::tableT<void>)>(&insert_channel_values));
cdChannel.def(
Expand Down Expand Up @@ -411,6 +453,16 @@ void Lua::animation::register_library(Lua::Interface &lua)
"GetInterpolatedValue", +[](lua_State *l, panima::Channel &channel, float t, bool clampToBounds) -> Lua::opt<Lua::udm_ng> { return get_interpolated_value(l, channel, t, clampToBounds); });
cdChannel.def(
"GetInterpolatedValue", +[](lua_State *l, panima::Channel &channel, float t) -> Lua::opt<Lua::udm_ng> { return get_interpolated_value(l, channel, t, true); });
// Default float argument not supported in clang (state: 23-09-12)
// cdChannel.def("Decimate", static_cast<void (panima::Channel::*)(float, float, float)>(&panima::Channel::Decimate), luabind::default_parameter_policy<4, 0.03f> {});
// cdChannel.def("Decimate", static_cast<void (panima::Channel::*)(float)>(&panima::Channel::Decimate), luabind::default_parameter_policy<2, 0.03f> {});
cdChannel.def("Decimate", static_cast<void (panima::Channel::*)(float, float, float)>(&panima::Channel::Decimate));
cdChannel.def(
"Decimate", +[](panima::Channel &channel, float tStart, float tEnd) { return channel.Decimate(tStart, tEnd); });
cdChannel.def("Decimate", static_cast<void (panima::Channel::*)(float)>(&panima::Channel::Decimate));
cdChannel.def(
"Decimate", +[](panima::Channel &channel) { return channel.Decimate(); });

cdChannel.def(
"GetValue", +[](lua_State *l, panima::Channel &channel, uint32_t idx) -> Lua::udm_ng {
if(idx >= channel.GetValueCount())
Expand Down

0 comments on commit ce7e0ee

Please sign in to comment.