Skip to content

Commit

Permalink
Update generate TileMap
Browse files Browse the repository at this point in the history
  • Loading branch information
Pham Hong Duc committed Nov 27, 2024
1 parent 2585473 commit 2160119
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 17 deletions.
8 changes: 1 addition & 7 deletions Projects/Recast/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ include_directories(${Recast_INCLUDE_DIR})

set_target_properties(Recast PROPERTIES VERSION ${SKYLICHT_VERSION})

if (BUILD_SHARED_LIBS)
if (BUILD_LINUX OR BUILD_MACOS)
add_compile_options(-fpic)
endif()
endif()

if (INSTALL_LIBS)
install(TARGETS Recast
EXPORT RecastTargets
Expand All @@ -30,4 +24,4 @@ install(EXPORT RecastTargets
NAMESPACE Skylicht::
DESTINATION ${SKYLICHT_TARGET_INSTALL_DIR}
)
endif()
endif()
7 changes: 6 additions & 1 deletion Projects/Skylicht/Graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ setup_project_group("${skylicht_graph_source}" ${CMAKE_CURRENT_SOURCE_DIR})
add_library(Graph ${ENGINE_SHARED_OR_STATIC_LIB} ${skylicht_graph_source})

if (BUILD_SHARED_LIBS)

if (BUILD_LINUX OR BUILD_MACOS)
add_compile_options(-fpic)
endif()

set_target_properties(Graph PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS true)
endif()

Expand All @@ -39,4 +44,4 @@ install(EXPORT GraphTargets
NAMESPACE Skylicht::
DESTINATION ${SKYLICHT_TARGET_INSTALL_DIR}
)
endif()
endif()
4 changes: 2 additions & 2 deletions Projects/Skylicht/Graph/RecastMesh/CRecastBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ namespace Skylicht

const core::aabbox3df& box = mesh->getBBox();
m_cfg.bmin[0] = box.MinEdge.X;
m_cfg.bmin[1] = box.MinEdge.Y;
m_cfg.bmin[1] = box.MinEdge.Y - 0.2f;
m_cfg.bmin[2] = box.MinEdge.Z;
m_cfg.bmax[0] = box.MaxEdge.X;
m_cfg.bmax[1] = box.MaxEdge.Y;
m_cfg.bmax[1] = box.MaxEdge.Y + 0.2f;
m_cfg.bmax[2] = box.MaxEdge.Z;
rcCalcGridSize(m_cfg.bmin, m_cfg.bmax, m_cfg.cs, &m_cfg.width, &m_cfg.height);

Expand Down
53 changes: 52 additions & 1 deletion Projects/Skylicht/Graph/TileMap/CTileMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ namespace Skylicht
tile->Z = z;
tile->BBox.MinEdge = bbox.MinEdge + core::vector3df(x * tileWidth, y * tileHeight, z * tileWidth);
tile->BBox.MaxEdge = tile->BBox.MinEdge + boxSize;
tile->Position = tile->BBox.getCenter();
m_tiles.push_back(tile);
}
}
Expand Down Expand Up @@ -110,14 +109,66 @@ namespace Skylicht
}
}

core::vector3df edges[8];
core::line3df lines[4];

for (int i = (int)m_tiles.size() - 1; i >= 0; i--)
{
if (m_tiles[i]->Tris.size() == 0)
{
delete m_tiles[i];
m_tiles.erase(i);
}
else
{
const core::aabbox3df& bbox = m_tiles[i]->BBox;
bbox.getEdges(edges);

lines[0].setLine(edges[1], edges[0]);
lines[1].setLine(edges[3], edges[2]);
lines[2].setLine(edges[5], edges[4]);
lines[3].setLine(edges[7], edges[6]);

core::vector3df center = bbox.getCenter();
core::vector3df centerTop = center;
core::vector3df centerBottom = center;
centerTop.Y = bbox.MaxEdge.Y;
centerBottom.Y = bbox.MinEdge.Y;
lines[4].setLine(centerTop, centerBottom);

bool allHit = true;

for (int j = 0; j < 4; j++)
{
if (!hitTris(lines[j], m_tiles[i]->Tris, m_tiles[i]->Position))
{
allHit = false;
break;
}
}

if (!allHit)
{
delete m_tiles[i];
m_tiles.erase(i);
}
}
}
}

bool CTileMap::hitTris(const core::line3df& line, core::array<core::triangle3df>& tris, core::vector3df& outPoint)
{
bool hit = false;
for (int i = 0, n = tris.size(); i < n; i++)
{
core::triangle3df& t = tris[i];
if (t.getIntersectionWithLimitedLine(line, outPoint))
{
hit = true;
break;;
}
}
return hit;
}

void CTileMap::release()
Expand Down
1 change: 1 addition & 0 deletions Projects/Skylicht/Graph/TileMap/CTileMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ namespace Skylicht

void generate(float tileWidth, float tileHeight, const core::aabbox3df& bbox);

bool hitTris(const core::line3df& line, core::array<core::triangle3df>& tris, core::vector3df& outPoint);
};
}
}
4 changes: 3 additions & 1 deletion Samples/Graph/Source/CDemo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "GameObject/CZone.h"

#define COLLISION_MODEL "SampleGraph/nav_test.obj"

class CDemo
{
protected:
Expand All @@ -21,4 +23,4 @@ class CDemo
virtual void close() = 0;

virtual void onViewRayClick(const core::line3df& ray, bool holdShift);
};
};
8 changes: 4 additions & 4 deletions Samples/Graph/Source/CDemoNavMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void CDemoNavMesh::init()
{
m_recastMesh = new Graph::CRecastMesh();

CEntityPrefab* mapPrefab = CMeshManager::getInstance()->loadModel("SampleGraph/nav_test.obj", "SampleGraph");
CEntityPrefab* mapPrefab = CMeshManager::getInstance()->loadModel(COLLISION_MODEL, "");
if (mapPrefab)
m_recastMesh->addMeshPrefab(mapPrefab, core::IdentityMatrix);
}
Expand Down Expand Up @@ -131,12 +131,12 @@ void CDemoNavMesh::update()

void CDemoNavMesh::onGUI()
{
if (ImGui::Button("Build NavMesh"))
if (ImGui::Button("Step 1 - Build NavMesh"))
{
buildNavMesh();
}

if (ImGui::Button("Build TileMap"))
if (ImGui::Button("Step 2 - Build TileMap"))
{
buildTileMap();
}
Expand All @@ -157,5 +157,5 @@ void CDemoNavMesh::buildNavMesh()

void CDemoNavMesh::buildTileMap()
{
m_tileMap->generate(4.0, 4.0, m_outputMesh);
m_tileMap->generate(1.0, 1.0, m_outputMesh);
}
2 changes: 1 addition & 1 deletion Samples/Graph/Source/CViewInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void CViewInit::initScene()
plane->getMaterial()->changeShader("BuiltIn/Shader/SpecularGlossiness/Deferred/MetersGrid.xml");

// map
CEntityPrefab* mapPrefab = CMeshManager::getInstance()->loadModel("SampleGraph/nav_test.obj", "SampleGraph");
CEntityPrefab* mapPrefab = CMeshManager::getInstance()->loadModel(COLLISION_MODEL, "");
if (mapPrefab)
{
m_map = zone->createEmptyObject();
Expand Down

0 comments on commit 2160119

Please sign in to comment.