---
title: Show models in several viewports
summary: A 3D view and a floor plan of the same models side by side, or more viewports, loaded once: one ModelManager, a Viewer3d per viewport, each with its own state and camera, closable in any order.
order: 39
languages: [javascript]
terms: [viewer, model-manager, object-state-manager, building-storey]
see-also: [how-to/viewer3d/show-a-floor-plan, how-to/viewer3d/embed-the-viewer, how-to/viewer3d/choose-the-viewer, demos/viewports, demos/repair-annotations]
---

# Show models in several viewports

Several viewports of the same models — a 3D view beside a floor plan, an elevation, a second 3D view — are several `Viewer3d`s on one [[term:model-manager|ModelManager]]. With the WebGPU viewer (Viewer3D-Next, [[page:how-to/viewer3d/choose-the-viewer]]) they share the models: each version is downloaded, decoded and stored on the graphics card once, however many viewports show it, so a viewport you add later is ready at once and costs little memory. Each viewport keeps its own camera, section planes and measurements, and, through an [[term:object-state-manager|ObjectStateManager]] of its own, its own visibility, selection and colours.

## Live demo

```demo
name: viewports
title: One model, several viewports
models: s1, s1-electrical
height: 600
notes: |
  The 3D view and a plan of the ground floor. **Add** opens another 3D view, plan or elevation (four at most); **×** closes one, in any order: the others keep working. A viewport added later says how long it took: at once, the models were on the graphics card. **Storey** picks the floor of the plans; a double click in a plan turns the 3D views to that spot. A selection in one viewport shows in all of them. **Same state in every viewport** puts all of them on one ObjectStateManager: a storey picked then hides the rest in every viewport.
```

## Before you start

You have a client and know how to embed one viewer ([[page:how-to/viewer3d/embed-the-viewer]]). The sharing needs Viewer3D-Next; with Viewer3D (WebGL) the same code works, but every viewer loads its own copy of the models.

## Steps

```example
name: viewer-several-viewports
code: javascript/viewer-several-viewports.js
css: javascript/viewer-several-viewports.css
notes: A page with a `<div id="perspective">`, a `<div id="plan">` and a `<button id="close-perspective">`. The plan shows the ground floor, the 3D view the whole building.
highlight: 4-5,8-11,15-17,20-21
```

1. **One ModelManager for the models.** Every viewport shows what it holds; `addModels`, `removeModels` and `setModels` reach all of them. The models are loaded once, whichever viewer is there first, and a viewer you create later shows them without a new load.
2. **A Viewer3d per viewport.** Give each one an ObjectStateManager of its own on that ModelManager: hiding the storeys above the ground floor in the plan then leaves the 3D view as it is. Put two viewports on *one* ObjectStateManager instead when they should always show the same state: a selection, a colour or a hidden object is then everyone's. A viewport added later shows that state from the start, with one exception: a storey shown alone (`setBuildingStoreysVisibleExclusive`) reaches only the viewers on the ObjectStateManager at that moment. Call it again once the new viewport is there, as the demo does.
3. **Shape each viewport.** A plan is the viewer with an orthographic camera (`getCamera().fov = 0`), in 2D mode and fitted from above ([[page:how-to/viewer3d/show-a-floor-plan]]); an elevation is an orthographic camera fitted with `viewDirection: [0, -1, 0]`. Each viewer has its own camera, navigation and tools.
4. **Close a viewport with `destroy()`.** Any one, in any order: the others keep the models. They are released when the last viewer showing them goes, or when the ModelManager unloads them. A viewport whose ObjectStateManager no other viewer uses also lets go of it with `modelManager.removeObjectStateManager(objectStateManager)`, so later loads do not fill it.

## What the viewports share {#shared}

| | Shared by the viewers of one ModelManager | Per viewport |
|---|---|---|
| The models' geometry, the download, the PDF drawings' tiles | yes | |
| Where the models are placed: LOCAL or GEO mode, alignments | yes | |
| Visibility, selection, colours, x-ray | | yes, through its ObjectStateManager |
| Showing or hiding a whole model (`setModelVisible`) | | yes |
| Camera, navigation, 2D mode, section planes, measurements, annotations | | yes |

Placement is the ModelManager's, because the viewports draw the same stored models: `setMode`, `setModelAlignment`, `updateModel` and `setOfflineMode` on any of the viewers apply to all of them, and their cameras follow. A viewport in GEO mode beside one in LOCAL mode, or an alignment preview in one viewport only, needs two ModelManagers.

## Separate data {#separate}

Viewers on *different* ModelManagers share nothing but the graphics card: each loads its own models. Use that when the viewports show different model sets that should not affect each other, for instance two versions side by side; showing different models of one set is also possible on one ModelManager, with `setModelVisible` per viewer.

Point clouds and map imagery are still loaded per viewport.

## Link the cameras {#link}

`viewer.linkCamera(other)` keeps two viewports on one viewpoint: whichever camera the user moves leads, and `other` takes this viewer's camera first. Two orthographic cameras, for instance two plans of different storeys, share their zoom too. `dispose()` on what it returns ends the link and leaves both cameras where they are.

```javascript
const link = viewer.linkCamera(other);
// ...
link.dispose();
```

The link knows nothing of 2D mode: a 3D camera linked to a plan would tilt the plan. Link viewports that look the same way (`get2dSide()` the same), and drop the link while they do not. Viewers on different ModelManagers can have different measurement frames, for example when their first models (the reference in GEO mode) differ; pass a bridge that converts between them, through a model loaded in both:

```javascript
const bridge = {
	toOther: (p) => other.modelToMeasurementCoordinates(vid, viewer.measurementToModelCoordinates(vid, p)),
	fromOther: (p) => viewer.modelToMeasurementCoordinates(vid, other.measurementToModelCoordinates(vid, p))
};
const link = viewer.linkCamera(other, {bridge});
```

The BIM.works app builds on this for its two viewers, and adds the 3D camera drawn on a plan, which drags to move it: [[page:how-to/using-bimworks/connect-two-viewers]].

## Check

- Open a second viewport after the models have loaded: it shows them at once, without a progress bar of its own.
- Close the first viewport: the second one still selects, measures and cuts as before.
- Hide a storey in the plan: the 3D view still shows it, unless both share one ObjectStateManager.
