How do I create a transparent glass shader?

Godot Version

4.2.1

Question

I’m trying to figure out how to create a shiny glass shader. I want this shader to have an Albedo that is completely transparent, but a very visible specular component. Unfortunately when I try this with a StandardMaterial3D, the specular becomes completely invisible when I set the alpha of the Albedo to 0. If I raise the alpha, the specular comes back, but then the glass gets cloudy.

How can I create a transparent material that only renders the specular highlight?

1 Like

You need refraction. An ideally transparent glass is invisible against a solid background. The complexities of refraction are usually too expensive for games, but if you only have one object at a time it can be done. I recommend looking at how other shaders do it or finding a shader that already does it, because the math of refraction is hard to explain.
However, if what you want is just a fakeish reflection on a very transparent object, just make the alpha higher on the light function of the shader when the light is affecting the color. You can do this easily from a regular shader if you know how to modify it.

1 Like

I don’t need realistic refraction - I just need a specular highlight to indicate the light source and maybe reflection of the world map. I would prefer perfectly transparent so that it does not obscure what the glass bottle contains, but I could put up with a little opaqueness if necessary.

I had a look at this (PBR Glass - Godot Shaders), but the specular also vanishes when the Albedo has alpha == 0. Please let me know if you know how to set up the StandardMaterial3D to produce this effect, or do it through a custom shader.

1 Like

I guess you could just replace

ALPHA = mix(fresnel * edge_color.a, 1.0, a);

with

ALPHA = mix(fresnel * edge_color.a, 1.0, a);
ALPHA += 0.5 * inversesqrt(ALPHA);

and see how that looks

1 Like

That just makes everything opaque. But playing with the Alpha is an interesting idea.

1 Like

I don’t know what the range of specular this uses is, but try reducing the left factor until it’s a reasonable look

oh, with sqrt() instead of inversesqrt() it looks interesting

1 Like

That does look better. Still, you need to add a bit of opacity to make the highlight stand out.

1 Like

If what you want is larger specular influence, just reduce the alpha→specular factor.

I don’t get what exactly you want.

1 Like

I came up with this. It’s not great, but it does keep the material clear except for the specular highlights. I don’t think realistic glass is possible with the current Godot pipeline.

shader_type spatial;
render_mode diffuse_burley, specular_schlick_ggx, blend_add;

uniform float shininess = 200;

void light() {
	vec3 half_vec = normalize(VIEW + LIGHT);
	float n_dot_half = max(0.0, dot(NORMAL, half_vec));
	float pf = pow(n_dot_half, shininess * SPECULAR_AMOUNT);
	
	SPECULAR_LIGHT += pf * ATTENUATION;
	ALPHA += pf * ATTENUATION;
}

void fragment() {
	ALPHA = 0.0;
}
1 Like

It is as possible as in any other game engine. Shaders are in the GPU, not even in godot after they are compiled.

1 Like