raycast method

  1. @override
void raycast(
  1. Raycaster raycaster,
  2. List<Intersection> intersects
)
override

Implementation

@override
void raycast(Raycaster raycaster, List<Intersection> intersects) {
  var geometry = this.geometry!;
  var matrixWorld = this.matrixWorld;
  var threshold = raycaster.params["Line"]["threshold"];
  var drawRange = geometry.drawRange;

  // Checking boundingSphere distance to ray

  if (geometry.boundingSphere == null) geometry.computeBoundingSphere();

  _sphere.copy(geometry.boundingSphere!);
  _sphere.applyMatrix4(matrixWorld);
  _sphere.radius += threshold;

  if (raycaster.ray.intersectsSphere(_sphere) == false) return;

  //

  _inverseMatrix.copy(matrixWorld).invert();
  _ray.copy(raycaster.ray).applyMatrix4(_inverseMatrix);

  var localThreshold = threshold / ((scale.x + scale.y + scale.z) / 3);
  var localThresholdSq = localThreshold * localThreshold;

  var vStart = Vector3.init();
  var vEnd = Vector3.init();
  var interSegment = Vector3.init();
  var interRay = Vector3.init();
  var step = type == "LineSegments" ? 2 : 1;

  var index = geometry.index;
  var attributes = geometry.attributes;
  var positionAttribute = attributes["position"];

  if (index != null) {
    final start = Math.max<int>(0, drawRange["start"]!);
    final end = Math.min<int>(
      index.count,
      (drawRange["start"]! + drawRange["count"]!),
    );

    for (var i = start, l = end - 1; i < l; i += step) {
      var a = index.getX(i)!;
      var b = index.getX(i + 1)!;

      vStart.fromBufferAttribute(positionAttribute, a.toInt());
      vEnd.fromBufferAttribute(positionAttribute, b.toInt());

      var distSq = _ray.distanceSqToSegment(vStart, vEnd, interRay, interSegment);

      if (distSq > localThresholdSq) continue;

      interRay.applyMatrix4(this.matrixWorld); //Move back to world space for distance calculation

      var distance = raycaster.ray.origin.distanceTo(interRay);

      if (distance < raycaster.near || distance > raycaster.far) continue;

      intersects.add(Intersection({
        "distance": distance,
        // What do we want? intersection point on the ray or on the segment??
        // point: raycaster.ray.at( distance ),
        "point": interSegment.clone().applyMatrix4(this.matrixWorld),
        "index": i,
        "face": null,
        "faceIndex": null,
        "object": this
      }));
    }
  } else {
    final start = Math.max<int>(0, drawRange["start"]!);
    final end = Math.min<int>(
      positionAttribute.count,
      (drawRange["start"]! + drawRange["count"]!),
    );

    for (int i = start, l = end - 1; i < l; i += step) {
      vStart.fromBufferAttribute(positionAttribute, i);
      vEnd.fromBufferAttribute(positionAttribute, i + 1);

      var distSq = _ray.distanceSqToSegment(vStart, vEnd, interRay, interSegment);

      if (distSq > localThresholdSq) continue;

      interRay.applyMatrix4(this.matrixWorld); //Move back to world space for distance calculation

      var distance = raycaster.ray.origin.distanceTo(interRay);

      if (distance < raycaster.near || distance > raycaster.far) continue;

      intersects.add(Intersection({
        "distance": distance,
        // What do we want? intersection point on the ray or on the segment??
        // point: raycaster.ray.at( distance ),
        "point": interSegment.clone().applyMatrix4(this.matrixWorld),
        "index": i,
        "face": null,
        "faceIndex": null,
        "object": this
      }));
    }
  }
}