Vector3Refract function

Vector3 Vector3Refract(
  1. Vector3 v,
  2. Vector3 n,
  3. double r
)

Refraction direction for incident ray v, surface normal n, index ratio r.

Implementation

Vector3 Vector3Refract(Vector3 v, Vector3 n, double r) {
  final dot = v.dot(n);
  final d = 1.0 - r * r * (1.0 - dot * dot);
  if (d < 0) return .zero();
  return v.scaled(r) - n.scaled(r * dot + math.sqrt(d));
}