Skip to content

Instantly share code, notes, and snippets.

@sneha-belkhale
Last active March 13, 2023 13:13
Show Gist options
  • Save sneha-belkhale/d944211b9af1e3575392d4e460676f30 to your computer and use it in GitHub Desktop.
Save sneha-belkhale/d944211b9af1e3575392d4e460676f30 to your computer and use it in GitHub Desktop.
Fractal Brownian Motion function to include in Unity Shader
float hash (float2 n)
{
return frac(sin(dot(n, float2(123.456789, 987.654321))) * 54321.9876 );
}
float noise(float2 p)
{
float2 i = floor(p);
float2 u = smoothstep(0.0, 1.0, frac(p));
float a = hash(i + float2(0,0));
float b = hash(i + float2(1,0));
float c = hash(i + float2(0,1));
float d = hash(i + float2(1,1));
float r = lerp(lerp(a, b, u.x),lerp(c, d, u.x), u.y);
return r * r;
}
float fbm(float2 p, int octaves)
{
float value = 0.0;
float amplitude = 0.5;
float e = 3.0;
for (int i = 0; i < octaves; ++ i)
{
value += amplitude * noise(p);
p = p * e;
amplitude *= 0.5;
e *= 0.95;
}
return value;
}
@sneha-belkhale
Copy link
Author

include this file in a Unity shader to generate complex noise effects::

#include "Fbm.cginc"
....

Jul-21-2019 20-09-27

float f = fbm(uv+fbm(5*uv + _Time.y, _octaves), _octaves);
float3 fbmColor = lerp(_color1, _color2, 2*f);

@mesmol
Copy link

mesmol commented Mar 13, 2023

Can it be used like a custom function in shadergraph ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment