Skip to content

Commit 0b7627b

Browse files
committed
Merge pull request #109845 from nikitalita/gltf-use-orm-texture
GLTF export: use ORM texture for occlusion/metallicRoughnessTexture if it exists
2 parents 0d89d2f + ec97115 commit 0b7627b

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

modules/gltf/gltf_document.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4427,8 +4427,9 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
44274427
bool has_roughness = base_material->get_texture(BaseMaterial3D::TEXTURE_ROUGHNESS).is_valid() && base_material->get_texture(BaseMaterial3D::TEXTURE_ROUGHNESS)->get_image().is_valid();
44284428
bool has_ao = base_material->get_feature(BaseMaterial3D::FEATURE_AMBIENT_OCCLUSION) && base_material->get_texture(BaseMaterial3D::TEXTURE_AMBIENT_OCCLUSION).is_valid();
44294429
bool has_metalness = base_material->get_texture(BaseMaterial3D::TEXTURE_METALLIC).is_valid() && base_material->get_texture(BaseMaterial3D::TEXTURE_METALLIC)->get_image().is_valid();
4430+
Ref<Texture2D> original_orm_tex = base_material->get_texture(BaseMaterial3D::TEXTURE_ORM);
4431+
GLTFTextureIndex orm_texture_index = -1;
44304432
if (has_ao || has_roughness || has_metalness) {
4431-
Dictionary mrt;
44324433
Ref<Texture2D> roughness_texture = base_material->get_texture(BaseMaterial3D::TEXTURE_ROUGHNESS);
44334434
BaseMaterial3D::TextureChannel roughness_channel = base_material->get_roughness_texture_channel();
44344435
Ref<Texture2D> metallic_texture = base_material->get_texture(BaseMaterial3D::TEXTURE_METALLIC);
@@ -4546,19 +4547,31 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
45464547
}
45474548
orm_image->generate_mipmaps();
45484549
orm_texture->set_image(orm_image);
4549-
GLTFTextureIndex orm_texture_index = -1;
45504550
if (has_ao || has_roughness || has_metalness) {
45514551
// If they all share the same path, use it for the name.
45524552
const String path = common_paths.size() == 1 ? *common_paths.begin() : String();
45534553
_set_material_texture_name(orm_texture, path, mat_name, "_orm");
45544554
orm_texture_index = _set_texture(p_state, orm_texture, base_material->get_texture_filter(), base_material->get_flag(BaseMaterial3D::FLAG_USE_TEXTURE_REPEAT));
45554555
}
4556+
} else if (original_orm_tex.is_valid() && original_orm_tex->get_image().is_valid()) {
4557+
has_ao = true;
4558+
has_roughness = true;
4559+
has_metalness = true;
4560+
Ref<Image> orm_image = original_orm_tex->get_image();
4561+
orm_image->decompress();
4562+
orm_image->convert(Image::FORMAT_RGBA8);
4563+
4564+
_set_material_texture_name(original_orm_tex, original_orm_tex->get_path(), mat_name, "_orm");
4565+
orm_texture_index = _set_texture(p_state, original_orm_tex, base_material->get_texture_filter(), base_material->get_flag(BaseMaterial3D::FLAG_USE_TEXTURE_REPEAT));
4566+
}
4567+
if (orm_texture_index != -1) {
45564568
if (has_ao) {
45574569
Dictionary occt;
45584570
occt["index"] = orm_texture_index;
45594571
mat_dict["occlusionTexture"] = occt;
45604572
}
45614573
if (has_roughness || has_metalness) {
4574+
Dictionary mrt;
45624575
mrt["index"] = orm_texture_index;
45634576
Dictionary extensions = _serialize_texture_transform_uv1(material);
45644577
if (!extensions.is_empty()) {
@@ -4583,7 +4596,7 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
45834596
// Code for uncompressing RG normal maps
45844597
Ref<Image> img = normal_texture->get_image();
45854598
if (img.is_valid()) {
4586-
Ref<ImageTexture> img_tex = img;
4599+
Ref<ImageTexture> img_tex = normal_texture;
45874600
if (img_tex.is_valid()) {
45884601
img = img_tex->get_image();
45894602
}

0 commit comments

Comments
 (0)