Three.js CT Slice Viewer (Axial, Coronal, Sagittal MPR)

Multi-planar reconstruction viewer in Three.js showing axial, coronal and sagittal CT slices in 3D and in three 2D panes, with radiology window/level presets, scroll-to-page, crosshairs and HU readout under the cursor.

Key topics: three.js ct viewer, mpr, axial coronal sagittal, window level, dicom slices, radiology, hounsfield units

What this code does

- Orthogonal Slices: each plane samples the same `Data3DTexture` at a fixed x, y or z, so moving a slice is just a uniform change.
- Window / Level: display gray = (HU − (L − W/2)) / W, with lung, mediastinum, bone and abdomen presets as used in radiology.
- MPR Layout: scissor viewports render a 3D overview plus three orthographic 2D panes in a single canvas.
- Crosshairs: colored lines in each pane mark where the other two planes cut through it.
- HU Readout: hovering a pane or 3D plane maps the pointer to a voxel and reports its Hounsfield value and tissue class.
- Radiological Convention: axial images show anterior at the top and the patient's right on the left of the screen.
- Controls: slice sliders, cine auto-scroll, window presets, manual window/level, colormap and 3D plane opacity (scroll over a pane to page through slices).

Continue Learning

JavaScript Implementation

ES6+
// Sample one axial slice of a 3D volume texture with window/level in a fragment shader
const volume = new THREE.Data3DTexture(voxels, size, size, size) // Uint8 HU-encoded voxels
volume.format = THREE.RedFormat
volume.minFilter = volume.magFilter = THREE.LinearFilter
volume.needsUpdate = true

const material = new THREE.ShaderMaterial({
  uniforms: { uVolume: { value: volume }, uSlice: { value: 0.5 }, uWindow: { value: 400 }, uLevel: { value: 40 } },
  vertexShader: 'varying vec2 vUv; void main () { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }',
  fragmentShader: `
    uniform sampler3D uVolume; uniform float uSlice, uWindow, uLevel; varying vec2 vUv;
    void main () {
      float hu = texture(uVolume, vec3(vUv, uSlice)).r * 2500.0 - 1000.0;
      gl_FragColor = vec4(vec3(clamp((hu - (uLevel - uWindow * 0.5)) / uWindow, 0.0, 1.0)), 1.0);
    }`,
  side: THREE.DoubleSide
})
const axial = new THREE.Mesh(new THREE.PlaneGeometry(1, 1), material)
scene.add(axial)

// Scroll through slices
renderer.domElement.addEventListener('wheel', (e) => {
  material.uniforms.uSlice.value = Math.min(1, Math.max(0, material.uniforms.uSlice.value + Math.sign(e.deltaY) / size))
})