1/*
2 * Copyright (C) 2024 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/**
18 * Draw a single splash.
19 * @param cellUv Range of [-0.5, 0.5]: left bottom (-0.5, -0.5), right top (0.5, 0.5).
20 * @param cellTime Normalized cellTime range of [0, 1].
21 */
22float drawSplash(vec2 cellUv, float cellTime) {
23    /** 0. Adjust UV and time. */
24    cellUv = cellUv * 0.5;
25    cellUv += 0.1;
26    float t = 0.408 + cellTime * 4.;
27
28    /** 1. Start of drawing a splash */
29
30    // Draw one side of a splash (a crescent) using two circles, by overlapping them with a slight
31    // offset. Then we reflect the splash to get the full splash.
32
33    // Reflect the splash.
34    vec2 uvAbs = abs(cellUv);
35
36    // Center of the splash (of the right splash)
37    vec2 center = vec2(0.22, -0.01);
38    float splashMaskCircle1 = sdfCircle(uvAbs - center, 0.164);
39    float splashMaskCircle2 = sdfCircle(uvAbs - vec2(0.04, 0.) - center, 0.164);
40
41    splashMaskCircle1 = smoothstep(0.052, 0.12, splashMaskCircle1);
42    splashMaskCircle2 = smoothstep(0.1, 0.152, splashMaskCircle2);
43
44    // Here, we have a full splash that covers the entire cell
45    float splash = splashMaskCircle1 - splashMaskCircle2;
46
47    // Mask the splash so that it doesn't cover the entire cell.
48    float circleMask = sdfCircle(cellUv - vec2(0., 0.15), -0.004);
49    float splashColor = splash * smoothstep(0.152, 0.1, circleMask);
50
51    // Another mask for the splash to reveal it vertically.
52    float maskThickHalf = 0.052;
53    float maskFade = 0.05;
54    vec2 maskMiddle = vec2(-0.1, -0.5);
55    maskMiddle.y *= sin(mod(t, 4.4));
56
57    float mask =
58       smoothstep(maskMiddle.y - maskThickHalf - maskFade,
59                  maskMiddle.y - maskThickHalf,
60                  cellUv.y) -
61       smoothstep(maskMiddle.y + maskThickHalf,
62                  maskMiddle.y + maskThickHalf + maskFade,
63                  cellUv.y);
64    splashColor *= mask;
65    /** End of drawing a splash */
66
67    /** 2. Start of drawing a vertical line of the splash */
68
69    // Draw the vertical line.
70    float verticalLine = 0.035;
71    float lineCenter = 0.192;
72    float lineColor = 0.4 *
73       (smoothstep(-verticalLine, 0., cellUv.x) - smoothstep(0., verticalLine, cellUv.x)) *
74        smoothstep(0.008, -0.156, sdfCircle(cellUv - vec2(0., lineCenter), 0.164));
75
76    // Mask the vertical line to reveal it vertically.
77    float lineMaskThickHalf = 0.124;
78    float lineMaskFade = 0.170;
79    vec2 lineMaskMiddle = vec2(-0.01, 2.71);
80    lineMaskMiddle.y *= sin(mod(t, 4.4) - 0.168);
81    float lineMask =
82       smoothstep(lineMaskMiddle.y - lineMaskThickHalf - lineMaskFade,
83                  lineMaskMiddle.y - lineMaskThickHalf,
84                  cellUv.y) -
85       smoothstep(lineMaskMiddle.y + lineMaskThickHalf,
86                  lineMaskMiddle.y + lineMaskThickHalf + lineMaskFade,
87                  cellUv.y);
88    float line = lineColor * lineMask;
89
90    /** End of drawing the vertical line of the splash */
91
92    /** 3. Composite the splash and the line. */
93    return splashColor * (1. - line) + line;
94}
95