Viewing glTF using GLTFLoader


How to load an IFC model from glTF (.glb) format directly into a xeokit web viewer.
Click on the preview below to run the example. Scroll down to learn how it's made.


Click to load
Placeholder image

Viewing glTF using GLTFLoader


HTML


Listed below is the HTML for this example.


<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Viewing glTF using GLTFLoader</title>
    <style>
        body {
            background-color: white;
            overflow: hidden;
            margin: 0;
            user-select: none;
        }

        #demoCanvas {
            width: 100%;
            height: 100%;
            position: absolute;
            background: white;
            border: 0;
        }
    </style>
</head>
<body>
<canvas id="demoCanvas"></canvas>
</body>
<script type="module" src="./index.js"></script>
</html>


JavaScript


Listed below is the JavaScript for this example, which we'll break down into steps.

1. Import the SDK from a bundle built for these examples

import * as xeokit from "../../js/xeokit-demo-bundle.js";
import {DemoHelper} from "../../js/DemoHelper.js";

2. Create a GLTFLoader to load .glb files

const gltfLoader = new xeokit.gltf.GLTFLoader();

3. Create a Scene to hold geometry and materials

const scene = new xeokit.scene.Scene();

4. Create a Data to hold semantic data

const data = new xeokit.data.Data();

5. Create a WebGLRenderer to use the browser's WebGL graphics API for rendering

const renderer = new xeokit.webglrenderer.WebGLRenderer({});

6. Create a Viewer that will use the WebGLRenderer to draw the Scene

const viewer = new xeokit.viewer.Viewer({
    id: "demoViewer",
    scene,
    renderer
});

7. Give the Viewer a single View to render the Scene in our HTML canvas element

const view = viewer.createView({
    id: "demoView",
    elementId: "demoCanvas"
});

8. Arrange the View's Camera within our +Z "up" coordinate system

view.camera.eye = [1841982.9384371885, 10.031355126263318, -5173286.744630201];
view.camera.look = [1842009.4968455553, 9.685518291306686, -5173295.851503017];
view.camera.up = [0.011650847910481935, 0.9999241456889114, -0.003995073374452514];

9. Add a CameraControl to interactively control the View's Camera with keyboard, mouse and touch input

new xeokit.cameracontrol.CameraControl(view, {});

10. Create a SceneModel to hold our model's geometry and materials

const sceneModel = scene.createModel({
    id: "demoModel"
});

11. Ignore the DemHelper

const demoHelper = new DemoHelper({
    viewer,
    data
});
demoHelper.init()
    .then(() => {

12. Create a DataModel to hold semantic data for our model

        const dataModel = data.createModel({
            id: "demoModel"
        });
        if (sceneModel instanceof xeokit.core.SDKError) {
            console.error(`Error creating SceneModel: ${sceneModel.message}`);
        } else {

13. Use GLTFLoader to load a glTF model into our SceneModel and DataModel

            fetch("../../models/MAP/gltf/model.glb").then(response => {
                response
                    .arrayBuffer()
                    .then(fileData => {
                        gltfLoader.load({
                            fileData,
                            sceneModel,
                            dataModel
                        }).then(() => {

14. Build the SceneModel and DataModel. The Scene and SceneModel will now contain a SceneObject for each displayable object in our model. The Data and DataModel will contain a DataObject for each IFC element in the model. Each SceneObject will have a corresponding DataObject with the same ID, to attach semantic meaning. The View will contain a ViewObject corresponding to each SceneObject, through which the appearance of the object can be controlled in the View.

                            dataModel.build();
                            sceneModel.build();
                            demoHelper.finished();
                        }).catch(message => {
                            console.error(`Error loading glTF: ${message}`);
                        });
                    });
            });
        }
    });