Pencil and watercolor shading for 3D in Godot, with a screen pass that puts the picture on paper.
Try it in your browser first. The viewer runs the real shader on a small graveyard: drag the sliders, switch between four ready-made looks, and edit the code to see it recompile. When you like it, grab the project.
Show full description...
Compatibility: Godot 4.x. The material and the outline work in GL Compatibility, Mobile and Forward+. No plugin, no autoload and no global setup.
What you get
- shaders/pencil.gdshader: The material. The main file: copy it into your project
- shaders/pencil_outline.gdshader: The line: the mesh pushed outwards a number of screen pixels, thicker and thinner along the contour. Goes in next_pass of the material
- shaders/pencil_screen.gdshader: The screen pass for GL Compatibility: pencil line from depth, paper
- shaders/pencil_screen_vulkan.gdshader: The same pass for Forward+ and Mobile (their depth runs 0 to 1). Generated, do not edit
- shaders/pencil_screen_forward.gdshader: Forward+ only: the creases come from the normals the engine already rendered. Generated, do not edit
- pencil.gd: Pencil: make materials, turn a whole scene pencil, set a parameter on all of it, add the screen pass
- main.tscn, demo.gd, looks/, toon_look.gd: The example scene: a graveyard made of nodes, and four looks (Sketchbook, Watercolour, Graphite, Ink wash) it cycles through
- kenney/: A few models of Kenney's Graveyard Kit (CC0, see kenney/LICENSE.txt) used by the example
Use it in your project
The fast way. Copy shaders/ and pencil.gd into your project, then:
Pencil.convert(level) # every lit material under `level` becomes pencil, outlined Pencil.set_param(level, &"wash", 0.6) # a parameter on every pencil material and outline Pencil.add_screen_pass($Camera3D) # the pencil line and the paper on the finished image
convert keeps colour, texture, UV scale and offset, vertex colours and emission of the materials it replaces. Unshaded and additive materials (lamps, glows) are left alone. Calling it twice does nothing the second time.
By hand. Make a ShaderMaterial with pencil.gdshader on a MeshInstance3D, and a second one with pencil_outline.gdshader in its Next Pass. Or Pencil.make(texture, color).
The screen pass is a quad in front of the camera. Pencil.add_screen_pass(camera) adds it and picks the shader for the running renderer. Pass true as the second argument to draw the creases from the engine's own normals: cleaner on curved surfaces, but only Forward+ has them. In another renderer it prints a warning and uses the depth-only pass. The result looks the same in every renderer; the normals only change how the creases are found.
Light in the scene comes from the lights of the scene: the shader reads them, nothing is baked. Ambient light is ignored on purpose, so the shade is the colour you choose. The hatching is drawn by the sun: only a directional light makes pencil lines; lamps (omni and spot) just light the surface.
The material
Surface: albedo and albedo_texture are the colour and the picture; uv_scale and uv_offset move the picture; use_vertex_color multiplies the mesh's vertex colours; emission is light the surface always keeps.
Wash: wash thins the colour like a watercolour wash (0 keeps it, 1 pales it towards wash_color, the tone of the paper). blotch makes the colour pool and dry darker in patches, as pigment does, and blotch_scale is the size of the patches.
Shading: shadow_color is what the surface colour is multiplied by where it is shaded; keep it pale and let the pencil do the work. light_threshold is where light gives way to shade, as the cosine of the angle to the light, and shade_reach is how far past it the lines take to grow into solid shade. crisp_shadows cuts the sun's shadows to a one-pixel edge.
Graphite: graphite_color and graphite_strength are the colour of the pencil and how dark it draws. hatch_space lays the lines on the Screen (the same size at any distance, they stay still as the camera moves) or on the Surface (stuck to the objects, so they follow them and shrink with distance). hatch_spacing is the distance between two lines (screen pixels, or centimetres on the surface) and hatch_angle their angle. crosshatch is where the darkest shade gets a second set of lines across the first.
Hand: waver makes the lines waver off straight and roughness makes the pencil skip and break along the line, as it does on rough paper.
Rim: rim is the strength of a pale rim of light round the silhouette, rim_color its colour and rim_width how far in it reaches.
The outline
outline_color and outline_width (in pixels of a 1080-line screen, the same share of any other size, near or far). outline_variation makes the width change along the line like pressure on a pencil (0 even, 1 swinging between none and double) and variation_scale is how often it changes. outline_smooth pushes the line out from the mesh's origin instead of along the normals, which closes the gaps that hard-edged meshes (a box) leave at their corners. outline_fade_start and outline_fade_end fade the line out with distance.
The screen pass
Line: line_color, line_width (in pixels of the rendered image), depth_threshold (how far apart two neighbouring depths must be to draw a silhouette), crease_threshold and creases (0 draws only silhouettes, 1 silhouettes and creases). wobble makes the line waver off its place, in pixels, and line_roughness makes the pencil skip along it. fade_start and fade_end fade the line with distance.
Paper: saturation and contrast; paper_color and paper_amount put paper with fibres under the picture, paper_scale makes the fibres coarser, and edge_fade fades the picture into bare paper towards the edges of the screen, like a painting that stops short of the page.
Example project
Open this folder in Godot 4.x and run it: main.tscn shows a graveyard with a long road and cycles through four looks. The graveyard, the sun and the camera are nodes in the scene tree. A look is a resource in looks/ holding shader parameters, so the whole style is in the inspector.
Prior art and credits
The code of this pack is mine. The ideas are shared knowledge of hand-drawn and painted looks, and these projects showed them before; I read the code of the first ones to compare and wrote my own version of each idea:
- Toon Look Free by heyheythere (itch.io, CC BY 4.0): the shade colour kept in the material's emission with the light added on top of it, and the one-call convert API. Its paid version has hatching; I did not read that code.
- Godot ComicShader by frankschoeman: the inverted-hull outline (the mesh drawn again with its back faces, pushed outwards).
- Godot StarRail Shader Demo by evident0 (MIT): the line as a screen pass that reads depth and normals.
- Hatching and crosshatching are the oldest tricks of pen and pencil drawing.
- The look itself is in the spirit of Sable and Valkyria Chronicles (and, further back, Moebius's comics and the ink paintings of Okami). This is an independent shader, not affiliated with those games.
Models: Kenney's Graveyard Kit (CC0).
License
CC0 (public domain): see LICENSE.txt in the download. Credit is appreciated, not required.
More assets by me: https://fcidade.itch.io
More from me