Setting up the Main Viewer

Summary

In this chapter, we will walk through setting the main focus of our application: the viewer.

Concepts

  • Declaring the HOOPS Web Viewer component
  • Setting callbacks via component events
  • Configuring additional WebViewer options

Create the Main Entry and Structure for the Application

To get started, we will create a new JS file titled app.js and place it in the src/js folder. The app.js file will contain our main application logic, as well as manage the setup and handling of our WebViewer object.

To better organize our code, we will be breaking up our code and using the external module approach for this development. We will be using the ES6 class syntax to encapsulate functionality. Let us start by making the main class in our app.js file. This is where we will instantiate our viewer and build our application through composition from other objects we will create later. Right now, we will just leave the class and constructor blank and return to it.

class main {
        constructor() {

        }
} // End main class

Registering the Web Viewer Components

This tutorial uses the Web Components UI kit (@ts3d-hoops/web-viewer-components) - the same one introduced in the Building A Basic Application tutorial. If you haven’t been through that tutorial yet, its Connecting to the HOOPS Web Viewer Component section is a useful primer on the <hoops-web-viewer> element and the <hoops-service-registry> / <hoops-web-viewer-context-manager> wrapper elements it needs.

Registering the components is a matter of importing the package once, for its side effect. Since we have provided the CSS file for you, you can import both it and the component package at the top of the app.js file. Your app.js file should now look something like this:

import '../css/tutorial-userData.css';
import '@ts3d-hoops/web-viewer-components';

// Application logic will begin once DOM content is loaded
window.onload = () => {
        const app = new main();
};

class main {
        constructor() {

        }
} // End main class

At this point, you are welcome to run npm run start in the command line to start our webpack server and serve the frontend framework that we have provided. You can leave the development server running, and it will update your frontend as we write more code. You should see something like this before writing any additional Javascript:

../../_images/inventory-mgmt-empty-app.png

Adding the Viewer to the Page

Because we have provided the HTML and styling, we should see the container element present that we can attach our HOOPS Web Viewer to. Unlike a plain-<div>-and-new WebViewer(...) setup, the <hoops-web-viewer> custom element is the rendering surface, wrapped in the same <hoops-service-registry> / <hoops-web-viewer-context-manager> plumbing used throughout the Basic App tutorial:

<!-- Main Viewer Component w/ Interaction -->
<div id="canvas" class="comp-container">
        <hoops-service-registry>
                <hoops-web-viewer-context-manager>
                        <hoops-web-viewer id="viewer" enginePath="./engine"></hoops-web-viewer>
                </hoops-web-viewer-context-manager>
        </hoops-service-registry>
        <div id="viewer-label" class="comp-label">HOOPS WebViewer</div>
</div>

The id we are interested in is still called viewer - it now lives on the <hoops-web-viewer> element itself rather than on a plain <div>. Notice there’s no model or endpointUri attribute here: leaving both off is how you get the equivalent of the old empty: true constructor option - the viewer starts with an empty scene, and we’ll load a model into it explicitly in a later chapter.

Note

The enginePath attribute serves the same purpose as the constructor option of the same name in the lower-level API: it tells HOOPS Web Viewer where to find the graphics engine files (engine.esm.wasm and related assets) at run time. It must point to a directory your web server or bundler actually serves - for example, copy the contents of @ts3d-hoops/sc-engine from your HOOPS Visualize Web installation into an engine folder alongside your bundled output.

Note

Unlike the old <div>-and-new WebViewer(...) setup, nothing sizes the viewer for you at runtime - the previous version’s WebViewer constructor used to set inline width/height styles on its container <div> itself, but <hoops-web-viewer> is sized entirely by ordinary CSS, the same way as in the Building A Basic Application tutorial. The provided tutorial-userData.css already includes a rule for this:

#canvas hoops-web-viewer {
        display: block;
        width: 100%;
        height: 100%;
}

If you look back at your web application, you will see nothing has changed yet - we haven’t wired up any application logic to the component. Don’t worry, we haven’t loaded a model yet either; you can specify one via the endpointUri attribute, but we’ve elected not to for now.

Connecting to the WebViewer Component and Setting Callbacks

The next step is to get a reference to the <hoops-web-viewer> element and subscribe to its events. If you are unfamiliar with the component’s event-based callback style, or need a refresher, please review the callbacks section of the Building A Basic Application tutorial: every entry in the old CallbackMap is re-dispatched as a same-named DOM CustomEvent prefixed with hwv (so modelStructureReady becomes hwvModelStructureReady), and its detail object always includes hwv, the underlying WebViewer instance. For our application, we are interested in two main events – hwvModelStructureReady and hwvSelectionArray.

Because we want to load a model to the scene only after the model structure is ready, we need to wait for the hwvModelStructureReady event to fire, or else we will get an error trying to access the Model class too quickly. The hwvSelectionArray event will be used to gather user selection events, and query model/node information.

class main {

        constructor() {

                // Get a reference to the HOOPS Web Viewer component
                this._viewerEl = document.getElementById("viewer");

                this._viewerEl.addEventListener("hwvModelStructureReady", ({ detail: { hwv } }) => {
                        // Keep a reference to the underlying WebViewer for the rest of the application to use
                        this._viewer = hwv;

                        // Set Background color for viewers
                        // Enable nav cube and axis triad
                        // Enable load model button
                });

                this._viewerEl.addEventListener("hwvSelectionArray", ({ detail: { selectionEvents } }) => {
                        // Reset info fields if no selection item was chosen
                        // Otherwise, display node information for the first node in the selection array
                        // If the selection nodeId is found in the application data, populate the inspector fields with application data
                });

        } // End main constructor

} // End main class

Note

Confirmed against a running build: the { selectionEvents } destructuring mirrors the { hwv } / { camera } detail shape shown for hwvSceneReady / hwvCamera in the Basic App tutorial. hwvSelectionArray’s detail object also carries a removed key alongside hwv and selectionEvents, which this application doesn’t use but is available if you want to react to deselection specifically.

From the comments, you can see we will be performing several steps once the model structure is ready. Let’s fill that in now, as it is rather straightforward.

When hwvModelStructureReady fires, we should be able to add some additional elements to our scene, like a navigation cube and axis triad. By default, you can access the navigation cube and axis triad via the View object on the WebViewer instance. We will retrieve these objects, enable them, and move the navigation cube from its default position to the lower right corner of the scene.

// Additional viewer options
this._viewer.view.getAxisTriad().enable();
this._viewer.view.getNavCube().enable();
this._viewer.view.getNavCube().setAnchor(OverlayAnchor.LowerRightCorner);

In addition, we will spice up our viewers by adding a colored background consistent with our UI theme. This is as simple as calling setBackgroundColor on the View object. You can specify a single background color, or make a gradient from top to bottom by providing two colors. We will choose to make the gradient.

// Background color for viewers
this._viewer.view.setBackgroundColor(new Color(0, 153, 220), new Color(218, 220, 222));

Don’t forget the import for Color and OverlayAnchor, alongside the CSS/component-registration imports at the top of app.js:

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

Lastly, but most importantly, we will enable the input button to load a model to the scene. We will write this functionality later, but waiting for hwvModelStructureReady to fire before enabling the button will ensure we get no loading errors by trying to load a model prematurely. Our final hwvModelStructureReady listener will look like this:

this._viewerEl.addEventListener("hwvModelStructureReady", ({ detail: { hwv } }) => {
        this._viewer = hwv;

        // Background color for viewers
        this._viewer.view.setBackgroundColor(new Color(100, 150, 200), new Color(222, 222, 222));

        // Additional viewer options
        this._viewer.view.getAxisTriad().enable();
        this._viewer.view.getNavCube().enable();
        this._viewer.view.getNavCube().setAnchor(OverlayAnchor.LowerRightCorner);

        let loadButton = document.getElementById("open-model-button");
        loadButton.disabled = false;
});

The hwvSelectionArray listener is more involved, and will rely on some other aspects of our application, so for now we will keep it empty and revisit it as we develop the pieces we need for its functionality.