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.rain
18 
19 import android.content.res.AssetManager
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 rain effect. */
26 data class RainEffectConfig(
27     /** The first layer of the shader, rain showering in the environment. */
28     val rainShowerShader: RuntimeShader,
29     /** The second layer of the shader, rain running on the glass window. */
30     val glassRainShader: RuntimeShader,
31     /** The final layer of the shader, which adds color grading. */
32     val colorGradingShader: RuntimeShader,
33     /** Shader that evaluates the outline based on the alpha value. */
34     val outlineShader: RuntimeShader,
35     /** The main lut (color grading) for the effect. */
36     val lut: Bitmap?,
37     /** A bitmap containing the foreground of the image. */
38     val foreground: Bitmap,
39     /** A bitmap containing the background of the image. */
40     val background: Bitmap,
41     /** The amount of the rain. This contributes to the color grading as well. */
42     @FloatRange(from = 0.0, to = 1.0) val intensity: Float,
43     /** The intensity of the color grading. 0: no color grading, 1: color grading in full effect. */
44     @FloatRange(from = 0.0, to = 1.0) val colorGradingIntensity: Float,
45 ) {
46     /**
47      * Constructor for [RainEffectConfig].
48      *
49      * @param assets asset manager.
50      * @param foreground a bitmap containing the foreground of the image.
51      * @param background a bitmap containing the background of the image.
52      * @param intensity initial intensity that affects the amount of rain and color grading.
53      *   Expected range is [0, 1]. You can always change the intensity dynamically. Defaults to 1.
54      */
55     constructor(
56         assets: AssetManager,
57         foreground: Bitmap,
58         background: Bitmap,
59         intensity: Float = DEFAULT_INTENSITY,
60     ) : this(
61         rainShowerShader = GraphicsUtils.loadShader(assets, RAIN_SHOWER_LAYER_SHADER_PATH),
62         glassRainShader = GraphicsUtils.loadShader(assets, GLASS_RAIN_LAYER_SHADER_PATH),
63         colorGradingShader = GraphicsUtils.loadShader(assets, COLOR_GRADING_SHADER_PATH),
64         outlineShader = GraphicsUtils.loadShader(assets, OUTLINE_SHADER_PATH),
65         lut = GraphicsUtils.loadTexture(assets, LOOKUP_TABLE_TEXTURE_PATH),
66         foreground,
67         background,
68         intensity,
69         COLOR_GRADING_INTENSITY
70     )
71 
72     private companion object {
73         private const val RAIN_SHOWER_LAYER_SHADER_PATH = "shaders/rain_shower_layer.agsl"
74         private const val GLASS_RAIN_LAYER_SHADER_PATH = "shaders/rain_glass_layer.agsl"
75         private const val COLOR_GRADING_SHADER_PATH = "shaders/color_grading_lut.agsl"
76         private const val OUTLINE_SHADER_PATH = "shaders/outline.agsl"
77         private const val LOOKUP_TABLE_TEXTURE_PATH = "textures/lut_rain_and_fog.png"
78         private const val DEFAULT_INTENSITY = 1f
79         private const val COLOR_GRADING_INTENSITY = 0.7f
80     }
81 }
82