Bring the Crane in from outer space?

Hi all - im importing a variety of 3rd party GLBs where i don’t necessarily have control of the creation of the file. Many have their assets way out in space. Like this crane - which is way outside of my skybox even though i’m importing its container to 0,0,0:

Any idea how to programmatically reset the GLB to center of world? PG here with some sample assets that you can change at top. It’s currently loading the crane shown above, which you’ll need to zoom out a ton to see.

When you have a lot of such assets it is better to prepare them before uploading. I am using GLTF-Transform for this purpose, which helps to remove errors and optimize GLB asset (up to 10 times size reduction for some Sketchfab models). There is the function center, which does exactly what you need.

2 Likes

awesome. will give center a try. thx so much.

That worked like a charm:

(looks like this particular model is a beast for other reasons … running at 11fps - but center function is great! nice one.).

2 Likes

So, you did it only on the crane so far? How many do you have? I’m having a similar issue but seriously I don’t really feel like ‘preparing’ all of these fancy assets. I’m wondering if there would be some other solution to position them. In particular, I’m wondering if I could find a way to get all submeshes bouding box and from there get a size of the total and reposition it by parenting it to a transformNode (with half the offset of the total of boudingBox size(s)? Or any other method that would not require a ‘prepare’?

I believe it is better to improve all your problem assets just with several clicks with the help of GLTF-Transform CLI before using them with Babylon (also because of the size reduction).

when importing GLB/GLTF models, it’s typical to see the model wrapped in a set of nodes that are usually not needed. Edit: looks like you’re unwrapping it fine, but there’s so many duplicates… look at the transforms on the root node, there’s a 180u Y axis xform that’s looks like the positioning problem you’re having?

I think I noticed in your PG’s that you seem to be using the “root node” to position. I also noticed that it appears there are multiple copies of the model being loaded (in the inspector, look at all the root node instances…) is that intended?

HTH

Part of prepping your assets for the scene is getting them all to the proper scale and into the same world space coordinates. Once you have prepared the way by normalizing and making consistent the coordinates and transformations for your models, actually positioning them in the scene becomes much easier.

If you’re in control of all the assets this is much easier, of course. If not though, you’ll have to spend the time to process those assets to properly pre-transform into the right scale and space. The CLI tool @labris mentions is one way to do that, or possibly one part of an asset creation and ingest pipeline if you want to look at a macro- view. The Sandbox and PG are great as ya’all already know for parts of this process too.

Your idea @mawa could potentially be another step in that pipeline, or an alternative; it all depends on your needs :grin:

1 Like

@mawa you can run transform across all the assets in bulk with a shell script. Here’s that script - with a few of things ive run commented out. Just drop assets in a “source” dir and run the script - and they’ll end up in “output”. I’m really loving this transform library. Im thinking that when someone uploads an asset, i’ll run it through a pipeline using this library to prep it as best i can as you suggest @jelster


for file in source/*.glb
do
    filename="${file##*/}"
    #gltf-transform optimize "$file" "output/$filename" --join false --flatten false --simplify 0.0
    #gltf-transform optimize "$file" "output/$filename"
    #gltf-transform optimize "$file" "output/$filename" --texture-compress webp
    #gltf-transform resize "$file" "output/$filename" --width 1024 --height 1024
    gltf-transform center "$file" "output/$filename"
    
done

@jelster regarding the duplicates - yea, i am loading and duplicating - to make sure that duplicate works without messing up the model (which is what’s happening in my app… this was the initial purpose of my PG actually - to debug this duplication case). Re: transforms - yea, this particular asset creator has got a bunch of odd stuff on his models. His source editor is Cinema4d, so maybe something in the process to FBX and then GLB is resulting in weird stuff. When I open his assets in Blender they dont look good. All those transforms have mal-impact. Funny that they look fine in Babylon.

3 Likes

Oh. Yea, which node should i be using to position? ^?

Maybe i’m using the root node incorrectly in general? Maybe that’s what’s causing this odd gizmo behavior too? That gizmo correctly modifies that sword… but it’s all askew after initial rotation. The gizmo doesn’t follow the meshes around.

Well, thank you. I guess I will have to give it a try. I was just hoping it would not be needed. To be honest, there are some things I do not fully understand. Like why can I normalize my asset to a unitcube and scaleto but cannot easily reposition the pivot? Either I’m missing something or something is missing?!

As exposed above, I would rather have it ‘an alternative’ than ‘an extension’. But that’s just ‘lazy me’ :sleeping: I suppose :grin: Thanks for your time anyways and have a great day :sunglasses:

How about this solution by chatGPT?

Yes, you can programmatically reset the GLB to the center of the world by using the Mesh class provided by Babylon.js.

BABYLON.SceneLoader.ImportMesh("", "path/to/your/glb/file.glb", "", scene, function (newMeshes) {
  // Get the bounding box of all the meshes in the imported model
  var boundingBox = BABYLON.Mesh.MergeMeshes(newMeshes).getBoundingInfo().boundingBox;

  // Calculate the center point of the bounding box
  var center = boundingBox.center;

  // Iterate over all the meshes and set their position relative to the center point
  newMeshes.forEach(function(mesh) {
    mesh.position = mesh.position.subtract(center);
  });
});
1 Like

While not a terrible solution, it may have a potentially serious drawback of introducing side effects; you will want to check that MergeMeshes doesn’t mutate the original meshes for this to work properly

1 Like

Something to check is that you’re exporting into a Left handed coordinate system, which is what BJS uses, but which some other tools and engines do not. It looks like the rotations are off by 90deg, which is why I mention it!

Great discussion here, but I had to post just to point out that the title is the best ever:
image

:sweat_smile:

Me: He told me “Bring the Crane in from outer space” …

GPT3: I would need to find a way to get a crane from outer space. This could involve launching a rocket with the crane inside, or finding an existing crane in space and somehow retrieving it.

I’m glad the problem here is easier than that :rofl: I bet aerospace engineers would love to just be able to set crane.position = new Vector3(0,0,0) :stuck_out_tongue: (now that I think about it, would (0,0,0) be the center of the earth or its surface? :thinking: )

I made world editor that imports any of 300,000 sketcfab models into the scene. First thing I do is resize the model to fit into 1x1x1 box, and place it 2 units in front of the view. That works only once the model is rendered (although calling computeWorldMatrix(true) on root mesh might or might not work).

All published under Apache2 license, so feel free to reuse :wink:
The code you may be interested in is take method (positioning): vrspace/world-editor.js at master · jalmasi/vrspace · GitHub
and bBox methods (rescaling): vrspace/world-manager.js at master · jalmasi/vrspace · GitHub

3 Likes

Thanks for sharing. Mostly appreciated, though I still think that it’s a lot of code and trouble to achieve something that should be simple. I mean I start the same as you, normalizing to unit cube and compute to world matrix… fine. But from there, it turns into a factory of positioning and scaling and I simply do not quite understand why we need to have this and why someone like you has to go through all this code to achieve something that should be (in my opinion) available ‘out of the box’. May be it’s just me, I don’t know. Luckely, I have only about 50 models and not 300k to do :sweat_smile: