Medium 3DAnimated
Gas giant
A banded, swirling planet with a blue atmosphere glow around it.
About the Gas giant shader
A banded, swirling planet with a blue atmosphere glow around it. 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 764 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
same setup as the globe: R = ?, n = normalized (p, z)- Swirl
u = atan2(n.x, n.z) + time * ?- Bands
sin(n.y * ? + ? * sin(u * 2.0 + n.y * ?))- Base
lerp(float3(0.85,0.6,0.35), float3(0.95,0.85,0.65), 0.5 + 0.5 * bands)- Light
normalize(float3(0.5, ?, ?)), planet = base * (? + ? * diffuse)- Rim
pow(1.0 - n.z, ?), float3(0.4, 0.6, 1.0) * rim * 0.5, added to the planet- Glow outside
exp(-max(length(p) - R, 0.0) * ?) * (1.0 - mask) * ? * float3(0.4,0.6,1.0)- Space
float3(0.02, 0.03, 0.06)
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 gas giant shader
- Bands depend on latitude (n.y); wobble them with a sine of longitude.
- Rim lighting brightens where the surface turns away from the camera (n.z near 0).
Techniques used
- Polar coordinates: angle and distance from a center, for spirals, rings and radial effects.
- Sine waves: smooth oscillation for waves, pulses and motion over time.
- Diffuse lighting: brightness from the angle between the surface and the light.
- Exponential falloff: glows, fog and light absorption that fade smoothly with distance.
- step: a hard 0-or-1 cutoff for crisp shapes and thresholds.
- lerp (mix): blend between two colors or values.
Gas giant 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) + time * 0.3; - 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 bands = sin(n.y * 14.0 + 1.5 * sin(u * 2.0 + n.y * 3.0));
float3 base = lerp(float3(0.85, 0.6, 0.35), float3(0.95, 0.85, 0.65), 0.5 + 0.5 * bands);
float diff = max(dot(n, normalize(float3(0.5, 0.7, 0.6))), 0.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).
Part 4: Choose the colours (continued)
Still inside frag()
float rim = pow(1.0 - n.z, 3.0);
float3 planet = base * (0.12 + 0.88 * diff) + float3(0.4, 0.6, 1.0) * rim * 0.5;
float glow = exp(-max(sqrt(r2) - R, 0.0) * 10.0) * (1.0 - mask) * 0.4; - pow(x, k) reshapes a value: a high power keeps only the brightest part, which makes small sharp highlights.
- exp(-x) falls smoothly from 1 towards 0, which is how glows, fog and absorbed light fade with distance.
- float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).
- max and min pick the larger or smaller of two values. They are how shapes are combined or clamped.
Part 5: Colour the pixel and return it
Still inside frag()
float3 col = lerp(float3(0.02, 0.03, 0.06) + float3(0.4, 0.6, 1.0) * glow, planet, mask);
return float4(col, 1.0); - lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.
- 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) + time * 0.3;
float bands = sin(n.y * 14.0 + 1.5 * sin(u * 2.0 + n.y * 3.0));
float3 base = lerp(float3(0.85, 0.6, 0.35), float3(0.95, 0.85, 0.65), 0.5 + 0.5 * bands);
float diff = max(dot(n, normalize(float3(0.5, 0.7, 0.6))), 0.0);
float rim = pow(1.0 - n.z, 3.0);
float3 planet = base * (0.12 + 0.88 * diff) + float3(0.4, 0.6, 1.0) * rim * 0.5;
float glow = exp(-max(sqrt(r2) - R, 0.0) * 10.0) * (1.0 - mask) * 0.4;
float3 col = lerp(float3(0.02, 0.03, 0.06) + float3(0.4, 0.6, 1.0) * glow, planet, mask);
return float4(col, 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) + time * 0.3;
float bands = sin(n.y * 14.0 + 1.5 * sin(u * 2.0 + n.y * 3.0));
vec3 base = mix(vec3(0.85, 0.6, 0.35), vec3(0.95, 0.85, 0.65), 0.5 + 0.5 * bands);
float diff = max(dot(n, normalize(vec3(0.5, 0.7, 0.6))), 0.0);
float rim = pow(1.0 - n.z, 3.0);
vec3 planet = base * (0.12 + 0.88 * diff) + vec3(0.4, 0.6, 1.0) * rim * 0.5;
float glow = exp(-max(sqrt(r2) - R, 0.0) * 10.0) * (1.0 - mask) * 0.4;
vec3 col = mix(vec3(0.02, 0.03, 0.06) + vec3(0.4, 0.6, 1.0) * glow, planet, mask);
return vec4(col, 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) + time * 0.3;
let bands = sin(n.y * 14.0 + 1.5 * sin(u * 2.0 + n.y * 3.0));
let base = mix(vec3f(0.85, 0.6, 0.35), vec3f(0.95, 0.85, 0.65), 0.5 + 0.5 * bands);
let diff = max(dot(n, normalize(vec3f(0.5, 0.7, 0.6))), 0.0);
let rim = pow(1.0 - n.z, 3.0);
let planet = base * (0.12 + 0.88 * diff) + vec3f(0.4, 0.6, 1.0) * rim * 0.5;
let glow = exp(-max(sqrt(r2) - R, 0.0) * 10.0) * (1.0 - mask) * 0.4;
let col = mix(vec3f(0.02, 0.03, 0.06) + vec3f(0.4, 0.6, 1.0) * glow, planet, mask);
return vec4f(col, 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) + TIME * 0.3;
float bands = sin(n.y * 14.0 + 1.5 * sin(u * 2.0 + n.y * 3.0));
vec3 base = mix(vec3(0.85, 0.6, 0.35), vec3(0.95, 0.85, 0.65), 0.5 + 0.5 * bands);
float diff = max(dot(n, normalize(vec3(0.5, 0.7, 0.6))), 0.0);
float rim = pow(1.0 - n.z, 3.0);
vec3 planet = base * (0.12 + 0.88 * diff) + vec3(0.4, 0.6, 1.0) * rim * 0.5;
float glow = exp(-max(sqrt(r2) - R, 0.0) * 10.0) * (1.0 - mask) * 0.4;
vec3 col = mix(vec3(0.02, 0.03, 0.06) + vec3(0.4, 0.6, 1.0) * glow, planet, mask);
return vec4(col, 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) + time * 0.3;
float bands = sin(n.y * 14.0 + 1.5 * sin(u * 2.0 + n.y * 3.0));
float3 base = mix(float3(0.85, 0.6, 0.35), float3(0.95, 0.85, 0.65), 0.5 + 0.5 * bands);
float diff = max(dot(n, normalize(float3(0.5, 0.7, 0.6))), 0.0);
float rim = pow(1.0 - n.z, 3.0);
float3 planet = base * (0.12 + 0.88 * diff) + float3(0.4, 0.6, 1.0) * rim * 0.5;
float glow = exp(-max(sqrt(r2) - R, 0.0) * 10.0) * (1.0 - mask) * 0.4;
float3 col = mix(float3(0.02, 0.03, 0.06) + float3(0.4, 0.6, 1.0) * glow, planet, mask);
return float4(col, 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 gas giant shader?
A banded, swirling planet with a blue atmosphere glow around it. A good first step: bands depend on latitude (n.y); wobble them with a sine of longitude. 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 gas giant shader use?
It is built from polar coordinates, sine waves, diffuse lighting, exponential falloff, step. Each one is a small idea you can reuse in other effects.
Does this gas giant 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.