Load models directly from a Three.js loader (skinned mesh in this instance).

100% © threejs.org

index.js
import {
    AnimationMixer,
    DirectionalLight,
    Clock,
    Color,
    Fog,
    HemisphereLight,
    Mesh,
    MeshPhongMaterial,
    PlaneGeometry,
    Vector3,
    WebGLRenderer,
} from 'three';
import { MapControls } from 'three/examples/jsm/controls/MapControls.js';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
import { clone } from 'three/examples/jsm/utils/SkeletonUtils.js';

import Instance from '@giro3d/giro3d/core/Instance.js';
import Inspector from '@giro3d/giro3d/gui/Inspector.js';



const viewerDiv = document.getElementById('viewerDiv');

// we can customize the renderer THREE will use
// Here, this is necessary to render the glb correctly.
// Giro3D will handle:
// - adding it in the DOM within viewerDiv
// - resizing it when the window or viewerDiv is resized
const renderer = new WebGLRenderer({ antialias: true });
renderer.shadowMap.enabled = true;

// Create the Giro3D instance
const instance = new Instance(viewerDiv, { crs: 'EPSG:3857', renderer: { renderer } });
const camera = instance.camera.camera3D;

// Creates controls
const controls = new MapControls(instance.camera.camera3D, instance.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.2;

// and setup our instance to use them.
instance.useTHREEControls(controls);

const clock = new Clock();

// we can access the THREE.js scene directly
instance.scene.background = new Color(0xa0a0a0);
instance.scene.fog = new Fog(0xa0a0a0, 10, 50);

// adding lights directly to scene is ok
const hemiLight = new HemisphereLight(0xffffff, 0x444444, 2);
hemiLight.position.set(0, 0, 20);
hemiLight.updateMatrixWorld();
instance.scene.add(hemiLight);

const dirLight = new DirectionalLight(0xffffff, 3);
dirLight.position.set(-3, 10, 10);
dirLight.castShadow = true;
dirLight.shadow.camera.top = 4;
dirLight.shadow.camera.bottom = -4;
dirLight.shadow.camera.left = -4;
dirLight.shadow.camera.right = 4;
dirLight.shadow.camera.near = 0.1;
dirLight.shadow.camera.far = 40;
instance.scene.add(dirLight);
instance.scene.add(dirLight.target);
dirLight.updateMatrixWorld();

// Let's now setup a "ground" to receive the shadows
const mesh = new Mesh(
    new PlaneGeometry(200, 200),
    new MeshPhongMaterial({ color: 0xcbcbcb, depthWrite: false }),
);
mesh.receiveShadow = true;
// Contrary to lights, every meshes should be added through `instance.add`, in order for Giro3D to
// be aware of them. Otherwise the objects will just disappear.
// For technical details, see how MainLoop.js calculates camera near and far.
instance.add(mesh);

// Let's load objects using one of the THREE loaders.
const loader = new GLTFLoader();
loader.load('https://threejs.org/examples/models/gltf/Soldier.glb', gltf => {
    gltf.scene.traverse(object => {
        if (object.isMesh) object.castShadow = true;
    });

    // this code is virtually identical to this example:
    // https://threejs.org/examples/webgl_animation_multiple
    const model1 = clone(gltf.scene);
    const model2 = clone(gltf.scene);
    const model3 = clone(gltf.scene);
    const models = [model1, model2, model3];

    const mixer1 = new AnimationMixer(model1);
    const mixer2 = new AnimationMixer(model2);
    const mixer3 = new AnimationMixer(model3);
    const mixers = [mixer1, mixer2, mixer3];

    mixer1.clipAction(gltf.animations[0]).play(); // idle
    mixer2.clipAction(gltf.animations[1]).play(); // run
    mixer3.clipAction(gltf.animations[3]).play(); // walk

    model1.position.x = 1;
    model1.rotation.x = Math.PI / 2;
    model1.updateMatrixWorld();
    model2.position.x = 0;
    model2.rotation.x = Math.PI / 2;
    model2.updateMatrixWorld();
    model3.position.x = 2;
    model3.rotation.x = Math.PI / 2;
    model3.updateMatrixWorld();

    // except for this part, we add directly to instance to make Giro3D aware of these models
    instance.add(model1);
    instance.add(model2);
    instance.add(model3);

    // let's move our camera and control target
    // We add 1 to z to look at the waist. The 0, 0, 0 is located at the soldier's feet.
    const lookAt = new Vector3(0, 0, 1).add(model1.position);
    camera.position.set(2, 6, 3);
    camera.lookAt(lookAt);
    controls.target.copy(lookAt);
    controls.saveState();

    // you can hook yourself to event of the rendering loop.
    instance.addEventListener('after-camera-update', () => {
        const delta = clock.getDelta();

        for (const mixer of mixers) {
            mixer.update(delta);
        }
        for (const model of models) {
            model.updateMatrixWorld();
            instance.notifyChange(model);
        }
    });

    instance.notifyChange();

    let where = models;
    document.getElementById('pick_source').addEventListener('change', e => {
        const newMode = parseInt(e.target.value, 10);
        if (newMode === 1) {
            where = [model1];
        } else if (newMode === 2) {
            where = [model2];
        } else if (newMode === 3) {
            where = [model3];
        } else {
            where = models;
        }
    });

    const formatter = new Intl.NumberFormat();

    function format(point) {
        return `x: ${formatter.format(point.x)}\n
                y: ${formatter.format(point.y)}\n
                z: ${formatter.format(point.z)}`;
    }

    instance.domElement.addEventListener('dblclick', e => {
        const picked = instance.pickObjectsAt(e, { limit: 1, where });
        if (picked.length === 0) {
            document.getElementById('selectedDiv').innerText = 'No object found';
        } else {
            document.getElementById('selectedDiv').innerHTML = `
                Picked ${picked[0].object.name} at:<br>
                ${format(picked[0].point)}!`;
        }
    });
});

Inspector.attach(document.getElementById('panelDiv'), instance);
index.html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Giro3D - Three.js loaders</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
  <style>
    body {
      padding: 0;
      margin: 0;
      width: 100vw;
      height: 100vh;
    }

    #viewerDiv {
      width: 100%;
      height: 100%;
    }

    #panelDiv {
      position: absolute;
      top: 0;
      left: 0;
    }
    
  </style>
</head>

<body>
  <div id="viewerDiv"></div>
  <div id="panelDiv"></div>
  <div class="m-3 position-absolute top-0 end-0 card">
    <div class="card-body">
        <div class="dropdown">
            <select class="btn btn-primary dropdown-toggle d-block" id="pick_source">
                <option value="0" selected>Pick from all models</option>
                <option value="1">Pick from Vanguard idle</option>
                <option value="3">Pick from Vanguard walking</option>
                <option value="2">Pick from Vanguard running</option>
            </select>
        </div>
        <div id="selectedDiv"></div>
    </div>
</div>

  <script type="module" src="index.js"></script>
  <script>
    /* activate popovers */
    const popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'));
    popoverTriggerList.map(
      // bootstrap is used as script in the template, disable warning about undef
      // eslint-disable-next-line no-undef
      popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl, {
        trigger: 'hover',
        placement: 'left',
        content: document.getElementById(popoverTriggerEl.getAttribute('data-bs-content')).innerHTML,
        html: true,
      }),
    );
  </script>
</body>

</html>
package.json
{
  "name": "three_loader",
  "dependencies": {
    "@giro3d/giro3d": "0.35.0"
  },
  "devDependencies": {
    "vite": "^3.2.3"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}