-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2baa32b
commit 254ab32
Showing
18 changed files
with
491 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
110 changes: 110 additions & 0 deletions
110
Volt/Volt/src/Volt/Asset/ImportersNew/MeshSerializer.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#include "vtpch.h" | ||
#include "MeshSerializer.h" | ||
|
||
#include "Volt/Asset/Mesh/Mesh.h" | ||
#include "Volt/Asset/Mesh/Material.h" | ||
#include "Volt/Asset/Serialization/AssetSerializationCommon.h" | ||
|
||
#include "Volt/Asset/AssetManager.h" | ||
|
||
namespace Volt | ||
{ | ||
constexpr uint32_t CURRENT_ASSET_VERSION = 1; | ||
|
||
struct MeshSerializationData | ||
{ | ||
AssetHandle materialHandle; | ||
|
||
std::vector<Vertex> vertices; | ||
std::vector<uint32_t> indices; | ||
|
||
glm::vec3 boundingSphereCenter; | ||
float boundingSphereRadius; | ||
|
||
std::vector<SubMesh> subMeshes; | ||
|
||
static void Serialize(BinaryStreamWriter& streamWriter, const MeshSerializationData& data) | ||
{ | ||
streamWriter.Write(data.materialHandle); | ||
streamWriter.WriteRaw(data.vertices); | ||
streamWriter.WriteRaw(data.indices); | ||
streamWriter.Write(data.boundingSphereCenter); | ||
streamWriter.Write(data.boundingSphereRadius); | ||
streamWriter.Write(data.subMeshes); | ||
} | ||
|
||
static void Deserialize(BinaryStreamReader& streamReader, MeshSerializationData& outData) | ||
{ | ||
streamReader.Read(outData.materialHandle); | ||
streamReader.ReadRaw(outData.vertices); | ||
streamReader.ReadRaw(outData.indices); | ||
streamReader.Read(outData.boundingSphereCenter); | ||
streamReader.Read(outData.boundingSphereRadius); | ||
streamReader.Read(outData.subMeshes); | ||
} | ||
}; | ||
|
||
void MeshSerializer::Serialize(const AssetMetadata& metadata, const Ref<Asset>& asset) const | ||
{ | ||
Ref<Mesh> mesh = std::reinterpret_pointer_cast<Mesh>(asset); | ||
|
||
BinaryStreamWriter streamWriter{}; | ||
AssetSerializer::WriteMetadata(metadata, CURRENT_ASSET_VERSION, streamWriter); | ||
|
||
MeshSerializationData serializationData{}; | ||
serializationData.materialHandle = mesh->GetMaterial()->handle; | ||
serializationData.vertices = mesh->GetVertices(); | ||
serializationData.indices = mesh->GetIndices(); | ||
serializationData.boundingSphereCenter = mesh->GetBoundingSphere().center; | ||
serializationData.boundingSphereRadius = mesh->GetBoundingSphere().radius; | ||
serializationData.subMeshes = mesh->GetSubMeshes(); | ||
|
||
streamWriter.Write(serializationData); | ||
|
||
const auto filePath = AssetManager::GetFilesystemPath(metadata.filePath); | ||
streamWriter.WriteToDisk(filePath); | ||
} | ||
|
||
bool MeshSerializer::Deserialize(const AssetMetadata& metadata, Ref<Asset> destinationAsset) const | ||
{ | ||
const auto filePath = AssetManager::GetFilesystemPath(metadata.filePath); | ||
|
||
if (!std::filesystem::exists(filePath)) | ||
{ | ||
VT_CORE_ERROR("File {0} not found!", metadata.filePath); | ||
destinationAsset->SetFlag(AssetFlag::Missing, true); | ||
return false; | ||
} | ||
|
||
BinaryStreamReader streamReader{ filePath }; | ||
|
||
if (!streamReader.IsStreamValid()) | ||
{ | ||
VT_CORE_ERROR("Failed to open file: {0}!", metadata.filePath); | ||
destinationAsset->SetFlag(AssetFlag::Invalid, true); | ||
return false; | ||
} | ||
|
||
SerializedAssetMetadata serializedMetadata = AssetSerializer::ReadMetadata(streamReader); | ||
|
||
MeshSerializationData serializationData{}; | ||
streamReader.Read(serializationData); | ||
|
||
Ref<Mesh> mesh = std::reinterpret_pointer_cast<Mesh>(destinationAsset); | ||
|
||
mesh->myMaterial = AssetManager::QueueAsset<Material>(serializationData.materialHandle); | ||
mesh->myVertices = serializationData.vertices; | ||
mesh->myIndices = serializationData.indices; | ||
mesh->myBoundingSphere.center = serializationData.boundingSphereCenter; | ||
mesh->myBoundingSphere.radius = serializationData.boundingSphereRadius; | ||
mesh->mySubMeshes = serializationData.subMeshes; | ||
|
||
for (auto& subMesh : mesh->mySubMeshes) | ||
{ | ||
subMesh.GenerateHash(); | ||
} | ||
|
||
mesh->Construct(); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.