Last active
August 9, 2022 13:26
-
-
Save lhog/550791922c4c4eea13a4d53e55e4956d to your computer and use it in GitHub Desktop.
Some GLSL Random and Noise functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const float M_PI = 3.1415926535897932384626433832795; | |
float rand(float p) | |
{ | |
return fract(sin(p) * 43758.5453123); | |
} | |
/* | |
float rand(vec2 p) | |
{ | |
return fract(sin(dot(p, vec2(12.9898, 4.1414))) * 43758.5453); | |
} | |
*/ | |
//sugested replacement | |
float rand(vec2 co) | |
{ | |
float a = 12.9898; | |
float b = 78.233; | |
float c = 43758.5453; | |
float dt = dot(co.xy ,vec2(a,b)); | |
float sn = mod(dt, M_PI); | |
return fract(sin(sn) * c); | |
} | |
vec3 rand3(vec3 p) | |
{ | |
vec3 q = vec3( | |
dot(p, vec3(127.1, 311.7, 74.7)), | |
dot(p, vec3(269.5, 183.3, 246.1)), | |
dot(p, vec3(113.5, 271.9, 124.6)) | |
); | |
return fract(sin(q) * 43758.5453123); | |
} | |
vec3 rand3(vec2 p) | |
{ | |
vec3 q = vec3( | |
dot(p, vec2(127.1, 311.7)), | |
dot(p, vec2(269.5, 183.3)), | |
dot(p, vec2(419.2, 371.9)) | |
); | |
return fract(sin(q) * 43758.5453); | |
} | |
float noise(float p) | |
{ | |
float fl = floor(p); | |
float fc = fract(p); | |
return mix(rand(fl), rand(fl + 1.0), fc); | |
} | |
float noise(vec2 n) | |
{ | |
const vec2 d = vec2(0.0, 1.0); | |
vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n)); | |
return mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment