How-to guide·3D viewerJavaScript

Set the camera from code

Choose the first view of a model, jump to the front, side and top views, zoom, switch to orthographic, save and restore a viewpoint, and fly to a viewpoint a BQL query computes.

The user moves the camera with the mouse; your page moves it with a few calls on the viewer and its camera.

Live demo#

The camera from codeLive 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.

Each button is one call. The panel shows getViewpoint() after every move. Save view, then move around and Restore view. From a door to the stairs flies to the viewpoint a BQL query computes and x-rays the stairs, which are behind walls from there.

Steps#

// The viewer of the embed example, with a model loaded
const camera = viewer.getCamera();

// The first view: from the south-west and a little above; viewDirection points from the
// model to the camera
await viewer.viewFitModels(["<version uuid of the first demo model>"], {viewDirection: [-0.5, -0.5, 0.3]});

// The standard views of everything loaded, animated
viewer.frontView();     // from -Y
viewer.sideView();      // from +X
viewer.topView();       // from above, +Y up

// Zoom as the mouse wheel does: a negative delta zooms in, a positive one out
viewer.zoom(-500);

// Orthographic and back
camera.fov = 0;
camera.fov = 45;

// Remember a view and fly back to it in 800 ms. Positions are in metres; the promise
// resolves when the animation starts
const saved = camera.getViewpoint();     // {position, direction, up, fov}, a copy
await camera.setViewpoint(saved, 800);

// A view a query computes: from the first door, looking at the stairs
const [row] = await client.bim.query(["<version uuid of the first demo model>"],
	"GET viewPoint(type IN (IFCSTAIR, IFCSTAIRFLIGHT)) WHERE type = IFCDOOR LIMIT 1");
const {position, direction, up} = row.viewPoint;      // {x, y, z} objects, in metres
await camera.setViewpoint({
	position: vec3.fromValues(position.x, position.y, position.z),
	direction: vec3.fromValues(direction.x, direction.y, direction.z),
	up: vec3.fromValues(up.x, up.y, up.z)
}, 1000);

Runs on a viewer with a model loaded, vec3 from @bimworks/viewercommon.

  1. The first view. After loading, the viewer fits the model from its default direction. viewer.viewFitModels(vids, {viewDirection}) fits from the direction you give: the vector points from the model to the camera, so [-0.5, -0.5, 0.3] looks from the south-west and a little above, [0, 0, 1] from straight above. objectStateManager.viewFit(qids, options) does the same for a set of objects, in every connected viewer.
  2. Standard views. frontView(), sideView() and topView() fit everything loaded from the front (−Y), the side (+X) and above (with +Y up), animated.
  3. Zoom. viewer.zoom(delta) zooms as the mouse wheel does: a negative delta zooms in, a positive one out.
  4. Orthographic. viewer.getCamera().fov = 0 switches to an orthographic camera, a positive field of view below 120 back to perspective (Show a floor plan uses it for plans).
  5. Save and restore. camera.getViewpoint() returns a copy of the current Viewpoint: position, direction (from the eye to the point it looks at), up and fov, in metres. camera.setViewpoint(viewpoint, ms) flies back in ms milliseconds, or jumps with 0; a viewpoint without fov keeps the current one. The promise resolves when the flight starts, not when it ends.
  6. From a query. The BQL feature viewPoint(<condition>) computes, per object, a viewpoint at the object that looks at the objects matching the condition: position, direction, up and target as {x, y, z} in metres (Spatial and advanced). Turn them into vectors for setViewpoint. Plain viewPoint, without a condition, answers only a position and an up vector.

Check#

Top shows the model from above with north up; a saved viewpoint brought back with setViewpoint shows the same picture as when it was saved.