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

Commit 968caefd authored by Yein Jo's avatar Yein Jo
Browse files

Fix NaN and dither in fog effect.

1. sqrt operation returns NaN if the number is negative, which then
   doesn't update fog effect. removed sqrt and update the value to look
   similar to what it used to be.
2. replace noise with triangle distribution noise for dithering &
   respect pixel density.

Bug: 303502646
Test: Manually with debug activity.
Change-Id: Ib55da0d63456555a489de3e762a4cc07a80de59a
parent 6eea7ceb
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -57,13 +57,15 @@ vec4 main(float2 fragCoord) {
    vec3 colorFloor = floor(colorTmp) * SLICE_LAST_IDX_INV;
    ivec2 uvFloor = ivec2(
        int(colorFloor.b * LAST_SLICE_FIRST_IDX + colorFloor.r * SLICE_LAST_IDX),
    int(colorFloor.g * SLICE_LAST_IDX));
        int(colorFloor.g * SLICE_LAST_IDX)
    );

    // Calculate the ceil UVs.
    vec3 colorCeil = ceil(colorTmp) * SLICE_LAST_IDX_INV;
    ivec2 uvCeil = ivec2(
        int(colorCeil.b * LAST_SLICE_FIRST_IDX + colorCeil.r * SLICE_LAST_IDX),
    int(colorCeil.g * SLICE_LAST_IDX));
        int(colorCeil.g * SLICE_LAST_IDX)
    );

    /*
     * Fetch the color from the LUT, and combine both floor and ceiling options based on the
+8 −6
Original line number Diff line number Diff line
@@ -20,10 +20,11 @@ uniform float2 uvOffsetFgd;
uniform float2 uvScaleFgd;
uniform float2 uvOffsetBgd;
uniform float2 uvScaleBgd;
uniform float2 timeForeground;
uniform float2 timeBackground;
uniform float screenAspectRatio;
uniform float2 screenSize;
uniform half2 timeForeground;
uniform half2 timeBackground;
uniform half screenAspectRatio;
uniform half2 screenSize;
uniform half pixelDensity;

#include "shaders/constants.agsl"
#include "shaders/utils.agsl"
@@ -56,12 +57,13 @@ vec4 main(float2 fragCoord) {
    float frontFog = smoothstep(-0.616, 0.552, fbm(vec3(uv * 0.886, 123.1), timeForeground));
    float bgdFog = smoothstep(-0.744, 0.28, fbm(vec3(uv * 1.2, 231.), timeBackground));

    float dithering =  (1. - idGenerator(uv) * 0.161);
    float dither = 1. - triangleNoise(fragCoord * pixelDensity) * 0.161;

    color.rgb = normalBlendWithWhiteSrc(color.rgb, 0.8 * dithering * bgdFog);
    color.rgb = normalBlendWithWhiteSrc(color.rgb, 0.8 * dither * bgdFog);
    // Add the foreground. Any effect from here will be in front of the subject.
    color.rgb = normalBlend(color.rgb, colorForeground.rgb, colorForeground.a);
    // foreground fog.
    color.rgb = normalBlendWithWhiteSrc(color.rgb, 0.5 * frontFog);

    return color;
}
+9 −0
Original line number Diff line number Diff line
@@ -45,6 +45,15 @@ float wiggle(float time, float wiggleSpeed) {
        * sin(wiggleSpeed * time) - 0.5;
}

// Noise range of [-1.0, 1.0[ with triangle distribution.
float triangleNoise(vec2 n) {
    n  = fract(n * vec2(5.3987, 5.4421));
    n += dot(n.yx, n.xy + vec2(21.5351, 14.3137));
    float xy = n.x * n.y;
    // compute in [0..2[ and remap to [-1.0..1.0[
    return fract(xy * 95.4307) + fract(xy * 75.04961) - 1.0;
}

/*
 * This is the normal blend mode in which the foreground is painted on top of the background based
 * on the foreground opacity.
+3 −1
Original line number Diff line number Diff line
@@ -130,7 +130,9 @@ class WeatherEngine(
            }

            WallpaperInfoContract.WeatherEffect.FOG -> {
                val fogConfig = FogEffectConfig.create(context.assets, foreground, background)
                val fogConfig = FogEffectConfig.create(
                    context.assets, foreground, background, context.resources.displayMetrics.density
                )
                activeEffect = FogEffect(fogConfig, screenSize.toSizeF())
            }

+4 −3
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@ import com.google.android.torus.utils.extensions.getAspectRatio
import com.google.android.wallpaper.weathereffects.WeatherEffect
import com.google.android.wallpaper.weathereffects.utils.ImageCrop
import kotlin.math.sin
import kotlin.math.sqrt
import kotlin.random.Random

/** Defines and generates the fog weather effect animation. */
@@ -52,8 +51,8 @@ class FogEffect(

    override fun update(deltaMillis: Long, frameTimeNanos: Long) {
        val time = 0.02f * frameTimeNanos.toFloat() * NANOS_TO_SECONDS
        val generalVariation: Float = 0.4f + 0.6f * sqrt(sin(time + sin(3f * time)))
        elapsedTime += generalVariation * deltaMillis * MILLIS_TO_SECONDS
        val variation = sin(time + sin(3f * time)) * 0.5f + 0.5f
        elapsedTime += variation * deltaMillis * MILLIS_TO_SECONDS

        val speed = elapsedTime * 0.248f

@@ -131,6 +130,8 @@ class FogEffect(
            "background",
            BitmapShader(fogConfig.background, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR)
        )

        fogConfig.shader.setFloatUniform("pixelDensity", fogConfig.pixelDensity)
    }

    private fun prepareColorGrading() {
Loading