Cognite 3D Web Viewer
Visualize Cognite's 3D models in a web browser with WebGL.
Table of Contents
Install
$ npm install @cognite/3d-viewer
Usage
With import
import { Cognite3DViewer } from '@cognite/3d-viewer';
const viewer = new Cognite3DViewer();
With script tag
<script src="{path to this folder}/lib/cognite.bundle.js"></script> // Now you have 'cognite' as a global object
<script>
var viewer = new cognite.Cognite3DViewer();
</script>
Sample code
const viewer = new Cognite3DViewer();
document.body.appendChild(viewer.canvas);
viewer.start();
const options = {
projectName,
modelId,
revisionId,
};
viewer.addModel(options).then(function(model) {
viewer.fitCameraToModel(model, 0);
});
Note: you may need additional styling to the canvas to make it fit your use-case
canvas {
width: 100%;
height: 100%;
}
Add progress listeners
function onProgress(progress) {
console.log(progress);
}
function onComplete() {
console.log('Model loaded');
}
const options = {
projectName,
modelId,
revisionId,
onProgress,
onComplete,
};
viewer.addModel(options)...
Make the model clickable
viewer.on('click', function(event) {
const { offsetX, offsetY } = event;
const nodeId = model.getNodeIdFromPixel(viewer.camera, viewer.renderer, offsetX, offsetY);
if (nodeId != null) {
model.selectNode(nodeId);
const boundingBox = model.getBoundingBox(nodeId);
viewer.fitCameraToBoundingBox(boundingBox, 2000);
} else {
}
});
API Reference
Table of Contents
Cognite3DViewer
Cognite3DViewer
is the root class of a Cognite 3D viewer. It controls all aspects of the 3D viewer.
Parameters
options
Object (optional, default {}
)
options.noBackground
boolean -- Transparent background or not (optional, default false
)options.viewCube
string? -- If defined then show a view cube and snap location of the view cube to this value. One of: 'topleft', 'topright', 'bottomleft', 'bottomright'.
Examples
const viewer = new Cognite3DViewer({
noBackground: true,
});
isSupported
Check if the viewer supports the current browser
Examples
if (!viewer.isSupported()) {
}
start
Start viewer, or resume after calling stop().
Examples
viewer.start();
stop
Stop the viewer. When stopped, the viewer will not do work, but it's state will be kept so it can
restart immediately after start() is called.
Examples
viewer.stop();
on
Add event listener to the viewer
call off to remove an event listener
Parameters
type
"click"
func
function (event:MouseEvent) -- The callback function
Examples
const onClick = event => { ... };
viewer.on('click', onClick);
off
Remove event listener from the viewer
call on to add an event listener
Parameters
type
("click"
)func
function -- The callback function used in on
Examples
viewer.off('click', onClick);
addModel
Add a new 3D model to the viewer.
call fitCameraToModel to see the model after the model has loaded
Parameters
options
Object
options.projectName
string -- The Cognite project the 3D model belongs tooptions.modelId
number -- The model's idoptions.revisionId
number -- The model's revision idoptions.sectors
[number]? -- List of sector ID's to loadoptions.onProgress
function (progress: Object)? -- Callback for progress eventsoptions.onComplete
function? -- Callback when the model is fully loaded
Examples
const options = {
projectName: 'COGNITE_TENANT_NAME',
modelId: 'COGNITE_3D_MODEL_ID',
revisionId: 'COGNITE_3D_REVISION_ID',
};
viewer.addModel(options).then(model => {
viewer.fitCameraToModel(model, 0);
});
Returns Promise<Cognite3DModel>
getCameraPosition
Returns camera's position
Examples
const position = viewer.getCameraPosition();
Returns THREE.Vector3 Position in global space
getCameraTarget
Returns camera's target
Examples
const target = viewer.getCameraTarget();
Returns THREE.Vector3 Target in global space
setCameraPosition
Set camera's position
Parameters
position
THREE.Vector3 -- Position in global space
Examples
const position = viewer.getCameraPosition();
const target = viewer.getCameraTarget();
viewer.setCameraPosition(position);
viewer.setCameraTarget(target);
setCameraTarget
Set camera's target
Parameters
target
THREE.Vector3 -- Target in global space
Examples
const position = viewer.getCameraPosition();
const target = viewer.getCameraTarget();
viewer.setCameraPosition(position);
viewer.setCameraTarget(target);
fitCameraToBoundingBox
Move camera to a place where the content of a bounding box is visible to the camera
Parameters
box
THREE.Box3 -- The bounding boxduration
number? -- The duration of the animation moving the camera. Set this to 0 (zero) to disable animation.radiusFactor
number? -- The the ratio of the distance from camera to center of box and radius of the box (optional, default 4
)
enableKeyboardNavigation
Enables camera navigation with the keyboard.
disableKeyboardNavigation
Disables camera navigation with the keyboard.
fitCameraToModel
Move camera to a place where the 3D model is visible. It uses the bounding box of the 3D model and calls fitCameraToBoundingBox
Parameters
getScreenshot
Take screenshot from the current camera position.
Parameters
width
number? -- Width of the final image. Default is current canvas size.height
number? -- Height of the final image. Default is current canvas size.
Returns Promise<string> A Blob URL to the image ('image/png')
Cognite3DModel
Extends THREE.Object3D
Cognite3DModel
is the class represeting a Cognite 3D model and it's state
getSubtreeNodeIds
Get all node ids in a subtree where the root node has id nodeId
Parameters
nodeId
number -- The ID of the root nodesubtreeSize
number? -- If you already know the subtreeSize then this can help avoid a API call
getBoundingBox
Get bounding box of a node
Parameters
nodeId
number -- The node's idbox
THREE.Box3? -- Optional target. Specify this to increase performance if the box can be reused.
Examples
const box = model.getBoundingBox(nodeId);
const reusableBox = new THREE.Box3();
const box = model.getBoundingBox(nodeId, reusableBox);
Returns THREE.Box3
getNodeIdFromPixel
Raycasting for nodes
Parameters
camera
THREE.Camera -- The camera to project fromrenderer
THREE.WebGLRenderer -- A renderer. The raycaster will render an image to memory for efficiency.x
number -- X coordinate in pixelsy
number -- Y coordinate in pixels
Examples
const box = model.getBoundingBox(nodeId);
const reusableBox = new THREE.Box3();
const box = model.getBoundingBox(nodeId, reusableBox);
Returns (number | null) -- If there was a hit then returns the node id
getNodeColor
Return the color on a node
Parameters
nodeId
number -- The node's id
Returns {r: number, g: number, b: number, a: number}
setNodeColor
Set the color on a node
Parameters
nodeId
number -- The node's idr
number -- The red color value (0-255)g
number -- The green color value (0-255)b
number -- The blue color value (0-255)a
number -- The alpha value (0-255) (optional, default 255
)
selectNode
Mark a node as selected
Parameters
nodeId
number -- The node's id
deselectNode
Mark a node as deselected
Parameters
nodeId
number -- The node's id
deselectAllNodes
Mark all selected nodes as deselected
showNode
Show a node
Parameters
nodeId
number -- The node's id
showAllNodes
Show all nodes
hideAllNodes
Hide all nodes
hideNode
Hide a node
Parameters
nodeId
number -- The node's id
Node
Type: object
Properties
id
number -- The node idtreeIndex
number -- The tree indexparentId
number -- The node id of the parentname
number -- The name of the nodesubtreeSize
number -- Size of the subtree where this node is the root. This number included the node it self.depth
number -- The depth level from the overall root node (0-indexed).boundingBox
THREE.Box3 -- The bounding box for the subtree where this node is the root.
getNode
Get a single node.
Parameters
projectName
string -- The Cognite project that the 3D model belongs tomodelId
number -- The ID of the 3D modelrevisionId
number -- The ID of the 3D model revisionnodeId
number -- The ID of the node to retrieve.
Returns Promise<Node>
getAssetMappingsFromNodeId
Get the assets associated with a given node.
Parameters
projectName
string -- The Cognite project that the 3D model belongs tomodelId
number -- The ID of the 3D modelrevisionId
number -- The ID of the 3D model revisionnodeId
number -- The ID of the node to get the associated assets of
Returns Promise<[AssetMapping]>
AssetMapping
Type: object
Properties
nodeId
number -- The node id for the mappingassetId
number -- The mapped asset idsubtreeSize
number -- Size of the subtree with the node with id = nodeId as root
getAssetMappingsFromAssetId
Get the node ids associated with a given asset.
Parameters
projectName
string -- The Cognite project that the 3D model belongs tomodelId
number -- The ID of the 3D modelrevisionId
number -- The ID of the 3D model revisionassetId
number -- The ID of the asset to get the associated nodes of
Returns Promise<[AssetMapping]>
Sector
Type: object
Properties
id
number -- The sector idparentId
number? -- The id of the parent sector. Undefined if it is the root sector.path
number -- A string representing the path from the root sector to this sector. Describes how to traverse a octree to get to this sector.depth
number -- The depth level from the root sector (0-indexed).threedFileId
number -- File id for the file describing geometries attached to the sector. Use /3d/files to retrieve it.boundingBox
THREE.Box3 -- The bounding box of which the sector covers.
getSectorsFromBoundingBox
Get sectors intesecting a bounding box.
Parameters
projectName
string -- The Cognite project that the 3D model belongs tomodelId
number -- The ID of the 3D modelrevisionId
number -- The ID of the 3D model revisionboundingBox
THREE.Box3 -- The bounding box
Returns Promise<[Sector]> A list of sectors intersecting the bounding box
Cognite3DModelLoader
Cognite3DModelLoader
is the class used to load a Cognite 3D model to Three.js.
load
Parameters
options
Object? (optional, default {}
)
options.projectName
string -- The Cognite project the 3D model belongs tooptions.modelId
number -- The model's idoptions.revisionId
number -- The model's revision idoptions.sectors
[number]? -- List of sector ID's to loadoptions.onProgress
function (progress: Object)? -- Callback for progress eventsoptions.onComplete
function? -- Callback when the model is fully loaded
Examples
const options = {
projectName: 'COGNITE_TENANT_NAME',
modelId: 'COGNITE_3D_MODEL_ID',
revisionId: 'COGNITE_3D_REVISION_ID',
};
Cognite3DModelLoader.load(options).then(model => {
scene.add(model);
});
Returns Promise<Cognite3DModel>