StarCalcUtility.cginc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef STAR_CALC_UTILITY
  2. #define STAR_CALC_UTILITY
  3. // Copyright(c) 2018 Funly LLC
  4. //
  5. // Author: Jason Ederle
  6. // Description: Helper functions for generating stars.
  7. // Contact: jason@funly.io
  8. #include "UnityCG.cginc"
  9. #include "SkyMathUtilities.cginc"
  10. float RangedRandom(float2 randomSeed, float minValue, float maxValue)
  11. {
  12. float dist = maxValue - minValue;
  13. float percent = rand(randomSeed);
  14. return minValue + (dist * percent);
  15. }
  16. float3 RandomUnitSpherePoint(float2 randomSeed)
  17. {
  18. float z = RangedRandom(randomSeed * .81239f, -1.0f, 1.0f);
  19. float a = RangedRandom(randomSeed * .12303f, 0.0f, UNITY_TWO_PI);
  20. float r = sqrt(1.0f - z * z);
  21. float x = r * cos(a);
  22. float y = r * sin(a);
  23. return normalize(float3(x, y, z));
  24. }
  25. float DistanceSquared(float3 a, float3 b)
  26. {
  27. float dx = a.x - b.x;
  28. float dy = a.y - b.y;
  29. float dz = a.z - b.z;
  30. return dx * dx + dy * dy + dz * dz;
  31. }
  32. float IsInRange(float value, float vMin, float vMax)
  33. {
  34. float minValid = step(vMin, value);
  35. float maxValid = !step(vMax - .00001f, value);
  36. return minValid * maxValid;
  37. }
  38. float IsInsideBox(float2 origin, float2 checkCoord, float texelWidth)
  39. {
  40. float xValid = IsInRange(checkCoord.x, origin.x, origin.x + texelWidth);
  41. float yValid = IsInRange(checkCoord.y, origin.y, origin.y + texelWidth);
  42. return xValid * yValid;
  43. }
  44. float IsSamePixel(float2 pixel1, float2 pixel2, float texelWidth)
  45. {
  46. float2 pixel1Count = floor(pixel1 / texelWidth);
  47. float2 pixel2Count = floor(pixel2 / texelWidth);
  48. if ((int)pixel1Count.x == (int)pixel2Count.x &&
  49. (int)pixel1Count.y == (int)pixel2Count.y)
  50. {
  51. return 1;
  52. }
  53. else
  54. {
  55. return 0;
  56. }
  57. }
  58. #endif