Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLTF: Fix wrong color space for GLTFLight on export #103853

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aaronfranke
Copy link
Member

@aaronfranke aaronfranke commented Mar 9, 2025

Oh my goodness, I wrote this 2 months ago and forgot to submit it as a PR... 🤦‍♂️

glTF uses linear color space for lights, while Godot's Light3D node uses sRGB color space (the docs don't specify, but I tested how it visually looks, and well, it's either sRGB or close to it).

In the past I had only tested lights with trivial colors, such as pure white or pure red. Or maybe I've only tested importing lights, and not exporting them. Godot performs a color space conversion during import, but does not perform the reverse on export. This means that the color of lights does not survive a round-trip. Additionally, the conversion was performed at the wrong step, leading to the GLTFLight data not matching the data in the glTF light. This PR fixes both of these problems. Now the color space is correctly converted when generating new nodes and when converting existing nodes.

To test this, I made a Light3D node and set its color to (0.75, 0.5, 0.25). In the current master, it incorrectly exports the Light3D color value as-is without performing a conversion, resulting in this file that doesn't visually match Godot:

{
    "asset": { "version": "2.0" },
    "extensions": {
        "KHR_lights_punctual": {
            "lights": [{ "color": [0.75, 0.5, 0.25], "type": "point" }]
        }
    },
    "extensionsUsed": ["KHR_lights_punctual"],
    "nodes": [
        {
            "extensions": { "KHR_lights_punctual": { "light": 0 } },
            "name": "OrangeLight"
        }
    ],
    "scene": 0,
    "scenes": [{ "nodes": [0] }]
}

With this PR, exporting the same scene gives almost the same file but with a color of [0.522521495819092, 0.214041143655777, 0.0508760772645473], which is the correct linear version of the sRGB (0.75, 0.5, 0.25), and then after import Godot reads the color as (0.75, 0.5, 0.25) again. With this PR, the exported glTF looks the same in Godot as it does in online glTF viewers such as glTF Viewer and glTF Sample Viewer.

Additionally, this PR adds the above color space information to the Light3D and GLTFLight documentation.

@aaronfranke aaronfranke added this to the 4.x milestone Mar 9, 2025
@aaronfranke aaronfranke requested review from a team as code owners March 9, 2025 09:02
@fire
Copy link
Member

fire commented Mar 9, 2025

I don’t think srgb with overbright is called srgb, but I think the idea is good. Need to fully review this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants