Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the bounding box or centers of models

Tags:

aframe

3d.io

I was wondering if there was a way to obtain the bounding box for the models that are inserted via 3dio.js, or otherwise calculate their center points? I'm looking to center them on the origin.

The images below show two models relative to the scene origin indicated by the red box.

3D-Model_1

3D-Model_2

like image 459
Neil Docherty Avatar asked Aug 15 '17 12:08

Neil Docherty


People also ask

How do you calculate bounding box?

The area of the box is the width times height. Here, 29 times 50, or 1450. The perimeter of the box is twice the width plus height. Here, that is 2(29+50), or 158.

How do you find the center of a bounding box in Python?

p1-> left top corner, p2-> left bottom corner, p3-> right bottom corner, p4-> right top corner as I believe is in Your case, You can treat the center of the bounding box as the intersection of the diagonals. You need to find equations for lines that diagonals are lying on: Y1 = a1*X1+b1; Y2 = a2*X2+b2 .

How do you define a bounding box?

A bounding box is the smallest rectangle with vertical and horizontal sides that completely surrounds an object. All portions of the object lie within the bounding box.

How do you make a bounding box in blender?

The bounding box is a rectangular box that is wrapped as tightly as possible around the selection. It is oriented parallel to the world axes. In this mode the pivot point lies at the center of the bounding box. You can set the pivot point to Bounding Box with Comma or via the menu in the editor's header.


2 Answers

You can access the three.js object of the 3d.io entity like this:

var threeElem = document.getElementById("custom-id").components['io3d-data3d'].data3dView.threeParent

Then you can use the native bounding box from three.js:

var bbox = new THREE.Box3().setFromObject(threeElem)

Like that you get the min/max bounds which you can use to determine the origin.

I hope that answers your question. Let me know!

Edit: for furniture it would probably be

var threeElem = document.getElementById("custom-id").components['io3d-furniture'].data3dView.threeParent
like image 137
Madlaina Kalunder Avatar answered Sep 25 '22 06:09

Madlaina Kalunder


Based on Madlaina's answer. I needed to ensure the model was loaded before

addModelToScene(type) {

    let scene = document.querySelector('a-scene');
    let model = document.createElement('a-entity');
    model.setAttribute('io3d-data3d', getModelKey(type) )    

    model.addEventListener('model-loaded', () => {
        // Access the three.js object of the 3d.io
        let threeElem = model.components['io3d-data3d'].data3dView.threeParent

        // create the bounding box
        let bbox = new THREE.Box3().setFromObject(threeElem)

        // Calculate the center-point offsets from the max and min points
        const offsetX = (bbox.max.x + bbox.min.x)/2
        const offsetY = (bbox.max.y + bbox.min.y)/2
        const offsetZ = (bbox.max.z + bbox.min.z)/2

        // apply the offset
        model.setAttribute('position', {x:-offsetX,y:-offsetY, z:-offsetZ})
    } );

    scene.appendChild(model);    

}

The result: Centered 3D-Model

like image 38
Neil Docherty Avatar answered Sep 24 '22 06:09

Neil Docherty