@@ -192,6 +192,51 @@ void Parser::emit_gltf_base_color(const std::string &base_color_path, const std:
192
192
}
193
193
}
194
194
195
+ void Parser::emit_gltf_ue_pbr (const std::string &ue_pbr)
196
+ {
197
+ Vulkan::MemoryMappedTexture pbr;
198
+ Vulkan::MemoryMappedTexture output;
199
+
200
+ pbr = Vulkan::load_texture_from_file (*GRANITE_FILESYSTEM (), ue_pbr, Vulkan::ColorSpace::Linear);
201
+ if (pbr.empty ())
202
+ {
203
+ LOGE (" Failed to open metallic texture %s, falling back to default material.\n " ,
204
+ ue_pbr.c_str ());
205
+ return ;
206
+ }
207
+
208
+ unsigned width = 0 ;
209
+ unsigned height = 0 ;
210
+ Hasher hasher;
211
+
212
+ if (pbr.get_layout ().get_format () != VK_FORMAT_R8G8B8A8_UNORM)
213
+ LOGE (" Unexpected format." );
214
+ width = pbr.get_layout ().get_width ();
215
+ height = pbr.get_layout ().get_height ();
216
+ hasher.string (ue_pbr);
217
+
218
+ std::string packed_path = std::string (" memory://" ) + std::to_string (hasher.get ()) + " .gtx" ;
219
+ materials.back ().paths [Util::ecast (TextureKind::Occlusion)] = packed_path;
220
+ materials.back ().paths [Util::ecast (TextureKind::MetallicRoughness)] = packed_path;
221
+
222
+ output.set_2d (VK_FORMAT_R8G8B8A8_UNORM, width, height);
223
+ if (!output.map_write (*GRANITE_FILESYSTEM (), packed_path))
224
+ {
225
+ LOGE (" Failed to map texture for writing.\n " );
226
+ return ;
227
+ }
228
+
229
+ for (unsigned y = 0 ; y < height; y++)
230
+ {
231
+ for (unsigned x = 0 ; x < width; x++)
232
+ {
233
+ auto *o = output.get_layout ().data_2d <u8vec4>(x, y);
234
+ auto *i = pbr.get_layout ().data_2d <u8vec4>(x, y);
235
+ *o = *i;
236
+ }
237
+ }
238
+ }
239
+
195
240
void Parser::emit_gltf_pbr_metallic_roughness (const std::string &metallic_path, const std::string &roughness_path)
196
241
{
197
242
Vulkan::MemoryMappedTexture metallic;
@@ -381,6 +426,7 @@ void Parser::load_material_library(const std::string &path)
381
426
std::string roughness;
382
427
std::string base_color;
383
428
std::string alpha_mask;
429
+ std::string ue_pbr;
384
430
385
431
for (auto &line : lines)
386
432
{
@@ -398,6 +444,8 @@ void Parser::load_material_library(const std::string &path)
398
444
{
399
445
if (!metallic.empty () || !roughness.empty ())
400
446
emit_gltf_pbr_metallic_roughness (metallic, roughness);
447
+ if (!ue_pbr.empty ())
448
+ emit_gltf_ue_pbr (ue_pbr);
401
449
if (!base_color.empty ())
402
450
emit_gltf_base_color (base_color, alpha_mask);
403
451
@@ -407,6 +455,7 @@ void Parser::load_material_library(const std::string &path)
407
455
roughness.clear ();
408
456
base_color.clear ();
409
457
alpha_mask.clear ();
458
+ ue_pbr.clear ();
410
459
}
411
460
else if (ident == " Kd" )
412
461
{
@@ -447,10 +496,19 @@ void Parser::load_material_library(const std::string &path)
447
496
throw std::logic_error (" No material" );
448
497
roughness = Path::relpath (path, elements.at (1 ));
449
498
}
499
+ else if (ident == " map_ue_pbr" )
500
+ {
501
+ // Custom hacks.
502
+ if (materials.empty ())
503
+ throw std::logic_error (" No material" );
504
+ ue_pbr = Path::relpath (path, elements.at (1 ));
505
+ }
450
506
}
451
507
452
508
if (!metallic.empty () || !roughness.empty ())
453
509
emit_gltf_pbr_metallic_roughness (metallic, roughness);
510
+ if (!ue_pbr.empty ())
511
+ emit_gltf_ue_pbr (ue_pbr);
454
512
if (!base_color.empty ())
455
513
emit_gltf_base_color (base_color, alpha_mask);
456
514
}
0 commit comments