SpiecsEngine
 
Loading...
Searching...
No Matches

◆ LoadFromMaterial()

bool Spices::MaterialLoader::LoadFromMaterial ( const std::string & fileName,
Material * outMaterial )
staticprivate

Load data from a .material file.

Parameters
[in]fileNameMaterial path in disk.
[in,out]outMaterialMaterial pointer, only pass this to it.
Returns
Returns true if load data successfully.

Read .material file as bytes.

Explain bytes as a YAML::Node.

Try get material name.

@breif Try get shaders this material used.

@breif Try get textures this material used.

Try get parameters this material used.

Read .material file as bytes.

Explain bytes as a YAML::Node.

Try get material name.

@breif Try get shaders this material used.

@breif Try get textures this material used.

Try get parameters this material used.

Definition at line 90 of file MaterialLoader.cpp.

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
110 std::ifstream stream(filePath);
111 std::stringstream strStream;
112 strStream << stream.rdbuf();
113
117 YAML::Node data = YAML::Load(strStream.str());
118
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
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
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
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 }
#define SPICES_PROFILE_ZONE
static bool FileLibrary_Exists(const char *path)
Determine whether the given string is existing a file.
const std::string defaultMaterialPath
Const variable: Original Material File Path.
STL namespace.

Referenced by Load().