prosperon/shaders/9slice.cg

40 lines
1.1 KiB
Plaintext
Raw Normal View History

2024-07-09 01:03:39 -05:00
@block vert
2024-10-19 18:37:27 -05:00
uniform vec4 rect; // uv rect
2024-07-09 01:03:39 -05:00
void vert()
{
uv = (uv*rect.zw)+rect.xy;
}
@end
@block frag
2024-10-19 18:37:27 -05:00
uniform vec4 border; // border given as [left,down,right,top], in uv coordinates of texture, so (pixel)border.x/texture.x, etc
uniform vec2 win_tex_scale; // size of the drawing area over the size of the tex
2024-07-09 13:48:15 -05:00
uniform vec4 shade;
2024-10-19 18:37:27 -05:00
uniform vec4 frag_rect;
2024-07-09 13:48:15 -05:00
2024-10-19 18:37:27 -05:00
float map(float value, float originalMin, float originalMax, float newMin, float newMax) {
return (value - originalMin) / (originalMax - originalMin) * (newMax - newMin) + newMin;
}
2024-07-09 01:03:39 -05:00
vec2 uv9slice(vec2 uv, vec2 s, vec4 b)
{
vec2 t = clamp((s * uv - b.xy) / (s - b.xy - b.zw), 0., 1.);
2024-10-19 18:37:27 -05:00
return mix(uv * s, 1. - s * (1. - uv), t);
2024-07-09 01:03:39 -05:00
}
void frag()
{
2024-10-19 18:37:27 -05:00
vec2 guv;
guv.x = map(uv.x, frag_rect.x, frag_rect.x+frag_rect.z, 0, 1);
guv.y = map(uv.y, frag_rect.y, frag_rect.y+frag_rect.w, 0, 1);
vec2 nuv = uv9slice(guv, win_tex_scale, border);
nuv.x = map(nuv.x, 0, 1, frag_rect.x, frag_rect.x+frag_rect.z);
nuv.y = map(nuv.y, 0, 1, frag_rect.y, frag_rect.y+frag_rect.w);
color = texture(sampler2D(diffuse,smp), nuv);
2024-07-09 01:03:39 -05:00
}
2024-10-19 18:37:27 -05:00
2024-07-09 01:03:39 -05:00
@end
2024-10-19 18:37:27 -05:00
#include <base.cg>