1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.graphics.drawable; 18 19 import android.annotation.ColorInt; 20 import android.graphics.RuntimeShader; 21 import android.graphics.Shader; 22 23 final class RippleShader extends RuntimeShader { 24 private static final String SHADER_UNIFORMS = "uniform vec2 in_origin;\n" 25 + "uniform vec2 in_touch;\n" 26 + "uniform float in_progress;\n" 27 + "uniform float in_maxRadius;\n" 28 + "uniform vec2 in_resolutionScale;\n" 29 + "uniform vec2 in_noiseScale;\n" 30 + "uniform float in_hasMask;\n" 31 + "uniform float in_noisePhase;\n" 32 + "uniform float in_turbulencePhase;\n" 33 + "uniform vec2 in_tCircle1;\n" 34 + "uniform vec2 in_tCircle2;\n" 35 + "uniform vec2 in_tCircle3;\n" 36 + "uniform vec2 in_tRotation1;\n" 37 + "uniform vec2 in_tRotation2;\n" 38 + "uniform vec2 in_tRotation3;\n" 39 + "layout(color) uniform vec4 in_color;\n" 40 + "layout(color) uniform vec4 in_sparkleColor;\n" 41 + "uniform shader in_shader;\n"; 42 private static final String SHADER_LIB = 43 "float triangleNoise(vec2 n) {\n" 44 + " n = fract(n * vec2(5.3987, 5.4421));\n" 45 + " n += dot(n.yx, n.xy + vec2(21.5351, 14.3137));\n" 46 + " float xy = n.x * n.y;\n" 47 + " return fract(xy * 95.4307) + fract(xy * 75.04961) - 1.0;\n" 48 + "}" 49 + "const float PI = 3.1415926535897932384626;\n" 50 + "\n" 51 + "float threshold(float v, float l, float h) {\n" 52 + " return step(l, v) * (1.0 - step(h, v));\n" 53 + "}\n" 54 + "float sparkles(vec2 uv, float t) {\n" 55 + " float n = triangleNoise(uv);\n" 56 + " float s = 0.0;\n" 57 + " for (float i = 0; i < 4; i += 1) {\n" 58 + " float l = i * 0.1;\n" 59 + " float h = l + 0.05;\n" 60 + " float o = sin(PI * (t + 0.35 * i));\n" 61 + " s += threshold(n + o, l, h);\n" 62 + " }\n" 63 + " return saturate(s) * in_sparkleColor.a;\n" 64 + "}\n" 65 + "float softCircle(vec2 uv, vec2 xy, float radius, float blur) {\n" 66 + " float blurHalf = blur * 0.5;\n" 67 + " float d = distance(uv, xy);\n" 68 + " return 1. - smoothstep(1. - blurHalf, 1. + blurHalf, d / radius);\n" 69 + "}\n" 70 + "float softRing(vec2 uv, vec2 xy, float radius, float progress, float blur) {\n" 71 + " float thickness = 0.05 * radius;\n" 72 + " float currentRadius = radius * progress;\n" 73 + " float circle_outer = softCircle(uv, xy, currentRadius + thickness, blur);\n" 74 + " float circle_inner = softCircle(uv, xy, max(currentRadius - thickness, 0.), " 75 + " blur);\n" 76 + " return saturate(circle_outer - circle_inner);\n" 77 + "}\n" 78 + "float subProgress(float start, float end, float progress) {\n" 79 + " float sub = clamp(progress, start, end);\n" 80 + " return (sub - start) / (end - start); \n" 81 + "}\n" 82 + "mat2 rotate2d(vec2 rad){\n" 83 + " return mat2(rad.x, -rad.y, rad.y, rad.x);\n" 84 + "}\n" 85 + "float circle_grid(vec2 resolution, vec2 coord, float time, vec2 center,\n" 86 + " vec2 rotation, float cell_diameter) {\n" 87 + " coord = rotate2d(rotation) * (center - coord) + center;\n" 88 + " coord = mod(coord, cell_diameter) / resolution;\n" 89 + " float normal_radius = cell_diameter / resolution.y * 0.5;\n" 90 + " float radius = 0.65 * normal_radius;\n" 91 + " return softCircle(coord, vec2(normal_radius), radius, radius * 50.0);\n" 92 + "}\n" 93 + "float turbulence(vec2 uv, float t) {\n" 94 + " const vec2 scale = vec2(0.8);\n" 95 + " uv = uv * scale;\n" 96 + " float g1 = circle_grid(scale, uv, t, in_tCircle1, in_tRotation1, 0.17);\n" 97 + " float g2 = circle_grid(scale, uv, t, in_tCircle2, in_tRotation2, 0.2);\n" 98 + " float g3 = circle_grid(scale, uv, t, in_tCircle3, in_tRotation3, 0.275);\n" 99 + " float v = (g1 * g1 + g2 - g3) * 0.5;\n" 100 + " return saturate(0.45 + 0.8 * v);\n" 101 + "}\n"; 102 private static final String SHADER_MAIN = "vec4 main(vec2 p) {\n" 103 + " float fadeIn = subProgress(0., 0.13, in_progress);\n" 104 + " float scaleIn = subProgress(0., 1.0, in_progress);\n" 105 + " float fadeOutNoise = subProgress(0.4, 0.5, in_progress);\n" 106 + " float fadeOutRipple = subProgress(0.4, 1., in_progress);\n" 107 + " vec2 center = mix(in_touch, in_origin, saturate(in_progress * 2.0));\n" 108 + " float ring = softRing(p, center, in_maxRadius, scaleIn, 1.);\n" 109 + " float alpha = min(fadeIn, 1. - fadeOutNoise);\n" 110 + " vec2 uv = p * in_resolutionScale;\n" 111 + " vec2 densityUv = uv - mod(uv, in_noiseScale);\n" 112 + " float turb = turbulence(uv, in_turbulencePhase);\n" 113 + " float sparkleAlpha = sparkles(densityUv, in_noisePhase) * ring * alpha * turb;\n" 114 + " float fade = min(fadeIn, 1. - fadeOutRipple);\n" 115 + " float waveAlpha = softCircle(p, center, in_maxRadius * scaleIn, 1.) * fade " 116 + "* in_color.a;\n" 117 + " vec4 waveColor = vec4(in_color.rgb * waveAlpha, waveAlpha);\n" 118 + " vec4 sparkleColor = vec4(in_sparkleColor.rgb * in_sparkleColor.a, " 119 + "in_sparkleColor.a);\n" 120 + " float mask = in_hasMask == 1. ? in_shader.eval(p).a > 0. ? 1. : 0. : 1.;\n" 121 + " return mix(waveColor, sparkleColor, sparkleAlpha) * mask;\n" 122 + "}"; 123 private static final String SHADER = SHADER_UNIFORMS + SHADER_LIB + SHADER_MAIN; 124 private static final double PI_ROTATE_RIGHT = Math.PI * 0.0078125; 125 private static final double PI_ROTATE_LEFT = Math.PI * -0.0078125; 126 RippleShader()127 RippleShader() { 128 super(SHADER); 129 } 130 setShader(Shader shader)131 public void setShader(Shader shader) { 132 if (shader != null) { 133 setInputShader("in_shader", shader); 134 } 135 setFloatUniform("in_hasMask", shader == null ? 0 : 1); 136 } 137 setRadius(float radius)138 public void setRadius(float radius) { 139 setFloatUniform("in_maxRadius", radius * 2.3f); 140 } 141 setOrigin(float x, float y)142 public void setOrigin(float x, float y) { 143 setFloatUniform("in_origin", x, y); 144 } 145 setTouch(float x, float y)146 public void setTouch(float x, float y) { 147 setFloatUniform("in_touch", x, y); 148 } 149 setProgress(float progress)150 public void setProgress(float progress) { 151 setFloatUniform("in_progress", progress); 152 } 153 154 /** 155 * Continuous offset used as noise phase. 156 */ setNoisePhase(float phase)157 public void setNoisePhase(float phase) { 158 setFloatUniform("in_noisePhase", phase * 0.001f); 159 160 // 161 // Keep in sync with: frameworks/base/libs/hwui/pipeline/skia/AnimatedDrawables.h 162 // 163 final float turbulencePhase = phase; 164 setFloatUniform("in_turbulencePhase", turbulencePhase); 165 final float scale = 1.5f; 166 setFloatUniform("in_tCircle1", 167 (float) (scale * 0.5 + (turbulencePhase * 0.01 * Math.cos(scale * 0.55))), 168 (float) (scale * 0.5 + (turbulencePhase * 0.01 * Math.sin(scale * 0.55)))); 169 setFloatUniform("in_tCircle2", 170 (float) (scale * 0.2 + (turbulencePhase * -0.0066 * Math.cos(scale * 0.45))), 171 (float) (scale * 0.2 + (turbulencePhase * -0.0066 * Math.sin(scale * 0.45)))); 172 setFloatUniform("in_tCircle3", 173 (float) (scale + (turbulencePhase * -0.0066 * Math.cos(scale * 0.35))), 174 (float) (scale + (turbulencePhase * -0.0066 * Math.sin(scale * 0.35)))); 175 final double rotation1 = turbulencePhase * PI_ROTATE_RIGHT + 1.7 * Math.PI; 176 setFloatUniform("in_tRotation1", 177 (float) Math.cos(rotation1), (float) Math.sin(rotation1)); 178 final double rotation2 = turbulencePhase * PI_ROTATE_LEFT + 2 * Math.PI; 179 setFloatUniform("in_tRotation2", 180 (float) Math.cos(rotation2), (float) Math.sin(rotation2)); 181 final double rotation3 = turbulencePhase * PI_ROTATE_RIGHT + 2.75 * Math.PI; 182 setFloatUniform("in_tRotation3", 183 (float) Math.cos(rotation3), (float) Math.sin(rotation3)); 184 } 185 186 /** 187 * Color of the circle that's under the sparkles. Sparkles will always be white. 188 */ setColor(@olorInt int colorInt, @ColorInt int sparkleColorInt)189 public void setColor(@ColorInt int colorInt, @ColorInt int sparkleColorInt) { 190 setColorUniform("in_color", colorInt); 191 setColorUniform("in_sparkleColor", sparkleColorInt); 192 } 193 setResolution(float w, float h)194 public void setResolution(float w, float h) { 195 final float densityScale = 2.1f; 196 setFloatUniform("in_resolutionScale", 1f / w, 1f / h); 197 setFloatUniform("in_noiseScale", densityScale / w, densityScale / h); 198 } 199 } 200