Medium 3DAnimated

Checker globe

A lit, slowly rotating sphere wearing a blue-and-white checker texture.

The target picture for the shader challenge "Checker globe"
Recreate this picture. You pass at a 99% match, then try to write it in fewer characters than the par.

About the Checker globe shader

A lit, slowly rotating sphere wearing a blue-and-white checker texture. Three-dimensional scenes drawn entirely by a fragment shader: lighting, shadows and shapes from signed distance functions.

How this challenge works

You write a fragment shader that draws the picture above, live in your browser. A match bar shows how close you are, and the score combines accuracy, code size (the par is 607 characters in Unity (Built-in)) and GPU speed. Work in HLSL, GLSL, WGSL, Metal or Godot and export the result when you are done.

What it is built from

Sphere
p = (uv - 0.5) * float2(aspect, 1.0) * 2.0, R = ?, normal = normalize(float3(p, sqrt(max(R*R - dot(p,p), 0.0))))
Texture coords
u = atan2(n.x, n.z) / ? + time * ?; v = acos(clamp(n.y, -1.0, 1.0)) / ?
Checker
abs(fmod(floor(u * ?) + floor(v * ?), 2.0))
Colors
float3(0.95,0.95,0.9) / float3(0.2,0.45,0.85)
Light
normalize(float3(0.5, ?, ?)), brightness ? + ? * diffuse
Background
float3(0.04, 0.05, 0.09)

The exact numbers are left out (shown as ?), but every colour is given. Measure the rest with the pixel readout on both pictures and Diff.

How to make a checker globe shader

  1. The normal doubles as a position on a unit sphere: convert it to longitude/latitude for texture coordinates.
  2. Same fake-3D trick as the Normals & light lesson.

Techniques used

  • Polar coordinates: angle and distance from a center, for spirals, rings and radial effects.
  • Diffuse lighting: brightness from the angle between the surface and the light.
  • step: a hard 0-or-1 cutoff for crisp shapes and thresholds.
  • lerp (mix): blend between two colors or values.

Checker globe shader code, explained step by step

Try to build it yourself first, because that is how it sticks. When you want to check your work or you are stuck, this is a complete working solution cut into parts, with the idea behind each part. The parts are in HLSL (Unity and Unreal); the full code below comes in every language.

Part 1: Set up the coordinates

Inside float4 frag(float2 uv)

float2 p = (uv - 0.5) * float2(aspect, 1.0) * 2.0;
float R = 0.8;
float r2 = dot(p, p);
  • Centre the coordinates on the middle of the picture and multiply x by the aspect ratio, so a circle stays round on a wide picture.
  • Subtracting 0.5 moves the origin from the bottom-left corner to the middle of the picture.

Part 2: Choose the colours

Still inside frag()

float mask = step(r2, R * R);
float3 n = normalize(float3(p, sqrt(max(R * R - r2, 0.0))));
float u = atan2(n.x, n.z) / 6.28318 + time * 0.1;
  • atan2 gives the angle around the centre, which is what spirals, spokes and radial patterns are built from.
  • step(a, x) is a hard cutoff: 1 once x passes a, 0 before.
  • time is the number of seconds since the shader started, so anything that uses it moves.
  • normalize() scales a direction to length 1, so only its direction matters.

Part 3: Choose the colours (continued)

Still inside frag()

float v = acos(clamp(n.y, -1.0, 1.0)) / 3.14159;
float ch = abs(fmod(floor(u * 12.0) + floor(v * 6.0), 2.0));
float3 base = lerp(float3(0.95, 0.95, 0.9), float3(0.2, 0.45, 0.85), ch);
  • lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.
  • floor() picks the cell a point is in and frac() is the position inside that cell. Together they repeat a pattern.
  • abs() mirrors a value around zero, which gives symmetric shapes, outlines and sharp ridges.
  • float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).

Part 4: Colour the pixel and return it

Still inside frag()

float diff = max(dot(n, normalize(float3(0.5, 0.7, 0.6))), 0.0);
float3 col = base * (0.2 + 0.8 * diff);
return float4(lerp(float3(0.04, 0.05, 0.09), col, mask), 1.0);
  • lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.
  • The dot product of the surface normal and the light direction is the diffuse light: bright where the surface faces the light, dark where it faces away.
  • normalize() scales a direction to length 1, so only its direction matters.
  • float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).
  • The shader returns the final colour: red, green, blue, and an alpha of 1 (fully opaque).

The full code

HLSL Unity, Unreal
float4 frag(float2 uv)
{
    float2 p = (uv - 0.5) * float2(aspect, 1.0) * 2.0;
    float R = 0.8;
    float r2 = dot(p, p);
    float mask = step(r2, R * R);
    float3 n = normalize(float3(p, sqrt(max(R * R - r2, 0.0))));
    float u = atan2(n.x, n.z) / 6.28318 + time * 0.1;
    float v = acos(clamp(n.y, -1.0, 1.0)) / 3.14159;
    float ch = abs(fmod(floor(u * 12.0) + floor(v * 6.0), 2.0));
    float3 base = lerp(float3(0.95, 0.95, 0.9), float3(0.2, 0.45, 0.85), ch);
    float diff = max(dot(n, normalize(float3(0.5, 0.7, 0.6))), 0.0);
    float3 col = base * (0.2 + 0.8 * diff);
    return float4(lerp(float3(0.04, 0.05, 0.09), col, mask), 1.0);
}
GLSL Shadertoy, three.js, OpenGL
vec4 frag(vec2 uv)
{
    vec2 p = (uv - 0.5) * vec2(aspect, 1.0) * 2.0;
    float R = 0.8;
    float r2 = dot(p, p);
    float mask = step(r2, R * R);
    vec3 n = normalize(vec3(p, sqrt(max(R * R - r2, 0.0))));
    float u = atan(n.x, n.z) / 6.28318 + time * 0.1;
    float v = acos(clamp(n.y, -1.0, 1.0)) / 3.14159;
    float ch = abs(mod(floor(u * 12.0) + floor(v * 6.0), 2.0));
    vec3 base = mix(vec3(0.95, 0.95, 0.9), vec3(0.2, 0.45, 0.85), ch);
    float diff = max(dot(n, normalize(vec3(0.5, 0.7, 0.6))), 0.0);
    vec3 col = base * (0.2 + 0.8 * diff);
    return vec4(mix(vec3(0.04, 0.05, 0.09), col, mask), 1.0);
}
WGSL WebGPU, Bevy
fn frag(uv: vec2f) -> vec4f
{
    let p = (uv - 0.5) * vec2f(aspect, 1.0) * 2.0;
    let R = 0.8;
    let r2 = dot(p, p);
    let mask = step(r2, R * R);
    let n = normalize(vec3f(p, sqrt(max(R * R - r2, 0.0))));
    let u = atan2(n.x, n.z) / 6.28318 + time * 0.1;
    let v = acos(clamp(n.y, -1.0, 1.0)) / 3.14159;
    let ch = abs(((floor(u * 12.0) + floor(v * 6.0)) % 2.0));
    let base = mix(vec3f(0.95, 0.95, 0.9), vec3f(0.2, 0.45, 0.85), ch);
    let diff = max(dot(n, normalize(vec3f(0.5, 0.7, 0.6))), 0.0);
    let col = base * (0.2 + 0.8 * diff);
    return vec4f(mix(vec3f(0.04, 0.05, 0.09), col, mask), 1.0);
}
Godot Godot 4 shading language
shader_type canvas_item;

vec4 frag(vec2 uv)
{
    vec2 p = (uv - 0.5) * vec2(aspect, 1.0) * 2.0;
    float R = 0.8;
    float r2 = dot(p, p);
    float mask = step(r2, R * R);
    vec3 n = normalize(vec3(p, sqrt(max(R * R - r2, 0.0))));
    float u = atan(n.x, n.z) / 6.28318 + TIME * 0.1;
    float v = acos(clamp(n.y, -1.0, 1.0)) / 3.14159;
    float ch = abs(mod(floor(u * 12.0) + floor(v * 6.0), 2.0));
    vec3 base = mix(vec3(0.95, 0.95, 0.9), vec3(0.2, 0.45, 0.85), ch);
    float diff = max(dot(n, normalize(vec3(0.5, 0.7, 0.6))), 0.0);
    vec3 col = base * (0.2 + 0.8 * diff);
    return vec4(mix(vec3(0.04, 0.05, 0.09), col, mask), 1.0);
}

void fragment()
{
    // Godot's UV starts at the top-left, the lessons use bottom-left
    COLOR = frag(vec2(UV.x, 1.0 - UV.y));
}
Metal iOS, macOS
float4 frag(float2 uv)
{
    float2 p = (uv - 0.5) * float2(aspect, 1.0) * 2.0;
    float R = 0.8;
    float r2 = dot(p, p);
    float mask = step(r2, R * R);
    float3 n = normalize(float3(p, sqrt(max(R * R - r2, 0.0))));
    float u = atan2(n.x, n.z) / 6.28318 + time * 0.1;
    float v = acos(clamp(n.y, -1.0, 1.0)) / 3.14159;
    float ch = abs(fmod(floor(u * 12.0) + floor(v * 6.0), 2.0));
    float3 base = mix(float3(0.95, 0.95, 0.9), float3(0.2, 0.45, 0.85), ch);
    float diff = max(dot(n, normalize(float3(0.5, 0.7, 0.6))), 0.0);
    float3 col = base * (0.2 + 0.8 * diff);
    return float4(mix(float3(0.04, 0.05, 0.09), col, mask), 1.0);
}

Open it in the editor and change it

Solve it in your language

Unity (Built-in), Unity (URP), Unreal, Shadertoy / WebGL, OpenGL (LWJGL, raylib), three.js, GameMaker, LÖVE (Lua), Godot, Apple (iOS, macOS), WebGPU / Bevy.

Start coding

Questions about this shader

How do I make a checker globe shader?

A lit, slowly rotating sphere wearing a blue-and-white checker texture. A good first step: the normal doubles as a position on a unit sphere: convert it to longitude/latitude for texture coordinates. In ShaderBattle you build it in the browser with a live match bar, so you see how close you are after every change.

What techniques does the checker globe shader use?

It is built from polar coordinates, diffuse lighting, step, lerp (mix). Each one is a small idea you can reuse in other effects.

Does this checker globe shader work in Unity, Unreal, Godot and Shadertoy?

Yes. The idea is the same in every shader language. ShaderBattle shows the code in HLSL (Unity, Unreal), GLSL (Shadertoy, three.js, OpenGL), WGSL (WebGPU), Metal and Godot, and exports a ready-to-paste version.