intersectDown method
Same result as casting a ray from (x, originY, z) straight down and taking the first hit, or null if nothing is below.
Implementation
THREE.Intersection? intersectDown(double x, double originY, double z) {
if (triangleCount == 0) return null;
final fx = (x - _minX) / _cellSize;
final fz = (z - _minZ) / _cellSize;
if (fx < 0 || fz < 0) return null;
final cx = fx.floor(), cz = fz.floor();
if (cx >= _nx || cz >= _nz) return null;
final cellIndex = cz * _nx + cx;
int bestTri = -1;
double bestY = -double.infinity;
double bestWa = 0, bestWb = 0, bestWc = 0;
for (int k = _cellStart[cellIndex], end = _cellStart[cellIndex + 1]; k < end; k++) {
final t = _cellTris[k];
_loadWorldTriangle(t);
// 2D barycentric weights of (x, z) in triangle a, b, c.
final d = (_b.z - _c.z) * (_a.x - _c.x) + (_c.x - _b.x) * (_a.z - _c.z);
if (d == 0) continue;
final wa = ((_b.z - _c.z) * (x - _c.x) + (_c.x - _b.x) * (z - _c.z)) / d;
final wb = ((_c.z - _a.z) * (x - _c.x) + (_a.x - _c.x) * (z - _c.z)) / d;
final wc = 1.0 - wa - wb;
// Small tolerance so points exactly on shared edges never fall
// through a crack due to rounding.
const eps = -1e-9;
if (wa < eps || wb < eps || wc < eps) continue;
final y = wa * _a.y + wb * _b.y + wc * _c.y;
if (y > originY) continue; // above the ray origin: the ray can't hit it
if (y > bestY) {
bestY = y;
bestTri = t;
bestWa = wa;
bestWb = wb;
bestWc = wc;
}
}
if (bestTri < 0) return null;
return _makeIntersection(bestTri, x, bestY, z, originY - bestY, bestWa, bestWb, bestWc);
}