Hello Web Viewer

Summary

In this section, we will create the code skeleton for a web application based on HOOPS Visualize Web and introduce you to some of the fundamental development concepts of the HOOPS Visualize Web Web Components UI kit.

Concepts

  • Creation of a Basic Application Skeleton
  • Starting the HOOPS Web Viewer Component
  • Introduction to Events
  • How to Switch Camera Operators
  • Changing Draw Modes

Creating a Minimal HTML Header

We need to create the initial framework required to get the HOOPS Web Viewer component running on your webpage. Open the file called index.html (or create it if not present) and copy the following into it:

<!doctype html>
<html lang="en">
<head>
        <title>My Viewer</title>

        <style type="text/css">
                hoops-web-viewer {
                        display: block;
                        width: 640px;
                        height: 480px;
                        outline: 1px solid black;
                }
        </style>
</head>

This adds an HTML header containing the style for the <hoops-web-viewer> element, which will be added next. Unlike the previous version of this tutorial, we don’t need a separate <div> to act as a canvas container - the <hoops-web-viewer> custom element is the rendering surface, and is sized with ordinary CSS.

Creating a Minimal HTML Body

Next, we will define the minimal HTML body. Because the packaged toolbar and tree components communicate with the viewer through shared context rather than through parameters you pass around by hand, we wrap everything in two small “plumbing” elements: <hoops-service-registry>, which registers the shared services those components use, and <hoops-web-viewer-context-manager>, which holds a reference to the active WebViewer and hands it to any component nested inside it. You’ll reuse this same three-level nesting for the rest of this tutorial series.

<body>
        <h3>Web Viewer Minimal Example</h3>
        <hoops-service-registry>
                <hoops-web-viewer-context-manager>
                        <hoops-web-viewer id="viewer"></hoops-web-viewer>
                </hoops-web-viewer-context-manager>
        </hoops-service-registry>

        <script type="module" src="./src/main.ts"></script>
</body>
</html>

Start the application by running your dev server (npx vite from the project root - see Project Layout if you haven’t set that up yet) and navigate to the printed local URL in your browser. For now, you should just see an empty page with the title:

../../_images/empty_page.png

Connecting to the HOOPS Web Viewer Component

Now let’s write src/main.ts. Registering the components is a matter of importing them for their side effect (each import call registers one or more custom elements):

// src/main.ts
import '@ts3d-hoops/web-viewer-components';

This registers every packaged Web Component this tutorial series uses as a side effect of importing it - including the three elements we just placed in index.html, and the toolbar buttons and model tree component we’ll add in later sections. It doesn’t need any help loading the WebAssembly rendering engine: that binary is a plain static file at this point (see Copying Files), and @ts3d-hoops/web-viewer fetches it from the site root automatically the first time <hoops-web-viewer> starts up.

Note

@ts3d-hoops/web-viewer-components doesn’t currently expose a separate entry point per component (a handful of exceptions exist, such as @ts3d-hoops/web-viewer-components/hoops-model-tree/hoops-model-tree, used for typing purposes in Working with the Model Tree) - importing the package registers its whole set of custom elements at once. That keeps this tutorial simple, but if bundle size matters for your application, check the exports map in the package’s package.json for the granular entry points available in your installed version.

That’s the entire file for now - there’s no new WebViewer(...) call to write. The <hoops-web-viewer> element constructs and starts the underlying viewer itself as soon as it’s attached to the page, sized to whatever CSS you gave it.

As you can see, it is not required to specify an initial model on startup - that can be done later via the HOOPS Web Viewer API, or, as we’ll do next, by setting an attribute.

Loading a Model

Let’s add a model to the scene. For this tutorial, we will use one of the sample models from the HOOPS Visualize Web installation. You can find it in the following location:

/quick_start/converted_models/standard/scs_models/microengine.scs

Create a new folder under your project root directory called data and copy this model into it. Add the endpointUri attribute to the <hoops-web-viewer> tag in index.html:

<hoops-web-viewer id="viewer" endpointUri="data/microengine.scs"></hoops-web-viewer>

The HOOPS Web Viewer component can load models either via its proprietary server through a websocket connection or directly via HTTP requests to a standard web server. Both loading variants use different filetypes. As mentioned before, we will not be using the streaming functionality of the Web Viewer in this tutorial, which means we will use files with the .scs extension. For more details on the various loading modes of the Web Viewer, please see the Programming Guide.

After running the application again, you should see the following on the screen:

../../_images/minimal_example.png

That is all that is required to have a fully functioning application that utilizes the HOOPS Web Viewer component of HOOPS Visualize Web. Go ahead and interact with the model. You will notice that you can fully manipulate the camera and select entities within the model.

Registering Callbacks

Before, we used the setCallbacks function to be notified about the Web Viewer’s state. That function is still there and still works exactly as before - see the Programming Guide - but <hoops-web-viewer> also re-dispatches every one of those callbacks as an ordinary DOM CustomEvent on the element itself, so you can use addEventListener instead if you prefer, which composes more naturally with other DOM code:

const viewerEl = document.getElementById('viewer') as HTMLElement & { viewer: unknown };

viewerEl.addEventListener('hwvSceneReady', ({ detail: { hwv } }) => {
        hwv.view.setBackgroundColor(Color.blue(), Color.white());
});

viewerEl.addEventListener('hwvModelStructureReady', () => {
        document.getElementById('ModelStructureReady')!.innerHTML = 'Model Structure Ready';
});

viewerEl.addEventListener('hwvCamera', ({ detail: { camera } }) => {
        document.getElementById('CameraData')!.innerHTML = JSON.stringify(camera.toJson(), null, 4);
});

Don’t forget the import for Color:

import { Color } from '@ts3d-hoops/web-viewer';

In this example, we are listening for three useful events:

  1. hwvSceneReady: This event fires as soon as the Web Viewer is active and can be interacted with. Any functions on the Web Viewer object should only be called after this event has fired. For example we are changing the background color to a gradient. However, interacting with the model itself should only be done after hwvModelStructureReady, explained below.
  2. hwvModelStructureReady: This event fires as soon as the model structure information has been loaded. Before it fires, no model structure related functionality (e.g., selection) is available in the HOOPS Web Viewer component. We will discuss the model structure and how to parse it in more detail in an upcoming section of this tutorial. For now, we will just display a message indicating that the model structure has been loaded.
  3. hwvCamera: This event fires whenever the camera in the view has changed.

Every one of these events carries a detail object that always includes hwv, the same WebViewer instance you’d otherwise get from setCallbacks, so anything the Programming Guide says about a given callback’s behavior still applies - only how you subscribe to it has changed.

To display the hwvModelStructureReady message, we also need to add an element to the DOM:

<div>
        <h4>Model Structure...</h4>
        <div id="ModelStructureReady">Not Ready</div>
</div>

For small models, like the ‘microengine’ model we use for this tutorial, the model tree will be available almost instantly. It can take more time for larger models, so it is important for your application to gracefully handle this time period when the model can be viewed, but interaction is limited.

In this example, we add another section to the DOM to display the current camera information:

<div>
        <h4>CameraCallback</h4>
        <pre id="CameraData"></pre>
</div>

There are many other useful events available on the Web Viewer component - one exists for every entry in the old CallbackMap. Please see the Programming Guide for the full list.

Switching Camera Operators

When we first loaded the model onto the page, we were able to interact with it because the orbit-style navigation operator is active by default. The HOOPS Web Viewer includes several built-in camera operators, and @ts3d-hoops/web-viewer-components ships a ready-made dropdown that switches between them, so - unlike the previous version of this tutorial - we don’t need to hand-build a <select> or wire up an onclick handler at all. Add the component next to the viewer in index.html:

<hoops-web-viewer-context-manager>
        <hoops-web-viewer id="viewer" endpointUri="data/microengine.scs"></hoops-web-viewer>
        <hoops-toolbar-camera-operator></hoops-toolbar-camera-operator>
</hoops-web-viewer-context-manager>

No additional import is needed in main.ts - the whole-package import from the previous section already registered it.

<hoops-toolbar-camera-operator> reads the WebViewer instance from the same context that <hoops-web-viewer> publishes, so there is no manual wiring - it finds the viewer automatically because it lives inside the same <hoops-web-viewer-context-manager>. Clicking one of its icons switches between the Navigate (orbit), Turntable, and WalkMode operators.

Note

Custom, application-specific operators (like the ones we’ll write in the next two sections) aren’t covered by this component - it only knows about the built-in camera operators. We’ll come back to switching those in Custom Operator Basics.

If you want to know what’s happening under the hood: activating a built-in camera operator is a single call -

webViewer.view.operatorManager.set(OperatorId.Navigate, CameraOperatorPosition);
  • where CameraOperatorPosition (0) is the conventional stack position for the “top” camera-navigation operator, exported from @ts3d-hoops/web-viewer-components/context-manager. You won’t need to call this yourself for built-in operators, but the same mechanism is exactly what we’ll reuse for custom ones.

Switching the Draw Mode

As another example of a built-in, ready-to-use component, here is how to let the user change the draw mode. Add <hoops-toolbar-drawmode> alongside the camera operator button:

<hoops-toolbar-camera-operator></hoops-toolbar-camera-operator>
<hoops-toolbar-drawmode></hoops-toolbar-drawmode>

That’s the entire integration - no additional imports (it was already registered by the whole-package import in main.ts), and no event handlers to write. The dropdown offers every built-in draw mode (Shaded, Wireframe, Wireframe On Shaded, Hidden Line, X-Ray, Gooch, and Toon) and keeps its own icon in sync with whichever mode is currently active.

Finally, let’s run the application again:

../../_images/minimal_application.png

You should be able to see the model, manipulate the camera, switch between camera operators, and change the draw mode of the scene - all without having written a single event handler.