prosperon/shaders/9slice.cg

40 lines
1.1 KiB
Plaintext

@block vert
uniform vec4 rect; // uv rect
void vert()
{
uv = (uv*rect.zw)+rect.xy;
}
@end
@block frag
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
uniform vec4 shade;
uniform vec4 frag_rect;
float map(float value, float originalMin, float originalMax, float newMin, float newMax) {
return (value - originalMin) / (originalMax - originalMin) * (newMax - newMin) + newMin;
}
vec2 uv9slice(vec2 uv, vec2 s, vec4 b)
{
vec2 t = clamp((s * uv - b.xy) / (s - b.xy - b.zw), 0., 1.);
return mix(uv * s, 1. - s * (1. - uv), t);
}
void frag()
{
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);
}
@end
#include <base.cg>