rayIntersect method

OctreeRay? rayIntersect(
  1. Ray ray
)

Implementation

OctreeRay? rayIntersect(Ray ray) {
  if(ray.direction.length() == 0) return null;

  List<Triangle> triangles = getRayTriangles(ray, []);
  late Triangle triangle;
  late Vector3 position;
  double distance = 1e100;
  Vector3? result;

  for (int i = 0; i < triangles.length; i ++ ) {
    result = ray.intersectTriangle( triangles[ i ].a, triangles[ i ].b, triangles[ i ].c, true, _v1 );
    if(result != null){
      double newdistance = result.sub( ray.origin ).length();

      if ( distance > newdistance ) {
        position = result.clone().add( ray.origin );
        distance = newdistance;
        triangle = triangles[ i ];
      }
    }
  }

  return distance < 1e100 ? OctreeRay(distance: distance, triangle: triangle, position: position) : null;
}