Random.fxh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //////////////////////////////////////////////////////////////////////////////
  2. // ©2006 Electronic Arts Inc
  3. //
  4. // A simple random number generator
  5. //////////////////////////////////////////////////////////////////////////////
  6. //
  7. // Random number generator
  8. //
  9. static const int MAX_RANDOM_VALUES = 16;
  10. static const float4 RandomValues[MAX_RANDOM_VALUES] = // random values in range 0..1
  11. {
  12. // To generate this table, use excel with rows of this formula: =CONCATENATE("float4(",RAND(),", ",RAND(),", ", RAND(),", ", RAND(), "),")
  13. float4(0.957897000503582, 0.378870525347763, 0.363571890095615, 0.328760908964182),
  14. float4(0.176237249178937, 0.914575110140635, 0.436093504459558, 0.347628420779846),
  15. float4(0.74321894178331, 0.442561117711613, 0.997454573448046, 0.362213792964882),
  16. float4(0.489662653412263, 0.666423292057291, 0.830569846431306, 0.974257713027646),
  17. float4(0.894767456143942, 0.823621517393205, 0.930970549797899, 0.0521984733736147),
  18. float4(0.141100733280401, 0.652700235943417, 0.00845252291957532, 0.643057387894008),
  19. float4(0.22979899343149, 0.897999019438672, 0.753821963794792, 0.131650373484698),
  20. float4(0.581538391481718, 0.283691652574809, 0.0915647105845903, 0.407480940983646),
  21. float4(0.270604393931209, 0.72281806272639, 0.16943672558123, 0.146905246115598),
  22. float4(0.599555574957733, 0.096453300376983, 0.876398320463974, 0.117222883790552),
  23. float4(0.510171077745377, 0.942157193101133, 0.195788791440078, 0.916756497322788),
  24. float4(0.0354679287382584, 0.534009408848821, 0.167321778635773, 0.557676417292525),
  25. float4(0.861564026888471, 0.225160105115149, 0.0823319997715926, 0.87900387469963),
  26. float4(0.173086589845635, 0.89351880243715, 0.0139240580314164, 0.178738120005564),
  27. float4(0.826938346482238, 0.771814362160947, 0.439535939575699, 0.49396301649165),
  28. float4(0.542361347073796, 0.721861671956157, 0.769731420798597, 0.587856677976187),
  29. };
  30. // Get a random value in a specific range. Range has minimum in x and spread (= max - min) in y
  31. float GetRandomFloatValue(float2 range, float index, float seed)
  32. {
  33. float fraction = frac(seed * 11.0f/MAX_RANDOM_VALUES + index * 3.0f / MAX_RANDOM_VALUES);
  34. return (range.x + RandomValues[fraction * MAX_RANDOM_VALUES].x * range.y);
  35. }
  36. float2 GetRandomFloatValues(float2 minimum, float2 range, float index, float seed)
  37. {
  38. float fraction = frac(seed * 11.0f/MAX_RANDOM_VALUES + index * 3.0f / MAX_RANDOM_VALUES);
  39. return minimum + RandomValues[fraction * MAX_RANDOM_VALUES].xy * range;
  40. }
  41. float3 GetRandomFloatValues(float3 minimum, float3 range, float index, float seed)
  42. {
  43. float fraction = frac(seed * 11.0f/MAX_RANDOM_VALUES + index * 3.0f / MAX_RANDOM_VALUES);
  44. return minimum + RandomValues[fraction * MAX_RANDOM_VALUES].xyz * range;
  45. }
  46. float4 GetRandomFloatValues(float4 minimum, float4 range, float index, float seed)
  47. {
  48. float fraction = frac(seed * 11.0f/MAX_RANDOM_VALUES + index * 3.0f / MAX_RANDOM_VALUES);
  49. return minimum + RandomValues[fraction * MAX_RANDOM_VALUES] * range;
  50. }