Skip to the content

3D

model-annotations

Labels pinned to points on a 3D model that follow them as the model turns, each a button that opens its note, hidden while its point is behind the camera or out of view; the same notes are always given as a list, so nobody has to find them by turning.

Effects and 3D data

When to use it

Pointing out parts of a 3D object: the parts of a machine, features of a building, finds on a scan. Put the tracker inside the scene and the labels over it, in the same positioned box. For a flat picture use hotspots; for a model with no notes use model-viewer.

See it working

The demonstration the browser checks drive, with the keyboard as well as the pointer. More of it is on the Effects and 3D data page.

Word rotate and pattern

Maps for teams, schools, or newsrooms

Animated beam

A map becomes a film.

Orbit

  • Maps
  • Charts
  • Films
  • Motion
Rivier

Confetti

Not yet published.

Point cloud

Ninety finds
#widthheightdepthkind
11.22.52.5Clay
23.22.73.2Stone
32.92.53.8Glass
40.30.64.3Clay
52.71.24.5Stone
642.54.4Glass
70.52.14.1Clay
81.63.13.5Stone
94.13.32.9Glass
101.61.32.1Clay
111.21.21.4Stone
123.11.80.9Glass
132.21.30.6Clay
142.12.70.5Stone
152.23.70.7Glass
161.62.11.2Clay
173.11.81.8Stone
182.61.82.5Glass
190.50.63.2Clay
2031.93.9Stone
213.73.44.3Glass
220.32.54.5Clay
231.92.74.4Stone
244.22.44.1Glass
251.20.63.5Clay
261.21.22.8Stone
273.42.52.1Glass
282.12.21.4Clay
291.73.10.9Stone
302.43.30.6Glass
311.91.20.5Clay
322.81.20.7Stone
332.41.81.2Glass
340.81.41.8Clay
353.12.82.5Stone
363.43.73.2Glass
370.22.13.9Clay
382.31.84.3Stone
394.21.84.5Glass
400.90.64.4Clay
411.324.1Stone
423.83.43.5Glass
4322.52.8Clay
441.52.62.1Stone
452.62.41.4Glass
462.10.60.9Clay
472.51.30.6Stone
482.22.60.5Glass
491.12.20.7Clay
503.23.11.2Stone
5133.21.8Glass
520.21.22.5Clay
532.61.23.3Stone
5441.93.9Glass
550.61.44.3Clay
561.52.84.5Stone
5743.74.4Glass
581.724.1Clay
591.31.73.5Stone
6031.72.8Glass
612.20.72.1Clay
622.221.4Stone
632.23.40.9Glass
641.52.50.6Clay
653.12.60.5Stone
662.72.30.7Glass
670.40.51.2Clay
682.91.31.8Stone
693.82.62.6Glass
700.32.23.3Clay
711.83.13.9Stone
724.23.24.3Glass
731.31.14.5Clay
741.21.14.4Stone
753.31.94.1Glass
762.21.53.5Clay
771.82.92.8Stone
782.33.72.1Glass
791.821.4Clay
802.91.70.9Stone
812.41.70.6Glass
820.70.70.5Clay
833.12.10.7Stone
843.53.51.2Glass
850.22.51.8Clay
862.22.62.6Stone
874.22.33.3Glass
8810.53.9Clay
891.31.34.3Stone
903.72.74.5Glass

Model annotations

  1. Lid — Hinged at the back.
  2. Side — A handle was here.
  3. Front — The maker's mark.

Add it

npx shadcn@latest add @rivier-ui/model-annotations

The registry is named once in components.json: see Installation.

Example

Import it from @/components/three/model-annotations.

const store = useAnnotationStore();
<div className="relative">
  <Scene label="A vase" aspect={1}>
    <ModelStage><Vase /></ModelStage>
    <ModelAnnotationTracker store={store} points={[{ id: "handle", position: [0.6, 0.4, 0] }]} />
  </Scene>
  <ModelAnnotations store={store} annotations={[{ id: "handle", label: "Handle", note: "Added later, in a darker clay." }]} />
</div>

What it needs

A client component: it uses state, effects or browser behaviour. Packages: @react-three/fiber, three.

It imports

Source

The whole file. Once copied it is yours: the registry item holds the same text.

src/components/three/model-annotations.tsx
"use client";
/**
 * @name model-annotations
 * @category three
 * @description Labels pinned to points on a 3D model that follow them as the model turns, each a button that opens its note, hidden while its point is behind the camera or out of view; the same notes are always given as a list, so nobody has to find them by turning.
 * @when Pointing out parts of a 3D object: the parts of a machine, features of a building, finds on a scan. Put the tracker inside the scene and the labels over it, in the same positioned box. For a flat picture use hotspots; for a model with no notes use model-viewer.
 * @example
 * const store = useAnnotationStore();
 * <div className="relative">
 *   <Scene label="A vase" aspect={1}>
 *     <ModelStage><Vase /></ModelStage>
 *     <ModelAnnotationTracker store={store} points={[{ id: "handle", position: [0.6, 0.4, 0] }]} />
 *   </Scene>
 *   <ModelAnnotations store={store} annotations={[{ id: "handle", label: "Handle", note: "Added later, in a darker clay." }]} />
 * </div>
 */
import { useFrame, useThree } from "@react-three/fiber";
import { useRef, useState, type ComponentProps } from "react";
import { Vector3 } from "three";
import { cn } from "@/lib/cn";
 
/** Connects the tracker in the canvas to the labels on the page: the tracker writes each label's place straight onto it every frame. */
export type AnnotationStore = { elements: Map<string, HTMLElement> };
 
/** A store for one set of annotations. */
export function useAnnotationStore(): AnnotationStore {
  const [store] = useState<AnnotationStore>(() => ({ elements: new Map() }));
  return store;
}
 
export type AnnotationPoint = { id: string; position: readonly [number, number, number] };
 
/** Inside the scene: projects each point to the canvas every frame and moves its label there. */
export function ModelAnnotationTracker({ store, points }: { store: AnnotationStore; points: readonly AnnotationPoint[] }) {
  const camera = useThree((state) => state.camera);
  const size = useThree((state) => state.size);
  const scratch = useRef(new Vector3());
  useFrame(() => {
    for (const point of points) {
      const element = store.elements.get(point.id);
      if (!element) continue;
      const v = scratch.current.set(point.position[0], point.position[1], point.position[2]);
      // The point in the camera's space: behind the camera when z is past 1 after projection.
      v.project(camera);
      const visible = v.z < 1 && Math.abs(v.x) <= 1.05 && Math.abs(v.y) <= 1.05;
      element.style.transform = `translate(${((v.x + 1) / 2) * size.width}px, ${((1 - v.y) / 2) * size.height}px) translate(-50%, -50%)`;
      element.dataset["hidden"] = visible ? "false" : "true";
    }
  });
  return null;
}
 
export type ModelAnnotation = { id: string; label: string; note?: string };
 
export type ModelAnnotationsProps = Omit<ComponentProps<"div">, "children"> & {
  store: AnnotationStore;
  annotations: readonly ModelAnnotation[];
  /** The heading of the list of notes. Default "Notes". */
  listLabel?: string;
};
 
/** Over the scene: a button per point, and under it the same notes as a list. */
export function ModelAnnotations({ store, annotations, listLabel = "Notes", className, ...props }: ModelAnnotationsProps) {
  const [open, setOpen] = useState<string | null>(null);
  return (
    <>
      <div data-slot="model-annotations" aria-hidden="true" className={cn("pointer-events-none absolute inset-0 overflow-hidden", className)} {...props}>
        {annotations.map((annotation, index) => (
          <div
            key={annotation.id}
            ref={(node) => {
              if (node) store.elements.set(annotation.id, node);
              else store.elements.delete(annotation.id);
            }}
            data-hidden="true"
            // Placed from the canvas's own left edge: the projection is in screen pixels, which do not mirror.
            className="pointer-events-auto absolute left-0 top-0 transition-opacity duration-instant data-[hidden=true]:pointer-events-none data-[hidden=true]:opacity-0 motion-reduce:transition-none"
          >
            <button
              type="button"
              tabIndex={-1}
              onClick={() => setOpen(open === annotation.id ? null : annotation.id)}
              className="flex items-center gap-1.5 rounded-control border bg-background px-2 py-1 text-xs font-medium"
            >
              <span className="flex size-5 items-center justify-center rounded-full bg-primary text-primary-foreground tabular-nums">{index + 1}</span>
              {annotation.label}
            </button>
            {open === annotation.id && annotation.note ? <p className="mt-1 max-w-56 rounded-surface border bg-popover p-2 text-xs text-popover-foreground">{annotation.note}</p> : null}
          </div>
        ))}
      </div>
      <ol data-slot="model-annotations-list" aria-label={listLabel} className="mt-3 flex flex-col gap-1 text-sm">
        {annotations.map((annotation, index) => (
          <li key={annotation.id} className="flex gap-2">
            <span aria-hidden="true" className="flex size-5 shrink-0 items-center justify-center rounded-full bg-primary text-xs text-primary-foreground tabular-nums">
              {index + 1}
            </span>
            <span>
              <span className="font-medium">{annotation.label}</span>
              {annotation.note ? <span className="text-muted-foreground"> — {annotation.note}</span> : null}
            </span>
          </li>
        ))}
      </ol>
    </>
  );
}