split method

void split(
  1. int level
)

Implementation

void split(int level) {
  List<Octree> subTrees = [];
  Vector3 halfsize = _v2.copy(box.max).sub(box.min).multiplyScalar(0.5);

  for (int x = 0; x < 2; x++) {
    for (int y = 0; y < 2; y++) {
      for (int z = 0; z < 2; z++) {
        Box3 box = Box3();
        final Vector3 v = _v1.set(x.toDouble(), y.toDouble(), z.toDouble());
        box.min.copy(box.min).add(v.multiply(halfsize));
        box.max.copy(box.min).add(halfsize);
        subTrees.add(Octree(box));
      }
    }
  }

  while (triangles.isNotEmpty) {
    Triangle triangle = triangles.removeLast();
    for (int i = 0; i < subTrees.length; i++) {
      if (subTrees[i].box.intersectsTriangle(triangle)) {
        subTrees[i].triangles.add(triangle);
      }
    }
  }

  for (int i = 0; i < subTrees.length; i++) {
    int len = subTrees[i].triangles.length;
    if (len > 8 && level < 16) {
      subTrees[i].split(level + 1);
    }
    if (len != 0) {
      subTrees.add(subTrees[i]);
    }
  }
}