Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 87a4f0e3 authored by Yein Jo's avatar Yein Jo
Browse files

Sync snow accumulation

- Update snow accumulation to match the latest change
- Remove blurredBackground as it's not used anymore
- Add MathUtils as WeatherEffect should not depend on Toruslib

Bug: 325090421
Test: Manual
Flag: NA
Change-Id: Ic26f69f3ce1daca95bbd2a1211218448c29bcbbf
parent 7e6dd4b4
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -22,14 +22,14 @@ uniform half snowThickness;
#include "shaders/utils.agsl"

float random(vec2 uv) {
    return fract(sin(dot(uv.xy, vec2(14.53898, 56.233))) * 45312.644263742);
    return fract(sin(dot(uv, vec2(14.53898, 56.233))) * 45312.644263742);
}

vec4 main(float2 fragCoord) {
    // fragCoord should be already the adjusted UVs to have the expected rect of the image.
    vec2 uv = fragCoord / imageWidth;
    float variation = 0.3 + simplex2d(11. * uv);
    float distance = 20. * variation * snowThickness;
    float distance = variation * snowThickness;

    float aN = foreground.eval(fragCoord + vec2(0., distance)).a;
    float aS = foreground.eval(fragCoord + vec2(0., -distance)).a;
+0 −1
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
uniform shader foreground;
uniform shader background;
uniform shader accumulatedSnow;
uniform shader blurredBackground;
uniform float2 uvOffsetFgd;
uniform float2 uvScaleFgd;
uniform float2 uvOffsetBgd;
+14 −12
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import com.google.android.wallpaper.weathereffects.graphics.FrameBuffer
import com.google.android.wallpaper.weathereffects.graphics.WeatherEffect
import com.google.android.wallpaper.weathereffects.graphics.utils.GraphicsUtils
import com.google.android.wallpaper.weathereffects.graphics.utils.ImageCrop
import com.google.android.wallpaper.weathereffects.graphics.utils.MathUtils
import java.util.concurrent.Executor
import kotlin.random.Random

@@ -39,6 +40,7 @@ class SnowEffect(
    private val mainExecutor: Executor
) : WeatherEffect {

    private var snowSpeed: Float = 0.8f
    private val snowPaint = Paint().also { it.shader = snowConfig.colorGradingShader }
    private var elapsedTime: Float = 0f

@@ -59,7 +61,8 @@ class SnowEffect(
    override fun resize(newSurfaceSize: SizeF) = adjustCropping(newSurfaceSize)

    override fun update(deltaMillis: Long, frameTimeNanos: Long) {
        elapsedTime += deltaMillis * MILLIS_TO_SECONDS
        elapsedTime += snowSpeed * deltaMillis * MILLIS_TO_SECONDS

        snowConfig.shader.setFloatUniform("time", elapsedTime)
        snowConfig.colorGradingShader.setInputShader("texture", snowConfig.shader)
    }
@@ -79,13 +82,21 @@ class SnowEffect(
    }

    override fun setIntensity(intensity: Float) {
        /**
         * Increase effect speed as weather intensity decreases. This compensates for the floaty
         * appearance when there are fewer particles at the original speed.
         */
        snowSpeed = MathUtils.map(intensity, 0f, 1f, 2.5f, 1.7f)

        snowConfig.shader.setFloatUniform("intensity", intensity)
        snowConfig.colorGradingShader.setFloatUniform(
            "intensity",
            snowConfig.colorGradingIntensity * intensity
        )
        snowConfig.accumulatedSnowShader.setFloatUniform("snowThickness", intensity)

        snowConfig.accumulatedSnowShader.setFloatUniform(
            "snowThickness",
            snowConfig.maxAccumulatedSnowThickness * intensity
        )
        // Regenerate accumulated snow since the uniform changed.
        generateAccumulatedSnow()
    }
@@ -142,15 +153,6 @@ class SnowEffect(
            "background",
            BitmapShader(snowConfig.background, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR)
        )

        snowConfig.shader.setInputBuffer(
            "blurredBackground",
            BitmapShader(
                snowConfig.blurredBackground,
                Shader.TileMode.MIRROR,
                Shader.TileMode.MIRROR
            )
        )
    }

    private fun prepareColorGrading() {
+5 −1
Original line number Diff line number Diff line
@@ -42,6 +42,8 @@ data class SnowEffectConfig(
    @FloatRange(from = 0.0, to = 1.0) val intensity: Float,
    /** The intensity of the color grading. 0: no color grading, 1: color grading in full effect. */
    @FloatRange(from = 0.0, to = 1.0) val colorGradingIntensity: Float,
    /** Max thickness for the accumulated snow. */
    val maxAccumulatedSnowThickness: Float,
) {
    /**
     * Constructor for [SnowEffectConfig].
@@ -67,7 +69,8 @@ data class SnowEffectConfig(
        background,
        blurredBackground = GraphicsUtils.blurImage(context, background, BLUR_RADIUS),
        intensity,
        COLOR_GRADING_INTENSITY
        COLOR_GRADING_INTENSITY,
        MAX_SNOW_THICKNESS
    )

    private companion object {
@@ -78,5 +81,6 @@ data class SnowEffectConfig(
        private const val BLUR_RADIUS = 20f
        private const val DEFAULT_INTENSITY = 1f
        private const val COLOR_GRADING_INTENSITY = 0.7f
        private const val MAX_SNOW_THICKNESS = 10f
    }
}
+175 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.wallpaper.weathereffects.graphics.utils

import kotlin.math.max
import kotlin.math.min

/** Numeric operations and constants not included in the main Math class. */
object MathUtils {
    const val DEG_TO_RAD = Math.PI / 180.0
    const val RAD_TO_DEG = 1 / DEG_TO_RAD
    const val TAU = Math.PI * 2.0

    /**
     * Maps a value from a range to a different one (with the option to clamp it to the new range).
     *
     * @param value The value to map from a value range to a different one.
     * @param inMin The minimum value of the original range.
     * @param inMax The maximum value of the original range.
     * @param outMin The minimum value of the new range.
     * @param outMax The maximum value of the new range.
     * @param clamp If you want to clamp the mapped value to the new range, set to true; otherwise
     *   set to false (by default is set to true).
     * @return The [value] mapped to the new range.
     */
    @JvmStatic
    @JvmOverloads
    fun map(
        value: Double,
        inMin: Double,
        inMax: Double,
        outMin: Double,
        outMax: Double,
        clamp: Boolean = true
    ): Double {
        if (clamp) {
            if (value < inMin) {
                return outMin
            }
            if (value > inMax) {
                return outMax
            }
        }
        return (value - inMin) / (inMax - inMin) * (outMax - outMin) + outMin
    }

    /**
     * Maps a value from a range to a different one (with the option to clamp it to the new range).
     *
     * @param value The value to map from a value range to a different one.
     * @param inMin The minimum value of the original range.
     * @param inMax The maximum value of the original range.
     * @param outMin The minimum value of the new range.
     * @param outMax The maximum value of the new range.
     * @param clamp If you want to clamp the mapped value to the new range, set to true; otherwise
     *   set to false (by default is set to true).
     * @return The [value] mapped to the new range.
     */
    @JvmStatic
    @JvmOverloads
    fun map(
        value: Float,
        inMin: Float,
        inMax: Float,
        outMin: Float,
        outMax: Float,
        clamp: Boolean = true
    ): Float {
        if (clamp) {
            if (value < inMin) {
                return outMin
            }
            if (value > inMax) {
                return outMax
            }
        }
        return (value - inMin) / (inMax - inMin) * (outMax - outMin) + outMin
    }

    /**
     * Linear interpolation between two values.
     *
     * @param start The first value.
     * @param end The second value.
     * @param amount Decides the how we mix the interpolated values; when is 0, it returns the init
     *   value. When is 1, it returns the end value. For any value in between it returns the
     *   linearly interpolated value between [init] and [end]. If [amount] is smaller than 0 or
     *   bigger than 1 and [clamp] is false, it continues returning values based on the line created
     *   using [init] and [end]; otherwise the value is clamped to [init] and [end].
     * @param clamp If you want to clamp the mapped value to the new range, set to true; otherwise
     *   set to false (by default is set to true).
     * @return The interpolated value.
     */
    @JvmStatic
    @JvmOverloads
    fun lerp(start: Double, end: Double, amount: Double, clamp: Boolean = true): Double {
        val amountClamped =
            if (clamp) {
                clamp(amount, 0.0, 1.0)
            } else {
                amount
            }
        return (end - start) * amountClamped + start
    }

    /**
     * Linear interpolation between two values.
     *
     * @param init The first value.
     * @param end The second value.
     * @param amount Decides the how we mix the interpolated values; when is 0, it returns the init
     *   value. When is 1, it returns the end value. For any value in between it returns the
     *   linearly interpolated value between [init] and [end]. If [amount] is smaller than 0 or
     *   bigger than 1 and [clamp] is false, it continues returning values based on the line created
     *   using [init] and [end]; otherwise the value is clamped to [init] and [end].
     * @param clamp If you want to clamp the mapped value to the new range, set to true; otherwise
     *   set to false (by default is set to true).
     * @return The interpolated value.
     */
    @JvmStatic
    @JvmOverloads
    fun lerp(init: Float, end: Float, amount: Float, clamp: Boolean = true): Float {
        val amountClamped =
            if (clamp) {
                clamp(amount, 0.0f, 1.0f)
            } else {
                amount
            }
        return (end - init) * amountClamped + init
    }

    /**
     * Secures that a value is not smaller or bigger than the given range.
     *
     * @param value The input value.
     * @param min The min value of the range. If [value] is smaller than this value, it is fixed to
     *   [min] value.
     * @param max The max value of the range. If [value] is bigger than this value, it is fixed to
     *   [min] value.
     * @return the value that is secured in the [[min], [max]] range.
     */
    @JvmStatic
    fun clamp(value: Double, min: Double, max: Double): Double {
        return max(min(value, max), min)
    }

    /**
     * Secures that a value is not smaller or bigger than the given range.
     *
     * @param value The input value.
     * @param min The min value of the range. If [value] is smaller than this value, it is fixed to
     *   [min] value.
     * @param max The max value of the range. If [value] is bigger than this value, it is fixed to
     *   [min] value.
     * @return the value that is secured in the [[min], [max]] range.
     */
    @JvmStatic
    fun clamp(value: Float, min: Float, max: Float): Float {
        return max(min(value, max), min)
    }
}