This page shows you how to create a complex scene with 3DHOP, describing how to apply transformations to objects and how to manage the whole scene.
Click to run live demo
Multi-object 3D scenes with 3DHOP
In the previous examples, we have used a single 3D model (already correctly oriented with respect to the world axis), or two 3D models (already correctly oriented with respect to the world axis AND correctly positioned one with respect to the other).
If your 3D model is not "straight" with respect to the world axis, and you need to control its orientation, or if you have more than one 3D model to arrange in the scene with a specific position/orientation/scale, you will have to use transformations.
In 3DHOP, it is possible to assign a transformation to both meshes and instances, providing the user a way to finely control the disposition of object in the scene.
A "transform" element is used to specify a combination of rotation, translation and scaling, or even a non-rigid transformation. Depending on where the "transform" element is used, you may obtain different effects. When a transformation is specified for one of the instances in the "modelInstances" section, the transformation will be applied only to that specific instance. On the other hand, when a transformation is specified for one of the meshes in the "meshes" section, it will be applied every time the mesh will be used (in ALL the instances and spots that use it). Finally, it is also possible to specify a transformation in the "scene" section, and that transformation will be applied for every element of the scene (ALL spots and instances). Transformations will stack: if an instance has a transformation, and also its mesh has a transformation, both will be used, concatenated; the same will apply to the scene transformation. Transformation stacking always follows the same order, first it is applied the scene transform, then the mesh transform and finally the instance/spot transform. This three-level transformation system is flexible enough for most of the situations.
This source code taken from the 3DHOP example of this tutorial, shows a mesh transformation applied at the "meshes" level: in this case we load a simple cube, and scale it to transform it into a "table" where we will arrange the other models.
This is the ideal place to specify those transformations that are used to make the 3D models "suitable" for your scene. For example, when a model is "not straight" with respect to the axis, and by using a rotation transformation we can put it "straight"; or when a model is not in the same scale of the rest of the scene, and by using a scaling transformation we can restore its correct size.
var presenter = null;
function setup3dhop() {
presenter = new Presenter("draw-canvas");
presenter.setScene({
meshes: {
"Laurana": {
url: "models/laurana.ply"
},
"Gargoyle": {
url: "models/gargo.nxs"
},
"Box": {
url: "models/cube.ply",
transform: { matrix: SglMat4.scaling([13.0, 0.5, 10.0]) },
}
},
...
Conversely, when we want to change the orientation/position/scale of a single instance, you need to specify a transformation when declaring the instance in the "modelInstances" section.
In the following block of code we can see how different instances are arranged in the scene.
... modelInstances: { "Base" : { mesh : "Box" }, "Center" : { mesh : "Laurana", transform : { matrix: [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 250.0, -50.0, 1.0] }, }, "Left" : { mesh : "Gargoyle", transform : {
translation: [-120.0, 12.5, 150.0], rotation : [0.0, 90, 0.0],
scale: [1.5, 1.5, 1.5] } }, "Right" : { mesh : "Gargoyle", transform : { matrix: SglMat4.mul(SglMat4.translation([120.0, 12.5, 150.0]), SglMat4.rotationAngleAxis(sglDegToRad(-90.0), [0.0, 1.0, 0.0])) } } }, ...
A transformation may be specified in three different ways:
If in a scene there is just a single instance, 3DHOP will automatically center the trackball on its barycenter, and scale the view to fit the model in the CANVAS element. This centering/scaling takes in account the transformations that may have been specified (for the mesh and/or the instance). This automatic process makes easy to create simple single-object viewers.
But what happens when you declare more than one instance in the same scene? The default behaviour is to center and scale following the FIRST instance declared in the scene. This may or may not be what you need, but it is possible to change this behaviour.
When declaring the scene elements, it is possible to add a "space" section. In this section, among many other things, you can specify where the trackball is centered and how the scene view is scaled. This is done by specifying a "centerMode" and/or a "radiusMode", plus some other parameters:
...
space: {
centerMode: "explicit",
explicitCenter: [0.0, 100.0, 0.0],
radiusMode: "specific",
whichInstanceRadius: "Base"
}
});
}
So, summarizing, to setup a complex scene with 3DHOP you just need to define the necessary transformations (in the "meshes" or in the "modelInstances" sections), and to specify the scene centeringa ns scaling according to your needs.
The complete sources of this example are provided together the 3DHOP code in the download section.
If you want instead to learn more about how to manage the visibility and the appearance of a mesh in the 3DHOP virtual scene click here and go to the next HOWTO.