← Pixel Dreamer Studio

A MatCap that stays the right way up from every side

Use NORMAL in fragment() as it is (it is in view space), flip its y, and leave the world normal out.

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.

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

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