Three.js DNA Double Helix (Interactive Molecular Model)

Interactive Three.js DNA double helix with complementary A–T / G–C base pairs, sugar-phosphate backbones, adjustable twist and rise, and an animated unzipping strand separation.

Key topics: three.js dna, double helix, molecular model, base pairs, biology visualization, instancedmesh

What this code does

- Helix Geometry: each base pair sits at angle `i × twist` and height `i × rise`, the same parametric form as B-DNA (~10.5 bp per turn).
- Complementary Bases: adenine pairs with thymine and guanine with cytosine; each half-rung is colored by its base.
- InstancedMesh: all sugars and half-rungs are drawn with two `InstancedMesh` objects, so 80+ pairs cost two draw calls.
- Backbone Ribbons: `CatmullRomCurve3` + `TubeGeometry` trace each strand through its sugar positions.
- Unzipping: strands peel apart from the 5' end, mimicking helicase separating the strands before replication.
- Controls: change pair count, sequence type, twist, rise, radius, unzip amount and spin speed.

Continue Learning

JavaScript Implementation

ES6+
const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(50, width / height, 0.1, 100)
camera.position.set(0, 0, 22)
const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setSize(width, height)
document.querySelector('#app').appendChild(renderer.domElement)

scene.add(new THREE.AmbientLight(0xffffff, 0.4))
const light = new THREE.DirectionalLight(0xffffff, 2)
light.position.set(6, 10, 8)
scene.add(light)

const colors = { A: 0xef4444, T: 0xf59e0b, G: 0x22c55e, C: 0x3b82f6 }
const pairs = { A: 'T', T: 'A', G: 'C', C: 'G' }
const helix = new THREE.Group()
scene.add(helix)

const sphere = new THREE.SphereGeometry(0.3, 16, 12)
const sugar = new THREE.MeshStandardMaterial({ color: 0xe5e7eb })
for (let i = 0; i < 40; i++) {
  const base = 'ATGC'[Math.floor(Math.random() * 4)]
  const angle = i * THREE.MathUtils.degToRad(36)
  const y = (i - 20) * 0.48
  const a = new THREE.Vector3(Math.cos(angle) * 2, y, Math.sin(angle) * 2)
  const b = new THREE.Vector3(Math.cos(angle + 2.45) * 2, y, Math.sin(angle + 2.45) * 2)
  ;[a, b].forEach(p => {
    const m = new THREE.Mesh(sphere, sugar)
    m.position.copy(p)
    helix.add(m)
  })
  // Two half-rungs, one per complementary base
  const mid = a.clone().add(b).multiplyScalar(0.5)
  ;[[a, base], [b, pairs[base]]].forEach(([p, kind]) => {
    const len = p.distanceTo(mid)
    const rung = new THREE.Mesh(
      new THREE.CylinderGeometry(0.12, 0.12, len, 8),
      new THREE.MeshStandardMaterial({ color: colors[kind] })
    )
    rung.position.copy(p).lerp(mid, 0.5)
    rung.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), mid.clone().sub(p).normalize())
    helix.add(rung)
  })
}

function animate () {
  requestAnimationFrame(animate)
  helix.rotation.y += 0.005
  renderer.render(scene, camera)
}
animate()