Jump to content

Issue regarding position update


Feldspar
 Share

Recommended Posts

Hi guys,

 

I'm having trouble positioning objects on my scene.

 

Here is the context :

I'm having 3 objects "table" on which the position is effectively non zero 

 

Capture.PNG

 

These 3 objects have a non-visible "floor" parent mesh, for the purpose of local positionning :

 

Capture.PNG

 

Unfortunately, all these table meshes appears at the center of the scene. The girl is in (0,0,0) for reference :

 

Capture.PNG

 

The solution I have found for the meshes to appear in the right spot, is to add this hella dirty code, just after i finish adding the objects :

setTimeout(function() {floor.markAsDirty()}, 100);

Here is the result :

 

Capture.PNG

 

If i don't use a setTimeout, and mark the floor as dirty right away, the tables are still in (0, 0, 0)

Any thoughts on what to update in the hierarchy, so I can get my objects in their right position ?

 

Thanks

Link to comment
Share on other sites

Okay i found it, it turns out when I import the tables, I have a function that recursively computes bounding boxes of the table and its children.

This custom function uses computeWorldMatrix on the mesh, and by some way I don't really understand, it prevented the table from being updated on the scene. Any idea why Deltakosh ?

 

Oh, and here is the custom function. It computes an overall boundingbox of the object and its children in the object's local frame :

BABYLON.Mesh.prototype.getBoundingBox = function(force) {    if(!force && this.boundingBox) {        return this.boundingBox;    }    var minX = Infinity,        minY = Infinity,        minZ = Infinity,        maxX = -Infinity,        maxY = -Infinity,        maxZ = -Infinity;    // Put back the bounding box in the local frame    this.computeWorldMatrix();    var worldToObject = this.getWorldMatrix().clone();    worldToObject.invert();        var customTraverse = function(mesh) {        mesh.computeWorldMatrix();          var subObjectToWorld = mesh.getWorldMatrix().clone();        // Top level Object local frame        var subObjectToObject = subObjectToWorld.multiply(worldToObject);        if(mesh instanceof BABYLON.Mesh && mesh.isVisible) {            var minMax = BABYLON.Tools.ExtractMinAndMaxWithTransform(mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind), 0, mesh._totalVertices, subObjectToObject);            var Minimum = minMax.minimum;            var Maximum = minMax.maximum;            // compute overall bbox            minX = Math.min(minX, Minimum.x);            minY = Math.min(minY, Minimum.y);            minZ = Math.min(minZ, Minimum.z);            maxX = Math.max(maxX, Maximum.x);            maxY = Math.max(maxY, Maximum.y);            maxZ = Math.max(maxZ, Maximum.z);        }        var children = mesh.getChildren();        for(var i = 0, l = children.length; i < l; i++) {            customTraverse(children[i]);        }    };  customTraverse(this);  var bBox_min = new BABYLON.Vector3(minX, minY, minZ);  var bBox_max = new BABYLON.Vector3(maxX, maxY, maxZ);    this._boundingInfo = new BABYLON.BoundingInfo(bBox_min, bBox_max);  return this._boundingInfo.boundingBox;};
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...