raycast method

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

Get intersections between a casted ray and this mesh. Raycaster.intersectObject will call this method, but the results are not ordered.

Implementation

@override
void raycast(Raycaster raycaster, List<Intersection> intersects) {
  final geometry = this.geometry;
  final material = this.material;
  final matrixWorld = this.matrixWorld;

  if (material == null) return;

  // Checking boundingSphere distance to ray

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

  if(geometry != null){
    _meshsphere.setFrom(geometry.boundingSphere!);
  }
  _meshsphere.applyMatrix4(matrixWorld);

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

  _meshinverseMatrix..setFrom(matrixWorld)..invert();
  _meshray..copyFrom(raycaster.ray)..applyMatrix4(_meshinverseMatrix);

  // Check boundingBox before continuing

  if (geometry?.boundingBox != null) {
    if (!_meshray.intersectsBox(geometry!.boundingBox!)) return;
  }

  Intersection? intersection;
  final index = geometry?.index;
  final position = geometry?.attributes["position"];
  final morphPosition = geometry?.morphAttributes["position"];
  final morphTargetsRelative = geometry?.morphTargetsRelative;
  final uv = geometry?.attributes["uv"];
  final uv2 = geometry?.attributes["uv2"];
  final groups = geometry?.groups;
  final drawRange = geometry?.drawRange;

  if (index != null) {
    // indexed buffer geometry

    if (material is GroupMaterial) {
      for (int i = 0, il = groups?.length ?? 0; i < il; i++) {
        final group = groups![i];
        final groupMaterial = material.children[group["materialIndex"]];

        final start = math.max<int>(group["start"], drawRange!["start"]!);
        final end = math.min<int>((group["start"] + group["count"]),
            (drawRange["start"]! + drawRange["count"]!));

        for (int j = start, jl = end; j < jl; j += 3) {
          int a = index.getX(j)!.toInt();
          int b = index.getX(j + 1)!.toInt();
          int c = index.getX(j + 2)!.toInt();

          intersection = checkBufferGeometryIntersection(
              this,
              groupMaterial,
              raycaster,
              _meshray,
              position,
              morphPosition,
              morphTargetsRelative,
              uv,
              uv2,
              a,
              b,
              c);

          if (intersection != null) {
            intersection.faceIndex = (j / 3).floor();
            // triangle number in indexed buffer semantics
            intersection.face?.materialIndex = group["materialIndex"];
            intersects.add(intersection);
          }
        }
      }
    } else {
      final start = math.max(0, drawRange!["start"]!);
      final end = math.min(index.count, (drawRange["start"]! + drawRange["count"]!));

      for (int i = start, il = end; i < il; i += 3) {
        int a = index.getX(i)!.toInt();
        int b = index.getX(i + 1)!.toInt();
        int c = index.getX(i + 2)!.toInt();

        intersection = checkBufferGeometryIntersection(
            this,
            material,
            raycaster,
            _meshray,
            position,
            morphPosition,
            morphTargetsRelative,
            uv,
            uv2,
            a,
            b,
            c);

        if (intersection != null) {
          intersection.faceIndex = (i / 3).floor();
          // triangle number in indexed buffer semantics
          intersects.add(intersection);
        }
      }
    }
  } else if (position != null) {
    // non-indexed buffer geometry

    if (material is GroupMaterial) {
      for (int i = 0, il = groups?.length ?? 0; i < il; i++) {
        final group = groups![i];
        final groupMaterial = material.children[group["materialIndex"]];

        final start = math.max<int>(group["start"], drawRange!["start"]!);
        final end = math.min<int>((group["start"] + group["count"]),
            (drawRange["start"]! + drawRange["count"]!));

        for (int j = start, jl = end; j < jl; j += 3) {
          final a = j;
          final b = j + 1;
          final c = j + 2;

          intersection = checkBufferGeometryIntersection(
              this,
              groupMaterial,
              raycaster,
              _meshray,
              position,
              morphPosition,
              morphTargetsRelative,
              uv,
              uv2,
              a,
              b,
              c);

          if (intersection != null) {
            intersection.faceIndex = (j / 3).floor();
            // triangle number in non-indexed buffer semantics
            intersection.face?.materialIndex = group["materialIndex"];
            intersects.add(intersection);
          }
        }
      }
    } else {
      final start = math.max(0, drawRange!["start"]!);
      final end = math.min<int>(
          position.count, (drawRange["start"]! + drawRange["count"]!));

      for (int i = start, il = end; i < il; i += 3) {
        final a = i;
        final b = i + 1;
        final c = i + 2;

        intersection = checkBufferGeometryIntersection(
            this,
            material,
            raycaster,
            _meshray,
            position,
            morphPosition,
            morphTargetsRelative,
            uv,
            uv2,
            a,
            b,
            c);

        if (intersection != null) {
          intersection.faceIndex = (i / 3).floor(); // triangle number in non-indexed buffer semantics
          intersects.add(intersection);
        }
      }
    }
  }
}