Bring the Crane in from outer space?

@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:

Right, like, Mesh.resizeTo(Vector3 size) method? Yes that could be handy, would make a nice contribution I guess. Shall we file a RFE then? :slight_smile:

We do have a normalizeToUnitCube function on the transformNode IIRC :slight_smile:

1 Like

Yep doing

mesh.normalizeToUnitCube();
mesh.scaling.scaleInPlace(size);

already does what you want.

1 Like

No, it doesn’t. It does only half of it. It doesn’t center the axis.

Here is how the babylon viewer does it:

if (unitSize) {
                meshesToNormalize.forEach((mesh) => {
                    mesh.normalizeToUnitCube(true);
                    mesh.computeWorldMatrix(true);
                });
            }
            if (center) {
                meshesToNormalize.forEach((mesh) => {
                    const boundingInfo = mesh.getHierarchyBoundingVectors(true);
                    const sizeVec = boundingInfo.max.subtract(boundingInfo.min);
                    const halfSizeVec = sizeVec.scale(0.5);
                    const center = boundingInfo.min.add(halfSizeVec);
                    mesh.position = center.scale(-1);

                    mesh.position.y += halfSizeVec.y;

                    // Recompute Info.
                    mesh.computeWorldMatrix(true);
                });
            }

I am not sure the positioning is worse an helper tbh ?

1 Like

Oh, thanks and my apologies :pray:.
Looks like this is exactly the part I was missing. See, I don’t use the viewer.
I knew nothing about getHierarchyBoundingVectors, which seems to be precisly the part I needed. I’m just happy to see that chatGTP also doesn’t know about it… if I account the clumsy solution it has pulled out relying on merging meshes to retrieve the bounding box :grin: I’m gonna try that shortly and meanwhile, have a great day :sunglasses:

OK, I can hereby now confirm that this solution works (and is actually ez once you know about it).
However, from my readings in this forum and the look at the PG examples from the API when searching for ‘getHierarchyBoundingVectors’, it seems to me like I’m not the only small brain :brain: :sweat_smile: knowing the *** of what he’s doing :dizzy_face: :wink: May be the above would deserve a couple of lines in the doc and a small PG (for those not using the viewer). Just a simple suggestion. I mean I’m good now :smiley: so again, thanks a lot for that :hugs:
Meanwhile, have a great sunday :sunglasses: :sunglasses:

Edit: @bigrig Following the above, I quickly added this to your PG. Thus “Bringing the crane in from outer space” without the need of launching a rocket :grin: nor editing or merging!?! the asset (this one’s for you chatGPT) :sweat_smile:

I’m gonna take this opportunity to share my anger about the faen designers posting assets (even for free) not respecting the rules of modelling for dummies course 1.0. The solution above works well for scaling and positioning but there’s this one thing it will never do, and this is ‘rotation’. EZ enough if coming from a right-handed system you can invert the scaling or rotate. BUT THEN, when the faen designer sets the axis neither facing forward nor backward but randomly rotated facing a direction that doesn’t make any sense, then you’re doomed. Nothing left but to edit the fancy thing or work it one by one on import. I mean I won’t shout after the poor guy creating a model as a hobby and posting it for free. But when an institution like the British Museum of Art releases 300 (beautiful) scans of great works of art and artefacts and the fancy designer doesn’t even take 10s. to set the axis correctly, it just makes me mad :grimacing: :unamused: That’s it for this subject I suppose. I said what I had to.
All, have a great day :sunglasses:

2 Likes

I believe the crane has officially been recovered, due to a concerted effort by the babylon community. NASA and all of the global governments thank you.

1 Like