-
Notifications
You must be signed in to change notification settings - Fork 288
Importing Models
xeolabs edited this page Jan 22, 2019
·
32 revisions
xeokit-sdk can load multiple models from a variety of source formats into the same 3D scene.
xeokit represents a 3D scene as a secene graph, which is hierarchy of Nodes, in which Meshes are the drawable elements at the leaves.
- See Scene Graphs for more info on scene graphs.
Each of xeokit's model loader plugins creates a tree of Nodes in which the root Node represents the model itself.
In the example below, we'll use a GLTFLoaderPlugin to load the Schependomlaan house model and a OBJLoaderPlugin to load a model of a car.
We'll use a Node to contain them in a simple scene graph, scaled down a bit, and we'll add a Mesh to represent the green ground plane.
The ground plane Mesh gets a plane-shaped Geometry created by a buildPlaneGeometry builder function.
Click the image below for a live demo.
import {Viewer} from "../src/viewer/Viewer.js";
import {Node} from "../src/scene/nodes/Node.js";
import {OBJLoaderPlugin} from "../src/viewer/plugins/OBJLoaderPlugin/OBJLoaderPlugin.js";
import {GLTFLoaderPlugin} from "../src/viewer/plugins/GLTFLoaderPlugin/GLTFLoaderPlugin.js";
import {Mesh} from "../src/scene/mesh/Mesh.js";
import {buildPlaneGeometry} from "../src/scene/geometry/builders/buildPlaneGeometry.js";
import {ReadableGeometry} from "../src/scene/geometry/ReadableGeometry.js";
import {PhongMaterial} from "../src/scene/materials/PhongMaterial.js";
const viewer = new Viewer({
canvasId: "myCanvas"
});
const objLoader = new OBJLoaderPlugin(viewer);
const gltfLoader = new GLTFLoaderPlugin(viewer);
new Node(viewer.scene, {
scale: [.5, .5, .5], // Scale the whole scene down a bit
children: [
// Car
objLoader.load({ // Returns a Node that will be registered on Viewer#scene#models
id: "myModel",
src: "./models/obj/sportsCar/sportsCar.obj",
position: [-5, -1, 0],
}),
// House
gltfLoader.load({ // Returns a Node that will be registered on Viewer#scene#models
id: "myModel2",
src: "./models/gltf/schependomlaan/scene.gltf",
metaModelSrc: "./metaModels/schependomlaan/metaModel.json", // Creates a MetaObject instances in scene.metaScene.metaObjects
rotation: [0, 50, 0],
edges: true
}),
// Ground plane
new Mesh(viewer.scene, {
geometry: buildPlaneGeometry(ReadableGeometry, viewer.scene, {
xSize: 500,
zSize: 500
}),
material: new PhongMaterial(viewer.scene, {
diffuse: [0.4, 1.0, 0.4],
backfaces: true
}),
position: [0, -1.0, 0],
pickable: false,
collidable: false
})
]
});