Custom Operator Basics
Summary
In this example, we will be introducing you to the concept of custom operators and how to respond to events by creating a simple operator that highlights entities under the mouse cursor. Operators allow you to hook into various events provided by the web browser and perform well-defined actions based on user input.
Concepts
- Create an operator framework
- Respond to events
- Use picking to find entity under cursor
- Select entities via the SelectionManager
Operator class skeleton
We start by creating a new TypeScript module called src/SelectOperator.ts which will contain the code for the operator we are about to create. We will only be handling onMouseDown events, so we have created only a single event handler for now.
// src/SelectOperator.ts
import { Event, Operators, type WebViewer } from '@ts3d-hoops/web-viewer';
export class SelectOperator implements Operators.Operator {
private readonly _hwv: WebViewer;
constructor(hwv: WebViewer) {
this._hwv = hwv;
}
onMouseDown(event: Event.MouseInputEvent) {
// mouseDown logic goes here
}
}
Unlike the previous version of this tutorial, there’s no separate <script> include to add - SelectOperator.ts is an ordinary module, so we make it available to the rest of the application with a normal import. Add this line near the top of src/main.ts:
import { SelectOperator } from './SelectOperator';
onMouseDown Action
The next step is to perform some action on onMouseDown. In this example, we want to highlight the geometry under the mouse cursor. To accomplish this we are using the pickFromPoint function which is part of the View object.
Note
The pickFromPoint function performs a ray-based collision with the geometry under the pick location within a given radius and returns the nodeid of the “best candidate” which is usually the entity closest to the camera on the pick location. This function requires a configuration object which determines which geometry types it should consider for performing the pick operation (faces, edges, points).
We also want to make sure that any existing selection is cleared out.
Note
The pickFromPoint function returns a promise. More information on promises and their usage in conjunction with the HOOPS Web Viewer API can be found here.
In this case, we are checking if the selection object has a valid NodeId, and if so, we highlight the element via the SelectionManager.
// src/SelectOperator.ts
import { Event, Operators, PickConfig, SelectionMask, type WebViewer } from '@ts3d-hoops/web-viewer';
export class SelectOperator implements Operators.Operator {
private readonly _hwv: WebViewer;
constructor(hwv: WebViewer) {
this._hwv = hwv;
}
onMouseDown(event: Event.MouseInputEvent) {
const config = new PickConfig(SelectionMask.Face | SelectionMask.Line);
this._hwv.selectionManager.clear();
this._hwv.view.pickFromPoint(event.getPosition(), config).then((selection) => {
if (selection.getNodeId() != null) {
this._hwv.selectionManager.set(selection);
}
});
}
}
Aside from the import path and the added type annotations, this is the same PickConfig/SelectionMask/SelectionManager logic as before - none of that API changed as part of the Web Components migration, only how you obtain the WebViewer instance to call it on, which we’ll cover next.
DOM Element for Custom Operator Switching
Hello Web Viewer already covered switching between the built-in camera operators (Navigate, Turntable, WalkMode) via <hoops-toolbar-camera-operator>. That component only knows about camera navigation, so for an application-specific operator like SelectOperator we still need a small piece of custom UI. Add a plain <select> next to the toolbar buttons in index.html, and give the context manager an id so we can reach it from main.ts:
<hoops-web-viewer-context-manager id="context-manager">
<hoops-web-viewer id="viewer" endpointUri="data/microengine.scs"></hoops-web-viewer>
<hoops-toolbar-camera-operator></hoops-toolbar-camera-operator>
<hoops-toolbar-drawmode></hoops-toolbar-drawmode>
</hoops-web-viewer-context-manager>
<select id="operatorType">
<option value="None">None</option>
<option value="AreaSelect">Area Select</option>
<option value="Select">Select</option>
</select>
Note
The camera operator and a custom “tool” operator like this one are two independent slots on the same operator stack - switching one doesn’t affect the other. <hoops-toolbar-camera-operator> manages the camera slot; the <select> above will manage the tool slot, the same way <hoops-toolbar-camera-operator> does internally.
Instantiating an Operator
A new operator must be first instantiated and then registered with the HOOPS Web Viewer before it can be used. Registering only makes the operator available to be used but does not activate it yet. It also returns a unique ID that we will use to activate the operator when the user switches to it. Add this to src/main.ts, alongside the hwvSceneReady listener introduced in the previous section:
import { OperatorId } from '@ts3d-hoops/web-viewer';
import { SelectOperator } from './SelectOperator';
let selectOperatorId: OperatorId;
viewerEl.addEventListener('hwvSceneReady', ({ detail: { hwv } }) => {
const selectOperator = new SelectOperator(hwv);
selectOperatorId = hwv.registerCustomOperator(selectOperator);
});
When an operator is registered, user interaction with the WebViewer will prompt the event to evaluate the operator stack. If the user action matches an operator on the stack, the corresponding event handler will be invoked. The traversal of the stack will stop once an action has been matched to an operator. By default, HOOPS Visualize Web enables the SelectionOperator and CameraNavigationOperator operators.
Switching to a New Operator
To switch to the new operator, we activate it through the context manager’s activeToolOperator property - the same mechanism <hoops-toolbar-camera-operator> uses internally, just for the tool slot instead of the camera slot. Add the following to src/main.ts:
import type { WebViewerContextManager } from '@ts3d-hoops/web-viewer-components';
const contextManagerEl = document.getElementById('context-manager') as HTMLElement & WebViewerContextManager;
const operatorSelect = document.getElementById('operatorType') as HTMLSelectElement;
operatorSelect.addEventListener('change', () => {
switch (operatorSelect.value) {
case 'AreaSelect':
contextManagerEl.activeToolOperator = OperatorId.AreaSelect;
break;
case 'Select':
contextManagerEl.activeToolOperator = selectOperatorId;
break;
default:
contextManagerEl.activeToolOperator = undefined;
}
});
Setting activeToolOperator to undefined deactivates whatever tool operator is currently active and returns interaction to plain camera navigation and selection - this is what the old code achieved by calling operatorManager.clear()/popping the stack. Note that AreaSelect is one of the built-in operators (see OperatorId), so - just like the custom selectOperatorId - it’s simply a value we hand to activeToolOperator; no registration step is needed for it.
Now you should be able to switch to your new operator via the dropdown and see the default highlighting in action.
Operators are a convenient programming paradigm for code in your application related to user interactions with the 3D window, but keep in mind that you can make calls to the HOOPS Web Viewer API from anywhere in your application and write your own operator-like functionality outside of the operator framework discussed here. The OperatorManager just exists to make your life easier - it is not required.