Shaders From Zero · Episode 14
Portal
Portal is Episode 14 of Shaders From Zero, a MasterAllArts free interactive lesson for artists learning creative 3D on the web.
Part of the MasterAllArts Creative 3D Roadmap .
A portal is a 2D effect on a 3D plane. The portal disc is an SDF (Episode 06). The swirl inside is rotated noise (Episode 05). The color comes from an IQ cosine palette (Episode 04). The rim shimmer is sine on the SDF boundary angle. Most of this episode is COMPOSITION — stacking techniques you already know on top of each other.
It's also one of the most recognizable shader effects in modern cinema: Dr. Strange's sling-ring portals. Same five lines, different palette.
Layer 1 — The disc
Standard SDF circle from Episode 06. UVs centered around 0.5, length gives radius,
subtract r for the signed distance.
void main() {
vec2 p = vUv - 0.5;
float d = length(p) - 0.4; // SDF
float fill = step(0.0, -d); // 1 inside, 0 outside
vec3 col = mix(vec3(0.05), vec3(0.7, 0.2, 0.8), fill);
gl_FragColor = vec4(col, 1.0);
}
Solid magenta circle on a dark background. The portal's shape, no content yet.
Layer 2 — Rotating noise inside
The reader knows FBM and domain warping from Episode 05. The new trick is to
rotate the SAMPLE COORDINATES by uTime before evaluating the noise —
the noise pattern itself spins.
vec2 c = vUv - 0.5;
float angle = uTime * 0.3;
mat2 R = mat2(cos(angle), -sin(angle), sin(angle), cos(angle));
vec2 q = R * c;
float n = fbm(q * 4.0 + uTime * 0.1);
The mat2 is a standard 2D rotation. Multiplying c by
R rotates the coordinate around origin. Sampling FBM at the rotated
coordinate effectively rotates the noise pattern with time.
Stage 2 in the demo. Disc filled with grayscale noise that swirls. Not yet portal-colored.
Layer 3 — IQ palette color
Drive the noise through the cosine palette from Episode 04. Phase shift by
uTime so the colors cycle.
vec3 col = 0.5 + 0.5 * cos(6.2832 * (n + uTime * 0.1 + vec3(0.0, 0.33, 0.67)));
Tune the palette knobs for "portal" colors — magentas, cyans, electric blues. These read as magical because they're the colors movies have trained us to associate with magic.
Layer 4 — Soft radial fade + center glow
Replace the hard step edge with a smoothstep:
float diskMask = smoothstep(0.0, -0.02, d); // soft falloff at the rim
col *= diskMask;
// brighten the middle
float core = smoothstep(0.2, 0.0, length(c)) * 0.3;
col += core;
The smoothstep fade replaces the hard pixel boundary with an anti-aliased one that reads as "depth" — the portal looks like it has a felt edge, not a paper one.
The center brightening sells "depth" further. Real portals have a bright core because we expect the "other side" to be brighter than where we're standing.
Layer 5 — Rim shimmer
Right at the SDF boundary, add a bright thin ring with angle-modulated sparkles.
float rim = smoothstep(0.012, 0.0, abs(d)); // thin band
float shimmer = 0.6 + 0.4 * sin(atan(c.y, c.x) * 30.0 + uTime * 5.0);
col += rim * shimmer * vec3(1.0, 0.95, 0.8);
Three ideas:
abs(d)is 0 at the SDF boundary, positive elsewhere.smoothstepgives a thin band of brightness right at the edge.atan(c.y, c.x)is the angle around the disc (polar coord, Episode 03).sin(angle * 30 + uTime * 5)creates 30 shimmer-peaks around the rim, rotating with time.
Putting it together
// FRAGMENT (final)
varying vec2 vUv;
uniform float uTime;
float hash(vec2 p) {
return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}
float noise(vec2 p) {
vec2 i = floor(p), f = fract(p);
float a = hash(i);
float b = hash(i + vec2(1.0, 0.0));
float c = hash(i + vec2(0.0, 1.0));
float d = hash(i + vec2(1.0, 1.0));
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
float fbm(vec2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) { v += a * noise(p); p *= 2.0; a *= 0.5; }
return v;
}
void main() {
vec2 c = vUv - 0.5;
float d = length(c) - 0.4;
// rotated noise sample
float angle = uTime * 0.3;
mat2 R = mat2(cos(angle), -sin(angle), sin(angle), cos(angle));
vec2 q = R * c;
float n = fbm(q * 4.0 + uTime * 0.1);
// palette
vec3 col = 0.5 + 0.5 * cos(6.2832 * (n + uTime * 0.1 + vec3(0.0, 0.33, 0.67)));
// soft mask + core glow
float diskMask = smoothstep(0.0, -0.02, d);
col *= diskMask;
col += smoothstep(0.2, 0.0, length(c)) * 0.3;
// rim shimmer
float rim = smoothstep(0.012, 0.0, abs(d));
float shimmer = 0.6 + 0.4 * sin(atan(c.y, c.x) * 30.0 + uTime * 5.0);
col += rim * shimmer * vec3(1.0, 0.95, 0.8);
gl_FragColor = vec4(col, 1.0);
}
Common pitfalls
- UVs not centered. Disc sits in the corner.
- Plane aspect ratio not 1:1. Disc is an oval. Use a square plane.
- Noise scale too tight. Looks like static, not magic. Use
fbm(q * 4.0)as a starting point. - Palette too desaturated. Looks gray, not portal. Bump the palette amplitudes.
- Rim shimmer too loud. Looks like Christmas lights. Keep the shimmer multiplier ≤ 0.4.
Exercises
- Swap palette to green and gold for "ancient magic." Adjust the
vec3(0.0, 0.33, 0.67)offsets to taste. - Invert: black disc with glowing rim, swirling content OUTSIDE the disc. The portal-from-the-other-side look.
- Replace the circle SDF with a vertical tear:
d = abs(c.x) - 0.05;. Now you have a tall skinny rip in spacetime.
What's next
Episode 15 — Stylized Toon Shader. Quantized lighting, posterized colors, fresnel rim, fresnel outline. The non-photoreal episode.