1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#ifndef __ASURA_EDITOR_SHADER_H__
#include "../shader.h"
#endif
// ŹָͼƬ
static AsuraEditor::Graphics::ShaderProgram image_slice_shader =
{
R"(
in vec2 _position;
uniform _mvp_matrix;
out vec2 offset; // top-left
void main()
{
gl_Position = _mvp_matrix * vec4(_position, 0, 1);
offset = _position - vec2(0.5, 0.5);
}
)",
R"(
in vec2 offset;
uniform vec4 _sliceline; // l, r, t, b
uniform vec2 _texsize; // w0, h0 in pixel
uniform sampler2D _maintex;
uniform vec2 _area; // w, h
// (a, b]Χڣ10
int factor(float v, float a, float b)
{
return clamp(sign(v-a), 0, 1) * step(-b, -v);
}
void main()
{
float x = offset.x;
float y = offset.y;
float l = _sliceline.x;
float r = _sliceline.y;
float t = _sliceline.z;
float b = _sliceline.w;
float w0 = _texsize.x;
float h0 = _texsize.y;
float w = _area.x;
float h = _area.y;
vec2 uv = vec2(0, 0);
//uv.x *= 0; // x = 0
uv.x *= factor(x, 0, l) * x / w0; // 0<x<=l
uv.x *= factor(x, l, w-r) * (l/w0 + (x-l)/(w-l-r) * (w0-l-r) / w0); // l<x<=w-r
uv.x *= factor(x, w-r, w) * (1 - (w-x)/w0); // w-r<x<=w
//uv.y *= 0; // y = 0
gl_FragColor = texture2D(_maintex, uv);
}
)"
};
|