Skip to content

Commit

Permalink
Fixed some errors in importing legacy nav mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkTreasure1 committed Mar 13, 2024
1 parent 11845fb commit 634168f
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 36 deletions.
101 changes: 101 additions & 0 deletions Volt/Navigation/src/Navigation/Filesystem/NavMeshImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,38 @@ namespace Volt
}
}

bool NavMeshImporter::LoadNavMeshLegacy(std::ifstream& input, Ref<dtNavMesh>& asset)
{
std::streampos currentPosition = input.tellg();

// Read header.
NavMeshSetHeader header;
if (!input.read(reinterpret_cast<char*>(&header), sizeof(NavMeshSetHeader)))
{
input.close();
return false;
}

input.seekg(currentPosition);

switch (header.magic)
{
case NAVMESHSET_MAGIC:
{
return LoadSingleNavMeshLegacy(input, asset);
}
case TILECACHESET_MAGIC:
{
//return LoadTiledNavMesh(input, asset);
}
default:
{
input.close();
return false;
}
}
}

bool NavMeshImporter::SaveSingleNavMesh(BinaryStreamWriter& output, Ref<NavMesh>& asset)
{
const auto* mesh = asset->GetNavMesh()->GetNavMesh().get();
Expand Down Expand Up @@ -142,5 +174,74 @@ namespace Volt
{
return true;
}

bool NavMeshImporter::LoadSingleNavMeshLegacy(std::ifstream& input, Ref<dtNavMesh>& asset)
{
// Read header.
NavMeshSetHeader header;
if (!input.read(reinterpret_cast<char*>(&header), sizeof(NavMeshSetHeader)))
{
input.close();
return false;
}

if (header.magic != NAVMESHSET_MAGIC)
{
input.close();
return false;
}
if (header.version != NAVMESHSET_VERSION)
{
input.close();
return false;
}

dtNavMesh* mesh = dtAllocNavMesh();
if (!mesh)
{
input.close();
dtFree(mesh);
return false;
}
dtStatus status = mesh->init(&header.params);
if (dtStatusFailed(status))
{
input.close();
dtFree(mesh);
return false;
}

// Read tiles.
for (int i = 0; i < header.numTiles; ++i)
{
NavMeshTileHeader tileHeader;
if (!input.read(reinterpret_cast<char*>(&tileHeader), sizeof(NavMeshTileHeader)))
{
input.close();
dtFree(mesh);
return false;
}

if (!tileHeader.tileRef || !tileHeader.dataSize)
break;

unsigned char* data = (unsigned char*)dtAlloc(tileHeader.dataSize, DT_ALLOC_PERM);
if (!data) break;
memset(data, 0, tileHeader.dataSize);
if (!input.read(reinterpret_cast<char*>(data), tileHeader.dataSize))
{
dtFree(data);
dtFree(mesh);
input.close();
return false;
}

mesh->addTile(data, tileHeader.dataSize, DT_TILE_FREE_DATA, tileHeader.tileRef, 0);
}

asset = CreateRef<dtNavMesh>();
asset.reset(mesh);
return true;
}
}
}
4 changes: 4 additions & 0 deletions Volt/Navigation/src/Navigation/Filesystem/NavMeshImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace Volt
static bool SaveNavMesh(BinaryStreamWriter& output, Ref<NavMesh>& asset);
static bool LoadNavMesh(BinaryStreamReader& input, Ref<dtNavMesh>& asset);

static bool LoadNavMeshLegacy(std::ifstream& input, Ref<dtNavMesh>& asset);

private:
struct NavMeshSetHeader
{
Expand All @@ -40,6 +42,8 @@ namespace Volt

static bool LoadSingleNavMesh(BinaryStreamReader& input, const NavMeshSetHeader& header, Ref<dtNavMesh>& asset);
static bool LoadTiledNavMesh(BinaryStreamReader& input, const NavMeshSetHeader& header, Ref<dtNavMesh>& asset);

static bool LoadSingleNavMeshLegacy(std::ifstream& input, Ref<dtNavMesh>& asset);
};
}
}
2 changes: 1 addition & 1 deletion Volt/Sandbox/src/Sandbox/Modals/ProjectUpgradeModal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void ProjectUpgradeModal::UpgradeCurrentProject()
ConvertAnimationGraphsToV0_1_2();
}

if (projectVersion.GetMinor() < 3 && projectVersion.GetMajor() == 0)
if (projectVersion.GetPatch() < 3 && projectVersion.GetMinor() < 2 && projectVersion.GetMajor() == 0)
{
ConvertPrefabsToV113();
ConvertScenesToV113();
Expand Down
4 changes: 0 additions & 4 deletions Volt/Volt/src/Volt/Asset/Importers/AssetImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ namespace Volt
material->InvalidatePipeline(shader);
}


auto materialDataNode = materialNode["data"];
if (materialDataNode)
{
Expand Down Expand Up @@ -868,8 +867,6 @@ namespace Volt

bool PostProcessingMaterialImporter::Load(const AssetMetadata& metadata, Ref<Asset>& asset) const
{
asset = CreateRef<PostProcessingMaterial>();

const auto filePath = AssetManager::GetFilesystemPath(metadata.filePath);

if (!std::filesystem::exists(filePath))
Expand Down Expand Up @@ -922,7 +919,6 @@ namespace Volt
shader = Renderer::GetDefaultData().defaultPostProcessingShader;
}

asset = CreateRef<PostProcessingMaterial>();
Ref<PostProcessingMaterial> postMat = std::reinterpret_pointer_cast<PostProcessingMaterial>(asset);
postMat->Initialize(shader);

Expand Down
10 changes: 5 additions & 5 deletions Volt/Volt/src/Volt/Asset/Importers/VTNavMeshImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ namespace Volt
}

auto dtnm = CreateRef<dtNavMesh>();
//if (!AI::NavMeshImporter::LoadNavMesh(file, dtnm))
//{
// return false;
//}
if (!AI::NavMeshImporter::LoadNavMeshLegacy(file, dtnm))
{
return false;
}

file.close();

asset = CreateRef<AI::NavMesh>();
std::reinterpret_pointer_cast<AI::NavMesh>(asset)->Initialize(dtnm);
return true;
}

Expand Down
26 changes: 0 additions & 26 deletions Volt/Volt/src/Volt/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
#include <GLFW/glfw3.h>
#include <imgui.h>

#include "Volt/Asset/ImportersNew/MeshSerializer.h"
#include "Volt/Asset/ImportersNew/SceneSerializer.h"

namespace Volt
{
Application::Application(const ApplicationInfo& info)
Expand Down Expand Up @@ -138,29 +135,6 @@ namespace Volt
{
m_steamImplementation = SteamImplementation::Create();
}


Ref<MeshSerializer> serializer = CreateRef<MeshSerializer>();
Ref<Mesh> mesh = AssetManager::GetAsset<Mesh>("Assets/SM_Cube.vtmesh");

AssetMetadata metadata{};
metadata.filePath = "Assets/TestCube.vtasset";

serializer->Serialize(metadata, mesh);

Ref<Mesh> testMesh = CreateRef<Mesh>();
serializer->Deserialize(metadata, testMesh);

Ref<SceneSerializer> sceneSerializer = CreateRef<SceneSerializer>();
Ref<Scene> scene = Scene::CreateDefaultScene("TestScene");

metadata.filePath = "Assets/TestScene/TestScene.vtasset";
sceneSerializer->Serialize(metadata, scene);

std::this_thread::sleep_for(std::chrono::milliseconds(100));

Ref<Scene> testScene = CreateRef<Scene>();
sceneSerializer->Deserialize(metadata, testScene);
}

Application::~Application()
Expand Down

0 comments on commit 634168f

Please sign in to comment.