Medium MeshAnimated
Twisting cube
A cube twisted around its vertical axis by an amount that swings back and forth.
About the Twisting cube shader
A cube twisted around its vertical axis by an amount that swings back and forth. 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 333 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
- Angle
a = pos.y * 2.0 * sin(time)- Rotate
pos.xz = mul(float2x2(cos(a), -sin(a), sin(a), cos(a)), pos.xz)- Color
flat shading: light normalize(float3(?, ?, ?)) on i_faceNormal, base float3(0.4, 0.85, 0.6) * (? + ? * diffuse)
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 twisting cube shader
- Rotate every vertex by an angle that depends on its height: the twist.
- The cube is subdivided (48×48 per face) so it bends smoothly.
Techniques used
- Sine waves: smooth oscillation for waves, pulses and motion over time.
- Diffuse lighting: brightness from the angle between the surface and the light.
Twisting cube 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: Vertex shader: Return the result
Inside float3 vert(float3 pos, float3 normal, float2 uv)
float a = pos.y * 2.0 * sin(time);
pos.xz = mul(float2x2(cos(a), -sin(a), sin(a), cos(a)), pos.xz);
return pos; - Putting time inside sin or cos makes the value swing back and forth, which is what animates the effect.
- time is the number of seconds since the shader started, so anything that uses it moves.
Part 2: Return the result
Inside float4 frag(float2 uv)
float diff = max(dot(i_faceNormal, normalize(float3(0.4, 0.8, 0.4))), 0.0);
return float4(float3(0.4, 0.85, 0.6) * (0.2 + 0.8 * diff), 1.0); - 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.
- A mesh shader receives the position, surface direction and view direction of each pixel (i_worldPos, i_normal, i_viewDir).
- 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
float3 vert(float3 pos, float3 normal, float2 uv)
{
float a = pos.y * 2.0 * sin(time);
pos.xz = mul(float2x2(cos(a), -sin(a), sin(a), cos(a)), pos.xz);
return pos;
}
float4 frag(float2 uv)
{
float diff = max(dot(i_faceNormal, normalize(float3(0.4, 0.8, 0.4))), 0.0);
return float4(float3(0.4, 0.85, 0.6) * (0.2 + 0.8 * diff), 1.0);
} GLSL three.js, Shadertoy, OpenGL
vec3 vert(vec3 pos, vec3 normal, vec2 uv)
{
float a = pos.y * 2.0 * sin(time);
pos.xz = (pos.xz * mat2(cos(a), -sin(a), sin(a), cos(a)));
return pos;
}
vec4 frag(vec2 uv)
{
float diff = max(dot(i_faceNormal, normalize(vec3(0.4, 0.8, 0.4))), 0.0);
return vec4(vec3(0.4, 0.85, 0.6) * (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 a twisting cube shader?
A cube twisted around its vertical axis by an amount that swings back and forth. A good first step: rotate every vertex by an angle that depends on its height: the twist. 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 twisting cube shader use?
It is built from sine waves, diffuse lighting. Each one is a small idea you can reuse in other effects.
Does this twisting cube 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.