Three.js CT Volume Rendering (DICOM-style Ray Marching)

GPU volume rendering of a 128³ chest CT in Three.js with WebGL2 3D textures, offering transfer-function compositing, maximum intensity projection and shaded isosurfaces, plus cut-away clipping. Uses a synthetic phantom, not patient data.

Key topics: three.js volume rendering, dicom viewer, ct scan 3d, ray marching, data3dtexture, mip, medical imaging, webgl2

What this code does

- 3D Texture: voxels live in a `Data3DTexture` (8-bit, `RedFormat`) that encodes Hounsfield units linearly from −1000 to +1500.
- Ray Marching: a back-faced unit cube fires one ray per pixel, clipped to the box and the cut-away planes, stepping front to back.
- Transfer Functions: a 256×1 RGBA lookup maps HU to color and opacity, such as faint skin, red muscle and ivory bone.
- Opacity Correction: per-sample alpha is rescaled by step length, so changing ray steps keeps the same look.
- MIP & Isosurface: MIP keeps the brightest sample along the ray (angiography style); isosurface stops at the first HU crossing and shades it with the volume gradient.
- Synthetic Phantom: lungs with vessels, heart chambers, aorta, spine, ribs, sternum and liver are built procedurally, so no real DICOM data is shipped.
- Controls: render mode, transfer preset, opacity, ray steps, iso value, MIP window/level, cut-away sliders and auto rotate.

Continue Learning

JavaScript Implementation

ES6+
// Ray-march a Data3DTexture inside a unit cube (WebGL2)
const size = 64
const data = new Uint8Array(size ** 3)
for (let z = 0; z < size; z++) {
  for (let y = 0; y < size; y++) {
    for (let x = 0; x < size; x++) {
      const d = Math.hypot(x - 32, y - 32, z - 32)
      data[x + y * size + z * size * size] = d < 24 ? 120 + (d < 12 ? 120 : 0) : 0 // shell + dense core
    }
  }
}
const volume = new THREE.Data3DTexture(data, size, size, size)
volume.format = THREE.RedFormat
volume.minFilter = volume.magFilter = THREE.LinearFilter
volume.needsUpdate = true

const material = new THREE.ShaderMaterial({
  side: THREE.BackSide,
  transparent: true,
  uniforms: { uVolume: { value: volume }, uCam: { value: new THREE.Vector3() } },
  vertexShader: 'varying vec3 vPos; void main () { vPos = position; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }',
  fragmentShader: `
    uniform sampler3D uVolume; uniform vec3 uCam; varying vec3 vPos;
    void main () {
      vec3 dir = normalize(vPos - uCam);
      vec3 inv = 1.0 / dir;
      vec3 t0 = (vec3(-0.5) - uCam) * inv, t1 = (vec3(0.5) - uCam) * inv;
      float tn = max(max(min(t0, t1).x, min(t0, t1).y), min(t0, t1).z);
      float tf = min(min(max(t0, t1).x, max(t0, t1).y), max(t0, t1).z);
      float mip = 0.0;
      for (float t = max(tn, 0.0); t < tf; t += 0.005) mip = max(mip, texture(uVolume, uCam + dir * t + 0.5).r);
      gl_FragColor = vec4(vec3(mip), 1.0);
    }`
})
const box = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1), material)
scene.add(box)

function animate () {
  requestAnimationFrame(animate)
  material.uniforms.uCam.value.copy(box.worldToLocal(camera.position.clone()))
  renderer.render(scene, camera)
}
animate()