• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     /** Pixel density of the display. Used for dithering. */
44     val pixelDensity: Float,
45     /** The intensity of the color grading. 0: no color grading, 1: color grading in full effect. */
46     @FloatRange(from = 0.0, to = 1.0) val colorGradingIntensity: Float,
47 ) {
48     /**
49      * Constructor for [FogEffectConfig].
50      *
51      * @param assets the application [AssetManager].
52      * @param pixelDensity pixel density of the display.
53      */
54     constructor(
55         assets: AssetManager,
56         pixelDensity: Float,
57     ) : this(
58         shader = GraphicsUtils.loadShader(assets, SHADER_PATH),
59         colorGradingShader = GraphicsUtils.loadShader(assets, COLOR_GRADING_SHADER_PATH),
60         lut = GraphicsUtils.loadTexture(assets, LOOKUP_TABLE_TEXTURE_PATH),
61         cloudsTexture =
62             GraphicsUtils.loadTexture(assets, CLOUDS_TEXTURE_PATH)
63                 ?: throw RuntimeException("Clouds texture is missing."),
64         fogTexture =
65             GraphicsUtils.loadTexture(assets, FOG_TEXTURE_PATH)
66                 ?: throw RuntimeException("Fog texture is missing."),
67         pixelDensity,
68         COLOR_GRADING_INTENSITY
69     )
70 
71     private companion object {
72         private const val SHADER_PATH = "shaders/fog_effect.agsl"
73         private const val COLOR_GRADING_SHADER_PATH = "shaders/color_grading_lut.agsl"
74         private const val LOOKUP_TABLE_TEXTURE_PATH = "textures/fog_lut.png"
75         private const val CLOUDS_TEXTURE_PATH = "textures/clouds.png"
76         private const val FOG_TEXTURE_PATH = "textures/fog.png"
77         private const val COLOR_GRADING_INTENSITY = 0.3f
78     }
79 }
80