Manga screentone in a Godot 4 spatial shader: the trick is FRAGCOORD inside light()
I wanted manga-style dots in the shadow side of a toon shader, and the part that took me longest was where to put it. It works best inside light(), because that is where you know how lit each pixel is.
The idea in three steps:
-
Work out how lit the pixel is: l = dot(NORMAL, LIGHT) * 0.5 + 0.5, times ATTENUATION.
-
Turn the dark part into a 0 to 1 amount: s = clamp((pattern_from - l) / pattern_from, 0.0, 1.0). With pattern_from at 0.6, dots only show in the darker part of the shadow.
-
Draw dots in screen space with FRAGCOORD, rotated by an angle and divided by the dot size in pixels. Each cell gets a circle whose radius grows with sqrt(s), so the dots get bigger as it gets darker, like printed screentone.
Then mix the shade toward the dot colour instead of drawing on top, so DIFFUSE_LIGHT still multiplies by ALBEDO and black dots stay black on any colour.
Because it uses screen pixels, the dot size stays the same at any distance, which is what you want for a manga look. Lines and crosshatch work the same way with abs(fract(q.y) - 0.5) instead of the circle.
I checked it in Godot 4.7 on Forward+, Mobile and Compatibility and it compiled and looked the same on all three.
Disclosure: this is from a toon shader maker I build. Previewing the styles is free in the browser, exporting the shader is part of a paid plan: gameshipkit.com/tools/toon-shader-maker-3d/