Error loading player: Unable to load from

Hi! I am new to the Babylon Dev scene and I am working on my first Babylon Game, a warship game. I got the world and stuff working, but I got problems during implemention of the player model.

I getting following error while trying to load the player model:
404 (Not Found) Error loading player: Unable to load from …/Resources/models/yacht.glb. Not Found.

What could be the problem??

My Code:
player.ts:

import { Mesh, Scene, Vector3, SceneLoader, AssetContainer, } from “@babylonjs/core”;
import { GameManager } from “…/Framework/Core/GameManager”;
import { GLTFFileLoader } from ‘@babylonjs/loaders’;

// Register the GLTF file loader
SceneLoader.RegisterPlugin(new GLTFFileLoader());

// …
export class Player {
private _mesh: Mesh;
private _scene: Scene;
private _targetPosition: Vector3;

constructor(scene: Scene) {
this._scene = scene;
this._targetPosition = Vector3.Zero();

}

public async load(): Promise {
return new Promise((resolve, reject) => {
const playerModelUrl = “…/​Resources/​models/​yacht.glb”;

  SceneLoader.LoadAssetContainer(
    "",
    playerModelUrl,
    this._scene,
    (container: AssetContainer) => {
      if (container.meshes.length > 0) {
        // Cast the first mesh to a Mesh object
        this._mesh = container.meshes[0] as Mesh;
        resolve();
      } else {
        reject(new Error('No meshes found in the player model'));
      }
    },
    null, 
    (message, exception) => {
      // Handle errors during loading
      reject(exception);
    }
  );
});

}

public update(deltaTime: number): void {
if (!this._mesh) {
return;
}

// Move player towards target position
const direction = this._targetPosition.subtract(this._mesh.position).normalize();
const speed = 1; // Adjust this as needed
this._mesh.position.addInPlace(direction.scale(deltaTime * speed));

}

public setTargetPosition(position: Vector3): void {
this._targetPosition = position;
}

public getMesh(): Mesh {
return this._mesh;
}

}

Hello and welcome to the Babylon community!

You should check if the file is in the correct directory and you can access it :slight_smile:

I have checked it severel times, the file directory is correct!

Are you serving the file properly through? With a web server?

With a local web server, I have also attached a screenshot of the file directory :slight_smile:

If you can share your full project (as a zip or github) it will be easier to see what’s going on.

Check “Reccources

It should be “Resources

Hey, unfornetly this was just a typo from me making this blog post, in the code base it is already:

const playerModelUrl = “…/​Resources/​models/​yacht.glb”;

It’s definitely a problem of not finding the glb file.

Maybe it´s dumb anwser and I think you´ve tried it, just adding one point:

const playerModelUrl = “./​Resources/​models/​yacht.glb”;

Also try accessing the full path of your project

No, there are no dumb answer! :smiley: Indeed, I have already tried both, but it still didnt work.

Did you tried loading other GLB files?
Just to know if the Loader is working.

— EDIT —

Also check the importing line from AssetContainer:

BABYLON.SceneLoader.LoadAssetContainer("./", "duck.gltf", scene, function (container) {
  const meshes = container.meshes;
  const materials = container.materials;

  container.addAllToScene();
});

Try setting first your relative or absolute location and after the “file.glb”

It should be something like this:

SceneLoader.LoadAssetContainer(
    "./Resoucres/models/",
    "yatch.glb",
    this._scene,
....

Hey, surethink! Here is the Link: **removed

Please read the readme file for setup instructions :slight_smile:

1 Like

Yes, I have tried other .glb files, there are also not working :confused: .But viewing them through the Babylon Asset Viewer works… Here is my now updatet sceneloader, but the problem still occurs:

public async load(): Promise {
return new Promise((resolve, reject) => {
// const playerModelUrl = “src/​Resources/​models/​yacht.glb”;

  SceneLoader.LoadAssetContainer(
    "./Resoucres/models/",
     "yacht.glb",
   // playerModelUrl,
    this._scene,
    (container: AssetContainer) => {
      const meshes = container.meshes;
      const materials = container.materials;
    
      container.addAllToScene();
      if (container.meshes.length > 0) {
        // Cast the first mesh to a Mesh object
        this._mesh = container.meshes[0] as Mesh;
        resolve();
      } else {
        reject(new Error('No meshes found in the player model'));
      }
    },
    null, 
    (message, exception) => {
      // Handle errors during loading
      reject(exception);
    }
  );
});

}

Ok! Following your structure if you´re using the same as the documentation, it should be like this:

import vehicleModelUrl from ‘…/…/Resources/models/yatch.glb’;

return new Promise((resolve, reject) => {
SceneLoader.LoadAssetContainer(
‘’,
vehicleModelAsset,
this.scene,
(container: AssetContainer) => {
// Do something with it
}
);
});

Also in the previous post i wrote bad “Resources” again…
SceneLoader.LoadAssetContainer(
“./Resources/models/”,
“yatch.glb”,
this._scene,

It should be a problem accesing your file, so first check the main structure of your app and check from where you´re loading your file

Hi, thanks for the update! I have followed your instructions and updated my player.ts, now it looks like:

//imports
import { Mesh, Scene, Vector3, SceneLoader, AssetContainer, } from “@babylonjs/core”;

import { GameManager } from “…/Framework/Core/GameManager”;

import { GLTFFileLoader } from ‘@babylonjs/loaders’;

import { vehicleModelUrl } from “…/Resources/models/yacht.glb”;

//previous code

public async load(): Promise {
return new Promise((resolve, reject) => {
// const playerModelUrl = “src/​Resources/​models/​yacht.glb”;

  SceneLoader.LoadAssetContainer(
    "./Resoucres/models/",
    // "yacht.glb",
    //playerModelUrl,
    vehicleModelUrl,
    this._scene,
    (container: AssetContainer) => {
      const meshes = container.meshes;
      const materials = container.materials;
    
      container.addAllToScene();
      if (container.meshes.length > 0) {
        // Cast the first mesh to a Mesh object
        this._mesh = container.meshes[0] as Mesh;
        resolve();
      } else {
        reject(new Error('No meshes found in the player model'));
      }
    },
    null, 
    (message, exception) => {
      // Handle errors during loading
      reject(exception);
    }
  );
});

}

//following code

Now I get following compiling error:

index.js:493 [webpack-dev-server] ERROR in ./src/Resources/models/yacht.glb
Module build failed (from ./node_modules/url-loader/dist/cjs.js):
Error: Cannot find module ‘file-loader’
Require stack:

  • D:\DEV\WARSHIPS_BABYLON_ALPHA\Babylon_Warships\node_modules\url-loader\dist\index.js
  • D:\DEV\WARSHIPS_BABYLON_ALPHA\Babylon_Warships\node_modules\url-loader\dist\cjs.js
  • D:\DEV\WARSHIPS_BABYLON_ALPHA\Babylon_Warships\node_modules\loader-runner\lib\loadLoader.js
  • D:\DEV\WARSHIPS_BABYLON_ALPHA\Babylon_Warships\node_modules\loader-runner\lib\LoaderRunner.js
  • D:\DEV\WARSHIPS_BABYLON_ALPHA\Babylon_Warships\node_modules\webpack\lib\NormalModuleFactory.js
  • D:\DEV\WARSHIPS_BABYLON_ALPHA\Babylon_Warships\node_modules\webpack\lib\Compiler.js
  • D:\DEV\WARSHIPS_BABYLON_ALPHA\Babylon_Warships\node_modules\webpack\lib\webpack.js
  • D:\DEV\WARSHIPS_BABYLON_ALPHA\Babylon_Warships\node_modules\webpack\lib\index.js
  • D:\DEV\WARSHIPS_BABYLON_ALPHA\Babylon_Warships\node_modules\webpack-cli\lib\webpack-cli.js
  • D:\DEV\WARSHIPS_BABYLON_ALPHA\Babylon_Warships\node_modules\webpack-cli\lib\bootstrap.js
  • D:\DEV\WARSHIPS_BABYLON_ALPHA\Babylon_Warships\node_modules\webpack-cli\bin\cli.js
  • D:\DEV\WARSHIPS_BABYLON_ALPHA\Babylon_Warships\node_modules\webpack\bin\webpack.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
    at Module._load (node:internal/modules/cjs/loader:920:27)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.loader (D:\DEV\WARSHIPS_BABYLON_ALPHA\Babylon_Warships\node_modules\url-loader\dist\index.js:120:20)

Do you got any hints for me? :slight_smile:

I also installed the missing file-loader with:

npm install --save-dev file-loader

and added following rule to my webpack.common.js:

module: {
rules: [
// … other rules …

{
  test: /\.(glb)$/,
  use: [
    'file-loader'
  ]
}

]
}

But unfornetly I getting the same probleem like before:

WARNING in ./src/Game/Player.ts 28:16-31

export ‘vehicleModelUrl’ (imported as ‘vehicleModelUrl’) was not found in ‘…/Resources/models/yacht.glb’ (possible exports: default)

Regarding the player model not appearing, here is a sum up of all what I had try so far without success:

  1. The model file might not be found: Check the path to your model file and ensure it’s correct and accessible from the script.
  2. The model might be too large or too small: If the scale of the model is too extreme, it might appear as though it’s not being loaded. Try scaling the model to see if it becomes visible.
  3. The model might be loading in the wrong position: If the model is being loaded outside the view of the camera, it will not be visible. Try setting a position for the model after it’s loaded.
  4. The model might not have a mesh: If the model file doesn’t contain a mesh, or if the mesh is not the first item in the meshes array, the code might be failing to assign anything to _mesh. You can try logging the contents of task.loadedMeshes to see what’s being loaded.
  1. The model might not have a mesh: If the model file doesn’t contain a mesh, or if the mesh is not the first item in the meshes array, the code might be failing to assign anything to _mesh. You can try logging the contents of task.loadedMeshes to see what’s being loaded.

Could you share your model?
I didn´t know this Babylon Boilerplate but it sounds for some reason your model is not loading for hierarchy reasons.

Or for other reason, it is not detecting the Babylon Loaders.
I don’t know if it would be viable in this case, but have you tried to implement the Babylon-Loader?

Yes, I have already implemented the Babylon_Loader like following:

//other imports
import { GLTFFileLoader } from ‘@babylonjs/loaders’;

38.4 Mb!
Wow! This model is huge.

  1. The model might be too large or too small: If the scale of the model is too extreme, it might appear as though it’s not being loaded. Try scaling the model to see if it becomes visible.

Maybe the first thing is try to optimize the mehes, also try to use Draco or MeshOpt Compression, you can use a simple online tool like.

Also try with another lighter model

Although at the moment I could not tell you if it is a problem with the model or with library references

1 Like

Hey everone! :smiley:
I have solved the problem in the following way:

Instead of serving the models directory using the webpack dev server, I have copy the models directory to the build directory using the copy-webpack-plugin. This way, the models will be available in the same directory structure in my build output.

First, I had to need to install the copy-webpack-plugin with:

npm install copy-webpack-plugin --save-dev

or

npm install --save-dev copy-webpack-plugin@latest

Then, I add it to mywebpack.common.js configuration:

const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
  // ...
  plugins: [
    new CopyPlugin({
      patterns: [
        { from: 'src/Resources/models', to: 'Resources/models' },
      ],
    }),
  ],
  // ...
}

This configuration tells webpack to copy the contents of src/Resources/models to Resources/models in my build directory.

Once I’ve done this, I had restart my webpack server and try loading the model again.

Note: Make sure your path to the model is correct when you load it in your application. If your build output directory is set as dist, then the model should be available at /Resources/models/yacht.glb after the build.

2 Likes