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     /** Pixel density of the display. Used for dithering. */
38     val pixelDensity: Float,
39     /** The intensity of the color grading. 0: no color grading, 1: color grading in full effect. */
40     @FloatRange(from = 0.0, to = 1.0) val colorGradingIntensity: Float,
41 ) {
42     /**
43      * Constructor for [RainEffectConfig].
44      *
45      * @param assets asset manager,
46      * @param pixelDensity pixel density of the display.
47      */
48     constructor(
49         assets: AssetManager,
50         pixelDensity: Float,
51     ) : this(
52         rainShowerShader = GraphicsUtils.loadShader(assets, RAIN_SHOWER_LAYER_SHADER_PATH),
53         glassRainShader = GraphicsUtils.loadShader(assets, GLASS_RAIN_LAYER_SHADER_PATH),
54         colorGradingShader = GraphicsUtils.loadShader(assets, COLOR_GRADING_SHADER_PATH),
55         outlineShader = GraphicsUtils.loadShader(assets, OUTLINE_SHADER_PATH),
56         lut = GraphicsUtils.loadTexture(assets, LOOKUP_TABLE_TEXTURE_PATH),
57         pixelDensity,
58         COLOR_GRADING_INTENSITY
59     )
60 
61     private companion object {
62         private const val RAIN_SHOWER_LAYER_SHADER_PATH = "shaders/rain_shower_layer.agsl"
63         private const val GLASS_RAIN_LAYER_SHADER_PATH = "shaders/rain_glass_layer.agsl"
64         private const val COLOR_GRADING_SHADER_PATH = "shaders/color_grading_lut.agsl"
65         private const val OUTLINE_SHADER_PATH = "shaders/outline.agsl"
66         private const val LOOKUP_TABLE_TEXTURE_PATH = "textures/rain_lut.png"
67         private const val COLOR_GRADING_INTENSITY = 0.3f
68     }
69 }
70