From 5e79bc07e98e5f6325fb4d31b55dd860e2fb05b4 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 15 Oct 2024 22:19:06 +0300 Subject: [PATCH] add Heightmap:mixin(...) --- .../lua/usertypes/lua_type_heightmap.cpp | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/logic/scripting/lua/usertypes/lua_type_heightmap.cpp b/src/logic/scripting/lua/usertypes/lua_type_heightmap.cpp index 06a031da..5dde49f7 100644 --- a/src/logic/scripting/lua/usertypes/lua_type_heightmap.cpp +++ b/src/logic/scripting/lua/usertypes/lua_type_heightmap.cpp @@ -164,6 +164,48 @@ static int l_binop_func(lua::State* L) { return 0; } +static int l_mixin(lua::State* L) { + if (auto heightmap = touserdata(L, 1)) { + uint w = heightmap->getWidth(); + uint h = heightmap->getHeight(); + auto heights = heightmap->getValues(); + + if (isnumber(L, 2)) { + float scalar = tonumber(L, 2); + if (isnumber(L, 3)) { + float t = tonumber(L, 3); + for (uint i = 0; i < w * h; i++) { + heights[i] = heights[i] * (1.0f - t) + scalar * t; + } + } else { + auto tmap = touserdata(L, 3); + auto tmapvalues = tmap->getValues(); + for (uint i = 0; i < w * h; i++) { + float t = tmapvalues[i]; + heights[i] = heights[i] * (1.0f - t) + scalar * t; + } + } + } else { + auto map = touserdata(L, 2); + auto mapvalues = map->getValues(); + if (isnumber(L, 3)) { + float t = tonumber(L, 3); + for (uint i = 0; i < w * h; i++) { + heights[i] = heights[i] * (1.0f - t) + mapvalues[i] * t; + } + } else { + auto tmap = touserdata(L, 3); + auto tmapvalues = tmap->getValues(); + for (uint i = 0; i < w * h; i++) { + float t = tmapvalues[i]; + heights[i] = heights[i] * (1.0f - t) + mapvalues[i] * t; + } + } + } + } + return 0; +} + template class Op> static int l_unaryop_func(lua::State* L) { Op op; @@ -221,6 +263,7 @@ static std::unordered_map methods { {"resize", lua::wrap}, {"crop", lua::wrap}, {"at", lua::wrap}, + {"mixin", lua::wrap}, }; static int l_meta_meta_call(lua::State* L) {