Skip to content

Commit e54b590

Browse files
authored
Merge pull request #399 from cwillisf/fix-brightness-effect
Change brightness effect to match Scratch 2.0 in 2D mode
2 parents f1a7aab + 355a8c5 commit e54b590

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

src/shaders/sprite.frag

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ uniform sampler2D u_skin;
4545

4646
varying vec2 v_texCoord;
4747

48-
#if !defined(DRAW_MODE_silhouette) && (defined(ENABLE_color) || defined(ENABLE_brightness))
48+
#if !defined(DRAW_MODE_silhouette) && (defined(ENABLE_color))
4949
// Branchless color conversions based on code from:
5050
// http://www.chilliant.com/rgb2hsv.html by Ian Taylor
5151
// Based in part on work by Sam Hocevar and Emil Persson
5252
// See also: https://en.wikipedia.org/wiki/HSL_and_HSV#Formal_derivation
5353

54-
// Smaller values can cause problems with "color" and "brightness" effects on some mobile devices
54+
// Smaller values can cause problems on some mobile devices
5555
const float epsilon = 1e-3;
5656

5757
// Convert an RGB color to Hue, Saturation, and Value.
@@ -103,7 +103,7 @@ vec3 convertHSV2RGB(vec3 hsv)
103103
float c = hsv.z * hsv.y;
104104
return rgb * c + hsv.z - c;
105105
}
106-
#endif // !defined(DRAW_MODE_silhouette) && (defined(ENABLE_color) || defined(ENABLE_brightness))
106+
#endif // !defined(DRAW_MODE_silhouette) && (defined(ENABLE_color))
107107

108108
const vec2 kCenter = vec2(0.5, 0.5);
109109

@@ -164,31 +164,27 @@ void main()
164164
gl_FragColor = u_silhouetteColor;
165165
#else // DRAW_MODE_silhouette
166166

167-
#if defined(ENABLE_color) || defined(ENABLE_brightness)
167+
#if defined(ENABLE_color)
168168
{
169169
vec3 hsv = convertRGB2HSV(gl_FragColor.xyz);
170170

171-
#ifdef ENABLE_color
172-
{
173-
// this code forces grayscale values to be slightly saturated
174-
// so that some slight change of hue will be visible
175-
const float minLightness = 0.11 / 2.0;
176-
const float minSaturation = 0.09;
177-
if (hsv.z < minLightness) hsv = vec3(0.0, 1.0, minLightness);
178-
else if (hsv.y < minSaturation) hsv = vec3(0.0, minSaturation, hsv.z);
171+
// this code forces grayscale values to be slightly saturated
172+
// so that some slight change of hue will be visible
173+
const float minLightness = 0.11 / 2.0;
174+
const float minSaturation = 0.09;
175+
if (hsv.z < minLightness) hsv = vec3(0.0, 1.0, minLightness);
176+
else if (hsv.y < minSaturation) hsv = vec3(0.0, minSaturation, hsv.z);
179177

180-
hsv.x = mod(hsv.x + u_color, 1.0);
181-
if (hsv.x < 0.0) hsv.x += 1.0;
182-
}
183-
#endif // ENABLE_color
184-
185-
#ifdef ENABLE_brightness
186-
hsv.z = clamp(hsv.z + u_brightness, 0.0, 1.0);
187-
#endif // ENABLE_brightness
178+
hsv.x = mod(hsv.x + u_color, 1.0);
179+
if (hsv.x < 0.0) hsv.x += 1.0;
188180

189181
gl_FragColor.rgb = convertHSV2RGB(hsv);
190182
}
191-
#endif // defined(ENABLE_color) || defined(ENABLE_brightness)
183+
#endif // defined(ENABLE_color)
184+
185+
#if defined(ENABLE_brightness)
186+
gl_FragColor.rgb = clamp(gl_FragColor.rgb + vec3(u_brightness), vec3(0), vec3(1));
187+
#endif // defined(ENABLE_brightness)
192188

193189
#ifdef DRAW_MODE_colorMask
194190
vec3 maskDistance = abs(gl_FragColor.rgb - u_colorMask);

0 commit comments

Comments
 (0)