Created
December 5, 2019 19:05
-
-
Save Piratkopia13/6db0896218e5d83479d38b0e43ab2b68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Returns a random direction vector inside a cone | |
// Angle defined in radians | |
// Example: direction=(0,1,0) and angle=pi returns ([-1,1],[0,1],[-1,1]) | |
float3 getConeSample(inout uint randSeed, float3 direction, float coneAngle) { | |
float cosAngle = cos(coneAngle); | |
// Generate points on the spherical cap around the north pole [1]. | |
// [1] See https://math.stackexchange.com/a/205589/81266 | |
float z = nextRand(randSeed) * (1.0f - cosAngle) + cosAngle; | |
float phi = nextRand(randSeed) * 2.0f * PI; | |
float x = sqrt(1.0f - z * z) * cos(phi); | |
float y = sqrt(1.0f - z * z) * sin(phi); | |
float3 north = float3(0.f, 0.f, 1.f); | |
// Find the rotation axis `u` and rotation angle `rot` [1] | |
float3 axis = normalize(cross(north, normalize(direction))); | |
float angle = acos(dot(normalize(direction), north)); | |
// Convert rotation axis and angle to 3x3 rotation matrix [2] | |
float3x3 R = angleAxis3x3(angle, axis); | |
return mul(R, float3(x, y, z)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment