Performs a ray - scene intersection.
Iterates over all primitives and calls their intersect.
Will only return hit points which are closer than the specified
prevBestDistance.
Source
Intersection intersect(Ray r, double prevBestDistance) {
double bestDistance = prevBestDistance;
Primitive bestPrimitive = null;
//print('Finding intersetion with ${nonIdxPrimitives.length} primitives..');
// iterate over non-indexable primitives
for (Primitive p in this.nonIdxPrimitives) {
var curHit = p.intersect(r, bestDistance);
// check if we have a better hit
if (curHit.distance < bestDistance && curHit.distance > EPS) {
// yep.
bestDistance = curHit.distance;
bestPrimitive = p;
}
}
// TODO iterate over indexable primitives
// package everything nicely
Intersection bestHit = new Intersection(bestDistance, bestPrimitive, r.getPoint3(bestDistance));
return bestHit;
}