Skip to content

Commit 495d8d2

Browse files
committed
Add obj export hack to emit UE style PBR maps.
1 parent ca3919c commit 495d8d2

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

scene-export/obj.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,51 @@ void Parser::emit_gltf_base_color(const std::string &base_color_path, const std:
192192
}
193193
}
194194

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+
195240
void Parser::emit_gltf_pbr_metallic_roughness(const std::string &metallic_path, const std::string &roughness_path)
196241
{
197242
Vulkan::MemoryMappedTexture metallic;
@@ -381,6 +426,7 @@ void Parser::load_material_library(const std::string &path)
381426
std::string roughness;
382427
std::string base_color;
383428
std::string alpha_mask;
429+
std::string ue_pbr;
384430

385431
for (auto &line : lines)
386432
{
@@ -398,6 +444,8 @@ void Parser::load_material_library(const std::string &path)
398444
{
399445
if (!metallic.empty() || !roughness.empty())
400446
emit_gltf_pbr_metallic_roughness(metallic, roughness);
447+
if (!ue_pbr.empty())
448+
emit_gltf_ue_pbr(ue_pbr);
401449
if (!base_color.empty())
402450
emit_gltf_base_color(base_color, alpha_mask);
403451

@@ -407,6 +455,7 @@ void Parser::load_material_library(const std::string &path)
407455
roughness.clear();
408456
base_color.clear();
409457
alpha_mask.clear();
458+
ue_pbr.clear();
410459
}
411460
else if (ident == "Kd")
412461
{
@@ -447,10 +496,19 @@ void Parser::load_material_library(const std::string &path)
447496
throw std::logic_error("No material");
448497
roughness = Path::relpath(path, elements.at(1));
449498
}
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+
}
450506
}
451507

452508
if (!metallic.empty() || !roughness.empty())
453509
emit_gltf_pbr_metallic_roughness(metallic, roughness);
510+
if (!ue_pbr.empty())
511+
emit_gltf_ue_pbr(ue_pbr);
454512
if (!base_color.empty())
455513
emit_gltf_base_color(base_color, alpha_mask);
456514
}

scene-export/obj.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class Parser
7272
using OBJVertex = std::vector<std::string>;
7373
void emit_vertex(const OBJVertex * const *face);
7474
void emit_gltf_pbr_metallic_roughness(const std::string &metallic, const std::string &roughness);
75+
void emit_gltf_ue_pbr(const std::string &ue_pbr);
7576
void emit_gltf_base_color(const std::string &metallic, const std::string &roughness);
7677
Node root_node;
7778
};

0 commit comments

Comments
 (0)