Skip to content

Instantly share code, notes, and snippets.

@shivaduke28
Last active October 23, 2023 16:01
Show Gist options
  • Save shivaduke28/273079a3bc493486db96c70a5bebaa7c to your computer and use it in GitHub Desktop.
Save shivaduke28/273079a3bc493486db96c70a5bebaa7c to your computer and use it in GitHub Desktop.
Solid angles of spherical triangle and square
float3 SolidAngle(float3 p0, float3 p1, float3 p2)
{
// edges
float cos01 = dot(p0, p1);
float cos12 = dot(p1, p2);
float cos20 = dot(p2, p0);
float sin01Inv = 1 / sqrt(1 - cos01 * cos01);
float sin12Inv = 1 / sqrt(1 - cos12 * cos12);
float sin20Inv = 1 / sqrt(1 - cos20 * cos20);
float cos0 = (cos12 - cos01 * cos12) * (sin01Inv * sin12Inv);
float cos1 = (cos20 - cos12 * cos01) * (sin12Inv * sin01Inv);
float cos2 = (cos12 - cos12 * cos20) * (sin12Inv * sin20Inv);
return acos(cos0) + acos(cos1) + acos(cos2) - UNITY_PI;
}
// p0, p1, p2, p3 are points in the unit sphere such that
// the path p0->p1->p2->p3 defines a boundary of a spherical square.
float4 SolidAngle(float3 p0, float3 p1, float3 p2, float3 p3)
{
// edges
float cos01 = dot(p0, p1);
float cos12 = dot(p1, p2);
float cos23 = dot(p2, p3);
float cos30 = dot(p3, p0);
// diagonals
float cos20 = dot(p2, p0);
float cos31 = dot(p3, p1);
float sin01Inv = 1 / sqrt(1 - cos01 * cos01);
float sin12Inv = 1 / sqrt(1 - cos12 * cos12);
float sin23Inv = 1 / sqrt(1 - cos23 * cos23);
float sin30Inv = 1 / sqrt(1 - cos30 * cos30);
// p0,p1,p3
float cos0 = (cos31 - cos01 * cos30) * (sin01Inv * sin30Inv);
// p1,p2,p0
float cos1 = (cos20 - cos12 * cos01) * (sin12Inv * sin01Inv);
// p2, p3, p1
float cos2 = (cos31 - cos12 * cos23) * (sin12Inv * sin23Inv);
// p3, p0, p2
float cos3 = (cos20 - cos23 * cos30) * (sin23Inv * sin30Inv);
return acos(cos0) + acos(cos1) + acos(cos2) + acos(cos3) - UNITY_TWO_PI;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment