computeBoundingSphere method

void computeBoundingSphere()

Implementation

void computeBoundingSphere() {
  boundingSphere ??= Sphere(null, null);

  var position = attributes["position"];
  var morphAttributesPosition = morphAttributes["position"];

  if (position != null && position is GLBufferAttribute) {
    boundingSphere!.set(Vector3.init(), 99999999999);

    return;
  }

  if (position != null) {
    // first, find the center of the bounding sphere

    var center = boundingSphere!.center;

    _bufferGeometrybox.setFromBufferAttribute(position);

    // process morph attributes if present

    if (morphAttributesPosition != null) {
      for (var i = 0, il = morphAttributesPosition.length; i < il; i++) {
        var morphAttribute = morphAttributesPosition[i];
        _bufferGeometryboxMorphTargets.setFromBufferAttribute(morphAttribute);

        if (morphTargetsRelative) {
          _bufferGeometryvector.addVectors(_bufferGeometrybox.min, _bufferGeometryboxMorphTargets.min);
          _bufferGeometrybox.expandByPoint(_bufferGeometryvector);

          _bufferGeometryvector.addVectors(_bufferGeometrybox.max, _bufferGeometryboxMorphTargets.max);
          _bufferGeometrybox.expandByPoint(_bufferGeometryvector);
        } else {
          _bufferGeometrybox.expandByPoint(_bufferGeometryboxMorphTargets.min);
          _bufferGeometrybox.expandByPoint(_bufferGeometryboxMorphTargets.max);
        }
      }
    }

    _bufferGeometrybox.getCenter(center);

    // second, try to find a boundingSphere with a radius smaller than the
    // boundingSphere of the boundingBox: sqrt(3) smaller in the best case
    num maxRadiusSq = 0;
    for (var i = 0, il = position.count; i < il; i++) {
      _bufferGeometryvector.fromBufferAttribute(position, i);
      maxRadiusSq = Math.max(
        maxRadiusSq,
        center.distanceToSquared(_bufferGeometryvector),
      );
    }

    // process morph attributes if present

    if (morphAttributesPosition != null) {
      for (var i = 0, il = morphAttributesPosition.length; i < il; i++) {
        var morphAttribute = morphAttributesPosition[i];
        var morphTargetsRelative = this.morphTargetsRelative;

        for (var j = 0, jl = morphAttribute.count; j < jl; j++) {
          _bufferGeometryvector.fromBufferAttribute(morphAttribute, j);

          if (morphTargetsRelative) {
            _bufferGeometryoffset.fromBufferAttribute(position, j);
            _bufferGeometryvector.add(_bufferGeometryoffset);
          }

          maxRadiusSq = Math.max(
            maxRadiusSq,
            center.distanceToSquared(_bufferGeometryvector),
          );
        }
      }
    }

    boundingSphere!.radius = Math.sqrt(maxRadiusSq);

    if (boundingSphere?.radius == null) {
      print(
          'three.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values. ${this}');
    }
  }
}