Skip to content

Instantly share code, notes, and snippets.

@Piratkopia13
Created December 5, 2019 18:55
Show Gist options
  • Save Piratkopia13/c2b646b5209196b0909a00d12c6fd823 to your computer and use it in GitHub Desktop.
Save Piratkopia13/c2b646b5209196b0909a00d12c6fd823 to your computer and use it in GitHub Desktop.
// Calculates the angle of a cone that starts at position worldPosition and perfectly
// encapsulates a sphere at position light.position with radius light.radius
float3 toLight = normalize(light.position - worldPosition);
// Calculate a vector perpendicular to L
float3 perpL = cross(toLight, float3(0.f, 1.0f, 0.f));
// Handle case where L = up -> perpL should then be (1,0,0)
if (all(perpL == 0.0f)) {
perpL.x = 1.0;
}
// Use perpL to get a vector from worldPosition to the edge of the light sphere
float3 toLightEdge = normalize((light.position + perpL * light.radius) - worldPosition);
// Angle between L and toLightEdge. Used as the cone angle when sampling shadow rays
float coneAngle = acos(dot(toLight, toLightEdge)) * 2.0f;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment