Random.fxh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //////////////////////////////////////////////////////////////////////////////
  2. // ©2006 Electronic Arts Inc
  3. //
  4. // A simple random number generator
  5. //////////////////////////////////////////////////////////////////////////////
  6. #ifndef _RANDOM_FXH_
  7. #define _RANDOM_FXH_
  8. //
  9. // Random number generator
  10. //
  11. #define MAX_RANDOM_VALUES 16
  12. // $note (WSK) - PS3 is having trouble dealing with the longer decimals so trimming it to 8 decimal points
  13. #if defined(EA_PLATFORM_PS3)
  14. STATICARRAY const float4 RandomValues[MAX_RANDOM_VALUES] = // random values in range 0..1
  15. {
  16. // To generate this table, use excel with rows of this formula: =CONCATENATE("float4(",RAND(),", ",RAND(),", ", RAND(),", ", RAND(), "),")
  17. float4(0.95789700, 0.37887052, 0.36357189, 0.32876090),
  18. float4(0.17623724, 0.91457511, 0.43609350, 0.34762842),
  19. float4(0.74321894, 0.44256111, 0.99745457, 0.36221379),
  20. float4(0.48966265, 0.66642329, 0.83056984, 0.97425771),
  21. float4(0.89476745, 0.82362151, 0.93097054, 0.05219847),
  22. float4(0.14110073, 0.65270023, 0.00845252, 0.64305738),
  23. float4(0.22979899, 0.89799901, 0.75382196, 0.13165037),
  24. float4(0.58153839, 0.28369165, 0.09156471, 0.40748094),
  25. float4(0.27060439, 0.72281806, 0.16943672, 0.14690524),
  26. float4(0.59955557, 0.09645330, 0.87639832, 0.11722288),
  27. float4(0.51017107, 0.94215719, 0.19578879, 0.91675649),
  28. float4(0.03546792, 0.53400940, 0.16732177, 0.55767641),
  29. float4(0.86156402, 0.22516010, 0.08233199, 0.87900387),
  30. float4(0.17308658, 0.89351880, 0.01392405, 0.17873812),
  31. float4(0.82693834, 0.77181436, 0.43953593, 0.49396301),
  32. float4(0.54236134, 0.72186167, 0.76973142, 0.58785667),
  33. };
  34. #else // #if defined(EA_PLATFORM_PS3)
  35. STATICARRAY const float4 RandomValues[MAX_RANDOM_VALUES] = // random values in range 0..1
  36. {
  37. // To generate this table, use excel with rows of this formula: =CONCATENATE("float4(",RAND(),", ",RAND(),", ", RAND(),", ", RAND(), "),")
  38. float4(0.957897000503582, 0.378870525347763, 0.363571890095615, 0.328760908964182),
  39. float4(0.176237249178937, 0.914575110140635, 0.436093504459558, 0.347628420779846),
  40. float4(0.74321894178331, 0.442561117711613, 0.997454573448046, 0.362213792964882),
  41. float4(0.489662653412263, 0.666423292057291, 0.830569846431306, 0.974257713027646),
  42. float4(0.894767456143942, 0.823621517393205, 0.930970549797899, 0.0521984733736147),
  43. float4(0.141100733280401, 0.652700235943417, 0.00845252291957532, 0.643057387894008),
  44. float4(0.22979899343149, 0.897999019438672, 0.753821963794792, 0.131650373484698),
  45. float4(0.581538391481718, 0.283691652574809, 0.0915647105845903, 0.407480940983646),
  46. float4(0.270604393931209, 0.72281806272639, 0.16943672558123, 0.146905246115598),
  47. float4(0.599555574957733, 0.096453300376983, 0.876398320463974, 0.117222883790552),
  48. float4(0.510171077745377, 0.942157193101133, 0.195788791440078, 0.916756497322788),
  49. float4(0.0354679287382584, 0.534009408848821, 0.167321778635773, 0.557676417292525),
  50. float4(0.861564026888471, 0.225160105115149, 0.0823319997715926, 0.87900387469963),
  51. float4(0.173086589845635, 0.89351880243715, 0.0139240580314164, 0.178738120005564),
  52. float4(0.826938346482238, 0.771814362160947, 0.439535939575699, 0.49396301649165),
  53. float4(0.542361347073796, 0.721861671956157, 0.769731420798597, 0.587856677976187),
  54. };
  55. #endif // #if defined(EA_PLATFORM_PS3)
  56. // Get a random value in a specific range. Range has minimum in x and spread (= max - min) in y
  57. float GetRandomFloatValue(float2 range, float index, float seed)
  58. {
  59. float fraction = frac(seed * 11.0f/MAX_RANDOM_VALUES + index * 3.0f / MAX_RANDOM_VALUES);
  60. return (range.x + RandomValues[fraction * MAX_RANDOM_VALUES].x * range.y);
  61. }
  62. float2 GetRandomFloatValues(float2 minimum, float2 range, float index, float seed)
  63. {
  64. float fraction = frac(seed * 11.0f/MAX_RANDOM_VALUES + index * 3.0f / MAX_RANDOM_VALUES);
  65. return minimum + RandomValues[fraction * MAX_RANDOM_VALUES].xy * range;
  66. }
  67. float3 GetRandomFloatValues(float3 minimum, float3 range, float index, float seed)
  68. {
  69. float fraction = frac(seed * 11.0f/MAX_RANDOM_VALUES + index * 3.0f / MAX_RANDOM_VALUES);
  70. return minimum + RandomValues[fraction * MAX_RANDOM_VALUES].xyz * range;
  71. }
  72. float4 GetRandomFloatValues(float4 minimum, float4 range, float index, float seed)
  73. {
  74. float fraction = frac(seed * 11.0f/MAX_RANDOM_VALUES + index * 3.0f / MAX_RANDOM_VALUES);
  75. return minimum + RandomValues[fraction * MAX_RANDOM_VALUES] * range;
  76. }
  77. #endif // _RANDOM_FXH_