February 11, 2021

glTF 2.0 Support in Forge Viewer

One of the new features introduced by Forge Viewer version 7.36 is the support for loading glTF (2.0) models. The feature is currently in beta but if you like to live on the edge - which I'm sure you do - read on!

glTF 2.0 is a popular 3D exchange file format aiming to become the "JPEG for 3D", with a rich ecosystem of viewers, importers, exporters, and other tools. And in Forge Viewer you can now load glTF files natively (in other words, without having to process them using the Model Derivative service) using a new viewer extension called Autodesk.glTF. Just like the Autodesk.PDF extension, all you need to do is load the extension, and then simply call the viewer's loadModel method, passing in a URL address of your .gltf file, for example, like so:

viewer.loadExtension('Autodesk.glTF').then(() => {
    viewer.loadModel('address/of/your/model.gltf');
});

If you'd prefer a ready-to-run sample app, check out the experiment/gltf2 branch of the https://github.com/petrbroz/forge-basic-app sample. The code includes two sample glTF files, one from https://github.com/KhronosGroup/glTF-Sample-Models, and one created from a Revit model in Forge (using the https://github.com/petrbroz/forge-convert-utils library). Here's all the code that's needed for the client side:

Autodesk.Viewing.Initializer({ accessToken: '' }, async function () {
    const viewer = new Autodesk.Viewing.GuiViewer3D(document.getElementById('preview'));
    viewer.start();
    viewer.setTheme('light-theme');
    await viewer.loadExtension('Autodesk.glTF');
    //viewer.loadModel('models/rac_basic_sample_project/gltf/model.gltf');
    viewer.loadModel('models/Sponza/glTF/Sponza.gltf');
});

Note that we don't need to pass in any access token to our viewer as we won't be accessing any of our models in Forge.

Disclaimer

Here's a couple of things to keep in mind when working with glTF files in Forge Viewer:

  • The viewer extension does not aim to be fully compliant with the glTF 2.0 specification
    • For example, the material/shader system in Forge Viewer is different from the PBR (physically-based rendering) materials used in glTF, so the viewer will not be able to represent all glTF materials to their full extent
    • Some features of the file format such as animations are not supported
  • If the glTF file defines multiple objects in a tree structure, the individual objects will be selectable in the viewer and the tree structure will appear in the Model Browser dialog; note however that many glTF files store the entire scene as a single object
  • The glTF specification does not use any concept of metadata attached to individual elements; the Properties dialog in the viewer will therefore not provide any additional information about selected elements

Feedback

If you run into any issues with the Autodesk.glTF extension, feel free to drop us a note, either on Stack Overflow, or via https://forge.autodesk.com/en/support/get-help. Thanks!

Posted by

Developer Advocate, Autodesk

Petr is a senior developer advocate at Autodesk. He has been developing some of the company’s web applications and services since 2011, and now he aims to help others build amazing, creative solutions using these tools, especially using Autodesk Forge APIs. Petr’s technical focus areas include everything around web and cloud, 3D graphics, and all kinds of "non-real reality".

Related Posts