|
| 1 | +#version 120 |
| 2 | +/** Copyright (c) 2022-2024, Harry Huang |
| 3 | + * At GPL-3.0 License |
| 4 | + */ |
| 5 | + |
| 6 | +// Complex fragment shader for TwoColorPolygonBatch with outline and box shadow effect. (Low Quality Version) |
| 7 | + |
| 8 | +varying vec2 v_texCoords; // From VS |
| 9 | +uniform sampler2D u_texture; // From TCPB |
| 10 | +uniform vec4 u_outlineColor; // Required |
| 11 | +uniform float u_outlineWidth; // Required |
| 12 | +uniform float u_outlineAlpha; // Required |
| 13 | +uniform vec4 u_shadowColor; // Required |
| 14 | +uniform ivec2 u_textureSize; // Required |
| 15 | +uniform float u_alpha; // Required |
| 16 | + |
| 17 | +const float c_alphaLow = 0.1; |
| 18 | +const float c_alphaHigh = 0.9; |
| 19 | +const float c_seamCoef = 0.6; |
| 20 | +const float c_outlineOverstate = 10.0; |
| 21 | +const float c_shadowOffset = 2.0; |
| 22 | + |
| 23 | +const vec2 OFFSETS[16] = vec2[16]( |
| 24 | + vec2(-1.000, 0.000), // 0 (Left) - Actually 180, but starting here for comparison |
| 25 | + vec2(-0.924, -0.383), // 22.5 |
| 26 | + vec2(-0.707, -0.707), // 45 (Down-Left) |
| 27 | + vec2(-0.383, -0.924), // 67.5 |
| 28 | + vec2( 0.000, -1.000), // 90 (Down) |
| 29 | + vec2( 0.383, -0.924), // 112.5 |
| 30 | + vec2( 0.707, -0.707), // 135 (Down-Right) |
| 31 | + vec2( 0.924, -0.383), // 157.5 |
| 32 | + vec2( 1.000, 0.000), // 180 (Right) - Actually 0 |
| 33 | + vec2( 0.924, 0.383), // 202.5 |
| 34 | + vec2( 0.707, 0.707), // 225 (Up-Right) |
| 35 | + vec2( 0.383, 0.924), // 247.5 |
| 36 | + vec2( 0.000, 1.000), // 270 (Up) |
| 37 | + vec2(-0.383, 0.924), // 292.5 |
| 38 | + vec2(-0.707, 0.707), // 315 (Up-Left) |
| 39 | + vec2(-0.924, 0.383) // 337.5 |
| 40 | +); |
| 41 | + |
| 42 | +vec4 getSimpleSampleSum(float width) { |
| 43 | + vec4 sum = vec4(0.0); |
| 44 | + for (int i = 0; i < OFFSETS.length(); i++) { |
| 45 | + sum += texture2D(u_texture, v_texCoords + OFFSETS[i] * width / u_textureSize); |
| 46 | + } |
| 47 | + return sum; |
| 48 | +} |
| 49 | + |
| 50 | +vec4 getOutlined(vec4 sum, vec4 texColor) { |
| 51 | + if (u_outlineColor.a > 0.0 && u_outlineWidth > 0.0 && u_outlineAlpha > 0.0) { |
| 52 | + vec2 relOutlineWidth = vec2(1.0) / u_textureSize * u_outlineWidth; |
| 53 | + vec4 neighbor = sum * c_outlineOverstate; |
| 54 | + if (neighbor.a > c_alphaLow) { |
| 55 | + texColor.rgb = u_outlineColor.rgb; |
| 56 | + texColor.a = min(1.0, neighbor.a) * u_outlineColor.a * u_outlineAlpha; |
| 57 | + } |
| 58 | + } |
| 59 | + return texColor; |
| 60 | +} |
| 61 | + |
| 62 | +vec4 getBoxShadow(vec4 texColor) { |
| 63 | + if (u_shadowColor.a > 0.0) { |
| 64 | + // Sample around to get maximum shadow alpha from neighbors |
| 65 | + vec4 shadowSum = getSimpleSampleSum(c_shadowOffset); |
| 66 | + float maxShadowAlpha = clamp(shadowSum.a, 0.0, 1.0); |
| 67 | + // Only apply shadow where the current pixel is transparent/semi-transparent |
| 68 | + // and there are opaque pixels nearby |
| 69 | + if (maxShadowAlpha > c_alphaLow && texColor.a < c_alphaHigh) { |
| 70 | + float shadowAlpha = maxShadowAlpha * u_shadowColor.a; |
| 71 | + // Blend shadow with current texColor using "over" compositing |
| 72 | + // Shadow sits behind, so only fill where texColor is not already visible |
| 73 | + float blendFactor = shadowAlpha * (1.0 - texColor.a); |
| 74 | + texColor.rgb = mix(texColor.rgb, u_shadowColor.rgb, blendFactor); |
| 75 | + texColor.a = texColor.a + blendFactor; |
| 76 | + } |
| 77 | + } |
| 78 | + return texColor; |
| 79 | +} |
| 80 | + |
| 81 | +void main() { |
| 82 | + vec4 texColor = texture2D(u_texture, v_texCoords); |
| 83 | + |
| 84 | + if (texColor.a < c_alphaHigh) { |
| 85 | + if (texColor.a < c_alphaLow) { |
| 86 | + // Outline effect |
| 87 | + texColor = getOutlined(getSimpleSampleSum(u_outlineWidth),texColor); |
| 88 | + } |
| 89 | + // Box shadow effect |
| 90 | + texColor = getBoxShadow(texColor); |
| 91 | + } else { |
| 92 | + // No effect |
| 93 | + } |
| 94 | + |
| 95 | + // Ultimate composing |
| 96 | + gl_FragColor = texColor; |
| 97 | + gl_FragColor.a *= u_alpha; |
| 98 | +} |
0 commit comments