A cluster in three dimensions.
The 3D view at the end of the Hoshen–Kopelman lesson draws about ten thousand small cubes and turns smoothly under a finger. Plotly would struggle with that many separate objects; three.js draws them in one call. On this page we rebuild that viewer from nothing in about eighty lines, and again the code shown is the code running here.
Series overview · Previous: Charts that mirror a notebook
What we are going to build
A \(32^3\) structure, generated in the browser from a smoothed random field cut at its median, labeled with the six-face rule. The group that reaches from the bottom face to the top face is orange; every other group is gray. Drag to rotate. It is a schematic structure, not a phase-field result, but its connectivity is computed, not drawn.
Loading the 3D view…
Four things every three.js picture needs
A renderer, which owns a <canvas> and talks to the graphics card through WebGL. A scene, which is the list of things to draw. A camera, which says where we look from and how wide the view is. And some light, without which a lit material is black. Once those exist, drawing is one line: renderer.render(scene, camera). Everything else in the file is deciding what to put in the scene and when to call that line.
Step 1: Imports
The structure generator and the voxel labeling already exist in the module behind the Hoshen–Kopelman page, so we import them. three.js is published as an ES module, which means we can import() it from a content network at the moment we need it rather than at page load. The r160 in the address pins the version, so the page cannot change under us when the library updates.
Loading the code…
Step 2: The structure and its visible surface
Drawing all \(32^3 = 32{,}768\) voxels would waste most of them: the inside of a solid is never visible. We keep only voxels with at least one empty face neighbor. For a structure at half filling that is roughly a third of the occupied voxels, and it is why the picture looks like a surface.
Loading the code…
Step 3: Renderer, scene, camera, light
The renderer's canvas is appended into our stage element. alpha: true lets the page background show through, and capping the pixel ratio at 2 stops phones with very dense screens from rendering four times more pixels than anyone can see. A hemisphere light gives soft fill from sky and ground colors; a directional light from the upper right gives the cubes their shading. The camera sits a little above the structure and looks at its center. The group is the thing we will rotate.
Loading the code…
Step 4: Thousands of cubes in one draw call
This is the step that makes three.js the right tool. An InstancedMesh draws one geometry many times; we give each instance its own position matrix and its own color. The graphics card receives one cube and a list of ten thousand placements, and the whole structure is drawn in a single call. Each cube is \(1/n\) wide, positioned so that the structure is centered on the origin inside a unit box, whose edges we draw as thin lines.
Loading the code…
Step 5: Rotate by dragging
We rotate the group rather than moving the camera, because that is what a reader expects from turning an object in the hand. Pointer events cover mouse, pen and touch with one set of listeners. setPointerCapture keeps the drag alive if the pointer leaves the stage mid-gesture, and the touch-action: none in the stylesheet stops a phone from scrolling the page instead.
Loading the code…
Step 6: The render loop, politely
A WebGL picture is redrawn by us, not by the browser, so we ask for a callback on every animation frame. Two courtesies keep this from costing the reader anything. We skip rendering while the stage is off screen, and we do not spin the structure on our own if the reader's system asks for reduced motion. A ResizeObserver keeps the canvas and camera matched to the stage when the page width changes.
Loading the code…
Step 7: Mount when visible
As with the Plotly charts, an IntersectionObserver starts the viewer when the stage approaches the viewport, and the library is downloaded only then.
Loading the code…
Where to go from here
The same file draws a real microstructure if makeStructure is replaced by a function that fetches a saved field. A \(128^3\) order-parameter array stored as 8-bit integers is two megabytes, which is smaller than Plotly. Replacing the cubes with a marching-cubes isosurface gives a smooth interface; three.js ships an implementation in its examples folder. For color by local curvature or by cluster size, setColorAt is already the hook.
What to try
- Change
nfrom 32 to 64. The surface voxel count grows about four times; notice that the frame rate barely moves, because the number of draw calls does not change. - Replace
MeshLambertMaterialwithMeshStandardMaterialand addroughness: 0.6. The lighting becomes physically based and the cubes look less flat. - Add a button that hides the gray groups by setting their color alpha, or simply by skipping them in
surfaceVoxels, as the Hoshen–Kopelman page does.
Series overview · Previous: Charts that mirror a notebook
three.js is released under the MIT license and loaded from cdnjs when the viewer scrolls into view.