# ViewportTexture colour != Color colour?

**URL:** <https://forum.tsuki.games/t/viewporttexture-colour-color-colour/134>\
**Category:** Q&A\
**Tags:** godot, 3d\
**Created:** [May 20, 2024, 9:15pm UTC](https://forum.tsuki.games/t/viewporttexture-colour-color-colour/134 "2024-05-20T21:15:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![outfrost](https://tsuki.b-cdn.net/user_avatar/forum.tsuki.games/outfrost/32/83_2.png) [@outfrost](https://forum.tsuki.games/u/outfrost)\
**Post date:** [May 20, 2024, 9:15pm UTC](https://forum.tsuki.games/t/viewporttexture-colour-color-colour/134/1 "2024-05-20T21:15:24Z")

</div>

I’ve come across an inconsistency in Godot 3, which is not a big issue for me right now, but I’m curious to find out where it comes from.

I have a `Viewport` rendering a UI with the background colour `#1d186b`, and writing to a `ViewportTexture` which I then pass into a `SpatialMaterial` as both the albedo and emission texture. Albedo colour is white, emission colour is black, emission mode is additive.

 ![image](https://tsuki-s3.b-cdn.net/original/1X/ca38defd57dd26a64bb98ac2c092fe990d900644.png)

When I make another `SpatialMaterial` with identical parameters, except without the texture, and with albedo and emission colour set to `#1d186b`, the resulting colour is noticeably different.

 ![image](https://tsuki-s3.b-cdn.net/original/1X/b864b26a0703649ac7c17db84b377ece297fea93.png)

Anyone happen to know why that might be?

---

<div class="post-metadata">

**Author:** ![exodrifter](https://tsuki.b-cdn.net/user_avatar/forum.tsuki.games/exodrifter/32/3_2.png) [@exodrifter](https://forum.tsuki.games/u/exodrifter)\
**Post date:** [May 21, 2024, 5:03am UTC](https://forum.tsuki.games/t/viewporttexture-colour-color-colour/134/2 "2024-05-21T05:03:23Z")

</div>

It looks like you ran into a color space conversion issue (see [godotengine/godot#60365](https://github.com/godotengine/godot/issues/60365)).

Godot renders 3D scenes in the linear color space before converting the generated texture to sRGB for display. When you then take that sRGB texture and use it in a 3D scene again, that texture is treated as if it were in the linear color space even though it’s actually in sRGB, resulting in brighter and washed-out colors. In other words, your colors are getting converted from linear to sRGB twice.

There’s a setting on the `Viewport` called [`keep_3d_linear`](https://docs.godotengine.org/en/3.5/classes/class_viewport.html#class-viewport-property-keep-3d-linear) that lets you ignore the conversion of the 3D scene from linear to sRGB, but this will **not** work in your case because you’re rendering a 2D UI, which is always in sRGB.\[1\]

Instead, you’ll have enable the `SpatialMaterial`’s [`flags_albedo_tex_force_srgb`](https://docs.godotengine.org/en/3.5/classes/class_spatialmaterial.html#class-spatialmaterial-property-flags-albedo-tex-force-srgb) value to `true`. This will convert the `ViewportTexture`, which is in sRGB, back to linear space:

 ![Two quad meshes are placed adjacent to each other in the Godot editor, the one on the left has the correct color while the one on the right has a lighter color. The one on the right is selected and the property has been set to true.](https://tsuki-s3.b-cdn.net/original/1X/ef1b5775c006a43b3e78e8be0da1ee38bdc3a681.png)

As you can see, the color is still incorrect, but that’s because the flag only corrects the color space of the albedo texture; the emission texture is still in sRGB. If you remove the emission texture and set the emission color to `#1d186b`, then you get exactly the same colors:

 ![Two quad meshes are placed adjacent to each other in the Godot editor, both with the same, correct color. The one on the right is selected and the albedo texture is set to the ViewportTexture, the emission texture has been set to null, and the emission color has been set to the correct color.](https://tsuki-s3.b-cdn.net/original/1X/81007fa30f2ec38640245665a4fcf8516f715e27.png)

In order to fully fix your problem in Godot 3, I think you’ll have to add a script to convert the `ViewportTexture` into linear space before using it as the emission texture in the `SpatialMaterial`. Your other option is to upgrade to Godot 4, where the issue has apparently been addressed (not sure when), and the colors match exactly when using a `ViewportTexture` that is rendering a UI as your albedo and emission textures:

 ![Two quad meshes are placed adjacent to each other in the Godot editor, both with the same, correct color. The one on the right is selected and both the albedo texture and emission textures are set to the same ViewportTexture.](https://tsuki-s3.b-cdn.net/original/1X/2e76475622e33131985452a137a9efa7803189c3.png)

* * *

1. [x.com](https://twitter.com/mux213/status/1514034551198470144)

---

<div class="post-metadata">

**Author:** ![outfrost](https://tsuki.b-cdn.net/user_avatar/forum.tsuki.games/outfrost/32/83_2.png) [@outfrost](https://forum.tsuki.games/u/outfrost)\
**Post date:** [May 21, 2024, 8:21pm UTC](https://forum.tsuki.games/t/viewporttexture-colour-color-colour/134/3 "2024-05-21T20:21:21Z")

</div>

Thanks a lot! It makes perfect sense now.

Unless I end up switching to Godot 4, I think I’ll use `flags_albedo_tex_force_srgb` combined with `flags_unshaded` on the `SpatialMaterial`, since it’s quite simple, and I probably don’t need specular etc. on the ingame LCD panels.

 ![image](https://tsuki-s3.b-cdn.net/original/1X/34d4cbbed91011af2abd9d3a5e27a2919a84680f.jpeg)

If I needed the emission texture to work right as well, I imagine I could correct the colour space using a custom shader.

---

<div class="post-metadata">

**Author:** ![system](https://tsuki.b-cdn.net/user_avatar/forum.tsuki.games/system/32/457_2.png) [@system](https://forum.tsuki.games/u/system)\
**Post date:** [June 20, 2024, 8:22pm UTC](https://forum.tsuki.games/t/viewporttexture-colour-color-colour/134/4 "2024-06-20T20:22:20Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
