Shaders From Zero · Episode 09

Hologram

Hologram is Episode 09 of Shaders From Zero, a MasterAllArts free interactive lesson for artists learning creative 3D on the web.

Part of the MasterAllArts Creative 3D Roadmap .

Episode 09 · Shaders From Zero

The hologram is the most-copied shader effect in cinema. Princess Leia in Star Wars (1977). The holotable in Marvel. The AR overlays in Westworld. Every sci-fi UI ever designed. It's also one of the most rewarding to build from scratch, because every layer is something you've already learned in Season 1. By the end of this episode you have a shader you can drop onto any mesh to make it look like a projection.

This is Episode 1 of Season 2 — "Real Effects." Each episode this season builds one named effect in five progressive layers. The demo lets you step through the layers so you feel each technique's contribution.

0 fps
IcosahedronGeometry · ShaderMaterial
Layer 1: solid tint. No hologram yet.

The four layers

The hologram look is a stack of four independent effects, each contributing something specific:

  • Fresnel rim (Episode 04 + dot products): edges glow brighter than the center. This is the single most important hologram cue.
  • Scanlines (Episode 01 + sin on world Y): horizontal bands that tell the eye "this is a projection, not a real surface."
  • Flicker (Episode 08 + fast sin): a tiny multiplicative brightness wobble. Sells the CRT/projector vibe.
  • Glitch (Episode 05 noise + UV displacement): rare horizontal slip events. The signature of a corrupted transmission.

Each layer multiplies into the final color. They're independent — you can rearrange the order or strip any one out without breaking the others.

Layer 1 — Base tint

Start with the boring case: solid color on every pixel. Sanity check the geometry, lighting, camera, and material setup before adding any effects. If your base tint doesn't render, none of the other layers will either.

fragmentShader: /*glsl*/`
  uniform vec3 uTint;
  void main() {
    gl_FragColor = vec4(uTint, 1.0);
  }
`

Pick a sci-fi cyan: vec3(0.3, 0.7, 1.0). The whole sphere is one flat color. Looks like a billiard ball.

Layer 2 — Fresnel rim

Fresnel is the optical effect where a surface looks more reflective at glancing angles than it does straight on. Look at a lake from far away — the surface is mirror-like. Look straight down into the same lake — you see through to the bottom.

The trick is the dot product of the surface normal and the view direction:

// vertex shader
varying vec3 vNormal;
varying vec3 vViewDir;

void main() {
  // normal in view space (the camera's perspective)
  vNormal = normalize(normalMatrix * normal);
  // view direction: from vertex toward camera
  vec4 mv = modelViewMatrix * vec4(position, 1.0);
  vViewDir = normalize(-mv.xyz);
  gl_Position = projectionMatrix * mv;
}

// fragment shader
varying vec3 vNormal;
varying vec3 vViewDir;
uniform vec3 uTint;
uniform float uFresnelPower;

void main() {
  float NdotV = max(dot(normalize(vNormal), normalize(vViewDir)), 0.0);
  float fresnel = pow(1.0 - NdotV, uFresnelPower);
  gl_FragColor = vec4(uTint * fresnel, fresnel);
}

Three things are happening:

  • 1.0 - NdotV is HIGH at glancing angles (where the normal points sideways relative to the view) and LOW where the normal points at the camera. Rim values approach 1.0; the center approaches 0.
  • pow(x, n) with n > 1 amplifies the rim and compresses the center. n = 3 or n = 5 is the sweet spot.
  • We use fresnel as both the color brightness and the alpha. The center fades out almost completely; the rim stays bright and opaque.

For the fresnel-alpha to actually do anything, set material.transparent = true and material.depthWrite = false on the JS side.

Power-user note: most production holograms use THREE.AdditiveBlending instead of alpha blending. Additive overlays brighten what's behind them, which is perfect for "this is a hologram projected onto the scene." Alpha blending is fine for our case (dark background) and easier to reason about.

Layer 3 — Scanlines

Holograms have horizontal bands because the imaginary projector raster-scans line by line — just like an old CRT television. The trick is a sine wave evaluated on the world Y coordinate, multiplied into the brightness.

varying vec3 vWorldPos;          // pass world position from vertex
uniform float uScanFreq;          // e.g., 60.0
uniform float uScanSpeed;         // e.g., 8.0

void main() {
  // ... fresnel as before
  float scan = sin(vWorldPos.y * uScanFreq - uTime * uScanSpeed) * 0.5 + 0.5;
  scan = scan * 0.6 + 0.4;        // don't dip to pure black
  vec3 col = uTint * fresnel * scan;
  gl_FragColor = vec4(col, fresnel);
}

Two details:

  • World Y, not local Y. If you used position.y (object space), the scanlines would rotate with the mesh and look pinned to the surface. World Y keeps them anchored to the world axis — they slide across the surface as the mesh rotates, which is what real projection raster-scan would do.
  • Don't dip to zero. A raw sine swings between -1 and 1. Remap to [0,1] with * 0.5 + 0.5, then bias up with * 0.6 + 0.4. Now the darkest scanline is at 40% brightness, not black. The bands feel like a CRT, not a barcode.

Layer 4 — Flicker

A real projector flickers. Real CRTs flicker. Real fluorescent tubes flicker. The eye reads tiny brightness wobbles as "this is a screen, not a surface."

float flicker = 0.9 + 0.1 * sin(uTime * 30.0);
col *= flicker;

Note the amplitude: 0.1. Crank it to 0.5 and the sphere strobes like an emergency alarm. Keep it small. The flicker is meant to be noticed only subconsciously.

Layer 5 — Glitch (the final touch)

Holograms occasionally tear. A line of pixels jumps sideways for a frame, then snaps back. We can fake this with a noise function that fires rarely.

float hash(vec2 p) {
  return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}

void main() {
  // pre-glitch: rarely shift world Y a tiny bit
  float g = step(0.98, hash(vec2(floor(vWorldPos.y * 10.0), floor(uTime * 4.0))));
  float yShift = g * 0.05;
  float scanY = vWorldPos.y + yShift;

  // then compute scanlines using scanY instead of vWorldPos.y
  float scan = sin(scanY * uScanFreq - uTime * uScanSpeed) * 0.5 + 0.5;
  scan = scan * 0.6 + 0.4;

  // ... rest as before
}

The step(0.98, hash(...)) means: 98% of the time the result is 0 (no glitch), 2% of the time it's 1 (full glitch). The floor(uTime * 4.0) means glitch events refresh 4 times per second — so they don't update every frame (which would look like static).

The shift is applied to scanY before computing scanlines, so the glitch manifests as a horizontal band of misaligned scanlines. Subtle. Effective.

Putting it together

// FINAL FRAGMENT SHADER
varying vec3 vNormal;
varying vec3 vViewDir;
varying vec3 vWorldPos;

uniform float uTime;
uniform vec3  uTint;
uniform float uFresnelPower;
uniform float uScanFreq;
uniform float uScanSpeed;

float hash(vec2 p) {
  return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}

void main() {
  // fresnel
  float NdotV = max(dot(normalize(vNormal), normalize(vViewDir)), 0.0);
  float fresnel = pow(1.0 - NdotV, uFresnelPower);

  // glitch slip
  float g = step(0.98, hash(vec2(floor(vWorldPos.y * 10.0), floor(uTime * 4.0))));
  float scanY = vWorldPos.y + g * 0.05;

  // scanlines
  float scan = sin(scanY * uScanFreq - uTime * uScanSpeed) * 0.5 + 0.5;
  scan = scan * 0.6 + 0.4;

  // flicker
  float flicker = 0.9 + 0.1 * sin(uTime * 30.0);

  vec3 col = uTint * fresnel * scan * flicker;
  gl_FragColor = vec4(col, fresnel);
}

Drop this on any mesh + transparent: true + depthWrite: false and you have a hologram. Tune the four parameters (uFresnelPower, uScanFreq, uScanSpeed, uTint) to taste.

Common pitfalls

  • No transparent = true. The fresnel-as-alpha does nothing; the mesh is fully opaque. Set it on the material.
  • Local position instead of world position. Scanlines rotate with the mesh, look pinned to the surface, not the world. Pass vWorldPos from the vertex shader.
  • Flicker amplitude too high. You wanted "subtle CRT," you got "emergency alarm." Keep 0.1 or below.
  • Glitch threshold too aggressive. Constant tearing is annoying. Use step(0.98, ...) not step(0.5, ...).
  • Depth-write artifacts when layering with other transparents. Set material.depthWrite = false. Sort by distance if you have multiple transparent objects.

Exercises

  1. Swap the cyan for an angry red and double the flicker frequency. Now it looks like a corrupted warning hologram. Compare with the original cyan.
  2. Animate uFresnelPower from 2.0 to 8.0 with a slow sine. The hologram "breathes" — solid then ghostly.
  3. Replace world-Y scanlines with screen-Y scanlines: in the fragment shader, use gl_FragCoord.y divided by canvas height as the scan coordinate. Now the scanlines are pinned to the screen, not the world. Stranger Things look.

What's next

Episode 10 — Dissolve / Burn. The Marvel snap. Per-pixel discard driven by noise, with an animated threshold sweep and a hot orange edge that makes it look like burning paper.