A MatCap image is a picture of a lit ball, and the shader looks up each pixel by the direction the surface faces on screen. In a Godot spatial shader NORMAL in fragment() already points that way, in view space, so no matrix is needed, but the image's y runs down while the normal's y runs up. In our Godot 4.7 test a MatCap with its bright spot in the top left showed it bottom left without the y flip. Built from the world normal it was right only from the front: from the right side the spot was gone, from above it was bottom left and from behind top right. The script below uses NORMAL with y flipped: top left from all 4 sides, and with the ball itself turned.
The code
Save it as matcap.gd and load it with const MatCap := preload("res://matcap.gd"). Then $Mesh.material_override = MatCap.make(preload("res://matcaps/clay.png")), with an optional tint as the second value. The shader code is in the script, so you can also copy it into a .gdshader file and use it in a ShaderMaterial in the editor.
extends RefCounted
## A MatCap material: the look of a sculpt, clay or a lit ball from one image, no lights needed.
## const MatCap := preload("res://matcap.gd")
## $Mesh.material_override = MatCap.make(preload("res://matcaps/clay.png"))
const CODE := """
shader_type spatial;
render_mode unshaded;
uniform sampler2D matcap : source_color, filter_linear, repeat_disable;
uniform vec4 tint : source_color = vec4(1.0);
void fragment() {
// NORMAL is already in view space here: x to the right, y up, z toward the camera.
// The image's y runs down, so y is flipped.
vec2 uv = vec2(NORMAL.x, -NORMAL.y) * 0.5 + 0.5;
ALBEDO = texture(matcap, uv).rgb * tint.rgb;
}
"""
static func make(tex: Texture2D, tint := Color.WHITE) -> ShaderMaterial:
var sh := Shader.new()
sh.code = CODE
var m := ShaderMaterial.new()
m.shader = sh
m.set_shader_parameter("matcap", tex)
m.set_shader_parameter("tint", tint)
return m
What we measured
A ball of radius 1 with a 64 px MatCap: dark grey with one bright spot in the top left of the image. We rendered it with the camera in front, on the right, above and behind, found the bright pixels on the screen and where they sat on the ball, as a share of the ball's radius on screen (59 px). Two runs gave the same numbers.
- No y flip (uv = NORMAL.xy * 0.5 + 0.5): the spot at x -0.42, y +0.42, bottom left, from all 4 sides.
- World normal (INV_VIEW_MATRIX * NORMAL), y flipped: top left from the front. From the right: 0 bright pixels, the spot was not on the ball. From above: bottom left. From behind: top right.
- The script: x -0.42, y -0.42, top left, from all 4 sides, 814 bright pixels each time (859 from above, where the ball was 61 px).
- The script with the ball itself turned 90 and 30 degrees: still top left, 814 bright pixels.
Why it happens
A MatCap is made by photographing or rendering a ball from the front, so it is only correct for normals as the camera sees them. In fragment() Godot gives you NORMAL in that space already: x to the right of the screen, y up, z toward the camera. Texture coordinates run with y down, so the up direction has to become the top of the image: vec2(NORMAL.x, -NORMAL.y) * 0.5 + 0.5. A world normal belongs to the scene, not to the camera. It only matches when the camera looks the same way as the world axes, which is why it looked right from the front only. Turning the object does not move the lighting either, which is what a MatCap is meant to do: the light comes with the camera.
What to change
- The shader is
unshaded, so the image is the whole lighting and the ball looks the same in a dark scene. - Snow or moss on the tops of things needs the opposite, the world normal: snow that stays on the roofs.
- Toon light from real lights instead of an image: toon diffuse mode and roughness.
Free MatCaps to drop in: 24 Free Sculpting MatCaps has clay, metal, glaze and toon MatCaps as 1024 px PNG, 8 and 16-bit, CC BY 4.0.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: 24 Free Sculpting MatCaps has clay, metal, glaze and toon MatCaps as 1024 px PNG, 8 and 16-bit, CC BY 4.0.
More how-tos: Shaders, materials and lights
- A 3D outline that stays the same width on screen
- Find and fix the seam in a repeating texture
- PS1 vertex wobble that is the same near and far
- Snow that stays on the roofs
- Packed ORM maps on a StandardMaterial3D
- Neon signs that share a material but not a colour
- Scroll or tile a 2D texture without stretched edge lines
- Keep floors, roads and roofs sharp when the camera looks along them
- Make WorldEnvironment color grading apply to a 2D game
- Get a hard cel-shaded light edge from StandardMaterial3D's Toon mode
- Make metallic materials stop looking black or flat
- Make a glowing sign or screen actually light the wall around it
- Use a LUT strip for color grading in Godot 4
- Tiling textures that keep their size on any mesh
- A sharp low-resolution PS1 look
- Only 8 lights show on a long wall in Compatibility
- Hit flash shader for 2D sprites
- Flash a 3D enemy white when it takes a hit
- Dissolve shader for 3D models
- 2D outline shader that is not cut off
- 3D outline shader without gaps at the corners
- A force field shield with a hit ripple
- Normal map inverted: DirectX vs OpenGL
- Full-screen post-process shader in 3D
- Scroll a shader texture for hours without the once-an-hour jump