TC_Noise.cginc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. Texture2D<float4> _PermTable2D, _Gradient3D;
  2. float3 fade(float3 t)
  3. {
  4. return t * t * t * (t * (t * 6 - 15) + 10);
  5. }
  6. float4 perm2d(float2 uv)
  7. {
  8. return _PermTable2D.SampleLevel(_LinearRepeat, uv, 0);
  9. }
  10. float gradperm(float x, float3 p)
  11. {
  12. float3 g = _Gradient3D.SampleLevel(_LinearRepeat, float2(x, 0), 0).rgb *2.0 - 1.0;
  13. return dot(g, p);
  14. }
  15. float inoise(float3 p)
  16. {
  17. float3 P = fmod(floor(p), 256.0);
  18. p -= floor(p);
  19. float3 f = fade(p);
  20. P = P / 256.0;
  21. const float one = 1.0 / 256.0;
  22. float4 AA = perm2d(P.xy) + P.z;
  23. return lerp(lerp(lerp(gradperm(AA.x, p),
  24. gradperm(AA.z, p + float3(-1, 0, 0)), f.x),
  25. lerp(gradperm(AA.y, p + float3(0, -1, 0)),
  26. gradperm(AA.w, p + float3(-1, -1, 0)), f.x), f.y),
  27. lerp(lerp(gradperm(AA.x + one, p + float3(0, 0, -1)),
  28. gradperm(AA.z + one, p + float3(-1, 0, -1)), f.x),
  29. lerp(gradperm(AA.y + one, p + float3(0, -1, -1)),
  30. gradperm(AA.w + one, p + float3(-1, -1, -1)), f.x), f.y), f.z);
  31. }
  32. // fractal sum, range -1.0 / 1.0
  33. float fBm(float3 p, int octaves)
  34. {
  35. float amp = 0.5;
  36. float sum = 0;
  37. p.xz -= 500;
  38. for (int i = 0; i < octaves; i++)
  39. {
  40. sum += inoise(p * _Frequency) * amp;
  41. _Frequency *= _Lacunarity;
  42. amp *= _Persistence;
  43. }
  44. return sum + 0.5; // remap
  45. }
  46. float fBm(float3 p)
  47. {
  48. float amp = 0.5;
  49. float sum = 0;
  50. p.xz -= 500;
  51. sum += inoise(p * _Frequency) * amp;
  52. _Frequency *= _Lacunarity;
  53. amp *= _Persistence;
  54. return sum + 0.5; // remap
  55. }
  56. // fractal abs sum, range 0.0 / 1.0
  57. float turbulence(float3 p, int octaves)
  58. {
  59. float sum = 0;
  60. float amp = 1.0;
  61. p.xz -= 500;
  62. for (int i = 0; i < octaves; i++)
  63. {
  64. sum += abs(inoise(p*_Frequency))*amp;
  65. _Frequency *= _Lacunarity;
  66. amp *= _Persistence;
  67. }
  68. return sum;
  69. }
  70. float turbulence(float3 p)
  71. {
  72. float sum = 0;
  73. float amp = 1.0;
  74. p.xz -= 500;
  75. sum += abs(inoise(p*_Frequency))*amp;
  76. _Frequency *= _Lacunarity;
  77. amp *= _Persistence;
  78. return sum;
  79. }
  80. // Ridged multifractal, range 0.0 / 1.0
  81. float ridge(float h, float offset)
  82. {
  83. h = abs(h);
  84. h = offset - h;
  85. h = h * h;
  86. return h;
  87. }
  88. float ridgedmf(float3 p, int octaves, float offset)
  89. {
  90. float sum = 0;
  91. float amp = 0.5;
  92. float prev = 1.0;
  93. p.xz -= 500;
  94. for (int i = 0; i < octaves; i++)
  95. {
  96. float n = ridge(inoise(p * _Frequency), offset);
  97. sum += n*amp*prev;
  98. prev = n;
  99. _Frequency *= _Lacunarity;
  100. amp *= _Persistence;
  101. }
  102. return sum;
  103. }
  104. float ridgedmf(float3 p, float offset)
  105. {
  106. float sum = 0;
  107. float amp = 0.5;
  108. float prev = 1.0;
  109. p.xz -= 500;
  110. float n = ridge(inoise(p * _Frequency), offset);
  111. sum += n*amp*prev;
  112. prev = n;
  113. _Frequency *= _Lacunarity;
  114. amp *= _Persistence;
  115. return sum;
  116. }