Setting up Views
In this chapter, we will walk through setting the main focus of our application: the viewers.
- Instantiating and Starting the WebViewers
- Setting Callbacks
- Configuring additional WebViewer options
The src/js/app.js file will contain our main application logic, as well as manage the setup and handling of our WebViewer objects.
To start, we have provided the starter code and will implement each function in the following steps.
At this point, you can run npm start on the command line to start the development server provided with the tutorial. The server will automatically update the browser as you save changes. You should see this:
Make sure you have run npm install before starting the server!
As you can see, our template has both a "Main View" and an "Overhead View". The main viewer will provide user interaction with the scene and provide the canvas for user control. The overhead viewer will serve as another camera in the scene, fixed above the scene, pointing straight down the -Z scene axis. The overhead viewer will not have user interaction (though you could enable it, if so desired).
In the HTML provided, you can see <div> elements present that will serve as containers for our Web Viewers.
The container IDs we are interested in are viewer and subviewer. The following code will be added to the constructor of your main class, using the viewer and subviewer div IDs. We will store each Web Viewer object in an array.
Add the following to the beginning of the constructor() starter code provided in the file app.js:
If you look back at your web application, you will see nothing has changed. At this point we have started the viewers but have not loaded a model. A lot of the initial setup between both viewers is going to be the same, so we can use Array.prototype.map() to apply the common logic to each viewer. We have provided the function setViewerCallbacks() in the starter code, which is where we will add this logic.
To start, we will create named instances of our viewers within setViewerCallbacks():
Next, we will set up the map() callback function. For each viewer, we will set a Web Viewer callback for modelStructureReady and sceneReady. By using callbacks, we can ensure our set up and configuration logic is executed at the appropriate time during the Web Viewer lifecycle. You can find additional details in our Programming Guide article Callbacks.
Append the following to setViewerCallbacks():
You can see from the comments what we intend for each callback. Let’s work through each one, though we will skip the printing plane for now – we will implement that in the next section.
We can abstract the loading process into its own loadModel() function, as a class method of main. The loadModel() function will take two parameters – the model name to be loaded, and the WebViewer the model should be loaded into.
Within the function, we want to be sure we are adding our model to the model tree in a smart manner. We will create a new model node off the root node, name it, and load our model to that node.
Add the following to the loadModel() function provided in the starter code:
We can call now call our new function in the modelStructureReady callback:
Note, the newly created class property _modelList to track the list of loaded models. Add the new class property to the constructor():
At last! Reload your browser and you will see both viewers with the model loaded and rendered! But notice the camera position is not very help, let's address that next.
By default, both viewers will load the model identically, but we know we would like to have two separate camera views for these viewers. To make a new Camera view, we need to specify all the parameters that make up the Camera state. This consists of three main vectors:
- Position in world space
- Target at which to point
- What direction should be considered "up"
Also, we specify the projection mode and the camera view width and height. Last, we can optionally provide a near clipping limit, where anything within that distance to the camera will be clipped. For the sake of this tutorial, we have provided the camera parameters for you, but you can adjust these as you wish. Add the following to the sceneReady callback because we do not want to update the scene until it is ready, which may happen after the modelStructureReady event. You should see your application update to reflect these camera changes.
Finally, we will spice up our viewers by adding a colored background consistent with our UI theme. This can be done by calling setBackgroundColor on the Communicator.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. Add this last line to sceneReady:
This completes the callback functions that apply to both viewers (except for the printing plane, which we will address in the next section). Your setViewerCallbacks() should look like this:
We can add additional callbacks for our main viewer, that we don’t want to show in the overhead viewer. We can do this a number of ways, but here we will write another modelStructureReady callback outside our map function for just the main viewer. You can set multiple callback functions on the same event without issue, so let’s demonstrate that.
Append the following to setViewerCallbacks() below this._viewerList.map(...) function:
When the sceneReady callback is executed, 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 Communicator.View object in the WebViewer. We will retrieve these objects, enable them, and move the navigation cube from its default position to the lower right corner of the scene.
Notice we are only setting this callback on the mainViewer object, not both like before.
With that, we should have a good start to our application. We will revisit this code as needed throughout the rest of the tutorial.