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 
17 package com.google.android.wallpaper.weathereffects.graphics.snow
18 
19 import android.content.Context
20 import android.graphics.Bitmap
21 import android.graphics.RuntimeShader
22 import androidx.annotation.FloatRange
23 import com.google.android.wallpaper.weathereffects.graphics.utils.GraphicsUtils
24 
25 /** Configuration for a snow effect. */
26 data class SnowEffectConfig(
27     /** The main shader of the effect. */
28     val shader: RuntimeShader,
29     /** The shader of accumulated snow effect. */
30     val accumulatedSnowShader: RuntimeShader,
31     /** The color grading shader. */
32     val colorGradingShader: RuntimeShader,
33     /**
34      * The noise texture, which will be used to add fluffiness to the snow flakes. The texture is
35      * expected to be tileable, and at least 16-bit per channel for render quality.
36      */
37     val noiseTexture: Bitmap,
38     /** The main lut (color grading) for the effect. */
39     val lut: Bitmap?,
40     /** A bitmap containing the foreground of the image. */
41     val foreground: Bitmap,
42     /** A bitmap containing the background of the image. */
43     val background: Bitmap,
44     /** The amount of the snow flakes. This contributes to the color grading as well. */
45     @FloatRange(from = 0.0, to = 1.0) val intensity: Float,
46     /** The intensity of the color grading. 0: no color grading, 1: color grading in full effect. */
47     @FloatRange(from = 0.0, to = 1.0) val colorGradingIntensity: Float,
48     /** Max thickness for the accumulated snow. */
49     val maxAccumulatedSnowThickness: Float,
50 ) {
51     /**
52      * Constructor for [SnowEffectConfig].
53      *
54      * @param context the application context.
55      * @param foreground a bitmap containing the foreground of the image.
56      * @param background a bitmap containing the background of the image.
57      * @param intensity initial intensity that affects the amount of snow flakes and color grading.
58      *   Expected range is [0, 1]. You can always change the intensity dynamically. Defaults to 1.
59      */
60     constructor(
61         context: Context,
62         foreground: Bitmap,
63         background: Bitmap,
64         intensity: Float = DEFAULT_INTENSITY,
65     ) : this(
66         shader = GraphicsUtils.loadShader(context.assets, SHADER_PATH),
67         accumulatedSnowShader =
68             GraphicsUtils.loadShader(context.assets, ACCUMULATED_SNOW_SHADER_PATH),
69         colorGradingShader = GraphicsUtils.loadShader(context.assets, COLOR_GRADING_SHADER_PATH),
70         noiseTexture = GraphicsUtils.loadTexture(context.assets, NOISE_TEXTURE_PATH)
71                 ?: throw RuntimeException("Noise texture is missing."),
72         lut = GraphicsUtils.loadTexture(context.assets, LOOKUP_TABLE_TEXTURE_PATH),
73         foreground,
74         background,
75         intensity,
76         COLOR_GRADING_INTENSITY,
77         MAX_SNOW_THICKNESS
78     )
79 
80     private companion object {
81         private const val SHADER_PATH = "shaders/snow_effect.agsl"
82         private const val ACCUMULATED_SNOW_SHADER_PATH = "shaders/snow_accumulation.agsl"
83         private const val COLOR_GRADING_SHADER_PATH = "shaders/color_grading_lut.agsl"
84         private const val NOISE_TEXTURE_PATH = "textures/clouds.png"
85         private const val LOOKUP_TABLE_TEXTURE_PATH = "textures/lut_rain_and_fog.png"
86         private const val DEFAULT_INTENSITY = 1f
87         private const val COLOR_GRADING_INTENSITY = 0.7f
88         private const val MAX_SNOW_THICKNESS = 10f
89     }
90 }
91