Can't import .glb file to scene with SceneLoader.ImportMeshAsync

    import {
        Engine,
        Scene,
        Vector3,
        HemisphericLight,
        MeshBuilder,
        FreeCamera,
        PhysicsAggregate,
        PhysicsShapeType,
        SceneLoader
    } from '@babylonjs/core';

    import '@babylonjs/loaders/glTF';

    import { HavokPlugin } from '@babylonjs/core/Physics';
    import HavokPhysics from '@babylonjs/havok';

    // Setup Babylon.js
    const canvas = document.getElementById('renderCanvas');
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    let engine = new Engine(canvas, true);
    let scene = new Scene(engine);

    // Initialize Havok
    const havok = await HavokPhysics();
    const plugin = new HavokPlugin(undefined, havok);
    scene.enablePhysics(new Vector3(0, -9.81, 0), plugin);

    // Camera and light
    let camera = new FreeCamera('camera1', new Vector3(0, 2, -15), scene);
    camera.setTarget(Vector3.Zero());
    camera.attachControl(canvas, true);
    let light = new HemisphericLight('light1', new Vector3(0, 1, 0), scene);

    // Ground
    let ground = MeshBuilder.CreateGround('ground', { width: 128, height: 128 }, scene);
    const groundAggr = new PhysicsAggregate(ground, PhysicsShapeType.BOX, { mass: 0 }, scene);

    let cps = MeshBuilder.CreateCapsule('capsule', {height: 2, radius: 0.5}, scene);
    cps.position.y = 1;
    let box = MeshBuilder.CreateBox('box', {size: 1}, scene);
    box.position.y = 0.5;
    box.position.x = 1;
    cps.checkCollisions = true;
    box.checkCollisions = true;
    // cps.position.z = 3.5;
    // cps.position.x = 2;
    const cpsAggr = new PhysicsAggregate(cps, PhysicsShapeType.CAPSULE, { mass: 1 }, scene);
    const boxAggr = new PhysicsAggregate(box, PhysicsShapeType.BOX, { mass: 1 }, scene);

    camera.applyGravity = true;
    camera.ellipsoid = new Vector3(1.5, 1, 1.5);
    camera.checkCollisions = true;
    ground.checkCollisions = true;

    const res = await SceneLoader.ImportMeshAsync("", "public/", "pistol_fps.glb", scene)

    scene.onPointerDown = () => {
        // Create the bullet as a small sphere
        const bullet = MeshBuilder.CreateSphere("bullet", { diameter: 0.1 }, scene);

        // Set the bullet's initial position to the camera's position
        bullet.position = camera.position.clone();

        // Create a PhysicsAggregate for the bullet with mass
        const bulletAggr = new PhysicsAggregate(bullet, PhysicsShapeType.SPHERE, { mass: 0.1 }, scene);

        // Get the direction the camera is looking
        const forward = camera.getForwardRay().direction;

        // Apply force in the direction the camera is looking
        const forceMagnitude = 100; // Adjust this value to control the speed of the bullet
        const force = forward.scale(forceMagnitude);
        console.log(force)
        bulletAggr.body.applyImpulse(force, bullet.getAbsolutePosition());
        console.log('initial speed', bulletAggr.body.getLinearVelocity())

        // Optional: Set a timeout to dispose of the bullet after some time
        setTimeout(() => {
            bullet.dispose();
        }, 5000); // Dispose after 5 seconds
    }

    engine.runRenderLoop(() => {
      scene.render();
    });

    camera.keysUp.push(87); //forwards
    camera.keysDown.push(83); //backwards
    camera.keysLeft.push(65);
    camera.keysRight.push(68);

    //mouse sens. (higher is slower)
    camera.angularSensibility = 800;
    camera.inertia = 0.5;

    canvas.addEventListener("click", event => {
      canvas.requestPointerLock = canvas.requestPointerLock || canvas.msRequestPointerLock || canvas.mozRequestPointerLock || canvas.webkitRequestPointerLock;
      if(canvas.requestPointerLock) {
       canvas.requestPointerLock();
      }
    }, false);

    window.addEventListener('resize', () => {
      engine.resize();
    });

Is there the same error if you use some absolute URL of GLB model (to check)?
Or, probably it should be not "public/" but "/"?

Yes, the same error occurred with absolute path.

Check if @babylonjs/core and @babylonjs/loaders are of the same Babylon version.

1 Like

Thanks. The core package was 7.19.1 while loaders is 7.20.1. Updated it.

1 Like