Choose the 3D viewer
Run Viewer3D-Next (WebGPU) where the browser can and Viewer3D (WebGL) where it cannot, downloading only the one in use, or keep to one of the two; what decides, how to steer it with rules, and what happens when WebGPU fails.
There are two 3D viewers with the same API. Viewer3D (@bimworks/viewer3d) renders with WebGL2 and runs in every current browser. Viewer3D-Next (@bimworks/viewer3d-next) renders with WebGPU: it loads large models faster and draws them with less memory, but it needs a browser with WebGPU supported and enabled. Firefox on Linux and Android do not switch WebGPU on by default, Safari has it from version 26, and some phones offer only a reduced form of it. All of the common browsers on windows and MacOS desktop have WebGPU enabled by default.
@bimworks/viewer3d-auto decides per browser. It downloads only the viewer it picks, and puts a Viewer3D in its place when Viewer3D-Next does not start.
One difference besides speed: Viewer3D-Next shares the models between the viewers of one ModelManager, so a 3D view and a plan beside it load and store them once; Viewer3D loads a copy per viewer (Show models in several viewports).
Pick an option#
| You want | Install | What the page downloads |
|---|---|---|
| Viewer3D everywhere, as before | @bimworks/viewer3d |
Viewer3D |
| Viewer3D-Next where it runs, Viewer3D elsewhere | @bimworks/viewer3d-auto (it brings both) |
The one it picks |
| Viewer3D-Next only | @bimworks/viewer3d-next |
Viewer3D-Next; in a browser that cannot run it the viewer does not start |
The three packages are installed like the others (JavaScript and TypeScript) and are also part of the single-file builds (Downloads).
Let the browser decide#
createViewer3d takes the three arguments of the Viewer3d constructor and an options object. It resolves when the viewer has started. beforeInitialize runs after the viewer is made and before it starts, so the container can go into the page there:
import {BimWorksClientFactory} from "@bimworks/jssdk";
import {ModelManager, ObjectStateManager} from "@bimworks/viewercommon";
import {createViewer3d} from "@bimworks/viewer3d-auto";
const factory = new BimWorksClientFactory("https://bim.works");
const client = await factory.createClientWithApiToken("<your API token>");
const modelManager = new ModelManager(client);
const objectStateManager = new ObjectStateManager(modelManager);
const {viewer, engine, reason} = await createViewer3d(objectStateManager, undefined, {
technologyAllowed: {webGl2: true, webGpu: true, webWorker: true, sharedArrayBuffer: true, transferable: true}
}, {
beforeInitialize: (viewer) => document.getElementById("viewer").appendChild(viewer.container)
});
console.log(engine, reason); // "webgpu" "capable", or for instance "webgl" "no-webgpu-api"
await modelManager.addModels([await client.fs.getNodeVersion("<version uuid of the first demo model>")]);What it does:
- Probe. It asks the browser for a WebGPU adapter, once per page. Without the WebGPU API it answers at once.
- Decide. It picks Viewer3D-Next when the adapter is not a software fallback, has the limits Viewer3D-Next needs, and Viewer3D-Next has not failed on that adapter in the last week. Otherwise it picks Viewer3D.
- Download. It imports that viewer's package. With a bundler, each package is a chunk of its own.
- Fall back. When Viewer3D-Next does not start (
initializeViewerrejects withViewer3dEngineUnavailableError), it destroys it, remembers the failure for this browser's adapter, and makes a Viewer3D on the sameObjectStateManager. That Viewer3D loads the models theModelManageralready holds.beforeInitializeruns again for it, and the result saysreason: "start-failed"with the rejection inerror.
The answer is also kept in the browser as a hint, so on the next page load the chosen viewer starts downloading while the probe still runs. The hint and the failure memory hold for one version of your application: pass version to tie them to yours.
To decide before you build anything, use loadViewer3d: it returns the chosen package as module, and you construct module.Viewer3d yourself. prefetchViewer3d starts the probe and the download without waiting, for instance while the page is still building its layout.
Force one viewer, or keep the choice to the user#
The engine option is "auto" by default:
"webgl"always uses Viewer3D."webgpu"uses Viewer3D-Next and falls back to Viewer3D where it cannot run (the result then saysfallback: true). WithallowFallback: falseit rejects withViewer3dEngineUnavailableErrorinstead.
A preference your users set, as in the BIM.works viewer settings, maps onto these three values.
Steer the automatic choice with rules#
A policy lists rules, and the first one that fits decides. A rule matches on the WebGPU adapter (vendor, architecture, device, description), on userAgent, and on mobile. Text is found inside the value, ignoring case, and a field you leave out matches anything:
const {viewer} = await createViewer3d(objectStateManager, undefined, settings, {
policy: {rules: [
// a driver that misbehaves on WebGPU stays on WebGL
{match: {vendor: "qualcomm", description: "adreno (tm) 6"}, engine: "webgl"}
]}
});probeViewer3dEngine (from @bimworks/viewercommon or @bimworks/viewer3d-auto) gives the decision with its reason and the adapter it saw (info) without loading a viewer. That is useful for reporting which adapters your users have.
Use Viewer3D-Next on its own#
Without the automatic package, construct Viewer3d from @bimworks/viewer3d-next as in Embed the viewer, and check first with its isSupported(). Where WebGPU cannot run it, initializeViewer() rejects with Viewer3dEngineUnavailableError, whose reason is one of these:
"no-webgpu-api";"no-adapter";"no-device";"start-failed".
The container then says that WebGPU is required.
When WebGPU stops later#
Viewer3D-Next recovers from a lost GPU device by itself: it makes a new device and reloads the models. When the device keeps getting lost (three times in ten minutes), or no new one can be made, it gives up and tells you through the engine failure listener.
Viewer3D calls the same listener when its WebGL context is lost, for instance when the browser's GPU process is killed. It does not restore the context, so you build a new viewer. You register the same listener on either viewer:
import {createViewer3d, recordViewer3dEngineFailure} from "@bimworks/viewer3d-auto";
const show = (viewer) => document.getElementById("viewer").appendChild(viewer.container);
let {viewer} = await createViewer3d(objectStateManager, undefined, settings, {beforeInitialize: show});
const rebuild = async (failure) => {
// failure.engine "webgpu": failure.reason "repeated-device-loss" or "recovery-failed"
// failure.engine "webgl": failure.reason "context-lost"
if (failure.engine === "webgpu") {
recordViewer3dEngineFailure(failure.reason);
}
viewer.destroy();
({viewer} = await createViewer3d(objectStateManager, undefined, settings, {engine: "webgl", beforeInitialize: show}));
viewer.addEngineFailureListener(rebuild);
};
viewer.addEngineFailureListener(rebuild);The BIM.works UI reloads the page instead. After Viewer3D-Next gives up, the rest of the session runs on the WebGL viewer. After a lost WebGL context, the page reloads with the same viewer. A third loss within ten minutes only shows a warning.
Check#
engine in the result of createViewer3d is "webgpu" in a current Chrome or Edge, and "webgl" with reason: "no-webgpu-api" in Firefox on Linux. The browser's network panel lists one viewer package, not both.