How-to guide·3D viewerJavaScript

Show models in several viewports

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.

Several viewports of the same models — a 3D view beside a floor plan, an elevation, a second 3D view — are several Viewer3ds on one ModelManager. With the WebGPU viewer (Viewer3D-Next, Choose the 3D 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 ObjectStateManager of its own, its own visibility, selection and colours.

Live demo#

One model, several viewportsLive demo
Loads the demo model in this page with the demo API token of your account: open BIM.works and sign in once in this browser.

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 (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#

const factory = new BimWorksClientFactory("https://bim.works");
const client = await factory.createClientWithApiToken("<your API token>");

// One ModelManager: the models are loaded once, whatever number of viewers shows them
const modelManager = new ModelManager(client);

// A viewer per viewport, each on an ObjectStateManager of its own: its own visibility, selection and colours
const perspective = new Viewer3d(new ObjectStateManager(modelManager));
const planState = new ObjectStateManager(modelManager);
const plan = new Viewer3d(planState);
await Promise.all([perspective.initializeViewer(), plan.initializeViewer()]);
document.getElementById("perspective").appendChild(perspective.container);
document.getElementById("plan").appendChild(plan.container);

// The plan: an orthographic camera looking down, in 2D mode
plan.getCamera().fov = 0;
plan.set2dMode(true);

// One download for both viewports
const version = await client.fs.getNodeVersion("<version uuid of the first demo model>");
await modelManager.addModels([version]);

// The plan shows the ground floor only; the 3D view keeps the whole building
const listed = await client.bim.listBuildingStoreys([version.uuid]);
const ground = listed.buildingStoreys.find((storey) => storey.floor == 0) || listed.buildingStoreys[0];
const storeyQids = ground.buildingStoreys.map((buildingStorey) => buildingStorey.qid);
await planState.setBuildingStoreysVisibleExclusive(storeyQids);
await planState.removeFromVisibleSet(modelManager.getTypeQids("IFCCOVERING"));
await plan.viewFit(modelManager.getObjectQidsForBuildingStoreyQids(storeyQids, false), {viewDirection: [0, 0, 1], margin: 3});

// Closing a viewport, in any order: the other keeps the models; they go with the last viewer
document.getElementById("close-perspective").addEventListener("click", () => {
	perspective.destroy();
});
html, body {
	height: 100%;
	margin: 0;
}

body {
	display: grid;
	grid-template-columns: 1fr 1fr;
	grid-template-rows: auto 1fr;
}

#close-perspective {
	grid-column: 1 / -1;
	justify-self: start;
	margin: 8px;
}

#perspective, #plan {
	position: relative;
	min-height: 0;
	display: flex;
}

#perspective > *, #plan > * {
	flex: 1;
}

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.

  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 (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 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#

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.

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: 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.