Merge pull request #114033 from manuelmaceira/fix-gltf-emissive-factor

GLTF: Fix emissive texture import when `emissiveFactor` is absent
This commit is contained in:
Thaddeus Crews 2025-12-15 13:02:03 -06:00
commit 2589c4da64
No known key found for this signature in database
GPG Key ID: 8C6E5FEB5FC03CCC
4 changed files with 109 additions and 1 deletions

View File

@ -3105,7 +3105,11 @@ Error GLTFDocument::_parse_materials(Ref<GLTFState> p_state) {
if (bct.has("index")) {
material->set_texture(BaseMaterial3D::TEXTURE_EMISSION, _get_texture(p_state, bct["index"], TEXTURE_TYPE_GENERIC));
material->set_feature(BaseMaterial3D::FEATURE_EMISSION, true);
material->set_emission(Color(0, 0, 0));
material->set_emission_operator(BaseMaterial3D::EMISSION_OP_MULTIPLY);
// glTF spec: emissiveFactor × emissiveTexture. Use WHITE if no factor specified.
if (!material_dict.has("emissiveFactor")) {
material->set_emission(Color(1, 1, 1));
}
}
}

View File

@ -0,0 +1,36 @@
{
"asset": { "version": "2.0", "generator": "Test for emissiveTexture without emissiveFactor" },
"scene": 0,
"scenes": [{ "name": "Scene", "nodes": [0] }],
"nodes": [{ "name": "Cube", "mesh": 0 }],
"meshes": [{
"name": "Cube",
"primitives": [{
"attributes": { "POSITION": 0 },
"material": 0
}]
}],
"materials": [{
"name": "EmissiveNoFactor",
"emissiveTexture": { "index": 0 },
"pbrMetallicRoughness": {}
}],
"textures": [{ "source": 0 }],
"images": [{ "uri": "texture.png" }],
"accessors": [{
"bufferView": 0,
"componentType": 5126,
"count": 3,
"type": "VEC3",
"max": [1, 1, 0],
"min": [-1, -1, 0]
}],
"bufferViews": [{
"buffer": 0,
"byteLength": 36
}],
"buffers": [{
"uri": "data:application/octet-stream;base64,AAAAAAAAgL8AAAAAAAAAAAAAAAAAAIA/AACAvwAAgD8AAAAA",
"byteLength": 36
}]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 B

View File

@ -0,0 +1,68 @@
/**************************************************************************/
/* test_gltf_emissive.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "test_gltf.h"
#ifdef TOOLS_ENABLED
namespace TestGltf {
TEST_CASE("[SceneTree][Node] GLTF emissiveTexture without emissiveFactor uses white emission") {
init("gltf_emissive_no_factor", "res://");
Node *loaded = gltf_import("res://emissive_no_factor.gltf");
CHECK_MESSAGE(loaded != nullptr, "Failed to load GLB.");
MeshInstance3D *mesh = Object::cast_to<MeshInstance3D>(loaded->find_child("Cube", true, true));
CHECK_MESSAGE(mesh != nullptr, "Mesh not found.");
Ref<StandardMaterial3D> mat = mesh->get_active_material(0);
CHECK_MESSAGE(mat.is_valid(), "Material not found.");
// Emission should be enabled.
CHECK(mat->get_feature(BaseMaterial3D::FEATURE_EMISSION));
// Emission operator should be MULTIPLY per glTF spec.
CHECK(mat->get_emission_operator() == BaseMaterial3D::EMISSION_OP_MULTIPLY);
// Without emissiveFactor, emission color should be WHITE, not BLACK.
Color c = mat->get_emission();
CHECK_MESSAGE(c.r > 0.9f, "Emission red should be ~1.0 when emissiveFactor is absent.");
CHECK_MESSAGE(c.g > 0.9f, "Emission green should be ~1.0 when emissiveFactor is absent.");
CHECK_MESSAGE(c.b > 0.9f, "Emission blue should be ~1.0 when emissiveFactor is absent.");
memdelete(loaded);
}
} // namespace TestGltf
#endif // TOOLS_ENABLED