-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextureSerializer.cpp
More file actions
148 lines (120 loc) · 4.34 KB
/
Copy pathTextureSerializer.cpp
File metadata and controls
148 lines (120 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "lpch.h"
#include "TextureSerializer.h"
#include "Lux/Asset/AssetManager.h"
#include "Lux/Renderer/Renderer.h"
#include "Lux/Renderer/SceneEnvironment.h"
#include "Lux/Serialization/TextureRuntimeSerializer.h"
#include "Lux/Project/Project.h"
#include "Lux/Utilities/FileSystem.h"
#include <filesystem>
namespace Lux
{
namespace
{
bool PathStartsWith(const std::filesystem::path& path, const std::filesystem::path& directory)
{
const std::filesystem::path normalizedPath = path.lexically_normal();
const std::filesystem::path normalizedDirectory = directory.lexically_normal();
auto pathIt = normalizedPath.begin();
for (auto directoryIt = normalizedDirectory.begin(); directoryIt != normalizedDirectory.end(); ++directoryIt, ++pathIt)
{
if (pathIt == normalizedPath.end() || *pathIt != *directoryIt)
return false;
}
return true;
}
bool IsMeshSourceTexture(const std::filesystem::path& assetPath)
{
Ref<Project> project = Project::GetActive();
if (!project)
return false;
const std::filesystem::path meshSourcePath = project->GetConfig().MeshSourcePath;
if (meshSourcePath.empty())
return false;
return PathStartsWith(assetPath, meshSourcePath);
}
}
void TextureSerializer::Serialize(const AssetMetadata& metadata, const Ref<Asset>& asset) const
{
// Source-format textures (.png/.jpg/…) are their own serialized form – nothing to write back.
}
bool TextureSerializer::TryLoadData(const AssetMetadata& metadata, Ref<Asset>& asset) const
{
const std::filesystem::path filepath = Project::GetEditorAssetManager()
? Project::GetEditorAssetManager()->GetFileSystemPath(metadata)
: (Project::GetActiveAssetDirectory() / metadata.FilePath);
if (metadata.Type == AssetType::EnvMap)
{
if (!std::filesystem::exists(filepath))
return false;
auto [radianceMap, irradianceMap] = Renderer::CreateEnvironmentMap(filepath.string());
if (!radianceMap || !irradianceMap)
return false;
asset = Ref<Environment>::Create(radianceMap, irradianceMap);
if (asset)
asset->Handle = metadata.Handle;
return asset != nullptr;
}
TextureSpecification spec;
if (IsMeshSourceTexture(metadata.FilePath))
{
spec.FlipVertically = false;
spec.MaxAnisotropy = 16.0f;
}
Ref<Texture2D> texture = Texture2D::Create(spec, filepath);
if (!texture || !texture->Loaded())
return false;
texture->Handle = metadata.Handle;
asset = texture;
return true;
}
bool TextureSerializer::SerializeToAssetPack(AssetHandle handle, FileStreamWriter& stream, AssetSerializationInfo& outInfo) const
{
outInfo.Offset = stream.GetStreamPosition();
if (AssetManager::GetAssetType(handle) == AssetType::EnvMap)
{
auto assetManager = Project::GetEditorAssetManager();
if (!assetManager)
return false;
const AssetMetadata metadata = assetManager->GetMetadata(handle);
Buffer sourceBuffer = FileSystem::ReadBytes(assetManager->GetFileSystemPath(metadata));
if (!sourceBuffer)
return false;
stream.WriteBuffer(sourceBuffer);
sourceBuffer.Release();
outInfo.Size = stream.GetStreamPosition() - outInfo.Offset;
return true;
}
Ref<Texture2D> texture = AssetManager::GetAsset<Texture2D>(handle);
if (!texture)
return false;
outInfo.Size = TextureRuntimeSerializer::SerializeTexture2DToFile(texture, stream);
return true;
}
Ref<Asset> TextureSerializer::DeserializeFromAssetPack(FileStreamReader& stream, const AssetPackFile::AssetInfo& assetInfo) const
{
stream.SetStreamPosition(assetInfo.PackedOffset);
const AssetType assetType = (AssetType)assetInfo.Type;
if (assetType == AssetType::EnvMap)
{
Buffer sourceBuffer;
stream.ReadBuffer(sourceBuffer);
if (!sourceBuffer)
return nullptr;
TextureSpecification equirectSpec;
equirectSpec.DebugName = "PackedEnvEquirect";
equirectSpec.FlipVertically = false;
equirectSpec.Width = (uint32_t)sourceBuffer.Size;
equirectSpec.Height = 0;
Ref<Texture2D> equirectTexture = Texture2D::Create(equirectSpec, sourceBuffer);
sourceBuffer.Release();
if (!equirectTexture || !equirectTexture->Loaded())
return nullptr;
auto [radiance, irradiance] = Renderer::CreateEnvironmentMap(equirectTexture);
if (!radiance || !irradiance)
return nullptr;
return Ref<Environment>::Create(radiance, irradiance);
}
return TextureRuntimeSerializer::DeserializeTexture2D(stream);
}
}