blob: 716e1272f87191eea81feb5fbbf31a3e9f9eeb03 [file] [log] [blame]
Ahan Wu67e7f102019-01-14 20:38:14 +08001precision mediump float;
2
Ahan Wu4c9a5502019-03-04 15:35:28 +08003// The actual wallpaper texture.
Ahan Wu67e7f102019-01-14 20:38:14 +08004uniform sampler2D uTexture;
Ahan Wu4c9a5502019-03-04 15:35:28 +08005
6// The 85th percenile for the luminance histogram of the image (a value between 0 and 1).
7// This value represents the point in histogram that includes 85% of the pixels of the image.
8uniform float uPer85;
9
10// Reveal is the animation value that goes from 1 (the image is hidden) to 0 (the image is visible).
Ahan Wu67e7f102019-01-14 20:38:14 +080011uniform float uReveal;
Ahan Wu4c9a5502019-03-04 15:35:28 +080012
13// The opacity of locked screen (constant value).
Ahan Wu67e7f102019-01-14 20:38:14 +080014uniform float uAod2Opacity;
15varying vec2 vTextureCoordinates;
16
Ahan Wu4c9a5502019-03-04 15:35:28 +080017/*
18 * Calculates the relative luminance of the pixel.
19 */
Ahan Wu67e7f102019-01-14 20:38:14 +080020vec3 luminosity(vec3 color) {
21 float lum = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
22 return vec3(lum);
23}
24
25vec4 transform(vec3 diffuse) {
Ahan Wu4c9a5502019-03-04 15:35:28 +080026 // Getting the luminance for this pixel
Ahan Wu67e7f102019-01-14 20:38:14 +080027 vec3 lum = luminosity(diffuse);
Ahan Wu4c9a5502019-03-04 15:35:28 +080028
29 /*
30 * while the reveal > per85, it shows the luminance image (B&W image)
31 * then when moving passed that value, the image gets colored.
32 */
33 float trans = smoothstep(0., uPer85, uReveal);
34 diffuse = mix(diffuse, lum, trans);
35
36 // 'lower' value represents the capped 'reveal' value to the range [0, per85]
37 float selector = step(uPer85, uReveal);
38 float lower = mix(uReveal, uPer85, selector);
39
40 /*
41 * Remaps image:
42 * - from reveal=1 to reveal=per85 => lower=per85, diffuse=luminance
43 * That means that remaps black and white image pixel
44 * from a possible values of [0,1] to [per85, 1] (if the pixel is darker than per85,
45 * it's gonna be black, if it's between per85 and 1, it's gonna be gray
46 * and if it's 1 it's gonna be white).
47 * - from reveal=per85 to reveal=0 => lower=reveal, 'diffuse' changes from luminance to color
48 * That means that remaps each image pixel color (rgb)
49 * from a possible values of [0,1] to [lower, 1] (if the pixel color is darker than 'lower',
50 * it's gonna be 0, if it's between 'lower' and 1, it's gonna be remap to a value
51 * between 0 and 1 and if it's 1 it's gonna be 1).
52 * - if reveal=0 => lower=0, diffuse=color image
53 * The image is shown as it is, colored.
54 */
55 vec3 remaps = smoothstep(lower, 1., diffuse);
56
57 // Interpolate between diffuse and remaps using reveal to avoid over saturation.
58 diffuse = mix(diffuse, remaps, uReveal);
59
60 /*
61 * Fades in the pixel value:
62 * - if reveal=1 => fadeInOpacity=0
63 * - from reveal=1 to reveal=per85 => 0<=fadeInOpacity<=1
64 * - if reveal>per85 => fadeInOpacity=1
65 */
66 float fadeInOpacity = 1. - smoothstep(uPer85, 1., uReveal);
67 diffuse *= uAod2Opacity * fadeInOpacity;
68
Ahan Wu67e7f102019-01-14 20:38:14 +080069 return vec4(diffuse.r, diffuse.g, diffuse.b, 1.);
70}
71
72void main() {
Ahan Wu4c9a5502019-03-04 15:35:28 +080073 // gets the pixel value of the wallpaper for this uv coordinates on screen.
Ahan Wu67e7f102019-01-14 20:38:14 +080074 vec4 fragColor = texture2D(uTexture, vTextureCoordinates);
75 gl_FragColor = transform(fragColor.rgb);
76}