revival/game/Assets/Shaders/random.cginc

43 lines
1.2 KiB
HLSL
Raw Normal View History

2021-04-24 03:06:03 +02:00
float4 hash42(float2 p)
{
float4 p4 = frac(float4(p.xyxy) * float4(443.8975, 397.2973, 491.1871, 470.7827));
p4 += dot(p4.wzxy, p4 + 19.19);
return frac(float4(p4.x * p4.y, p4.x * p4.z, p4.y * p4.w, p4.x * p4.w));
}
float hash(float n)
{
return frac(sin(n) * 43758.5453123);
}
float n(in float3 x)
{
float3 p = floor(x);
float3 f = frac(x);
f = f * f * (3.0 - 2.0 * f);
float n = p.x + p.y * 57.0 + 113.0 * p.z;
float res = lerp(lerp(lerp(hash(n + 0.0), hash(n + 1.0), f.x),
lerp(hash(n + 57.0), hash(n + 58.0), f.x), f.y),
lerp(lerp(hash(n + 113.0), hash(n + 114.0), f.x),
lerp(hash(n + 170.0), hash(n + 171.0), f.x), f.y), f.z);
return res;
}
//tape noise
float tape_noise(float2 p, float2 t)
{
float y = p.y;
float s = t.x + 200;
float v = (n(float3(y * .01 + s, 1., 1.0)) + .0)
* (n(float3(y * .011 + 1000.0 + s, 1., 1.0)) + .0)
* (n(float3(y * .51 + 421.0 + s, 1., 1.0)) + .0);
v *= hash42(float2(p.x + t.y + 200, p.y)).x + 2;
v = abs(pow(v + .3, 1.));
if (v < .5) v = 0.; //threshold
return v;
}