Skip to content

Instantly share code, notes, and snippets.

@LeonBaudouin
Last active September 13, 2022 22:32
Show Gist options
  • Save LeonBaudouin/2de1b39f95fdaa05357411892fed0453 to your computer and use it in GitHub Desktop.
Save LeonBaudouin/2de1b39f95fdaa05357411892fed0453 to your computer and use it in GitHub Desktop.
// ------------
// --- MATH ---
// ------------
float remap(float value, float start1, float stop1, float start2, float stop2) {
return start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
}
float cremap(float value, float start1, float stop1, float start2, float stop2) {
float r = remap(value, start1, stop1, start2, stop2);
return clamp(r, min(start2, stop2), max(start2, stop2));
}
float inRange(float v, float minV, float maxV) {
return min(step(minV, v), step(v, maxV));
}
float isNorm(vec2 _st) {
if (_st.x > 1. || _st.y > 1. || _st.x < 0. || _st.y < 0.)
return 0.;
return 1.;
}
// --------------
// --- RANDOM ---
// --------------
float rand(float n){return fract(sin(n) * 43758.5453123);}
float rand(vec2 co){
return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453);
}
vec3 rand(vec3 c) {
float j = 4096.0*sin(dot(c,vec3(17.0, 59.4, 15.0)));
vec3 r;
r.z = fract(512.0*j);
j *= .125;
r.x = fract(512.0*j);
j *= .125;
r.y = fract(512.0*j);
return r-0.5;
}
// --------------
// --- IMAGES ---
// --------------
vec2 adjustUvToImage(vec2 _st, vec2 center, float texRatio, float quadRatio, bool fit) {
float correctedRatio = quadRatio / texRatio;
vec2 imageUv = _st - center;
imageUv *= vec2(correctedRatio, 1.);
if (fit)
imageUv /= mix(1. / correctedRatio, correctedRatio, step(correctedRatio, 1.));
imageUv /= mix(correctedRatio, 1., step(correctedRatio, 1.));
imageUv += center;
return imageUv;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment