Search Unity

Question How to get gradient of simplex noise (snoise function)?

Discussion in 'Shaders' started by JoeStrout, Nov 17, 2022.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'm trying to get both a noise sample, and the gradient.

    I think the snoise math function I've been successfully using (without the gradient) is among the functions defined here. And that same source claims there is another version of the method:

    So I'm trying this:

    Code (hlsl):
    1.            float3 noiseGradient;
    2.            float3 samplePos = IN.worldPos * _Freq;
    3.            float noise = snoise(samplePos, noiseGradient);
    But I get: Shader error: 'snoise': cannot implicitly convert from 'float3' to 'float4' at line 57 (on d3d11)

    Obviously I've tried changing either or both of these to float4, but I get the same error. I also get the same error if I add even more arguments, so maybe I'm wrong and this version of snoise doesn't exist at all.

    In any case, my core issue is still:

    How can I get a noise sample and also get the gradient of the noise at that point?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Oops. Never mind; the snoise function I'm using was provided by a .cginc file I had long since forgotten about. And it doesn't have a 2-parameter version, nor any other way to get the gradient.

    I think I'm going to try getting my noise by sampling a noise texture... then maybe I can use the ddx and ddy functions to get the gradient of that. I hope.