[ad_1]
I have been in a position to implement 9Slice for textures when the feel is simply itself, meaning, it doesn’t include any subtexture besides itself. That is the code I am utilizing:
#model 330 core
in vec2 uv;
in vec4 colour;
in vec2 dimension;
in vec4 nine_slice;
uniform sampler2D tex;
uniform float dt;
uniform vec2 mouse_position;
uniform vec2 texture_size;
structure(location = 0) out vec4 out_color;
vec2 uv9slice(vec2 uv, vec2 s, vec4 b) {
vec2 t = clamp((s * uv - b.xy) / (s - b.xy - b.zw), 0., 1.);
return combine(uv * s, 1. - s * (1. - uv), t);
}
vec4 draw_nine_slice() {
vec2 _s = dimension.xy / texture_size;
vec4 _b = nine_slice / texture_size.xxyy;
vec2 _uv = uv9slice(uv, _s, _b);
vec3 _col = vec3(texture(tex, _uv).x);
return vec4(_col, 1.0);
}
void important(void) {
out_color = draw_nine_slice();
}
The place vars are:
- dimension: Measurement of the 9 patch (the imgui window proven on the second picture)
- nine_slice: These are the paddings vec4(left, proper, backside, top)
- texture_size: That is the dimensions of the feel, which shall be mounted. If it’s a non-atlas texture, the dimensions is the dimensions of the entire picture, if it’s a subtexture from an atlas, the dimensions would be the dimension of the subtexture contained in the atlas. That is the feel:
And that is the way it seems with the 9-Slice:
Working instance of single texture 9-Slice
In order you may see, it expands properly. The issue comes once I use my texture atlas (change the white colour of the sub textures with grey, as they might not be seen on this web site white background):
I’ve a approach in my program to get the feel coordinates, dimension and every part from every subtexture within the atlas, and once I render them as a non-9Slice it really works nice, the issue is the shader. If I attempt to run the engine with a subtexture from the atlas, the shader I posted earlier than will not work, because it takes the entire texture as if it was a single one (which works on the previuos case however not this one). That is what occurs when operating with the shader:
Which make sense (do not thoughts the transparency not working). I do know I’ve to vary how the coordinates are calculated within the shader to be relative to the sub-texture coordinates, however I can’t actually appear to grasp the best way to do it, that is what I attempted:
#model 330 core
in vec2 uv;
in vec4 colour;
in vec2 dimension;
in vec4 nine_slice;
uniform sampler2D tex;
uniform float dt;
uniform vec2 mouse_position;
uniform vec2 texture_size; // If drawing a subtexture from an atlas, that is the dimensions of the subtexture
structure(location = 0) out vec4 out_color;
vec2 uv9slice(vec2 _uv, vec2 _s, vec4 _b) {
vec2 _t = clamp((_s * _uv - _b.xz) / (_s - _b.xz - _b.yw), 0.0, 1.0);
vec2 _t_0 = _uv * _s;
vec2 _t_1 = 1.0 - _s * (1.0 - _uv);
return clamp(combine(_t_0, _t_1, _t), vec2(0, 0.75), vec2(0.25, 1.0));
}
vec4 draw_nine_slice() {
vec2 _s = dimension.xy / texture_size;
vec4 _b = nine_slice / texture_size.xxyy;
vec2 _uv = uv9slice(uv, _s, _b);
vec3 _col = vec3(texture(tex, _uv).x);
return vec4(_col, 1.0);
}
void important(void) {
out_color = draw_nine_slice()
}
Values are hardcoded in right here, as a result of I am doing a take a look at. Backside-Left coord of the feel within the atlas is (0.0, 0.75) and Top-Proper is (0.25, 1.0).
I assumed clamping the values inside the sub-texture vary would work, however it doesn’t. I attempted additionally different combos however noting appears to be working, does anybody have an concept on the best way to obtain the primary behaviour with the one texture on the second state of affairs with an atlas?
Tried alternative ways to transform from entire picture coordinates to a sub-space of the picture, one of many makes an attempt is within the description of the problem, no success with any of them.
[ad_2]