Skip to content

Commit

Permalink
Started concepting new asset serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkTreasure1 committed Feb 29, 2024
1 parent 2c383f9 commit 2baa32b
Show file tree
Hide file tree
Showing 27 changed files with 566 additions and 133 deletions.
6 changes: 4 additions & 2 deletions Volt/Sandbox/src/Sandbox/Utility/EditorResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <Volt/Asset/Importers/TextureImporter.h>
#include <Volt/Asset/Importers/MeshTypeImporter.h>

#include <Volt/Asset/Mesh/Mesh.h>

#include <Volt/Rendering/Shape.h>
#include <Volt/Rendering/Renderer.h>
#include <Volt/Rendering/Texture/Texture2D.h>
Expand Down Expand Up @@ -152,8 +154,8 @@ Ref<Volt::Texture2D> EditorResources::TryLoadIcon(const std::filesystem::path& p

Ref<Volt::Mesh> EditorResources::TryLoadMesh(const std::filesystem::path& path)
{
Ref<Volt::Mesh> mesh = Volt::MeshTypeImporter::ImportMesh(path);
if (!mesh)
Ref<Volt::Mesh> mesh = CreateRef<Volt::Mesh>();
if (!Volt::MeshTypeImporter::ImportMesh(path, *mesh))
{
mesh = Volt::Shape::CreateUnitCube();
}
Expand Down
20 changes: 10 additions & 10 deletions Volt/Sandbox/src/Sandbox/Utility/EditorUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ bool EditorUtils::ReimportSourceMesh(Volt::AssetHandle assetHandle, Ref<Volt::Sk
{
case Volt::AssetType::Animation:
{
Ref<Volt::Animation> newAnim = Volt::MeshTypeImporter::ImportAnimation(Volt::ProjectManager::GetDirectory() / sourcePath, targetSkeleton);
if (!newAnim)
Ref<Volt::Animation> newAnim = CreateRef<Volt::Animation>();
if (!Volt::MeshTypeImporter::ImportAnimation(Volt::ProjectManager::GetDirectory() / sourcePath, targetSkeleton, *newAnim))
{
UI::Notify(NotificationType::Error, "Unable to re import animation!", std::format("Failed to import animation from {0}!", sourcePath.string()));
break;
Expand All @@ -253,8 +253,8 @@ bool EditorUtils::ReimportSourceMesh(Volt::AssetHandle assetHandle, Ref<Volt::Sk

case Volt::AssetType::Skeleton:
{
Ref<Volt::Skeleton> newSkel = Volt::MeshTypeImporter::ImportSkeleton(Volt::ProjectManager::GetDirectory() / sourcePath);
if (!newSkel)
Ref<Volt::Skeleton> newSkel = CreateRef<Volt::Skeleton>();
if (!Volt::MeshTypeImporter::ImportSkeleton(Volt::ProjectManager::GetDirectory() / sourcePath, *newSkel))
{
UI::Notify(NotificationType::Error, "Unable to re import skeleton!", std::format("Failed to import skeleton from {0}!", sourcePath.string()));
break;
Expand All @@ -274,8 +274,8 @@ bool EditorUtils::ReimportSourceMesh(Volt::AssetHandle assetHandle, Ref<Volt::Sk
{
Ref<Volt::Mesh> originalMesh = std::reinterpret_pointer_cast<Volt::Mesh>(originalAsset);

Ref<Volt::Mesh> newMesh = Volt::MeshTypeImporter::ImportMesh(Volt::ProjectManager::GetDirectory() / sourcePath);
if (!newMesh || !newMesh->IsValid())
Ref<Volt::Mesh> newMesh = CreateRef<Volt::Mesh>();
if (!Volt::MeshTypeImporter::ImportMesh(Volt::ProjectManager::GetDirectory() / sourcePath, *newMesh))
{
UI::Notify(NotificationType::Error, "Unable to re import mesh!", std::format("Failed to import mesh from {0}!", sourcePath.string()));
break;
Expand Down Expand Up @@ -503,8 +503,8 @@ ImportState EditorUtils::MeshImportModal(const std::string& aId, MeshImportData&

if (aImportData.importSkeleton)
{
Ref<Volt::Skeleton> skeleton = Volt::MeshTypeImporter::ImportSkeleton(Volt::ProjectManager::GetDirectory() / aMeshToImport);
if (!skeleton)
Ref<Volt::Skeleton> skeleton = CreateRef<Volt::Skeleton>();
if (!Volt::MeshTypeImporter::ImportSkeleton(Volt::ProjectManager::GetDirectory() / aMeshToImport, *skeleton))
{
UI::Notify(NotificationType::Error, "Failed to import skeleton!", std::format("Failed to import skeleton from {}!", aMeshToImport.string()));
}
Expand All @@ -527,8 +527,8 @@ ImportState EditorUtils::MeshImportModal(const std::string& aId, MeshImportData&
}
else
{
Ref<Volt::Animation> animation = Volt::MeshTypeImporter::ImportAnimation(Volt::ProjectManager::GetDirectory() / aMeshToImport, targetSkeleton);
if (!animation)
Ref<Volt::Animation> animation = CreateRef<Volt::Animation>();
if (!Volt::MeshTypeImporter::ImportAnimation(Volt::ProjectManager::GetDirectory() / aMeshToImport, targetSkeleton, *animation))
{
UI::Notify(NotificationType::Error, "Failed to import animation!", std::format("Failed to import animaition from {}!", aMeshToImport.string()));
}
Expand Down
91 changes: 44 additions & 47 deletions Volt/Volt/src/Volt/Asset/Importers/FbxImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace Volt
{
Ref<Mesh> FbxImporter::ImportMeshImpl(const std::filesystem::path& path)
bool FbxImporter::ImportMeshImpl(const std::filesystem::path& path, Mesh& dstMesh)
{
TGA::FBX::Importer::InitImporter();

Expand All @@ -27,29 +27,28 @@ namespace Volt
{
if (!TGA::FBX::Importer::LoadMeshW(path.wstring(), tgaMesh))
{
return nullptr;
return false;
}
}
catch (const std::exception& e)
{
VT_CORE_ERROR("[FBXImporter] Unable to import animation! Reason: {0}", e.what());
return nullptr;
return false;
}

if (!tgaMesh.IsValid())
{
return nullptr;
return false;
}

Ref<Mesh> mesh = CreateRef<Mesh>();
mesh->myMaterial = CreateRef<Material>();
mesh->myMaterial->myName = path.stem().string() + "_mat";
dstMesh.myMaterial = CreateRef<Material>();
dstMesh.myMaterial->myName = path.stem().string() + "_mat";

for (const auto& element : tgaMesh.Elements)
{
auto& newSubMesh = mesh->mySubMeshes.emplace_back();
newSubMesh.vertexStartOffset = static_cast<uint32_t>(mesh->myVertices.size());
newSubMesh.indexStartOffset = static_cast<uint32_t>(mesh->myIndices.size());
auto& newSubMesh = dstMesh.mySubMeshes.emplace_back();
newSubMesh.vertexStartOffset = static_cast<uint32_t>(dstMesh.myVertices.size());
newSubMesh.indexStartOffset = static_cast<uint32_t>(dstMesh.myIndices.size());
newSubMesh.vertexCount = static_cast<uint32_t>(element.Vertices.size());
newSubMesh.indexCount = static_cast<uint32_t>(element.Indices.size());
newSubMesh.materialIndex = element.MaterialIndex;
Expand All @@ -66,7 +65,7 @@ namespace Volt

for (const auto& tgaVertex : element.Vertices)
{
auto& newVertex = mesh->myVertices.emplace_back();
auto& newVertex = dstMesh.myVertices.emplace_back();
newVertex.position = *reinterpret_cast<const glm::vec4*>(tgaVertex.Position);
newVertex.normal = *reinterpret_cast<const glm::vec3*>(tgaVertex.Normal);
newVertex.tangent = *reinterpret_cast<const glm::vec3*>(tgaVertex.Tangent);
Expand All @@ -75,32 +74,32 @@ namespace Volt
newVertex.weights = *reinterpret_cast<const glm::vec4*>(tgaVertex.BoneWeights);
}

mesh->myIndices.insert(mesh->myIndices.end(), element.Indices.begin(), element.Indices.end());
dstMesh.myIndices.insert(dstMesh.myIndices.end(), element.Indices.begin(), element.Indices.end());
}

if (tgaMesh.Materials.empty())
{
mesh->myMaterial->CreateSubMaterial(ShaderRegistry::GetShader("Illum"));
dstMesh.myMaterial->CreateSubMaterial(ShaderRegistry::GetShader("Illum"));
}
else
{
for (const auto& material : tgaMesh.Materials)
{
mesh->myMaterial->CreateSubMaterial(ShaderRegistry::GetShader("Illum"), material.MaterialName);
dstMesh.myMaterial->CreateSubMaterial(ShaderRegistry::GetShader("Illum"), material.MaterialName);
}
}

mesh->myBoundingBox = BoundingBox{ *reinterpret_cast<const glm::vec3*>(tgaMesh.BoxBounds.Max), *reinterpret_cast<const glm::vec3*>(tgaMesh.BoxBounds.Min) };
mesh->myBoundingSphere.center = { tgaMesh.BoxSphereBounds.Center[0], tgaMesh.BoxSphereBounds.Center[1], tgaMesh.BoxSphereBounds.Center[2] };
mesh->myBoundingSphere.radius = tgaMesh.BoxSphereBounds.Radius;
dstMesh.myBoundingBox = BoundingBox{ *reinterpret_cast<const glm::vec3*>(tgaMesh.BoxBounds.Max), *reinterpret_cast<const glm::vec3*>(tgaMesh.BoxBounds.Min) };
dstMesh.myBoundingSphere.center = { tgaMesh.BoxSphereBounds.Center[0], tgaMesh.BoxSphereBounds.Center[1], tgaMesh.BoxSphereBounds.Center[2] };
dstMesh.myBoundingSphere.radius = tgaMesh.BoxSphereBounds.Radius;

TGA::FBX::Importer::UninitImporter();

mesh->Construct();
return mesh;
dstMesh.Construct();
return true;
}

Ref<Skeleton> FbxImporter::ImportSkeletonImpl(const std::filesystem::path& path)
bool FbxImporter::ImportSkeletonImpl(const std::filesystem::path& path, Skeleton& dstSkeleton)
{
TGA::FBX::Importer::InitImporter();

Expand All @@ -110,27 +109,26 @@ namespace Volt
{
if (!TGA::FBX::Importer::LoadMeshW(path.wstring(), tgaMesh))
{
return nullptr;
return false;
}
}
catch (const std::exception& e)
{
VT_CORE_ERROR("[FBXImporter] Unable to import animation! Reason: {0}", e.what());
return nullptr;
return false;
}

Ref<Skeleton> skeleton = CreateRef<Skeleton>();
skeleton->myJoints.resize(tgaMesh.Skeleton.Bones.size());
skeleton->myInverseBindPose.resize(tgaMesh.Skeleton.Bones.size());
skeleton->myRestPose.resize(tgaMesh.Skeleton.Bones.size());
dstSkeleton.myJoints.resize(tgaMesh.Skeleton.Bones.size());
dstSkeleton.myInverseBindPose.resize(tgaMesh.Skeleton.Bones.size());
dstSkeleton.myRestPose.resize(tgaMesh.Skeleton.Bones.size());

ProcessSkeleton(skeleton, tgaMesh.Skeleton.Bones, 0);
ProcessSkeleton(dstSkeleton, tgaMesh.Skeleton.Bones, 0);

TGA::FBX::Importer::UninitImporter();
return skeleton;
return true;
}

Ref<Animation> FbxImporter::ImportAnimationImpl(const std::filesystem::path& path, Ref<Skeleton> targetSkeleton)
bool FbxImporter::ImportAnimationImpl(const std::filesystem::path& path, Ref<Skeleton> targetSkeleton, Animation& dstAnimation)
{
TGA::FBX::Importer::InitImporter();

Expand All @@ -140,22 +138,21 @@ namespace Volt
{
if (!TGA::FBX::Importer::LoadAnimationW(path.wstring(), tgaAnimation))
{
return nullptr;
return false;
}
}
catch (const std::exception& e)
{
VT_CORE_ERROR("[FBXImporter] Unable to import animation! Reason: {0}", e.what());
return nullptr;
return false;
}

Ref<Animation> animation = CreateRef<Animation>();
animation->myFramesPerSecond = static_cast<uint32_t>(tgaAnimation.FramesPerSecond);
animation->myDuration = static_cast<float>(tgaAnimation.Duration);
dstAnimation.myFramesPerSecond = static_cast<uint32_t>(tgaAnimation.FramesPerSecond);
dstAnimation.myDuration = static_cast<float>(tgaAnimation.Duration);

for (const auto& tgaFrame : tgaAnimation.Frames)
{
auto& newFrame = animation->myFrames.emplace_back();
auto& newFrame = dstAnimation.myFrames.emplace_back();
newFrame.localTRS.resize(targetSkeleton->myJoints.size());

for (const auto& [jointName, localTQS] : tgaFrame.LocalTQS)
Expand All @@ -176,7 +173,7 @@ namespace Volt
}

TGA::FBX::Importer::UninitImporter();
return animation;
return true;
}

void FbxImporter::ExportMeshImpl(std::vector<Ref<Mesh>>, const std::filesystem::path&)
Expand Down Expand Up @@ -268,23 +265,23 @@ namespace Volt
//exporter->Destroy();
}

void FbxImporter::ProcessSkeleton(Ref<Skeleton> skeleton, const std::vector<TGA::FBX::Skeleton::Bone>& bones, uint32_t currentIndex)
void FbxImporter::ProcessSkeleton(Skeleton& skeleton, const std::vector<TGA::FBX::Skeleton::Bone>& bones, uint32_t currentIndex)
{
const auto& currentJoint = bones.at(currentIndex);

skeleton->myJoints[currentIndex].name = currentJoint.Name;
skeleton->myJoints[currentIndex].parentIndex = currentJoint.ParentIdx;
skeleton->myInverseBindPose[currentIndex] = glm::transpose(*reinterpret_cast<const glm::mat4*>(currentJoint.BindPoseInverse.Data));
skeleton->myJointNameToIndex[currentJoint.Name] = static_cast<size_t>(currentIndex);
skeleton.myJoints[currentIndex].name = currentJoint.Name;
skeleton.myJoints[currentIndex].parentIndex = currentJoint.ParentIdx;
skeleton.myInverseBindPose[currentIndex] = glm::transpose(*reinterpret_cast<const glm::mat4*>(currentJoint.BindPoseInverse.Data));
skeleton.myJointNameToIndex[currentJoint.Name] = static_cast<size_t>(currentIndex);

skeleton->myRestPose[currentIndex].position = { currentJoint.restPosition[0], currentJoint.restPosition[1], currentJoint.restPosition[2] };
skeleton.myRestPose[currentIndex].position = { currentJoint.restPosition[0], currentJoint.restPosition[1], currentJoint.restPosition[2] };

skeleton->myRestPose[currentIndex].rotation.x = currentJoint.restRotation[0];
skeleton->myRestPose[currentIndex].rotation.y = currentJoint.restRotation[1];
skeleton->myRestPose[currentIndex].rotation.z = currentJoint.restRotation[2];
skeleton->myRestPose[currentIndex].rotation.w = currentJoint.restRotation[3];
skeleton.myRestPose[currentIndex].rotation.x = currentJoint.restRotation[0];
skeleton.myRestPose[currentIndex].rotation.y = currentJoint.restRotation[1];
skeleton.myRestPose[currentIndex].rotation.z = currentJoint.restRotation[2];
skeleton.myRestPose[currentIndex].rotation.w = currentJoint.restRotation[3];

skeleton->myRestPose[currentIndex].scale = { currentJoint.restScale[0], currentJoint.restScale[1], currentJoint.restScale[2] };
skeleton.myRestPose[currentIndex].scale = { currentJoint.restScale[0], currentJoint.restScale[1], currentJoint.restScale[2] };

for (const auto& child : currentJoint.Children)
{
Expand Down
8 changes: 4 additions & 4 deletions Volt/Volt/src/Volt/Asset/Importers/FbxImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ namespace Volt
FbxImporter() = default;

protected:
Ref<Mesh> ImportMeshImpl(const std::filesystem::path& path) override;
Ref<Skeleton> ImportSkeletonImpl(const std::filesystem::path& path) override;
Ref<Animation> ImportAnimationImpl(const std::filesystem::path& path, Ref<Skeleton> targetSkeleton) override;
bool ImportMeshImpl(const std::filesystem::path& path, Mesh& dstMesh) override;
bool ImportSkeletonImpl(const std::filesystem::path& path, Skeleton& dstSkeleton) override;
bool ImportAnimationImpl(const std::filesystem::path& path, Ref<Skeleton> targetSkeleton, Animation& dstAnimation) override;

void ExportMeshImpl(std::vector<Ref<Mesh>> assets, const std::filesystem::path& path) override;
void ExportSkeletonImpl(std::vector<Ref<Skeleton>> assets, const std::filesystem::path&) override {};
Expand All @@ -30,6 +30,6 @@ namespace Volt
size_t hash;
};

void ProcessSkeleton(Ref<Skeleton> skeleton, const std::vector<TGA::FBX::Skeleton::Bone>& bones, uint32_t currentIndex);
void ProcessSkeleton(Skeleton& skeleton, const std::vector<TGA::FBX::Skeleton::Bone>& bones, uint32_t currentIndex);
};
}
Loading

0 comments on commit 2baa32b

Please sign in to comment.