prosperon/source/shaders/9slice.sglsl

65 lines
1.3 KiB
Plaintext
Raw Normal View History

@vs vs9
in vec2 vert;
in vec2 vuv;
in vec4 vborder;
in vec2 vscale;
in vec4 vcolor;
2023-06-05 10:32:45 -05:00
out vec2 uv;
out vec4 border;
out vec2 scale;
out vec4 fcolor;
uniform vs9_params { mat4 projection; };
void main()
{
gl_Position = projection * vec4(vert, 0.0, 1.0);
uv = vuv;
border = vborder;
scale = vscale;
fcolor = vcolor;
}
@end
@fs fs9
2023-06-05 10:32:45 -05:00
in vec2 uv; /* image uv */
in vec4 border; /* uv length of border, normalized to image dimensions; left, bottom, right, top */
2023-06-28 11:35:41 -05:00
in vec2 scale; /* polygon dimensions ~ texture dimensions */
2023-06-05 10:32:45 -05:00
in vec4 fcolor;
out vec4 color;
uniform texture2D image;
uniform sampler smp;
2023-06-05 10:32:45 -05:00
float map(float value, float min1, float max1, float min2, float max2)
{
return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
}
float processAxis(float coord, float texBorder, float winBorder)
{
if (coord < winBorder)
return map(coord, 0, winBorder, 0, texBorder);
if (coord < 1 - winBorder)
return map(coord, winBorder, 1 - winBorder, texBorder, 1 - texBorder);
return map(coord, 1 - winBorder, 1, 1 - texBorder, 1);
}
2023-06-28 11:35:41 -05:00
vec2 uv9slice(vec2 uv, vec2 s, vec4 b)
2023-06-05 10:32:45 -05:00
{
vec2 t = clamp((s * uv - b.xy) / (s - b.xy - b.zw), 0.0, 1.0);
return mix(uv * s, 1.0 - s * (1.0 - uv), t);
}
void main()
{
2023-06-05 14:48:53 -05:00
vec2 nuv = uv9slice(uv, scale, border);
color = fcolor * texture(sampler2D(image,smp), nuv);
2023-06-05 10:32:45 -05:00
}
@end
@program slice9 vs9 fs9