1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| static uint XorShift32(ref uint state) { uint x = state; x ^= x << 13; x ^= x >> 17; x ^= x << 15; state = x; return x; }
public static float RandomFloat01(ref uint state) { return (XorShift32(ref state) & 0xFFFFFF) / 16777216.0f; }
public static float3 RandomInUnitDisk(ref uint state) { float3 p; do { p = 2.0f * new float3(RandomFloat01(ref state), RandomFloat01(ref state), 0) - new float3(1, 1, 0); } while (lengthsq(p) >= 1.0); return p; }
public static float3 RandomInUnitSphere(ref uint state) { float3 p; do { p = 2.0f * new float3(RandomFloat01(ref state), RandomFloat01(ref state), RandomFloat01(ref state)) - new float3(1, 1, 1); } while (lengthsq(p) >= 1.0); return p; }
public static float3 RandomUnitVector(ref uint state) { float z = RandomFloat01(ref state) * 2.0f - 1.0f; float a = RandomFloat01(ref state) * 2.0f * kPI; float r = sqrt(1.0f - z * z); float x, y; sincos(a, out x, out y); return new float3(r * x, r* y, z); }
|