Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Perlin noise with frequency / force

Discussion in 'Shaders' started by oukibt_unity, Oct 11, 2022.

  1. oukibt_unity

    oukibt_unity

    Joined:
    May 6, 2022
    Posts:
    19
    Hello everyone.

    I wanna regulate Perlin Noise force manually.
    So I mean for example frequency is a shader Range(0.0, 1.0) variable

    If frequency is 0.5, i'll got default Perlin Noise
    if 0.0, i'll got full black texture
    if 1.0, i'll got full white texutre
    if it's 0.25, i'll got more often black spots
    if it's 0.75, i'll got more often white spots

    Like this:



    Perlin Noise hlsl file:
    Code (CSharp):
    1. float wglnoise_mod(float x, float y)
    2. {
    3.     return x - y * floor(x / y);
    4. }
    5.  
    6. float2 wglnoise_mod(float2 x, float2 y)
    7. {
    8.     return x - y * floor(x / y);
    9. }
    10.  
    11. float3 wglnoise_mod(float3 x, float3 y)
    12. {
    13.     return x - y * floor(x / y);
    14. }
    15.  
    16. float4 wglnoise_mod(float4 x, float4 y)
    17. {
    18.     return x - y * floor(x / y);
    19. }
    20.  
    21. float2 wglnoise_fade(float2 t)
    22. {
    23.     return t * t * t * (t * (t * 6 - 15) + 10);
    24. }
    25.  
    26. float3 wglnoise_fade(float3 t)
    27. {
    28.     return t * t * t * (t * (t * 6 - 15) + 10);
    29. }
    30.  
    31. float wglnoise_mod289(float x)
    32. {
    33.     return x - floor(x / 289) * 289;
    34. }
    35.  
    36. float2 wglnoise_mod289(float2 x)
    37. {
    38.     return x - floor(x / 289) * 289;
    39. }
    40.  
    41. float3 wglnoise_mod289(float3 x)
    42. {
    43.     return x - floor(x / 289) * 289;
    44. }
    45.  
    46. float4 wglnoise_mod289(float4 x)
    47. {
    48.     return x - floor(x / 289) * 289;
    49. }
    50.  
    51. float3 wglnoise_permute(float3 x)
    52. {
    53.     return wglnoise_mod289((x * 34 + 1) * x);
    54. }
    55.  
    56. float4 wglnoise_permute(float4 x)
    57. {
    58.     return wglnoise_mod289((x * 34 + 1) * x);
    59. }
    60.  
    61. float ClassicNoise_impl(float2 pi0, float2 pf0, float2 pi1, float2 pf1)
    62. {
    63.     pi0 = wglnoise_mod289(pi0);
    64.     pi1 = wglnoise_mod289(pi1);
    65.  
    66.     float4 ix = float2(pi0.x, pi1.x).xyxy;
    67.     float4 iy = float2(pi0.y, pi1.y).xxyy;
    68.     float4 fx = float2(pf0.x, pf1.x).xyxy;
    69.     float4 fy = float2(pf0.y, pf1.y).xxyy;
    70.  
    71.     float4 i = wglnoise_permute(wglnoise_permute(ix) + iy);
    72.  
    73.     float4 phi = i / 41 * 3.14159265359 * 2;
    74.     float2 g00 = float2(cos(phi.x), sin(phi.x));
    75.     float2 g10 = float2(cos(phi.y), sin(phi.y));
    76.     float2 g01 = float2(cos(phi.z), sin(phi.z));
    77.     float2 g11 = float2(cos(phi.w), sin(phi.w));
    78.  
    79.     float n00 = dot(g00, float2(fx.x, fy.x));
    80.     float n10 = dot(g10, float2(fx.y, fy.y));
    81.     float n01 = dot(g01, float2(fx.z, fy.z));
    82.     float n11 = dot(g11, float2(fx.w, fy.w));
    83.  
    84.     float2 fade_xy = wglnoise_fade(pf0);
    85.     float2 n_x = lerp(float2(n00, n01), float2(n10, n11), fade_xy.x);
    86.     float n_xy = lerp(n_x.x, n_x.y, fade_xy.y);
    87.  
    88.     return 1.44 * n_xy;
    89. }
    90.  
    91. float ClassicNoise(float2 p)
    92. {
    93.     float2 i = floor(p);
    94.     float2 f = frac(p);
    95.  
    96.     return ClassicNoise_impl(i, f, i + 1.0, f - 1.0);
    97. }
    98.  
    99. float PeriodicNoise(float2 p, float2 rep)
    100. {
    101.     float2 i0 = wglnoise_mod(floor(p), rep);
    102.     float2 i1 = wglnoise_mod(i0 + 1.0, rep);
    103.     float2 f = frac(p);
    104.  
    105.     return ClassicNoise_impl(i0, f, i1, f - 1.0);
    106. }
    using in unity shader:
    Code (CSharp):
    1. fixed4 frag (v2f i) : SV_Target
    2. {
    3.     float2 pos = i.uv * 50.0;
    4.     float4 col = ClassicNoise(pos);
    5.  
    6.     return col;
    7. }