Easy Mesh
UV checker torus
Wrap a lit checkerboard around the torus using its UV coordinates.
About the UV checker torus shader
Wrap a lit checkerboard around the torus using its UV coordinates. Vertex and fragment shaders on real 3D meshes: moving vertices, lighting, toon shading, holograms and dissolves, as used in Unity, Unreal and Godot.
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 284 characters in Unity (Built-in)) and GPU speed. Work in HLSL, GLSL, WGSL, Metal or Godot and export the result when you are done.
The numbers you need
- Checker
abs(fmod(floor(uv.x * 24.0) + floor(uv.y * 8.0), 2.0))- Colors
float3(0.95, 0.95, 0.9) / float3(0.15, 0.25, 0.5)- Light
normalize(float3(0.6, 0.8, 0.5)), brightness 0.2 + 0.8 * diffuse (use i_normal)
How to make a uv checker torus shader
- UVs are how artists map 2D images onto 3D shapes: u goes around the ring, v around the tube.
Techniques used
- Diffuse lighting: brightness from the angle between the surface and the light.
- lerp (mix): blend between two colors or values.
UV checker torus 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)
float c = abs(fmod(floor(uv.x * 24.0) + floor(uv.y * 8.0), 2.0));
float3 base = lerp(float3(0.95, 0.95, 0.9), float3(0.15, 0.25, 0.5), c);
float diff = max(dot(i_normal, normalize(float3(0.6, 0.8, 0.5))), 0.0); - uv runs from 0 to 1 across the picture, so uv.x and uv.y say how far along the screen this pixel is.
- 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.
- 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.
Part 2: Return the result
Still inside frag()
return float4(base * (0.2 + 0.8 * diff), 1.0); - 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)
{
float c = abs(fmod(floor(uv.x * 24.0) + floor(uv.y * 8.0), 2.0));
float3 base = lerp(float3(0.95, 0.95, 0.9), float3(0.15, 0.25, 0.5), c);
float diff = max(dot(i_normal, normalize(float3(0.6, 0.8, 0.5))), 0.0);
return float4(base * (0.2 + 0.8 * diff), 1.0);
} GLSL three.js, Shadertoy, OpenGL
vec4 frag(vec2 uv)
{
float c = abs(mod(floor(uv.x * 24.0) + floor(uv.y * 8.0), 2.0));
vec3 base = mix(vec3(0.95, 0.95, 0.9), vec3(0.15, 0.25, 0.5), c);
float diff = max(dot(i_normal, normalize(vec3(0.6, 0.8, 0.5))), 0.0);
return vec4(base * (0.2 + 0.8 * diff), 1.0);
} 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 codingQuestions about this shader
How do I make an UV checker torus shader?
Wrap a lit checkerboard around the torus using its UV coordinates. A good first step: uVs are how artists map 2D images onto 3D shapes: u goes around the ring, v around the tube. 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 UV checker torus shader use?
It is built from diffuse lighting, lerp (mix). Each one is a small idea you can reuse in other effects.
Does this UV checker torus 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.