Three.js Protein Molecular Surface (Marching Cubes)
Molecular surface of a synthetic folded protein in Three.js, built as a Gaussian density isosurface with MarchingCubes and colored by electrostatic potential or hydrophobicity from a 3D texture, with a rainbow backbone inside.
Key topics: three.js protein, molecular surface, marching cubes, electrostatic potential, hydrophobicity, structural biology, isosurface
Game Over
What this code does
- Synthetic Fold: a self-avoiding Cα chain with 3.8 Å virtual bonds is packed into a globule; buried side chains tend to be hydrophobic and surface ones charged.
- Gaussian Surface: each atom adds exp(−b(d²/r² − 1)) to a scalar grid, and `MarchingCubes` extracts the iso = 1 surface.
- Electrostatics: a 32³ `Data3DTexture` stores Coulomb potential with a distance-dependent dielectric; red is negative and blue positive.
- Hydrophobicity: the same texture's green channel holds a smoothed hydrophobicity map (orange is greasy, teal is polar).
- Shader Injection: `onBeforeCompile` adds a sampler3D lookup to `MeshStandardMaterial`, keeping PBR lighting.
- Controls: color mode, opacity, backbone, blobbiness, radius scale, grid resolution, residue count, spin and new protein.
Related Demos
Continue Learning
JavaScript Implementation
import { MarchingCubes } from 'three/examples/jsm/objects/MarchingCubes.js'
// Gaussian molecular surface: sum one blob per atom into a scalar field, then polygonize
const res = 64
const surface = new MarchingCubes(res, new THREE.MeshStandardMaterial({ color: 0x9fb4d8 }), false, false, 200000)
surface.isolation = 1
surface.scale.setScalar(30) // field spans [-1, 1] in local space → ±30 Å
scene.add(surface)
const atoms = Array.from({ length: 200 }, () => ({
p: new THREE.Vector3().randomDirection().multiplyScalar(Math.cbrt(Math.random()) * 18),
r: 2
}))
for (let z = 0; z < res; z++) {
for (let y = 0; y < res; y++) {
for (let x = 0; x < res; x++) {
const w = new THREE.Vector3(x, y, z).multiplyScalar(2 / res).subScalar(1).multiplyScalar(30)
let rho = 0
for (const a of atoms) rho += Math.exp(-1.5 * (w.distanceToSquared(a.p) / (a.r * a.r) - 1))
surface.field[x + y * res + z * res * res] = rho
}
}
}
surface.update()