1/*
2 * Copyright (C) 2023 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
17uniform shader foreground;
18uniform half snowThickness;
19uniform half scale;
20uniform half screenWidth;
21
22#include "shaders/simplex2d.agsl"
23#include "shaders/utils.agsl"
24
25float random(vec2 uv) {
26    return fract(sin(dot(uv, vec2(14.53898, 56.233))) * 45312.644263742);
27}
28
29vec4 main(float2 fragCoord) {
30    // fragCoord should be already the adjusted UVs to have the expected rect of the image.
31    vec2 uv = fragCoord * scale / screenWidth;
32    float variation = 0.3 + simplex2d(11. * uv);
33    float distance = variation * snowThickness;
34
35    float aN = foreground.eval(fragCoord + vec2(0., distance)).a;
36    float aS = foreground.eval(fragCoord + vec2(0., -distance)).a;
37    float dY = (aN - aS) * 0.5;
38    dY = max(dY, 0.0);
39
40    float accumulatedSnow = smoothstep(0.1, 1.8, dY * 5.0);
41    vec4 color = vec4(0., 0., 0., 1.);
42    color.r = accumulatedSnow;
43    color.g = random(uv);
44    color.b = variation;
45    return color;
46}
47