Skip to content

Commit 1231566

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

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/shaders/sprite.frag

Lines changed: 16 additions & 11 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+
113123
#ifdef ENABLE_ghost
114-
texColor *= u_ghost;
124+
gl_FragColor *= u_ghost;
115125
#endif // ENABLE_ghost
116126

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;
127+
#endif // defined(ENABLE_color) || defined(ENABLE_brightness)
123128
}

0 commit comments

Comments
 (0)