I’m having trouble loading local images in Babylon GUI when using a Babylon.js Editor project with Vite.
I’ve tried both approaches:
Adding an image through the Babylon.js Editor GUI Editor.
Creating a BABYLON.GUI.Image directly from code.
For example:
const image = new BABYLON.GUI.Image(
"myImage",
"assets/2d/android.png"
);
advancedTexture.addControl(image);
The image does not appear. I also tried different relative paths and checked that the image file exists in the project.
Interestingly, I can see that the image is being requested/loaded in some cases, but it still doesn’t render correctly. At one point I only got a blue rectangle instead of the image.
I’m using a Babylon.js Editor project with Vite and Babylon.js 9.12.1.
Thanks a lot for looking into this and for creating the issue! I really appreciate it.
One quick question: when the new release is available, will the Babylon.js Editor update automatically, or will I need to download/install the new version manually?
It’s normal!
Nothing special, you’ll just need to launch the editor and a notification on your computer will saying you have to restart it to apply the update
I’ll let you know once I deployed it
Relative paths are now working relatively to your project’s absolute path so you could type assets/amiga.jpg in the field for the source of the image in the GUI Editor and it’ll be converted to an absolute path in the field. Don’t worry when saving the path still remains relative and everything will work as expected when you’ll deploy it on a server and nothing absolute is saved.
I’m deploying today or tomorrow (I’m adding a last fix for the release)
I now get the following error when running the project:
Error while playing the scene:
Cannot read properties of null (reading 'getBoundingInfo')
[ERROR] TypeError: Cannot read properties of null (reading 'getBoundingInfo')
I believe this error may be related to something else, since the object calling getBoundingInfo() is a Mesh and everything was working correctly before the update.
I’m also having an issue with a GUI Image:
The image is visible correctly in the GUI Editor.
However, when I click Play in the Editor, the image is not visible.
The image path is:
assets/amiga.jpg
I also noticed another error when running the project with npm run dev. In the browser console I get:
Failed to load resource: the server responded with a status of 400 (Bad Request)
Thanks a lot for your help on this. This is so sad because I tried my game directly in Editor which uses almost ALL features and everything worked fine.
Do you have a Github repository? If you are ok, that would help me a lot to help you/me debugging
Thanks! I don’t have a Github repository for this project, and it would be difficult to share the whole game.
However, I can compress the project and send you a ZIP if that helps.
I also found a very simple way to reproduce the @nodeFromScene issue:
Create a Mesh, for example a Box.
Create another scene with another Box.
Add a script to the second scene using @nodeFromScene.
Put/instantiate that scene inside the first scene.
Play the scene directly in the Editor.
When running it directly in the Editor, the log for the @nodeFromScene property is empty/null.
However, when I run the same project with npm run dev, @nodeFromScene correctly finds the Mesh and the log contains the Mesh object.
As a temporary workaround, I solved the issue in my game by storing the player’s hitbox reference directly in my GameManager instead of relying on @nodeFromScene.
There is also another issue with the GUI. I have a GUI asset with an Image called logo. The image is visible correctly in the GUI Editor, but when I play the scene directly in the Editor, it is not visible.
This is the code I use to load the GUI and set the image:
@visibleAsAsset("gui", "My GUI asset")
private _gui!: AdvancedDynamicTexture;
this._texture = AdvancedDynamicTexture.CreateFullscreenUI("hud");
this._texture.parseContent(this._gui.content, true);
const logo = this._texture.getControlByName("logo") as Image;
if (logo) {
logo.isPointerBlocker = false;
}
The image is visible in the GUI Editor, but not when playing the scene.
I can send you a ZIP with the minimal reproduction case if that would be useful.
For the GUI part I just found the problem and it was here since the beginning, I added import "babylonjs-editor-tools/loading/script/preload/plugins/gui"; in my script file this is why it was working on my side
I’m releasing the fix ASAP if you can send me in private a zip of your project that would help me a lot so I can fix all bugs and deploy right after (today or tomorrow) for a v5.5.2
As for the GUI, it also works in this new version and images can now be previewed. It’s also worth noting that now, to display a GUI created with the editor, all you need is
@visibleAsAsset("gui", "My GUI asset")
private _gui!: AdvancedDynamicTexture;
and drag the file into the properties.
Regarding the player hit issue, I want to add that I was confused — @nodeFromScene actually searched within the same scene before, which was my mistake for assuming it searched globally. But @julien-moreau added this very useful new feature.