Toon Shader

Godot 4

Toon shader which uses the light function.

Shader code
/*
	Toon Shader by Firerabbit
	
	MIT License
*/

shader_type spatial;
render_mode ambient_light_disabled;

uniform vec4 color : source_color = vec4(1.0);
uniform sampler2D tex : source_color, hint_default_white;

uniform float shadow : hint_range(0.0, 1.0) = 0.5;
uniform float shadow_width : hint_range(0.001, 0.5) = 0.0;
uniform vec4 shadow_color: source_color = vec4(0.705);

uniform float _specular_smoothness : hint_range(0.0,0.5) = 0.01;
uniform float _specular_strength : hint_range(0.0,0.25) = 0.075;
uniform float _glossiness : hint_range(0.0,0.5) = 0.1;

uniform float _rim_size : hint_range(0,1) = 0.5;
uniform float _rim_smoothness : hint_range(0.0,0.5) = 0.01;


void fragment() {
	ALBEDO = pow(texture(tex, UV).rgb, vec3(2.2)) * color.rgb;
}


void light() {
	float NdotL = dot(NORMAL, LIGHT) * ATTENUATION.x;
	NdotL = smoothstep(shadow - shadow_width, shadow + shadow_width, NdotL);
	
	// specular
	vec3 H = normalize(VIEW + LIGHT);
	float NdotH = max(0, dot(NORMAL, H));
	float specular_intensity = pow(NdotH, 1.0 / _glossiness);
	vec3 specular = vec3(smoothstep(0.5 - _specular_smoothness, 0.5 + _specular_smoothness, specular_intensity));
	
	// rim
	float rimDot = 1.0 - dot(VIEW, NORMAL);
	float rim_intensity = rimDot * NdotL;
	vec3 rim = vec3(smoothstep(1.0 -_rim_size - _rim_smoothness, 1.0 -_rim_size + _rim_smoothness, rim_intensity));
	
	DIFFUSE_LIGHT = mix(ALBEDO * shadow_color.rgb, (ALBEDO + (rim + specular) * _specular_strength) * LIGHT_COLOR.rgb * 0.33, NdotL + 0.33 * (smoothstep(1.0 -_rim_size - _rim_smoothness, 1.0 -_rim_size + _rim_smoothness, rimDot)));
}
Tags
toon
The shader code and all code snippets in this post are under MIT license and can be used freely. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from Firerabbit

Scale2x filter

View-MatCap

Terrain Mesh Blending with Dithering

Related shaders

Toon Style 3D Water Shader – No textures needed

Toon Shader for Godot 4

2D Cell/Toon Style Shader

Subscribe
Notify of
guest

7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Henodude
Henodude
2 years ago

Nice! I haven’t tested it out yet, but this seems to be exactly what I need.

Henodude
Henodude
2 years ago

Just so you know, this is mislabeled as a canvas item shader.

Purple
Purple
1 year ago

I dont why . But cannot change color everything is Blueish !!!

Purple
Purple
1 year ago
Reply to  Purple

hint_color become -> source_color
and hint_white -> hint_default_white ! and now it work for 4.0

thanks im having fun with this shader !

Last edited 1 year ago by Purple
Halom
Halom
9 months ago

This Shader looks amazing it’s exactly what I need!
Problem is, it’s not working properly for me in Godot 4.

In line 31. I get the following error, since ATTENUATION is no longer a float:

Invalid member for 'float' expression: '.x'.

And if I remove the ‘.x’ from the attenuation it works – but I get this weird cutoff in shadow:
https://imgur.com/saIsB5P