Can't center STL on Origin, Possible bug

I can’t seem to get a loaded STL’s position set to the origin. I have tried 2 different ways in my code.

   meshFat.position = new BABYLON.Vector3(0, 0, 0);
   meshFat.setAbsolutePosition(BABYLON.Vector3.Zero());

Is this a bug or my misunderstanding of local and/or global coordinates?

import * as BABYLON from 'babylonjs';
import 'babylonjs-loaders'

export default class Renderer {
    private _canvas: HTMLCanvasElement;
    private _engine: BABYLON.Engine;
    private _scene: BABYLON.Scene;

    createScene(canvas: HTMLCanvasElement, engine: BABYLON.Engine) {
        this._canvas = canvas;

        this._engine = engine;

        // This creates a basic Babylon Scene object (non-mesh)
        const scene = new BABYLON.Scene(engine);
        this._scene = scene;

        var camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI/2, 0.7, 350, new BABYLON.Vector3(14,14,14), scene);       camera.attachControl(canvas, true);

        // This creates a light, aiming 0,1,0 - to the sky (non-mesh)
        const light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);

        // Default intensity is 1. Let's dim the light a small amount
        light.intensity = 0.7;

        var loader = new BABYLON.AssetsManager(scene);
        console.log("loader created");

        
        var head2 = loader.addMeshTask("head2", "", "https://github.com/Borillion/misc/raw/main/", "head.stl");
        var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 64, segments: 32}, scene);        
        sphere.showBoundingBox = true;

        head2.onSuccess = function() {
            var meshThin;
            var meshFat;
        
                meshFat = head2.loadedMeshes[0];
                meshFat.showBoundingBox = true;
                meshFat.position = new BABYLON.Vector3(0, 0, 0);
                meshFat.setAbsolutePosition(BABYLON.Vector3.Zero());
                console.log(meshFat.absolutePosition);
            }


            loader.load();
    }


    initialize(canvas: HTMLCanvasElement) {
        const engine = new BABYLON.Engine(canvas, true);
        this.createScene(canvas, engine);

        engine.runRenderLoop(() => {
            this._scene.render();
        });

        window.addEventListener('resize', function () {
            engine.resize();
        });
    }
}

const renderer = new Renderer();
renderer.initialize(document.getElementById('render-canvas') as HTMLCanvasElement);

Hi @Bob_R
Your camera targets (14, 14, 14),
could this be the cause?

I can’t quite make out what’s wrong based on the screenshot.

I think your pivot(center of transformations) is not in center of model. or really your camera target has offset And you dont look in center of scene? Try get coordinates after load mesh and if you get 0,0,0 try place in center of mesh pivot in your DCC program. Or create playground with your problem

A playground won’t work, doesn’t seem to have support for the plugin to load the stl. I will try to get the pivot on my system and draw a little box there.

Both the sphere and the head report as having coordinates of (0,0,0) but that isn’t congruent with appears in the output displayed.

But you’re creating your camera with a (14,14,14) target :slight_smile:

new BABYLON.ArcRotateCamera(“Camera”, -Math.PI/2, 0.7, 350, new BABYLON.Vector3(14,14,14), scene);

Edit
Just realised it’s the “head” mesh in the top-right corner of your screenshot… i thought it looked like another sphere, thus i was confused haha

Can you share your file?

yup, doesn’t seem to make a difference. See here. I this case I set the sphere to be at Vector3(0,0,0) as well as the camera direction vector. It makes no difference at all!


** LOL no its like a robot skull , GitHub has a viewer here. misc/head.stl at main · Borillion/misc · GitHub

The file is here. https://github.com/Borillion/misc/raw/main/head.stl

@Bob_R
Okay, so this won’t load in PG as main github.com domain doesn’t allow CORS
i’ll take a look at the model

Edit
and here’s the issue, not centered

Edit 2
You can try this;

headMesh.position.subtractInPlace(headMesh.getBoundingInfo().boundingBox.centerWorld);

And if you need it baked (mesh.position vector3 showing 0,0,0)
this should do the trick

headMesh.bakeCurrentTransformIntoVertices()

1 Like

So I am not entirely sure of what I am looking at. Do you mean that the file itself has offsets built into it? Also out of curiosity what tool did you use to generate the image?

the head mesh appears to be baked or pivoted at an offset, yes
0 mm at lower left corner of screenshot is the world center

it is from default windows10 3D app

Edit
Indeed it is baked at an offset.

I went to fix the mesh and export from babylon to stl,
but getting odd results from STLExporter,

@Pryme8 didn’t you make the exporter? :slight_smile:

I’m getting 3x size, (position data duplicated x 3) verts & indices both 642702 -> 1928106
model rotated on 2 axis,
and faces appear flipped… any thoughts?

Interesting thanks, I obviously never would have thought of that. Someone made it for me in Blender based off a file I provided them, have to ask if they can give me the original blender file.

** I use linux and macos primarily so I don’t have that use that Win 10 3D app. Def worth checking out.

hmm if someone else made this I would def like to know that.

sorry, not related to the model :slight_smile:
i think pryme8 made the babylon STLExporter plugin.
updated previous post to reflect that.

LOL kk, I was mildly surprised heh.

1 Like

If it was not loading at origin then that means the model was not at origin on the original file. STLs dont track object position, just vertex data.

1 Like

I have no clue, would have to see the scene and data. I suspect its a problem with the model.

I’ll try with a different model in a bit and see if it presists

1 Like

a basic box is having simular results
faces/side orientation is flipped,
indices & verts go from 36/24 to 108/108
square box, so i can’t tell about rotation :smile:

using

BABYLON.STLExport.CreateSTL( [ mesh ], true, “mesh”, true);

PG; same box, one exported as .babylon, one as .stl

Looks like it’s just inversed winding order. Ia that the stl exporter from scene? Do you.have the scene you are using to generate it?