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.fog
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 fog effect. */
26 data class FogEffectConfig(
27     /** The main shader of the effect. */
28     val shader: RuntimeShader,
29     /** The color grading shader. */
30     val colorGradingShader: RuntimeShader,
31     /** The main lut (color grading) for the effect. */
32     val lut: Bitmap?,
33     /**
34      * The clouds texture, which will be placed in front of the foreground. The texture is expected
35      * to be tileable, and at least 16-bit per channel for render quality.
36      */
37     val cloudsTexture: Bitmap,
38     /**
39      * The fog texture. This will be placed behind the foreground. The texture is expected to be
40      * tileable, and at least 16-bit per channel for render quality.
41      */
42     val fogTexture: Bitmap,
43     /** A bitmap containing the foreground of the image. */
44     val foreground: Bitmap,
45     /** A bitmap containing the background of the image. */
46     val background: Bitmap,
47     /** Pixel density of the display. Used for dithering. */
48     val pixelDensity: Float,
49     /** The amount of the fog. This contributes to the color grading as well. */
50     @FloatRange(from = 0.0, to = 1.0) val intensity: Float,
51     /** The intensity of the color grading. 0: no color grading, 1: color grading in full effect. */
52     @FloatRange(from = 0.0, to = 1.0) val colorGradingIntensity: Float,
53 ) {
54     /**
55      * Constructor for [FogEffectConfig].
56      *
57      * @param assets the application [AssetManager].
58      * @param foreground a bitmap containing the foreground of the image.
59      * @param background a bitmap containing the background of the image.
60      * @param pixelDensity pixel density of the display.
61      * @param intensity initial intensity that affects the amount of fog and color grading. Expected
62      *   range is [0, 1]. You can always change the intensity dynamically. Defaults to 1.
63      */
64     constructor(
65         assets: AssetManager,
66         foreground: Bitmap,
67         background: Bitmap,
68         pixelDensity: Float,
69         intensity: Float = DEFAULT_INTENSITY,
70     ) : this(
71         shader = GraphicsUtils.loadShader(assets, SHADER_PATH),
72         colorGradingShader = GraphicsUtils.loadShader(assets, COLOR_GRADING_SHADER_PATH),
73         lut = GraphicsUtils.loadTexture(assets, LOOKUP_TABLE_TEXTURE_PATH),
74         cloudsTexture = GraphicsUtils.loadTexture(assets, CLOUDS_TEXTURE_PATH)
75                 ?: throw RuntimeException("Clouds texture is missing."),
76         fogTexture = GraphicsUtils.loadTexture(assets, FOG_TEXTURE_PATH)
77                 ?: throw RuntimeException("Fog texture is missing."),
78         foreground,
79         background,
80         pixelDensity,
81         intensity,
82         COLOR_GRADING_INTENSITY
83     )
84 
85     private companion object {
86         private const val SHADER_PATH = "shaders/fog_effect.agsl"
87         private const val COLOR_GRADING_SHADER_PATH = "shaders/color_grading_lut.agsl"
88         private const val LOOKUP_TABLE_TEXTURE_PATH = "textures/lut_rain_and_fog.png"
89         private const val CLOUDS_TEXTURE_PATH = "textures/clouds.png"
90         private const val FOG_TEXTURE_PATH = "textures/fog.png"
91         private const val DEFAULT_INTENSITY = 1f
92         private const val COLOR_GRADING_INTENSITY = 0.7f
93     }
94 }
95