-
Notifications
You must be signed in to change notification settings - Fork 293
Scene Graphs
xeolabs edited this page Jan 3, 2019
·
28 revisions
WIP
//------------------------------------------------------------------------------------------------------------------
// Import the modules we need for this example
//------------------------------------------------------------------------------------------------------------------
import {Viewer} from "../src/viewer/Viewer.js";
import {Mesh} from "../src/scene/mesh/Mesh.js";
import {Node} from "../src/scene/nodes/Node.js";
import {PhongMaterial} from "../src/scene/materials/PhongMaterial.js";
//------------------------------------------------------------------------------------------------------------------
// Create a Viewer and arrange the camera
//------------------------------------------------------------------------------------------------------------------
const viewer = new Viewer({
canvasId: "myCanvas"
});
viewer.camera.eye = [-21.80, 4.01, 6.56];
viewer.camera.look = [0, -5.75, 0];
viewer.camera.up = [0.37, 0.91, -0.11];
//------------------------------------------------------------------------------------------------------------------
// Build a simple scene graph representing a table with four legs
//------------------------------------------------------------------------------------------------------------------
new Node(viewer.scene, {
objectId: "table", // <---------- Node with "objectId" represents a 3D object
rotation: [0, 50, 0],
position: [0, 0, 0],
scale: [1, 1, 1],
children: [
new Mesh(viewer.scene, { // Red table leg
objectId: "redLeg",
position: [-4, -6, -4],
scale: [1, 3, 1],
rotation: [0, 0, 0],
material: new PhongMaterial(viewer.scene, {
diffuse: [1, 0.3, 0.3]
})
}),
new Mesh(viewer.scene, { // Green table leg
objectId: "greenLeg",
position: [4, -6, -4],
scale: [1, 3, 1],
rotation: [0, 0, 0],
material: new PhongMaterial(viewer.scene, {
diffuse: [0.3, 1.0, 0.3]
})
}),
new Mesh(viewer.scene, {// Blue table leg
objectId: "blueLeg",
position: [4, -6, 4],
scale: [1, 3, 1],
rotation: [0, 0, 0],
material: new PhongMaterial(viewer.scene, {
diffuse: [0.3, 0.3, 1.0]
})
}),
new Mesh(viewer.scene, { // Yellow table leg
objectId: "yellowLeg",
position: [-4, -6, 4],
scale: [1, 3, 1],
rotation: [0, 0, 0],
material: new PhongMaterial(viewer.scene, {
diffuse: [1.0, 1.0, 0.0]
})
}),
new Mesh(viewer.scene, { // Purple table top
objectId: "tableTop",
position: [0, -3, 0],
scale: [6, 0.5, 6],
rotation: [0, 0, 0],
material: new PhongMaterial(viewer.scene, {
diffuse: [1.0, 0.3, 1.0]
})
})
]
});
//------------------------------------------------------------------------------------------------------------------
// Find scene graph nodes by their object IDs
//------------------------------------------------------------------------------------------------------------------
var table = viewer.scene.objects["table"];
var redLeg = viewer.scene.objects["redLeg"];
var greenLeg = viewer.scene.objects["greenLeg"];
var blueLeg = viewer.scene.objects["blueLeg"];
//------------------------------------------------------------------------------------------------------------------
// Periodically update transforms on our scene nodes
//------------------------------------------------------------------------------------------------------------------
viewer.scene.on("tick", function () {
// Rotate legs
redLeg.rotateY(0.5);
greenLeg.rotateY(0.5);
blueLeg.rotateY(0.5);
// Rotate table
table.rotateY(0.5);
table.rotateX(0.3);
});