Skip to content

Instantly share code, notes, and snippets.

@mnikn
Last active June 24, 2022 03:44
Show Gist options
  • Save mnikn/de81288af5b80a8aeeb4a1b99eb6da73 to your computer and use it in GitHub Desktop.
Save mnikn/de81288af5b80a8aeeb4a1b99eb6da73 to your computer and use it in GitHub Desktop.
shader-utils
float rect(vec2 uv, vec2 size) {
float blur = 0.000;
// 让 size 成正比
size = 0.5 - size * 0.5;
// 先画一边
vec2 r = vec2(step(size.x, uv.x), step(size.y, uv.y));
// 再画相反的边,相乘得出相交位置就是对应的矩形
r *= vec2(step(size.x, 1.0 - uv.x), step(size.y, 1.0 - uv.y));
// x 和 y 相乘得出矩形的像素点值
return r.x * r.y;
}
float circle(vec2 uv, float r) {
uv -= 0.5;
uv.x *= iResolution.x / iResolution.y;
float blur = 0.005;
// 用 uv 做长度,点距离零点的距离会呈圆形递增
float d = length(uv);
d = step(d, r);
return d;
}
float traperbox(vec2 uv, float wb, float wt, float yb, float yt, float blur) {
float m = smoothstep(-blur, blur, uv.y - yb);
m *= smoothstep(blur, -blur, uv.y - yt);
uv.x = abs(uv.x);
float w = mix(wb, wt, (uv.y - yb) / (yt - yb));
m *= smoothstep(blur, -blur, uv.x -w);
return m;
}
float roundedRect(vec2 uv, vec2 size, float radius, float blur) {
float t = length(max(abs(uv) - size + radius,0.0)) - radius;
return smoothstep(0.0, blur, t);
}
float sat(float t) {
return clamp(t, 0.0, 1.0);
}
float remap01(float a, float b, float t) {
return sat((t - a) / (b - a));
}
float remap(float a, float b, float c, float d, float t) {
return remap01(a, b, t) * (d - c) + c;
}
// Function from Iñigo Quiles
// https://www.shadertoy.com/view/MsS3Wc
vec3 hsb2rgb( in vec3 c ){
vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),
6.0)-3.0)-1.0,
0.0,
1.0 );
rgb = rgb*rgb*(3.0-2.0*rgb);
return c.z * mix( vec3(1.0), rgb, c.y);
}
vec2 scale2d(vec2 uv, float scale) {
mat2 r = mat2(scale, 0, 0, scale) * mat2(uv.x, uv.y, 0, 0);
return vec2(r[0][0], r[0][1]);
}
vec2 rotate2d(vec2 uv, float angle) {
mat2 r = mat2(cos(angle), -sin(angle), sin(angle), cos(angle)) * mat2(uv.x, uv.y, 0, 0);
return vec2(r[0][0], r[0][1]);
}
float shape(vec2 uv, int N){
uv = uv*2.-1.;
float a = atan(uv.x,uv.y)+PI;
float r = PI * 2.0 /float(N);
return cos(floor(.5+a/r)*r-a)*length(uv);
}
float rand(vec2 uv) {
return fract(sin(dot(uv, vec2(4343.0, 54545.0))) * 4344444.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment