Skip to content

Commit

Permalink
pref: remove shader side seaming effect
Browse files Browse the repository at this point in the history
  • Loading branch information
isHarryh committed Feb 12, 2025
1 parent 08b1bdb commit 344713c
Showing 1 changed file with 8 additions and 50 deletions.
58 changes: 8 additions & 50 deletions assets/shaders/OutlineFragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ uniform vec4 u_shadowColor; // Required
uniform ivec2 u_textureSize; // Required
uniform float u_alpha; // Required

const float c_alphaLv0 = 0.1;
const float c_alphaLv1 = 0.4;
const float c_alphaLv2 = 0.9;
const float c_alphaLow = 0.1;
const float c_alphaHigh = 0.9;
const float c_seamCoef = 0.6;
const float c_outlineOverstate = 10.0;
const float c_shadowOffset = 2.0;
Expand All @@ -30,21 +29,6 @@ const float gaussianNeighborKernel[25] = float[25] (
0.0035434, 0.0158805, 0.0261825, 0.0158805, 0.0035434
);

vec4[24] getSimpleNeighbors(vec2 unitLength) {
vec4 neighbors[24];
int i = 0;
for (int y = -2; y <= 2; y++) {
for (int x = -2; x <= 2; x++) {
if (!(y == 0 && x == 0)) {
vec2 offset = vec2(x, y) * unitLength;
neighbors[i] = texture2D(u_texture, v_texCoords + offset);
i++;
}
}
}
return neighbors;
}

vec4[24] getGaussianNeighbors(vec2 unitLength) {
vec4 neighbors[24];
int ni = 0;
Expand Down Expand Up @@ -76,35 +60,14 @@ vec4 getOutlined() {
if (u_outlineColor.a > 0.0 && u_outlineWidth > 0.0 && u_outlineAlpha > 0.0) {
vec2 relOutlineWidth = vec2(1.0) / u_textureSize * u_outlineWidth;
vec4 neighbor = getGaussianNeighborsSum(relOutlineWidth) * c_outlineOverstate;
if (neighbor.a > c_alphaLv0) {
if (neighbor.a > c_alphaLow) {
texColor.rgb = u_outlineColor.rgb;
texColor.a = min(1.0, neighbor.a) * u_outlineColor.a * u_outlineAlpha;
}
}
return texColor;
}

vec4 getSeamed() {
vec4 texColor = texture2D(u_texture, v_texCoords);
vec2 relPixelSize = vec2(1.0) / u_textureSize;
vec4[24] neighbors = getSimpleNeighbors(relPixelSize);
vec4 sampleColor = vec4(0.0);
int sampleSize = 0;
for (int i = 0; i < neighbors.length(); i++) {
if (neighbors[i].a > c_alphaLv2) {
sampleColor += neighbors[i];
sampleSize++;
}
}
if (sampleSize > 0) {
texColor.rgb = mix(sampleColor.rgb / sampleSize, texColor.rgb, c_seamCoef);
texColor.a = min(1.0, texColor.a * 2.0);
} else {
texColor.a = c_alphaLv2;
}
return texColor;
}

vec4 getBoxShadow() {
if (u_shadowColor.a <= 0.0) {
return vec4(0.0);
Expand All @@ -117,20 +80,15 @@ vec4 getBoxShadow() {
void main() {
vec4 texColor = texture2D(u_texture, v_texCoords);

if (texColor.a < c_alphaLv2) {
if (texColor.a < c_alphaLv0) {
// Outline effect apply on transparent areas
if (texColor.a < c_alphaHigh) {
if (texColor.a < c_alphaLow) {
// Outline effect
texColor = getOutlined();
} else if (texColor.a < c_alphaLv1) {
// No effect apply on these areas
} else {
// Seaming apply on gap areas
texColor = getSeamed();
}
// Box shadow
// Box shadow effect
texColor = mix(getBoxShadow(), texColor, texColor.a);
} else {
// No effect apply on other areas
// No effect
}

// Ultimate composing
Expand Down

0 comments on commit 344713c

Please sign in to comment.