Skip to content

Commit

Permalink
feat(ecs): add BaseBVHComponent::GetVertex
Browse files Browse the repository at this point in the history
- Add Lua binding
  • Loading branch information
Silverlan committed Jan 12, 2023
1 parent fff66d9 commit a62a322
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Expand Up @@ -108,6 +108,7 @@ namespace pragma
bool SetVertexData(const std::vector<BvhTriangle> &data);
void RebuildBvh();
void ClearBvh();
std::optional<Vector3> GetVertex(size_t idx) const;

// For internal use only
static std::shared_ptr<pragma::BvhData> RebuildBvh(
Expand Down
26 changes: 26 additions & 0 deletions core/shared/src/entities/components/base_bvh_component.cpp
Expand Up @@ -289,6 +289,32 @@ std::shared_ptr<BvhData> BaseBvhComponent::SetBvhData(std::shared_ptr<BvhData> &
return tmp;
}

std::optional<Vector3> BaseBvhComponent::GetVertex(size_t idx) const
{
std::scoped_lock lock {m_bvhDataMutex};
auto primIdx = idx /3;
if(primIdx >= m_bvhData->primitives.size())
return {};
auto &prim = m_bvhData->primitives[primIdx];
auto subIdx = idx %3;
switch(subIdx)
{
case 0:
return *reinterpret_cast<const Vector3*>(&prim.p0.values);
case 1:
{
auto p1 = prim.p1();
return *reinterpret_cast<const Vector3*>(&p1.values);
}
case 2:
{
auto p2 = prim.p2();
return *reinterpret_cast<const Vector3*>(&p2.values);
}
}
return {};
}

bool BaseBvhComponent::SetVertexData(pragma::BvhData &bvhData,const std::vector<BvhTriangle> &data)
{
if(bvhData.primitives.size() != data.size())
Expand Down
1 change: 1 addition & 0 deletions core/shared/src/lua/classes/entity_components.cpp
Expand Up @@ -531,6 +531,7 @@ void pragma::lua::register_entity_component_classes(luabind::module_ &mod)

auto defBvh = Lua::create_base_entity_component_class<pragma::BaseBvhComponent>("BaseBvhComponent");
defBvh.def("RebuildBvh",static_cast<void(pragma::BaseBvhComponent::*)()>(&pragma::BaseBvhComponent::RebuildBvh));
defBvh.def("GetVertex",&pragma::BaseBvhComponent::GetVertex);
defBvh.def("IntersectionTest2",+[](pragma::BaseBvhComponent &c,const Vector3 &origin,const Vector3 &dir,float minDist,float maxDist) {
auto t = std::chrono::steady_clock::now();
c.IntersectionTest(origin,dir,minDist,maxDist);
Expand Down

0 comments on commit a62a322

Please sign in to comment.