Skip to content

Commit

Permalink
more lua bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
turanszkij committed Feb 24, 2024
1 parent 19d88ab commit a40562d
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 30 deletions.
2 changes: 1 addition & 1 deletion WickedEngine/wiPathQuery_BindLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace wi::lua
return 0;
}

pathquery.process(start->GetFloat3(), goal->GetFloat3(), voxelgrid->voxelgrid);
pathquery.process(start->GetFloat3(), goal->GetFloat3(), *voxelgrid->voxelgrid);

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion WickedEngine/wiRenderer_BindLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ namespace wi::lua::renderer
VoxelGrid_BindLua* a = Luna<VoxelGrid_BindLua>::lightcheck(L, 1);
if (a)
{
wi::renderer::DrawVoxelGrid(&a->voxelgrid);
wi::renderer::DrawVoxelGrid(a->voxelgrid);
}
else
wi::lua::SError(L, "DrawVoxelGrid(VoxelGrid voxelgrid) first argument must be a VoxelGrid type!");
Expand Down
2 changes: 1 addition & 1 deletion WickedEngine/wiScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5817,7 +5817,7 @@ namespace wi::scene
}
}

void Scene::VoxelizeScene(wi::VoxelGrid& voxelgrid, bool subtract, wi::enums::FILTER filterMask, uint32_t layerMask, uint32_t lod)
void Scene::VoxelizeScene(wi::VoxelGrid& voxelgrid, bool subtract, uint32_t filterMask, uint32_t layerMask, uint32_t lod)
{
wi::jobsystem::context ctx;
if ((filterMask & FILTER_COLLIDER))
Expand Down
2 changes: 1 addition & 1 deletion WickedEngine/wiScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ namespace wi::scene
void VoxelizeObject(size_t objectIndex, wi::VoxelGrid& grid, bool subtract = false, uint32_t lod = 0);

// Voxelize all meshes that match the filters into a voxel grid
void VoxelizeScene(wi::VoxelGrid& voxelgrid, bool subtract = false, wi::enums::FILTER filterMask = wi::enums::FILTER_ALL, uint32_t layerMask = ~0, uint32_t lod = 0);
void VoxelizeScene(wi::VoxelGrid& voxelgrid, bool subtract = false, uint32_t filterMask = wi::enums::FILTER_ALL, uint32_t layerMask = ~0, uint32_t lod = 0);

// Get the current position on the surface of an object, tracked by the triangle barycentrics
XMFLOAT3 GetPositionOnSurface(wi::ecs::Entity objectEntity, int vertexID0, int vertexID1, int vertexID2, const XMFLOAT2& bary) const;
Expand Down
124 changes: 122 additions & 2 deletions WickedEngine/wiScene_BindLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ Luna<Scene_BindLua>::FunctionType Scene_BindLua::methods[] = {
lunamethod(Scene_BindLua, Component_CreateDecal),
lunamethod(Scene_BindLua, Component_CreateSprite),
lunamethod(Scene_BindLua, Component_CreateFont),
lunamethod(Scene_BindLua, Component_CreateVoxelGrid),

lunamethod(Scene_BindLua, Component_GetName),
lunamethod(Scene_BindLua, Component_GetLayer),
Expand All @@ -567,6 +568,7 @@ Luna<Scene_BindLua>::FunctionType Scene_BindLua::methods[] = {
lunamethod(Scene_BindLua, Component_GetDecal),
lunamethod(Scene_BindLua, Component_GetSprite),
lunamethod(Scene_BindLua, Component_GetFont),
lunamethod(Scene_BindLua, Component_GetVoxelGrid),

lunamethod(Scene_BindLua, Component_GetNameArray),
lunamethod(Scene_BindLua, Component_GetLayerArray),
Expand All @@ -593,6 +595,7 @@ Luna<Scene_BindLua>::FunctionType Scene_BindLua::methods[] = {
lunamethod(Scene_BindLua, Component_GetDecalArray),
lunamethod(Scene_BindLua, Component_GetSpriteArray),
lunamethod(Scene_BindLua, Component_GetFontArray),
lunamethod(Scene_BindLua, Component_GetVoxelGridArray),

lunamethod(Scene_BindLua, Entity_GetNameArray),
lunamethod(Scene_BindLua, Entity_GetLayerArray),
Expand All @@ -619,7 +622,7 @@ Luna<Scene_BindLua>::FunctionType Scene_BindLua::methods[] = {
lunamethod(Scene_BindLua, Entity_GetHumanoidArray),
lunamethod(Scene_BindLua, Entity_GetDecalArray),
lunamethod(Scene_BindLua, Entity_GetSpriteArray),
lunamethod(Scene_BindLua, Entity_GetFontArray),
lunamethod(Scene_BindLua, Entity_GetVoxelGridArray),

lunamethod(Scene_BindLua, Component_RemoveName),
lunamethod(Scene_BindLua, Component_RemoveLayer),
Expand Down Expand Up @@ -647,6 +650,7 @@ Luna<Scene_BindLua>::FunctionType Scene_BindLua::methods[] = {
lunamethod(Scene_BindLua, Component_RemoveDecal),
lunamethod(Scene_BindLua, Component_RemoveSprite),
lunamethod(Scene_BindLua, Component_RemoveFont),
lunamethod(Scene_BindLua, Component_RemoveVoxelGrid),

lunamethod(Scene_BindLua, Component_Attach),
lunamethod(Scene_BindLua, Component_Detach),
Expand All @@ -657,6 +661,7 @@ Luna<Scene_BindLua>::FunctionType Scene_BindLua::methods[] = {
lunamethod(Scene_BindLua, SetWeather),
lunamethod(Scene_BindLua, RetargetAnimation),
lunamethod(Scene_BindLua, VoxelizeObject),
lunamethod(Scene_BindLua, VoxelizeScene),
{ NULL, NULL }
};
Luna<Scene_BindLua>::PropertyType Scene_BindLua::properties[] = {
Expand Down Expand Up @@ -1265,6 +1270,23 @@ int Scene_BindLua::Component_CreateFont(lua_State* L)
}
return 0;
}
int Scene_BindLua::Component_CreateVoxelGrid(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 0)
{
Entity entity = (Entity)wi::lua::SGetLongLong(L, 1);

wi::VoxelGrid& component = scene->voxel_grids.Create(entity);
Luna<VoxelGrid_BindLua>::push(L, component);
return 1;
}
else
{
wi::lua::SError(L, "Scene::Component_CreateVoxelGrid(Entity entity) not enough arguments!");
}
return 0;
}

int Scene_BindLua::Component_GetName(lua_State* L)
{
Expand Down Expand Up @@ -1816,6 +1838,28 @@ int Scene_BindLua::Component_GetFont(lua_State* L)
}
return 0;
}
int Scene_BindLua::Component_GetVoxelGrid(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 0)
{
Entity entity = (Entity)wi::lua::SGetLongLong(L, 1);

wi::VoxelGrid* component = scene->voxel_grids.GetComponent(entity);
if (component == nullptr)
{
return 0;
}

Luna<VoxelGrid_BindLua>::push(L, *component);
return 1;
}
else
{
wi::lua::SError(L, "Scene::Component_GetVoxelGrid(Entity entity) not enough arguments!");
}
return 0;
}

int Scene_BindLua::Component_GetNameArray(lua_State* L)
{
Expand Down Expand Up @@ -2092,6 +2136,17 @@ int Scene_BindLua::Component_GetFontArray(lua_State* L)
}
return 1;
}
int Scene_BindLua::Component_GetVoxelGridArray(lua_State* L)
{
lua_createtable(L, (int)scene->voxel_grids.GetCount(), 0);
int newTable = lua_gettop(L);
for (size_t i = 0; i < scene->voxel_grids.GetCount(); ++i)
{
Luna<VoxelGrid_BindLua>::push(L, scene->voxel_grids[i]);
lua_rawseti(L, newTable, lua_Integer(i + 1));
}
return 1;
}

int Scene_BindLua::Entity_GetNameArray(lua_State* L)
{
Expand Down Expand Up @@ -2379,6 +2434,17 @@ int Scene_BindLua::Entity_GetFontArray(lua_State* L)
}
return 1;
}
int Scene_BindLua::Entity_GetVoxelGridArray(lua_State* L)
{
lua_createtable(L, (int)scene->voxel_grids.GetCount(), 0);
int newTable = lua_gettop(L);
for (size_t i = 0; i < scene->voxel_grids.GetCount(); ++i)
{
wi::lua::SSetLongLong(L, scene->voxel_grids.GetEntity(i));
lua_rawseti(L, newTable, lua_Integer(i + 1));
}
return 1;
}

int Scene_BindLua::Component_RemoveName(lua_State* L)
{
Expand Down Expand Up @@ -2822,6 +2888,23 @@ int Scene_BindLua::Component_RemoveFont(lua_State* L)
}
return 0;
}
int Scene_BindLua::Component_RemoveVoxelGrid(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc > 0)
{
Entity entity = (Entity)wi::lua::SGetLongLong(L, 1);
if (scene->voxel_grids.Contains(entity))
{
scene->voxel_grids.Remove(entity);
}
}
else
{
wi::lua::SError(L, "Scene::Component_RemoveVoxelGrid(Entity entity) not enough arguments!");
}
return 0;
}

int Scene_BindLua::Component_Attach(lua_State* L)
{
Expand Down Expand Up @@ -2967,7 +3050,44 @@ int Scene_BindLua::VoxelizeObject(lua_State* L)
lod = (uint32_t)wi::lua::SGetInt(L, 4);
}
}
scene->VoxelizeObject(objectIndex, voxelgrid->voxelgrid, subtract, lod);
scene->VoxelizeObject(objectIndex, *voxelgrid->voxelgrid, subtract, lod);
return 0;
}
int Scene_BindLua::VoxelizeScene(lua_State* L)
{
int argc = wi::lua::SGetArgCount(L);
if (argc < 1)
{
wi::lua::SError(L, "VoxelizeScene(VoxelGrid voxelgrid, opt bool subtract = false, opt uint filterMask = ~0u, opt uint layerMask = ~0u, opt uint lod = 0) not enough arguments!");
return 0;
}
VoxelGrid_BindLua* voxelgrid = Luna<VoxelGrid_BindLua>::lightcheck(L, 1);
if (voxelgrid == nullptr)
{
wi::lua::SError(L, "VoxelizeScene(VoxelGrid voxelgrid, opt bool subtract = false, opt uint filterMask = ~0u, opt uint layerMask = ~0u, opt uint lod = 0) first argument is not a VoxelGrid!");
return 0;
}
bool subtract = false;
uint32_t filterMask = wi::enums::FILTER_ALL;
uint32_t layerMask = ~0u;
uint32_t lod = 0;
if (argc > 1)
{
subtract = wi::lua::SGetBool(L, 2);
if (argc > 2)
{
filterMask = (uint32_t)wi::lua::SGetInt(L, 3);
if (argc > 3)
{
layerMask = (uint32_t)wi::lua::SGetInt(L, 4);
if (argc > 4)
{
lod = (uint32_t)wi::lua::SGetInt(L, 5);
}
}
}
}
scene->VoxelizeScene(*voxelgrid->voxelgrid, subtract, filterMask, layerMask, lod);
return 0;
}

Expand Down
6 changes: 6 additions & 0 deletions WickedEngine/wiScene_BindLua.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace wi::lua::scene
int Component_CreateDecal(lua_State* L);
int Component_CreateSprite(lua_State* L);
int Component_CreateFont(lua_State* L);
int Component_CreateVoxelGrid(lua_State* L);

int Component_GetName(lua_State* L);
int Component_GetLayer(lua_State* L);
Expand All @@ -93,6 +94,7 @@ namespace wi::lua::scene
int Component_GetDecal(lua_State* L);
int Component_GetSprite(lua_State* L);
int Component_GetFont(lua_State* L);
int Component_GetVoxelGrid(lua_State* L);

int Component_GetNameArray(lua_State* L);
int Component_GetLayerArray(lua_State* L);
Expand All @@ -119,6 +121,7 @@ namespace wi::lua::scene
int Component_GetDecalArray(lua_State* L);
int Component_GetSpriteArray(lua_State* L);
int Component_GetFontArray(lua_State* L);
int Component_GetVoxelGridArray(lua_State* L);

int Entity_GetNameArray(lua_State* L);
int Entity_GetLayerArray(lua_State* L);
Expand Down Expand Up @@ -146,6 +149,7 @@ namespace wi::lua::scene
int Entity_GetDecalArray(lua_State* L);
int Entity_GetSpriteArray(lua_State* L);
int Entity_GetFontArray(lua_State* L);
int Entity_GetVoxelGridArray(lua_State* L);

int Component_RemoveName(lua_State* L);
int Component_RemoveLayer(lua_State* L);
Expand Down Expand Up @@ -173,6 +177,7 @@ namespace wi::lua::scene
int Component_RemoveDecal(lua_State* L);
int Component_RemoveSprite(lua_State* L);
int Component_RemoveFont(lua_State* L);
int Component_RemoveVoxelGrid(lua_State* L);

int Component_Attach(lua_State* L);
int Component_Detach(lua_State* L);
Expand All @@ -186,6 +191,7 @@ namespace wi::lua::scene
int RetargetAnimation(lua_State* L);

int VoxelizeObject(lua_State* L);
int VoxelizeScene(lua_State* L);
};

class NameComponent_BindLua
Expand Down
25 changes: 25 additions & 0 deletions WickedEngine/wiVoxelGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,31 @@ namespace wi
set_voxelsize(XMFLOAT3(halfwidth.x / resolution.x, halfwidth.y / resolution.y, halfwidth.z / resolution.z));
}

void VoxelGrid::add(const VoxelGrid& other)
{
if (voxels.size() != other.voxels.size())
{
assert(0);
return;
}
for (size_t i = 0; i < voxels.size(); ++i)
{
voxels[i] |= other.voxels[i];
}
}
void VoxelGrid::subtract(const VoxelGrid& other)
{
if (voxels.size() != other.voxels.size())
{
assert(0);
return;
}
for (size_t i = 0; i < voxels.size(); ++i)
{
voxels[i] &= ~other.voxels[i];
}
}

void VoxelGrid::Serialize(wi::Archive& archive, wi::ecs::EntitySerializer& seri)
{
if (archive.IsReadMode())
Expand Down
2 changes: 2 additions & 0 deletions WickedEngine/wiVoxelGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ namespace wi
void set_voxelsize(const XMFLOAT3& size);
wi::primitive::AABB get_aabb() const;
void from_aabb(const wi::primitive::AABB& aabb);
void add(const VoxelGrid& other);
void subtract(const VoxelGrid& other);
void debugdraw(const XMFLOAT4X4& ViewProjection, wi::graphics::CommandList cmd) const;

inline bool IsValid() const { return !voxels.empty(); }
Expand Down
Loading

0 comments on commit a40562d

Please sign in to comment.