Skip to content

Commit a42e39b

Browse files
authored
fixed shader render issue when border is 0 (#571)
Fixed the webgl shaders that draw borders, now it doing a check that when all border values are zero its skips the border calculations and draw functions.
2 parents 12d565e + 1757678 commit a42e39b

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

src/core/shaders/webgl/RoundedWithBorder.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export const RoundedWithBorder: WebGlShaderType<RoundedWithBorderProps> = {
6565
varying vec4 v_innerRadius;
6666
varying vec2 v_innerSize;
6767
varying vec2 v_halfDimensions;
68+
varying float v_borderZero;
6869
6970
void main() {
7071
vec2 normalized = a_position * u_pixelRatio;
@@ -76,14 +77,18 @@ export const RoundedWithBorder: WebGlShaderType<RoundedWithBorderProps> = {
7677
7778
v_halfDimensions = u_dimensions * 0.5;
7879
79-
v_innerRadius = vec4(
80-
max(0.0, u_radius.x - max(u_borderWidth.x, u_borderWidth.w) - 0.5),
81-
max(0.0, u_radius.y - max(u_borderWidth.x, u_borderWidth.y) - 0.5),
82-
max(0.0, u_radius.z - max(u_borderWidth.z, u_borderWidth.y) - 0.5),
83-
max(0.0, u_radius.w - max(u_borderWidth.z, u_borderWidth.w) - 0.5)
84-
);
80+
v_borderZero = u_borderWidth == vec4(0.0) ? 1.0 : 0.0;
8581
86-
v_innerSize = (vec2(u_dimensions.x - (u_borderWidth[3] + u_borderWidth[1]) + 1.0, u_dimensions.y - (u_borderWidth[0] + u_borderWidth[2])) - 2.0) * 0.5;
82+
if(v_borderZero == 0.0) {
83+
v_innerRadius = vec4(
84+
max(0.0, u_radius.x - max(u_borderWidth.x, u_borderWidth.w) - 0.5),
85+
max(0.0, u_radius.y - max(u_borderWidth.x, u_borderWidth.y) - 0.5),
86+
max(0.0, u_radius.z - max(u_borderWidth.z, u_borderWidth.y) - 0.5),
87+
max(0.0, u_radius.w - max(u_borderWidth.z, u_borderWidth.w) - 0.5)
88+
);
89+
90+
v_innerSize = (vec2(u_dimensions.x - (u_borderWidth[3] + u_borderWidth[1]) + 1.0, u_dimensions.y - (u_borderWidth[0] + u_borderWidth[2])) - 2.0) * 0.5;
91+
}
8792
8893
gl_Position = vec4(normalized.x * screenSpace.x - 1.0, normalized.y * -abs(screenSpace.y) + 1.0, 0.0, 1.0);
8994
gl_Position.y = -sign(screenSpace.y) * gl_Position.y;
@@ -114,6 +119,7 @@ export const RoundedWithBorder: WebGlShaderType<RoundedWithBorderProps> = {
114119
varying vec2 v_halfDimensions;
115120
varying vec4 v_innerRadius;
116121
varying vec2 v_innerSize;
122+
varying float v_borderZero;
117123
118124
float roundedBox(vec2 p, vec2 s, vec4 r) {
119125
r.xy = (p.x > 0.0) ? r.yz : r.xw;
@@ -130,6 +136,11 @@ export const RoundedWithBorder: WebGlShaderType<RoundedWithBorderProps> = {
130136
131137
float outerAlpha = 1.0 - smoothstep(0.0, 1.0, outerDist);
132138
139+
if(v_borderZero == 1.0) {
140+
gl_FragColor = mix(vec4(0.0), color, outerAlpha) * u_alpha;
141+
return;
142+
}
143+
133144
boxUv.x += u_borderWidth.y > u_borderWidth.w ? (u_borderWidth.y - u_borderWidth.w) * 0.5 : -(u_borderWidth.w - u_borderWidth.y) * 0.5;
134145
boxUv.y += u_borderWidth.z > u_borderWidth.x ? ((u_borderWidth.z - u_borderWidth.x) * 0.5 + 0.5) : -(u_borderWidth.x - u_borderWidth.z) * 0.5;
135146

src/core/shaders/webgl/RoundedWithBorderAndShadow.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export const RoundedWithBorderAndShadow: WebGlShaderType<RoundedWithBorderAndSha
6868
varying vec4 v_innerRadius;
6969
varying vec2 v_innerSize;
7070
varying vec2 v_halfDimensions;
71+
varying float v_borderZero;
7172
7273
void main() {
7374
vec2 screenSpace = vec2(2.0 / u_resolution.x, -2.0 / u_resolution.y);
@@ -81,18 +82,23 @@ export const RoundedWithBorderAndShadow: WebGlShaderType<RoundedWithBorderAndSha
8182
8283
v_halfDimensions = u_dimensions * 0.5;
8384
84-
v_innerRadius = vec4(
85-
max(0.0, u_radius.x - max(u_borderWidth.x, u_borderWidth.w) - 0.5),
86-
max(0.0, u_radius.y - max(u_borderWidth.x, u_borderWidth.y) - 0.5),
87-
max(0.0, u_radius.z - max(u_borderWidth.z, u_borderWidth.y) - 0.5),
88-
max(0.0, u_radius.w - max(u_borderWidth.z, u_borderWidth.w) - 0.5)
89-
);
90-
91-
v_innerSize = (vec2(u_dimensions.x - (u_borderWidth[3] + u_borderWidth[1]) - 1.0, u_dimensions.y - (u_borderWidth[0] + u_borderWidth[2])) - 2.0) * 0.5;
92-
9385
v_color = a_color;
9486
v_nodeCoords = a_nodeCoords + (screenSpace + shadowEdge) / (u_dimensions);
9587
v_textureCoords = a_textureCoords + (screenSpace + shadowEdge) / (u_dimensions);
88+
89+
v_borderZero = u_borderWidth == vec4(0.0) ? 1.0 : 0.0;
90+
91+
92+
if(v_borderZero == 0.0) {
93+
v_innerRadius = vec4(
94+
max(0.0, u_radius.x - max(u_borderWidth.x, u_borderWidth.w) - 0.5),
95+
max(0.0, u_radius.y - max(u_borderWidth.x, u_borderWidth.y) - 0.5),
96+
max(0.0, u_radius.z - max(u_borderWidth.z, u_borderWidth.y) - 0.5),
97+
max(0.0, u_radius.w - max(u_borderWidth.z, u_borderWidth.w) - 0.5)
98+
);
99+
100+
v_innerSize = (vec2(u_dimensions.x - (u_borderWidth[3] + u_borderWidth[1]) - 1.0, u_dimensions.y - (u_borderWidth[0] + u_borderWidth[2])) - 2.0) * 0.5;
101+
}
96102
}
97103
`,
98104
fragment: `
@@ -122,6 +128,7 @@ export const RoundedWithBorderAndShadow: WebGlShaderType<RoundedWithBorderAndSha
122128
varying vec2 v_halfDimensions;
123129
varying vec4 v_innerRadius;
124130
varying vec2 v_innerSize;
131+
varying float v_borderZero;
125132
126133
float roundedBox(vec2 p, vec2 s, vec4 r) {
127134
r.xy = (p.x > 0.0) ? r.yz : r.xw;
@@ -146,15 +153,20 @@ export const RoundedWithBorderAndShadow: WebGlShaderType<RoundedWithBorderAndSha
146153
147154
float outerAlpha = 1.0 - smoothstep(0.0, 1.0, outerDist);
148155
156+
float shadowAlpha = shadowBox(boxUv - u_shadow.xy, v_halfDimensions + u_shadow.w, u_radius + u_shadow.z);
157+
vec4 shadow = mix(vec4(0.0), u_shadowColor, shadowAlpha);
158+
159+
if(v_borderZero == 1.0) {
160+
gl_FragColor = mix(shadow, color, outerAlpha) * u_alpha;
161+
return;
162+
}
163+
149164
boxUv.x += u_borderWidth.y > u_borderWidth.w ? (u_borderWidth.y - u_borderWidth.w) * 0.5 : -(u_borderWidth.w - u_borderWidth.y) * 0.5;
150165
boxUv.y += u_borderWidth.z > u_borderWidth.x ? ((u_borderWidth.z - u_borderWidth.x) * 0.5 + 0.5) : -(u_borderWidth.x - u_borderWidth.z) * 0.5;
151166
152167
float innerDist = roundedBox(boxUv, v_innerSize, v_innerRadius);
153168
float innerAlpha = 1.0 - smoothstep(0.0, 1.0, innerDist);
154169
155-
float shadowAlpha = shadowBox(boxUv - u_shadow.xy, v_halfDimensions + u_shadow.w, u_radius + u_shadow.z);
156-
157-
vec4 shadow = mix(vec4(0.0), u_shadowColor, shadowAlpha);
158170
vec4 resColor = mix(u_borderColor, color, innerAlpha);
159171
resColor = mix(shadow, resColor, outerAlpha);
160172
gl_FragColor = resColor * u_alpha;

0 commit comments

Comments
 (0)