Three.js Geometry Shader Effects (Emulated in WebGL)
Geometry-shader-style effects in Three.js (exploding triangles, shrinking to centroids, extruded spikes and noise displacement) emulated by baking per-face attributes into vertex data, since WebGL has no geometry stage.
Key topics: three.js geometry shader, explode effect, per-face attributes, vertex shader, barycentric wireframe, webgl
Game Over
What this code does
- No Geometry Stage: WebGL exposes only vertex and fragment shaders, so per-primitive work must be precomputed.
- Per-Face Attributes: a non-indexed mesh gives every triangle its own vertices, each carrying the face centroid, normal and seed.
- Explode & Tumble: vertices rotate about their centroid and move out along the face normal, keeping each triangle intact.
- Spikes: tagging one vertex per face via barycentric coordinates lets the shader extrude a point from every triangle.
- Barycentric Wireframe: the same barycentric attribute plus `fwidth` draws crisp edges without line primitives.
- Controls: effect, amount, model, wireframe and spin.
Related Demos
Continue Learning
JavaScript Implementation
// Emulating a geometry shader: give every vertex its triangle's centroid and normal
const geometry = new THREE.IcosahedronGeometry(1.4, 4).toNonIndexed()
const pos = geometry.attributes.position
const centers = new Float32Array(pos.count * 3)
for (let i = 0; i < pos.count; i += 3) {
const c = new THREE.Vector3().fromBufferAttribute(pos, i)
.add(new THREE.Vector3().fromBufferAttribute(pos, i + 1))
.add(new THREE.Vector3().fromBufferAttribute(pos, i + 2)).divideScalar(3)
for (let k = 0; k < 3; k++) centers.set([c.x, c.y, c.z], (i + k) * 3)
}
geometry.setAttribute('aCenter', new THREE.BufferAttribute(centers, 3))
// vertex shader: vec3 p = mix(position, aCenter, uShrink); p += normalize(aCenter) * uExplode;