Three.js Energy Shield / Force Field Shader
Sci-fi force field in Three.js with a hexagonal grid, Fresnel glow, impact ripples that travel over the sphere, and a soft glow where the shield intersects the ground and ship, using a scene depth pre-pass.
Key topics: three.js force field, energy shield shader, hex grid, impact ripple, depth intersection, sci-fi
Game Over
What this code does
- Hex Grid: the sphere UVs are tiled with hexagons and the distance to the nearest edge draws the lines.
- Impact Ripples: each hit stores a direction and age, and rings expand along the great-circle distance before fading.
- Intersection Glow: the scene without the shield renders to a `DepthTexture`, and the shield brightens where its depth nears that depth.
- Fresnel & Pulse: rim brightness plus a slow vertical pulse keep the shield alive between hits.
- Projectiles: shots travel inward and register an impact when they reach the shield radius.
- Controls: color, hex density, intersection glow, auto fire, fire rate and volley (double-click to aim a shot).
Related Demos
Continue Learning
JavaScript Implementation
// Shield ripple: great-circle distance from each impact drives an expanding ring
const rippleGLSL = `
uniform vec4 uHits[8]; // xyz = hit direction on the unit sphere, w = age
float ripple = 0.0;
for (int i = 0; i < 8; i++) {
if (uHits[i].w < 0.0) continue;
float d = acos(clamp(dot(normalize(vLocalPos), uHits[i].xyz), -1.0, 1.0));
ripple += smoothstep(0.25, 0.0, abs(d - uHits[i].w * 2.2)) * exp(-uHits[i].w * 1.6);
}
// intersection glow: compare our depth with the scene depth from a pre-pass
float sceneZ = perspectiveDepthToViewZ(texture2D(uDepth, gl_FragCoord.xy / uResolution).r, uNear, uFar);
float glow = 1.0 - smoothstep(0.0, 0.35, abs(sceneZ - vViewZ));`