Godot - Simple spartial shader results too bright (unshaded, custom)

1.2k views Asked by At

:)

TL:DR

When writing a custom spatial shader the ALBEDO color isn't the output color, even if render_mode unshaded is set.


Long story

I created a new project in Godot v3.4.4.stable.official [419e713a2] and simply added two CSGBox.

For the left one I assigned a SpatialMaterial and set it to unshaded and assigned an albedo texture (see below). The result is as expected.

However, for the right one I assigned a ShaderMaterial and wrote the following, very simple, shader:

shader_type spatial;
render_mode unshaded;

uniform sampler2D albedo;

void fragment() {
  ALBEDO = texture(albedo, UV).rgb;
}

After assigning the same texture to the albedo shader parameter, the result is a lot too bright. I already tried several render_modes, variables such as SPECULAR and other things like texture import settings, but couldn't figure out how to get the same result as the left box.

The texture

The texture I choose

The result

The two cubes

Left side: As expected using SpatialMaterial, Right side: Too bright using ShaderMaterial

Sample project

Well, it's nothing big, but if you want to take a look: https://files.catbox.moe/nj407g.zip

Thank you in advance ❤

1

There are 1 answers

0
Theraot On BEST ANSWER

This is something that is often dismissed in tutorials and explanations: Adding hint_albedo tells Godot to do an sRGB to linear conversion. The documentation about uniforms mentions:

It's important to understand that textures that are supplied as color require hints for proper sRGB->linear conversion (i.e. hint_albedo), as Godot's 3D engine renders in linear color space.

So change this line:

uniform sampler2D albedo;

To this:

uniform sampler2D albedo:hint_albedo;

And it should give you the expected result.