perspectiveFromFieldOfView function

List<double> perspectiveFromFieldOfView(
  1. List<double> out,
  2. dynamic fov,
  3. dynamic near,
  4. dynamic far
)

Generates a perspective projection matrix with the given field of view. This is primarily useful for generating projection matrices to be used with the still experiemental WebVR API.

@param {mat4} out mat4 frustum matrix will be written into @param {Object} fov Object containing the following values: upDegrees, downDegrees, leftDegrees, rightDegrees @param {number} near Near bound of the frustum @param {number} far Far bound of the frustum @returns {mat4} out

Implementation

List<double> perspectiveFromFieldOfView(List<double> out, fov, near, far) {
  final upTan = math.tan((fov.upDegrees * math.pi) / 180.0);
  final downTan = math.tan((fov.downDegrees * math.pi) / 180.0);
  final leftTan = math.tan((fov.leftDegrees * math.pi) / 180.0);
  final rightTan = math.tan((fov.rightDegrees * math.pi) / 180.0);
  final xScale = 2.0 / (leftTan + rightTan);
  final yScale = 2.0 / (upTan + downTan);

  out[0] = xScale;
  out[1] = 0.0;
  out[2] = 0.0;
  out[3] = 0.0;
  out[4] = 0.0;
  out[5] = yScale;
  out[6] = 0.0;
  out[7] = 0.0;
  out[8] = -((leftTan - rightTan) * xScale * 0.5);
  out[9] = (upTan - downTan) * yScale * 0.5;
  out[10] = far / (near - far);
  out[11] = -1.0;
  out[12] = 0.0;
  out[13] = 0.0;
  out[14] = (far * near) / (near - far);
  out[15] = 0.0;
  return out;
}