Skip to content

Bounding box is not properly calculated for the InstancedMesh objects in Box3.expandByObject #18643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
4 of 13 tasks
Fang3s opened this issue Feb 16, 2020 · 2 comments
Closed
4 of 13 tasks

Comments

@Fang3s
Copy link

Fang3s commented Feb 16, 2020

Description of the problem

Just as the issue title indicated, I create an InstancedMesh with more than one instances at different positions, but it's just the Box3 result of the Geometry attached to it. After reading the code, I have made a fix.

var _box = new THREE.Box3();
var _vector = new THREE.Vector3();
THREE.Box3.prototype.expandByObject = function (object) {

    // Computes the world-axis-aligned bounding box of an object (including its children),
    // accounting for both the object's, and children's, world transforms

    object.updateWorldMatrix(false, false);

    var geometry = object.geometry;

    if (geometry !== undefined) {

        if (geometry.boundingBox === null) {

            geometry.computeBoundingBox();

        }

        if (object.isInstancedMesh) {
            var matrixWorld = object.matrixWorld,
                matrix4Array = object.instanceMatrix.array,
                arrayLength = matrix4Array.length;
            for (var posOffset = 12; posOffset < arrayLength; posOffset += 16) {
                _vector.set(matrix4Array[posOffset], matrix4Array[1 + posOffset], matrix4Array[2 + posOffset]);
                _box.expandByPoint(_vector);
            }
        } else {
            _box.copy(geometry.boundingBox);
        }
        _box.applyMatrix4(object.matrixWorld);

        this.expandByPoint(_box.min);
        this.expandByPoint(_box.max);

    }

    var children = object.children;

    for (var i = 0, l = children.length; i < l; i++) {

        this.expandByObject(children[i]);

    }

    return this;
}

See also:

Please also include a live example if possible. You can start from these templates:

Three.js version
  • Dev
  • r113
  • ...
Browser
  • All of them
  • Chrome
  • Firefox
  • Internet Explorer
OS
  • All of them
  • Windows
  • macOS
  • Linux
  • Android
  • iOS
Hardware Requirements (graphics card, VR Device, ...)
@Mugen87
Copy link
Collaborator

Mugen87 commented Feb 16, 2020

Duplicate of #18334

@Mugen87 Mugen87 marked this as a duplicate of #18334 Feb 16, 2020
@Mugen87 Mugen87 closed this as completed Feb 16, 2020
@Saltallica
Copy link

@Fang3s A huge thank you to providing this code. InstancedMeshes are cumbersome to work with and miss a lot of the practical methods and abilities of normal geometry - I hope that changes over time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants