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