How to code a spatial shader that looks like default shader?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By kitfox

I’m new to shader writing in Godot and ma finding that the shaders I’ve written appear very overlit and washed out compared to the default shaders Godot applies to meshes imported from Blender.

The purpose of this custom shader is to use vertex colors to blend between two different albedo textures. There is a base texture to be used wherever the vertex color is black and a second texture is overlaid wherever the vertex color is red. I’ve created this mesh in Blender and have imported it into a Godot scene. However, when it is drawn, the textures appear much lighter than they should be. I’ve tried playing with the specular, roughness and metallic settings, but they do not help.

Any idea how I could fix the lighting here?

(If someone could post the code for a shader that just draws a plain spatial mesh with a single texture, I think that would be a good reference point for demonstrating what a basic shader should look like - the water shader in the docs is not terribly helpful since it illustrates a very unusual case).

shader_type spatial;
render_mode cull_disabled;

uniform sampler2D baseAlbedo;
uniform sampler2D redAlbedo;

void fragment() 
{
	vec3 base = texture(baseAlbedo, UV).rgb;
	vec3 red = texture(redAlbedo, UV).rgb;
	
	ALBEDO = COLOR.r * red.rgb + (1.0 - COLOR.r) * base.rgb;
	ROUGHNESS = 1.0;
	SPECULAR = 0.0;
	METALLIC = 0.0;
}

shader

:bust_in_silhouette: Reply From: kitfox

Looks like I had to add hint_albedo to my sampler declaration. No idea why this acts the way it does, but my shader textures are lit correctly now.

uniform sampler2D baseAlbedo : hint_albedo;
uniform sampler2D redAlbedo : hint_albedo;

This hint probably tells Godot to convert the texture to the sRGB color space upon import (and treat it as such). Godot expects all albedo textures to be sRGB in 3D.

Calinou | 2020-03-05 12:48

:bust_in_silhouette: Reply From: Joel_127

You can convert Godot’s default materials into shader script, that should help you :wink: