Vector3Unproject function

Vector3 Vector3Unproject(
  1. Vector3 source,
  2. Matrix4 projection,
  3. Matrix4 view
)

Unproject a screen-space point back into object space.

Implementation

Vector3 Vector3Unproject(Vector3 source, Matrix4 projection, Matrix4 view) {
  final inv = (projection * view)..invert();
  final q = inv.transform(Vector4(source.x, source.y, source.z, 1.0));
  if (q.w != 0) return Vector3(q.x / q.w, q.y / q.w, q.z / q.w);
  return Vector3(q.x, q.y, q.z);
}