Jump to content

Loading assets directly from passed files


Recommended Posts

I was working with loading external meshes/scenes through assetmanager or sceneloader methods of babylonjs.

I wanted to ask if there is a feature also to load meshes directly from preloaded files, like currently i need to provide a local URL of the file (stored on my machine) and file name. But what if a user uploads a glb file and I want to just pass the file directly to babylon instead of storing it at a local storage and passing the location.



Link to comment
Share on other sites

Hi Nikita,

EDIT: I was wrong! See below. BJS does indeed provide an easier method.

This should be quite doable (I believe the Playground performs a similar trick.) but Babylon doesn't provide a method by itself.

Mine the code in these examples to upload the file to browser memory. You may then need to call window.URL.createObjectURL() on the uploaded file to attach it (invisibly) to the DOM first. After that, you can pass the object URL the browser generates to the AssetManager as the rootUrl for Append() or AppendAsync() -- the sceneFilename property can be ignored.

Sorry I'm not at a machine I can test this on, but I think the above will work. Let me know!

Edited by SpaceToast
Link to comment
Share on other sites

I think I should have directly explained what my project is about, apologies.

I am creating a tool in typescript, where user can upload multiple mesh files and the tool will download a csv which contains a matrix for number of indices, vertices, sub meshes  etc (later will be extended).

I am initializing a null engine for this. the html input element will pass on the updated files. Then it will load mesh files one by one and calculate the complexity parameters of each and fill the matrix and download the csv.


So according to the above answers, 

I used URL.createObjectURL(file) with SceneLoader.ImportMesh and also with assetManager, but browser throws: Unable to find a plugin to load  files. Trying to use .babylon default plugin.

but when I use same techniques with local url (folder path eg. "./assets/"), it works fine.


Here is what I did for local files:

let meshTask: BABYLON.MeshAssetTask = this.assetsManager.addMeshTask(meshFile.name + "MeshTask", "", "./assets/", meshFile.name);
        meshTask.onSuccess = (task) => {
            task.loadedMeshes.forEach((loadedMesh) => {
                vertices += loadedMesh.getTotalVertices();
                indices += loadedMesh.getIndices().length;
            meshInfoT = new MeshInfo(meshFile.name, vertices, indices, subMeshes);
        meshTask.onError = (_, message, exception) => {
            console.error("Error loading mesh " + meshFile.name + "\nMessage: " + message + "\nException: " + exception);


Do suggest me if there are any other techniques where I can get mesh information for multiple files without actually loading them on scene.



Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...