2
3
4
5
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"
18
19
23
24
28
29
33
34
38
39
43
44
45
46
47
50 const std::string& shaderStage ,
51 const std::string& shaderPath
55
56
57
58
59
62 const std::string& name ,
71
72
76
77
83 ss <<
"MaterialLoader: Could not find a valid file from the give filename: [" << fileName <<
"]";
85 SPICES_CORE_WARN(ss.str());
96 for (
auto& it : ResourceSystem::GetSearchFolder())
98 filePath = it + defaultMaterialPath +
"Material." + fileName +
".material";
99 if (FileLibrary::FileLibrary_Exists(filePath.c_str()))
105 if (!isFind)
return false;
108
109
110 std::ifstream stream(filePath);
111 std::stringstream strStream;
112 strStream << stream.rdbuf();
115
116
117 YAML::Node data = YAML::Load(strStream.str());
120
121
122 if (!data[
"Material"])
124 std::stringstream ss;
125 ss << filePath <<
": Not find a Material Node.";
127 SPICES_CORE_ERROR(ss.str())
131 std::string materialName = data[
"Material"].as<std::string>();
134
135
136 auto shaders = data[
"Shaders"];
139 for (
auto& shader : shaders)
141 if(shader[
"Stage"].IsDefined() && shader[
"Path"].IsDefined())
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>());
148 std::stringstream ss;
149 ss <<
"Stage/Path not found in " << fileName;
150 SPICES_CORE_ERROR(ss.str())
156 std::stringstream ss;
157 ss << filePath <<
": Not find a Shaders Node.";
159 SPICES_CORE_ERROR(ss.str())
164
165
166 auto textures = data[
"Textures"];
169 for (
auto& texture : textures)
171 if (texture[
"Name"].IsDefined() && texture[
"Value"].IsDefined())
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>());
178 std::stringstream ss;
179 ss <<
"Name/Value not found in " << fileName;
180 SPICES_CORE_ERROR(ss.str())
186
187
188 auto parameters = data[
"Parameters"];
191 for (
auto& parameter : parameters)
193 ConstantParams constantParams;
194 if (parameter[
"Name"].IsDefined() && parameter[
"Value"].IsDefined())
197 constantParams.value = parameter[
"Value"].as<ConstantParam>();
198 constantParams.defaultValue = parameter[
"Value"].as<ConstantParam>();
202 std::stringstream ss;
203 ss <<
"Name/Value not found in " << fileName;
204 SPICES_CORE_ERROR(ss.str())
207 if (parameter[
"MinValue"].IsDefined())
209 constantParams.hasMinValue =
true;
210 constantParams.min = parameter[
"MinValue"].as<ConstantParam>();
212 if (parameter[
"MaxValue"].IsDefined())
214 constantParams.hasMaxValue =
true;
215 constantParams.max = parameter[
"MaxValue"].as<ConstantParam>();
218 outMaterial->m_ConstantParams.push_back(parameter[
"Name"].as<std::string>(), constantParams);
230 std::string filePath;
231 for (
auto& it : ResourceSystem::GetSearchFolder())
233 filePath = it + defaultBinMaterialPath +
"Material." + fileName +
".sasset";
234 if (FileLibrary::FileLibrary_Exists(filePath.c_str()))
240 if (!isFind)
return false;
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
299 void SerializeShaderConfig(YAML::Emitter& out,
const std::string& shaderStage,
const std::string& shaderPath)
301 out << YAML::BeginMap;
302 out << YAML::Key <<
"ShaderStage" << YAML::Value << shaderStage;
303 out << YAML::Key <<
"ShaderPath" << YAML::Value << shaderPath;
307 void SerializeTextureConfig(YAML::Emitter& out,
const std::string& name,
const TextureParam& param)
309 out << YAML::BeginMap;
310 out << YAML::Key <<
"TextureName" << YAML::Value << name;
311 out << YAML::Key <<
"TextureParam" << YAML::Value << param;
#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.
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.
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 ¶m)
Serialize Texture Config.
constexpr char LoaderSignOver[100]
Const variable: Material File Confirm header over.
@ FILE_MODE_READ
model : read
This Struct is FILE* handle pointer Wrapper.
This struct's data is defined from .material file.