SpiecsEngine
 
Loading...
Searching...
No Matches
MaterialLoader.cpp
Go to the documentation of this file.
1/**
2* @file MaterialLoader.cpp.
3* @brief The MaterialLoader Class Implementation.
4* @author Spices.
5*/
6
7#include "Pchheader.h"
9#include "Core/Library/FileLibrary.h"
10#include "Core/Library/StringLibrary.h"
11#include "Utils/YamlUtils.h"
12#include "Resources/Material/Material.h"
13#include "Systems/ResourceSystem.h"
14
15namespace Spices {
16
17 /**
18 * @brief Const variable: Bin Material File Path.
19 */
20 const std::string defaultBinMaterialPath = "Materials/bin/";
21
22 /**
23 * @brief Const variable: Original Material File Path.
24 */
25 const std::string defaultMaterialPath = "Materials/src/";
26
27 /**
28 * @brief Const variable: Bin Shader File Path.
29 */
30 const std::string defaultBinShaderPath = "Shaders/spv/";
31
32 /**
33 * @brief Const variable: Material File Confirm header start.
34 */
35 constexpr char LoaderSignStart[100] = "#ItisSpicesMaterialSign: DataStart";
36
37 /**
38 * @brief Const variable: Material File Confirm header over.
39 */
40 constexpr char LoaderSignOver[100] = "#ItisSpicesMaterialSign: DateOver";
41
42 /**
43 * @brief Serialize Shader Config.
44 * @param[in,out] out YAML Emitter.
45 * @param[in] shaderStage What Stage shader use.
46 * @param[in] shaderPath Shader path(short path).
47 */
48 static void SerializeShaderConfig(
49 YAML::Emitter& out ,
50 const std::string& shaderStage ,
51 const std::string& shaderPath
52 );
53
54 /**
55 * @brief Serialize Texture Config.
56 * @param[in,out] out YAML Emitter.
57 * @param[in] name Texture name.
58 * @param[in] param Texture parameter.
59 */
60 static void SerializeTextureConfig(
61 YAML::Emitter& out ,
62 const std::string& name ,
63 const TextureParam& param
64 );
65
66 bool MaterialLoader::Load(const std::string& fileName, Material* outMaterial)
67 {
69
70 /**
71 * @brief Load from .sasset file first.
72 */
73 if (LoadFromSASSET(fileName, outMaterial)) return true;
74
75 /**
76 * @brief Load from .material file second.
77 */
78 else if (LoadFromMaterial(fileName, outMaterial)) return true;
79
80 else
81 {
82 std::stringstream ss;
83 ss << "MaterialLoader: Could not find a valid file from the give filename: [" << fileName << "]";
84
85 SPICES_CORE_WARN(ss.str());
86 return false;
87 }
88 }
89
90 bool MaterialLoader::LoadFromMaterial(const std::string& fileName, Material* outMaterial)
91 {
93
94 bool isFind = false;
95 std::string filePath;
96 for (auto& it : ResourceSystem::GetSearchFolder())
97 {
98 filePath = it + defaultMaterialPath + "Material." + fileName + ".material";
99 if (FileLibrary::FileLibrary_Exists(filePath.c_str()))
100 {
101 isFind = true;
102 break;
103 }
104 }
105 if (!isFind) return false;
106
107 /**
108 * @brief Read .material file as bytes.
109 */
110 std::ifstream stream(filePath);
111 std::stringstream strStream;
112 strStream << stream.rdbuf();
113
114 /**
115 * @brief Explain bytes as a YAML::Node.
116 */
117 YAML::Node data = YAML::Load(strStream.str());
118
119 /**
120 * @brief Try get material name.
121 */
122 if (!data["Material"])
123 {
124 std::stringstream ss;
125 ss << filePath << ": Not find a Material Node.";
126
127 SPICES_CORE_ERROR(ss.str())
128 return false;
129 }
130
131 std::string materialName = data["Material"].as<std::string>();
132
133 /**
134 * @breif Try get shaders this material used.
135 */
136 auto shaders = data["Shaders"];
137 if (shaders)
138 {
139 for (auto& shader : shaders)
140 {
141 if(shader["Stage"].IsDefined() && shader["Path"].IsDefined())
142 {
143 outMaterial->m_Shaders [shader["Stage"].as<std::string>()].push_back(shader["Path"].as<std::string>());
144 outMaterial->m_DefaultShaders[shader["Stage"].as<std::string>()].push_back(shader["Path"].as<std::string>());
145 }
146 else
147 {
148 std::stringstream ss;
149 ss << "Stage/Path not found in " << fileName;
150 SPICES_CORE_ERROR(ss.str())
151 }
152 }
153 }
154 else
155 {
156 std::stringstream ss;
157 ss << filePath << ": Not find a Shaders Node.";
158
159 SPICES_CORE_ERROR(ss.str())
160 return false;
161 }
162
163 /**
164 * @breif Try get textures this material used.
165 */
166 auto textures = data["Textures"];
167 if (textures)
168 {
169 for (auto& texture : textures)
170 {
171 if (texture["Name"].IsDefined() && texture["Value"].IsDefined())
172 {
173 outMaterial->m_TextureParams .push_back(texture["Name"].as<std::string>(), texture["Value"].as<TextureParam>());
174 outMaterial->m_DefaultTextureParams.push_back(texture["Name"].as<std::string>(), texture["Value"].as<TextureParam>());
175 }
176 else
177 {
178 std::stringstream ss;
179 ss << "Name/Value not found in " << fileName;
180 SPICES_CORE_ERROR(ss.str())
181 }
182 }
183 }
184
185 /**
186 * @brief Try get parameters this material used.
187 */
188 auto parameters = data["Parameters"];
189 if(parameters)
190 {
191 for (auto& parameter : parameters)
192 {
193 ConstantParams constantParams;
194 if (parameter["Name"].IsDefined() && parameter["Value"].IsDefined())
195 {
196
197 constantParams.value = parameter["Value"].as<ConstantParam>();
198 constantParams.defaultValue = parameter["Value"].as<ConstantParam>();
199 }
200 else
201 {
202 std::stringstream ss;
203 ss << "Name/Value not found in " << fileName;
204 SPICES_CORE_ERROR(ss.str())
205 }
206
207 if (parameter["MinValue"].IsDefined())
208 {
209 constantParams.hasMinValue = true;
210 constantParams.min = parameter["MinValue"].as<ConstantParam>();
211 }
212 if (parameter["MaxValue"].IsDefined())
213 {
214 constantParams.hasMaxValue = true;
215 constantParams.max = parameter["MaxValue"].as<ConstantParam>();
216 }
217
218 outMaterial->m_ConstantParams.push_back(parameter["Name"].as<std::string>(), constantParams);
219 }
220 }
221
222 return true;
223 }
224
225 bool MaterialLoader::LoadFromSASSET(const std::string& fileName, Material* outMaterial)
226 {
228
229 bool isFind = false;
230 std::string filePath;
231 for (auto& it : ResourceSystem::GetSearchFolder())
232 {
233 filePath = it + defaultBinMaterialPath + "Material." + fileName + ".sasset";
234 if (FileLibrary::FileLibrary_Exists(filePath.c_str()))
235 {
236 isFind = true;
237 break;
238 }
239 }
240 if (!isFind) return false;
241
242 FileHandle f;
244
245 uint64_t read = 0;
246
247 char startSign[100];
248 FileLibrary::FileLibrary_Read(&f, sizeof(char) * 100, &startSign, &read);
249
251 {
253 return false;
254 }
255
256 // TODO: ReadData
257
258 char overSign[100];
259 FileLibrary::FileLibrary_Read(&f, sizeof(char) * 100, &overSign, &read);
260
262 {
264 return false;
265 }
266
268
269 return true;
270 }
271
273 {
274 /*const std::string outFilePath = SPICES_ENGINE_ASSETS_PATH + "Materials/src/Material.Default.material";
275
276 YAML::Emitter out;
277 out << YAML::BeginMap;
278 out << YAML::Key << "Material" << YAML::Value << "Default";
279
280 out << YAML::Key << "Shaders" << YAML::Value << YAML::BeginSeq;
281 SerializeShaderConfig(out, "vertShader", "MeshRenderer");
282 SerializeShaderConfig(out, "fragShader", "MeshRenderer");
283 out << YAML::EndSeq;
284
285 out << YAML::Key << "Textures" << YAML::Value << YAML::BeginSeq;
286 SerializeTextureConfig(out, "diffuse", { 1, nullptr, "stone_tile_vjqifhu/vjqifhu_4K_Albedo.jpg", 1, 0, 0, { 1, 1, 1 }, 1 });
287 SerializeTextureConfig(out, "normal", { 1, nullptr, "stone_tile_vjqifhu/vjqifhu_4K_Normal.jpg", 1, 0, 1, {1, 1, 1}, 1 });
288 SerializeTextureConfig(out, "specular", { 1, nullptr, "stone_tile_vjqifhu/vjqifhu_4K_Specular.jpg", 1, 0, 2, {1, 1, 1}, 1 });
289 out << YAML::EndSeq;
290
291 YAML::EndMap;
292
293 std::ofstream fout(outFilePath);
294 fout << out.c_str();*/
295
296 return true;
297 }
298
299 void SerializeShaderConfig(YAML::Emitter& out, const std::string& shaderStage, const std::string& shaderPath)
300 {
301 out << YAML::BeginMap;
302 out << YAML::Key << "ShaderStage" << YAML::Value << shaderStage;
303 out << YAML::Key << "ShaderPath" << YAML::Value << shaderPath;
304 out << YAML::EndMap;
305 }
306
307 void SerializeTextureConfig(YAML::Emitter& out, const std::string& name, const TextureParam& param)
308 {
309 out << YAML::BeginMap;
310 out << YAML::Key << "TextureName" << YAML::Value << name;
311 out << YAML::Key << "TextureParam" << YAML::Value << param;
312 out << YAML::EndMap;
313 }
314}
#define SPICES_PROFILE_ZONE
static bool FileLibrary_Read(const FileHandle *handle, uint64_t data_size, void *out_data, uint64_t *out_bytes_read)
Read Specific size of data form the current file handle pointer, and move pointer the same size.
static bool FileLibrary_Open(const char *path, FileModes mode, bool binary, FileHandle *out_handle)
Open the file using given string.
static void FileLibrary_Close(FileHandle *handle)
Close the file by the file handle.
File Static Function Library.
Definition FileLibrary.h:49
static bool SaveDefaultMaterial()
Test function.
static bool Load(const std::string &fileName, Material *outMaterial)
Public called API, it is entrance.
static bool LoadFromSASSET(const std::string &fileName, Material *outMaterial)
Load data from a .sasset file.
static bool LoadFromMaterial(const std::string &fileName, Material *outMaterial)
Load data from a .material file.
This enum defines tree types of material file.
Material Class. This class contains a branch of parameter and shader, also descriptor.
Definition Material.h:62
static bool StringsEqual(const char *str0, const char *str1)
Determine if the strings given are equal. Platform Specific.
String Static Function Library.
const std::string defaultMaterialPath
Const variable: Original Material File Path.
const std::string defaultBinMaterialPath
Const variable: Bin Material File Path.
static void SerializeShaderConfig(YAML::Emitter &out, const std::string &shaderStage, const std::string &shaderPath)
Serialize Shader Config.
const std::string defaultBinShaderPath
Const variable: Bin Shader File Path.
constexpr char LoaderSignStart[100]
Const variable: Material File Confirm header start.
static void SerializeTextureConfig(YAML::Emitter &out, const std::string &name, const TextureParam &param)
Serialize Texture Config.
constexpr char LoaderSignOver[100]
Const variable: Material File Confirm header over.
@ FILE_MODE_READ
model : read
Definition FileLibrary.h:37
This Struct is FILE* handle pointer Wrapper.
Definition FileLibrary.h:15
This struct's data is defined from .material file.
Definition Material.h:27