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.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 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     /** Pixel density of the display. Used for dithering. */
41     val pixelDensity: Float,
42     /** The intensity of the color grading. 0: no color grading, 1: color grading in full effect. */
43     @FloatRange(from = 0.0, to = 1.0) val colorGradingIntensity: Float,
44     /** Max thickness for the accumulated snow. */
45     val maxAccumulatedSnowThickness: Float,
46 ) {
47     /**
48      * Constructor for [SnowEffectConfig].
49      *
50      * @param assets asset manager,
51      * @param pixelDensity pixel density of the display. Expected range is [0, 1]. You can always
52      *   change the intensity dynamically. Defaults to 1.
53      */
54     constructor(
55         assets: AssetManager,
56         pixelDensity: Float,
57     ) : this(
58         shader = GraphicsUtils.loadShader(assets, SHADER_PATH),
59         accumulatedSnowShader = GraphicsUtils.loadShader(assets, ACCUMULATED_SNOW_SHADER_PATH),
60         colorGradingShader = GraphicsUtils.loadShader(assets, COLOR_GRADING_SHADER_PATH),
61         noiseTexture =
62             GraphicsUtils.loadTexture(assets, NOISE_TEXTURE_PATH)
63                 ?: throw RuntimeException("Noise texture is missing."),
64         lut = GraphicsUtils.loadTexture(assets, LOOKUP_TABLE_TEXTURE_PATH),
65         pixelDensity,
66         COLOR_GRADING_INTENSITY,
67         MAX_SNOW_THICKNESS
68     )
69 
70     private companion object {
71         private const val SHADER_PATH = "shaders/snow_effect.agsl"
72         private const val ACCUMULATED_SNOW_SHADER_PATH = "shaders/snow_accumulation.agsl"
73         private const val COLOR_GRADING_SHADER_PATH = "shaders/color_grading_lut.agsl"
74         private const val NOISE_TEXTURE_PATH = "textures/clouds.png"
75         private const val LOOKUP_TABLE_TEXTURE_PATH = "textures/snow_lut.png"
76         private const val COLOR_GRADING_INTENSITY = 0.25f
77         private const val MAX_SNOW_THICKNESS = 10f
78     }
79 }
80