Skip to content

Commit b3da8fa

Browse files
committed
Use Scratch's workaround for fully transparent pixels
1 parent 95616e4 commit b3da8fa

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/shaders/sprite.frag

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,18 @@ const vec2 kCenter = vec2(0.5, 0.5);
8787

8888
void main()
8989
{
90-
vec4 texColor = texture2D(u_skin, v_texCoord);
90+
vec2 texcoord0 = v_texCoord;
91+
92+
gl_FragColor = texture2D(u_skin, texcoord0);
93+
94+
#if defined(ENABLE_color) || defined(ENABLE_brightness)
95+
// Divide premultiplied alpha values for proper color processing
96+
// Add epsilon to avoid dividing by 0 for fully transparent pixels
97+
gl_FragColor.rgb = clamp(gl_FragColor.rgb / (gl_FragColor.a + epsilon), 0.0, 1.0);
9198

9299
#ifdef ENABLE_color
93100
{
94-
vec3 hsv = convertRGB2HSV(texColor.rgb);
101+
vec3 hsv = convertRGB2HSV(gl_FragColor.rgb);
95102

96103
// Force grayscale values to be slightly saturated
97104
const float minLightness = 0.11 / 2.0;
@@ -102,22 +109,20 @@ void main()
102109
hsv.x = mod(hsv.x + u_color, 1.0);
103110
if (hsv.x < 0.0) hsv.x += 1.0;
104111

105-
texColor.rgb = convertHSV2RGB(hsv);
112+
gl_FragColor.rgb = convertHSV2RGB(hsv);
106113
}
107114
#endif // ENABLE_color
108115

109116
#ifdef ENABLE_brightness
110-
texColor.rgb = clamp(texColor.rgb + vec3(u_brightness), vec3(0), vec3(1));
117+
gl_FragColor.rgb = clamp(gl_FragColor.rgb + vec3(u_brightness), vec3(0), vec3(1));
111118
#endif // ENABLE_brightness
112119

120+
// Re-multiply color values
121+
gl_FragColor.rgb *= gl_FragColor.a + epsilon;
122+
123+
#endif // defined(ENABLE_color) || defined(ENABLE_brightness)
124+
113125
#ifdef ENABLE_ghost
114-
texColor *= u_ghost;
126+
gl_FragColor *= u_ghost;
115127
#endif // ENABLE_ghost
116-
117-
// Set RGB components to zero if the color is fully transparent
118-
// This is a workaround for rendering issues when alpha is zero
119-
if(texColor.a == 0.0)
120-
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
121-
else
122-
gl_FragColor = texColor;
123128
}

test/color_effects.png

2 Bytes
Loading

0 commit comments

Comments
 (0)