# model

A glTF model fitted into the element, lit, turnable and recolored from markup.

The model from `src` sits centered in the box, lit by a soft studio environment and tone mapped like a product shot, facing the viewer or resting in a three-quarter view seen slightly from above. From that pose it can turn on its own, follow scroll, lean toward the pointer and spin under a drag with a little inertia, staying where the visitor leaves it. A part of it takes the color picked in a group of swatches, blending smoothly to the new one.

Module `model`, category 3D and product. Import `@dioramajs/model/model`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/model/src/model.ts)

A low lounge chair in moss velvet in 3D, resting in a three-quarter view, turned by a drag or the arrow keys and recolored by the color param.

```html title="index.html"
<div class="chair" data-dio="model" data-dio-src="/media/product/lounge.glb" data-dio-part="Fabric" data-dio-color="#434c27" data-dio-yaw="32" data-dio-pitch="10" data-dio-scale="0.8" role="img" aria-label="Lounge chair in moss velvet, in 3D. Drag or use the arrow keys to turn it." tabindex="0" style="touch-action: pan-y">
  <img src="/media/product/lounge-moss.webp" alt="" width="960" height="800" />
</div>
```

## Usage

Put it on: any element; put a poster <img> inside, it shows without WebGL and until the model is ready.

`start()` registers `model`, so the attribute alone puts it on an element.

```html
<div class="viewer" data-dio="model" data-dio-src="/models/chair.glb"><img src="/models/chair.avif" alt="Lounge chair" /></div>
```

In page transitions: Leaves by dithering its pixels away while shrinking to 96%, so its opaque materials never switch to transparent mid-transition.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `src` (rebuilds) | `data-dio-src` | `string` | `''` | URL of the GLB or glTF file. |
| `part` (rebuilds) | `data-dio-part` | `string` | `''` | Material or mesh names to recolor, comma separated; empty keeps the file's colors. |
| `color` | `data-dio-color` | `color` | `'#ffffff'` | Color of the part, any CSS color. |
| `swatches` (rebuilds) | `data-dio-swatches` | `string` | `''` | Selector of a radio group or <select> whose pick recolors the part, as `color` does. |
| `duration` | `data-dio-duration` | `number` 0 to 2 s | `0.4` | Time a color or rest pose change takes. |
| `scale` | `data-dio-scale` | `number` 0.1 to 1.5 | `0.9` | Size in the box: 1 fits the model as it is seen in its rest pose, its near side larger in perspective, centered on what is drawn. |
| `yaw` | `data-dio-yaw` | `number` -180 to 180° | `0` | Rest pose turn around the vertical axis; positive turns it as a drag to the right does. |
| `pitch` | `data-dio-pitch` | `number` -20 to 20° | `0` | Rest pose tilt toward the viewer; positive shows it from above, -20 to 20. |
| `spin` | `data-dio-spin` | `number` -360 to 360 deg/s | `0` | Turns on its own around the vertical; 0 stands still. |
| `turn` | `data-dio-turn` | `number` -720 to 720° | `0` | Turns as the element passes through the viewport. |
| `tilt` | `data-dio-tilt` | `number` 0 to 35° | `8.6` | How far it leans toward the pointer. |
| `drag` | `data-dio-drag` | `boolean` | `true` | Turns it under a drag, and with the arrow keys when it has a tabindex. |
| `clip` (rebuilds) | `data-dio-clip` | `string` | `''` | Clip to loop by name, `*` for the first; step keys alone draw only at each key. |
| `exposure` | `data-dio-exposure` | `number` 0.2 to 3 | `1` | Exposure of the tone mapping. |

Changing a param marked rebuilds re-runs the effect setup.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`height`](/docs/api/inputs#input-height), [`over`](/docs/api/inputs#input-over), [`pointer`](/docs/api/inputs#pointer), [`presence`](/docs/api/inputs#presence), [`progress`](/docs/api/inputs#progress), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`width`](/docs/api/inputs#input-width). Draws only while something on it moves, then stops. Only while something moves: spin, a clip, drag inertia, the tilt settling, or a color or rest pose blending. A clip whose every track steps from key to key, like the hands of a ticking watch, draws one frame per key on a timer while visible, so a watch at rest costs a frame a second. None at rest without spin and clip. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it shows a still frame: motion inputs are zero and time stands still. The rest pose shows; spin, clips, scroll turn and tilt stop, and a new rest pose is taken at once. Dragging, arrow keys and color changes still work: the visitor starts them.

Runs on every device. No tilt, nothing hovers. A finger drags the model only when the element has `touch-action: pan-y` or `none`, otherwise the gesture scrolls the page.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { model: { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 5.2 KB the first time a page uses it. GPU: The model as authored, lit by one prefiltered environment. One own material per material of the file. Memory: Geometry and textures of the file, shared with every instance of the same URL, and the environment map.

See [the quality plugin](/docs/effects/quality).

## Recipes

### A product configurator

`swatches` points at a radio group or a `<select>`, and the part named in `part` takes the color of the picked value, easing to it over `duration` seconds in a perceptual color space. The radios stay a form: they submit, work with the keyboard and read as a group to a screen reader.

The lounge chair over six velvet swatches; picking one repaints the seat and back.

```html title="index.html"
<div class="configurator">
  <div class="chair" data-dio="model" data-dio-src="/media/product/lounge.glb" data-dio-part="Fabric" data-dio-swatches="#velvet" data-dio-yaw="32" data-dio-pitch="10" data-dio-scale="0.8" role="img" aria-label="Lounge chair in 3D, in the velvet picked below. Drag or use the arrow keys to turn it." tabindex="0" style="touch-action: pan-y">
    <img src="/media/product/lounge-moss.webp" alt="" width="960" height="800" />
  </div>
  <fieldset id="velvet" class="velvets">
    <legend>Velvet</legend>
    <label><input type="radio" name="velvet" value="#434c27" checked /> Moss</label>
    <label><input type="radio" name="velvet" value="#a8700f" /> Saffron</label>
    <label><input type="radio" name="velvet" value="#8a3519" /> Rust</label>
    <label><input type="radio" name="velvet" value="#0d4a4f" /> Peacock</label>
    <label><input type="radio" name="velvet" value="#232b42" /> Ink</label>
    <label><input type="radio" name="velvet" value="#b9b09f" /> Oyster</label>
  </fieldset>
</div>
```

`part` takes material or mesh names from the file, separated by commas. A name that is not there logs the names it has in development, so open the console when the color does not change. Give each velvet its own poster, too: without WebGL the `<img>` is all the visitor sees.

### Turn it as the page scrolls

`turn` adds that many degrees of turn while the element crosses the viewport, on top of the rest pose and whatever the visitor dragged, and it faces the rest pose when the element is in the middle. Inside a `data-dio-section` it takes the progress of the whole section instead, so it keeps turning while a sticky stage holds still.

```html
<section
  class="materials"
  data-dio-section
  style="height: 300vh"
>
  <div
    class="stage"
    style="position: sticky; top: 0; height: 100vh"
  >
    <div
      data-dio="model"
      data-dio-src="/models/chair.glb"
      data-dio-turn="360"
      data-dio-drag="false"
    >
      <img src="/models/chair.avif" />
    </div>
  </div>
</section>
```

### A hero that turns on its own

`spin` turns it around the vertical axis in degrees per second. Keep it slow: 10 to 20 reads as a display stand, faster reads as a loading spinner. Under reduced motion it rests in its pose.

```html
<div
  class="hero"
  data-dio="model"
  data-dio-src="/models/headphones.glb"
  data-dio-spin="14.4"
  data-dio-pitch="12"
  data-dio-load="eager"
>
  <img src="/img/headphones.avif" />
</div>
```

### Play an animation clip

`clip` loops one of the file’s animations by name, or the first with `*`. From code, the same params go to `attach()`.

```ts
app.attach('.robot', 'model', {
  src: '/models/robot.glb',
  clip: 'Walk',
  drag: false,
  scale: 0.8,
})
```

## Notes

The model draws only while something moves: a drag, spin, a clip, a color or pose easing, or the tilt settling. A chair at rest costs no frames. The file is the cost that matters: this chair is 1.5 MB, a quarter of it textures. Compress geometry with Meshopt or Draco and textures with KTX2 before you ship; the [3D models guide](/docs/guides/models) shows the commands.

Put a poster `<img>` inside the element, rendered from the same view. It shows while the file loads, without WebGL and in search results, and the model takes its place only once it has drawn. Match `yaw`, `pitch` and `scale` to the poster so the swap does not jump.

A turning model needs a name for assistive technology: `role="img"` with an `aria-label` that says what it is and how to turn it, and `tabindex="0"` for the arrow keys. Without `tabindex` it never takes focus, and `drag` alone needs a mouse or a finger.

On touch screens nothing hovers, so `tilt` does nothing. A finger turns the model only with `touch-action: pan-y` or `none` on the element; otherwise the gesture scrolls the page, which is the right default for a model in the flow of a long page.

## Related

- [book](/docs/effects/book.md): A cover image made a 3D book that turns to show its spine and opens on hover.
- [box](/docs/effects/box.md): A product box built from pictures of its faces: turn it, tilt it, open its lid.
- [coin](/docs/effects/coin.md): A coin, medal or token struck from a logo or a photo: it turns, catches the light and flips over.
- [device](/docs/effects/device.md): A screenshot or a video shown in a 3D phone, tablet, laptop or browser window.
- [explode](/docs/effects/explode.md): An exploded view of a model: its parts move apart as the section scrolls.
- [hotspots](/docs/effects/hotspots.md): Feature callouts pinned to points of a model, real buttons that follow it as it turns.
- [label](/docs/effects/label.md): A flat label wrapped around a can, a bottle or a jar that turns to show all of it.
- [panorama](/docs/effects/panorama.md): A 360° photo in a box that turns by drag, keys and gyroscope, with hotspots.
- [3D models](/docs/guides/models.md)
- [Loading and preloading](/docs/guides/loading.md)
- [Performance and quality](/docs/guides/performance.md)
