Exemple #1
0
  public Box3 setFromObject(Object3D object) {

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

    object.updateMatrixWorld(true);

    this.makeEmpty();

    object.traverse(
        new Object3D.Traverse() {

          @Override
          public void callback(Object3D node) {

            AbstractGeometry geometry = ((GeometryObject) node).getGeometry();

            Vector3 v1 = new Vector3();

            if (geometry != null) {

              if (geometry instanceof Geometry) {

                List<Vector3> vertices = ((Geometry) geometry).getVertices();

                for (int i = 0, il = vertices.size(); i < il; i++) {

                  v1.copy(vertices.get(i));

                  v1.apply(node.getMatrixWorld());

                  expandByPoint(v1);
                }

              } else if (geometry instanceof BufferGeometry
                  && ((BufferGeometry) geometry).getAttribute("position") != null) {

                Float32Array positions =
                    (Float32Array) ((BufferGeometry) geometry).getAttribute("position").getArray();

                for (int i = 0, il = positions.getLength(); i < il; i += 3) {

                  v1.set(positions.get(i), positions.get(i + 1), positions.get(i + 2));

                  v1.apply(node.getMatrixWorld());

                  expandByPoint(v1);
                }
              }
            }
          }
        });

    return this;
  }