In the import category of PGs and in the PG examples of the documentation, all the sceneloader is marking as Deprecated. I was wondering if it would not be useful to update with the new practice to be used rather than having official examples with noted functions not recommended. If they are not recommended, it should not be in the examples official.
For exemples this PGs :
And
I was looking for new practices quickly, and I only found Deprecated functions. So I told myself that it was necessary to submit the ideas of updating all the PG of the documentation.
First of all, I would like to state that I do not have much knowledge on this subject, but since I also received a similar error, I would like to state that I actually agree with you. Because I also have difficulty finding information text after version changes or updates.
Everything in Sceneloader has become Deprecated. What should be used now to take into account new ways of doing things ? I did not understand and I can’t find any information on it, or an example.
Thank you @Deltakosh for clarification, but how do I replace SceneLoader.ImportMeshAsync ?
Here is my use case. sceneloader.Importmeshasync is super simple: you load a mesh, it appears in the scene.
I charge all the meshes one by one in a loop using : for (const data of sceneData)SceneLoader.ImportMeshAsync
But with BABYLON.LoadAssetContainerAsync or BABYLON.AppendSceneAsync this does not work for my use case. Or I didn’t understand like doing it.
Here is what I do. I load my scene from a JSON file which contains the paths of models and metadata to rebuild it. :
async loadScene(projectName, levelName) {
try {
const response = await $.ajax({
url: "../Core/PHP/load_scene.php",
method: "GET",
data: {
"project": projectName,
"filePath": '../../_Projects/' + projectName + '/Data/scene_' + levelName + '.json'
}
});
const sceneData = JSON.parse(response);
if (utilities.isObjectEmpty(sceneData)) return;
for (const data of sceneData) {
const filePath = utilities.getPath(data.source);
const fileName = utilities.getFile(data.source);
const result = await BABYLON.SceneLoader.ImportMeshAsync("", filePath, fileName, this.scene);
const mesh = result.meshes[0];
//... Other code ...
}
} catch (error) {
console.error("Error when loading the scene : ", error);
}
}
LoadAssetContainerAsync loads all the meshes, but does not load the meshes one by one and AppendSceneAsync does the same thing A can ready. Why is there not BABYLON.ImportMeshAsync Isn’t it also available to load a model that is added to the scene as simply as possible?
I tried to do like that. But it generates an error:
RuntimeError: Unable to load from …/GameClient/Data/Meshes/Props/Barel.babylon: OK
for (const data of sceneData) {
const result = await BABYLON.LoadAssetContainerAsync(data.source, this.scene);
result.addToScene();
const mesh = result.meshes[0];
}
The path of the file exists, but it does not seem to want this load.
If I do it with:
for (const data of sceneData) {
const filePath = utilities.getPath(data.source);
const fileName = utilities.getFile(data.source);
const result = await BABYLON.SceneLoader.ImportMeshAsync("", filePath, fileName, this.scene);
const mesh = result.meshes[0];
}
There it works.
And I can’t find BABYLON.ImportMeshAsync() Neither in the doc nor in the PG.
Also this unique example PG with LoadassetContaineSync is in typescript and quite complicated to understand.
As far as I understand, addAllToScene allows you to add all entities from your imported scene. On the other hand, addToScene allows you to specify a filter function (predicate) to select only the specific entities that satisfy your predicate. Take a look at the following example, it adds all entities except the robot’s body (entity.name !== 'Box'):
Can this playground be useful? Basically is the same playground you quoted but with a single mesh (skull) imported with the asset container.
@Deltakosh it seems that ImportMeshAsync isn’t exported from sceneLoader.ts:
Unlike other exported functions, it doesn’t belong to a PascalCase wrapper. Could it be missing a wrapper and the corresponding export?
Thank you @SimoneDev
I think I will wait for the availability of importMeshAsync which would be more appropriate for my case. Because I load all the models one by one in a loop and an assetContainer or appendScene does not work for my use case.
Can you load your scene/mesh (…/GameClient/Data/Meshes/Props/Barel.babylon) in the playground without loop? Or do you have some errors? For instance swapping the resource url here:
Thank you for sharing! From the snippet, I can’t figure out the reason why and the only difference i see is the usage of filePath and fileName rather than data.source, so i thought that:
const result = await BABYLON.LoadAssetContainerAsync(filePath+fileName, this.scene);
would work.
Anyway, it intrigued me because, most of the time, I like to load assets from asset container, do some work (transformations, rotations, etc.) and only at the end show them. This helps me avoid glitches that might occur with ImportMeshAsync that shows the asset as soon is loaded. But… the beauty of Babylon.js is that it allows you to do so many things in so many different ways, so keep pushing!
When we switched to the module level functions, I tried to update all the docs and Playground examples, but I may have missed some. I will take a look through this thread and make sure we get the other ones updated.
Also when we switched, we didn’t expose an ImportMeshAsync module level function because the thought was that LoadAssetContainerAsync could do the same thing plus more, so it was redundant to add ImportMeshAsync. That said, we’ve heard feedback on this a few times now, so I will go ahead and add this one into the mix as well.
For the issue mentioned above with LoadAssetContainerAsync, I will take a look at that as well this week and circle back!
Thank you @ryantrem to reconsider the ImportMeshAsync and to revise the LoadAssetContainerAsync.
@SimoneDev In fact data.Source in my JSON file, is the full path of the model.
And filePath+fileName is the same thing but by separating the file and the for the importMesh with :
const result = await BABYLON.LoadAssetContainerAsync(filePath+fileName, this.scene);
//Or
const result = await BABYLON.LoadAssetContainerAsync(data.source, this.scene);
It generates the same error. But maybe this error comes from after. That is to say when I do: Const mesh = result.meshes [0]; I do not know how a assetContainer works, but from what you describe to load the models at the end and to do all the transformation before is interesting too.
@ryantrem No I have no playground. I develop my project locally.
But I will try to see if I can’t create one otherwise. I would try to do that tomorrow in the day hoping to reproduce the problem.
I have a PR out to add ImportMeshAsync as a module level function (and update doc comments to further help lead folks to the module level functions). That said, from everything you are describing, LoadAssetConatinerAsync should work just fine, so I’m definitely curious to see a Playground with a repro!
Also a second PR to update the docs and point to updated PGs for the ones you found that use the legacy SceneLoader.