Rename decay to falloff in GlassHighlight.Dynamic, update docs
This commit is contained in:
1 parent
3966c1be60
commit
80562325cf
3 files changed
+32
-18
No files matched your search
@@ -3,15 +3,30 @@ package com.kyant.liquidglass.dispersion
|
||||
import androidx.annotation.FloatRange
|
||||
import androidx.compose.runtime.Immutable
|
||||
|
||||
/**
|
||||
* The dispersion effect applied to the liquid glass.
|
||||
*/
|
||||
@Immutable
|
||||
sealed interface Dispersion {
|
||||
|
||||
/**
|
||||
* No dispersion effect.
|
||||
*/
|
||||
@Immutable
|
||||
data object None : Dispersion
|
||||
|
||||
/**
|
||||
* Automatic dispersion effect, which adapts to the inner refraction options.
|
||||
*/
|
||||
@Immutable
|
||||
data object Automatic : Dispersion
|
||||
|
||||
/**
|
||||
* A fractional dispersion effect, which disperses the glass by a fraction of its inner refraction options.
|
||||
*
|
||||
* @param fraction
|
||||
* The fraction of the inner refraction options to disperse the glass by.
|
||||
*/
|
||||
@Immutable
|
||||
data class Fractional(
|
||||
@param:FloatRange(from = 0.0, to = 1.0) val fraction: Float
|
||||
|
||||
@@ -35,7 +35,7 @@ sealed interface GlassHighlight {
|
||||
}
|
||||
|
||||
/**
|
||||
* A no-op highlight effect.
|
||||
* No highlight effect.
|
||||
*/
|
||||
@Immutable
|
||||
data object None : GlassHighlight {
|
||||
@@ -48,7 +48,7 @@ sealed interface GlassHighlight {
|
||||
}
|
||||
|
||||
/**
|
||||
* A solid highlight effect.
|
||||
* Solid highlight effect.
|
||||
*
|
||||
* @param width
|
||||
* The width of the highlight.
|
||||
@@ -67,7 +67,7 @@ sealed interface GlassHighlight {
|
||||
) : GlassHighlight
|
||||
|
||||
/**
|
||||
* A dynamic highlight effect that creates a shimmering effect.
|
||||
* Dynamic highlight effect with shimmering effect.
|
||||
*
|
||||
* @param width
|
||||
* The width of the highlight.
|
||||
@@ -81,8 +81,8 @@ sealed interface GlassHighlight {
|
||||
* @param angle
|
||||
* The angle of the highlight in degrees.
|
||||
*
|
||||
* @param decay
|
||||
* The decay factor for the highlight, controlling how quickly it fades out.
|
||||
* @param falloff
|
||||
* The falloff of the highlight. Higher value results in a sharper highlight.
|
||||
*/
|
||||
@Immutable
|
||||
data class Dynamic(
|
||||
@@ -90,7 +90,7 @@ sealed interface GlassHighlight {
|
||||
override val color: Color = Color.White.copy(alpha = 0.4f),
|
||||
override val blendMode: BlendMode = BlendMode.Plus,
|
||||
val angle: Float = 45f,
|
||||
@param:FloatRange(from = 0.0) val decay: Float = 1.5f
|
||||
@param:FloatRange(from = 0.0) val falloff: Float = 1.5f
|
||||
) : GlassHighlight {
|
||||
|
||||
private var highlightShaderCache: RuntimeShader? = null
|
||||
@@ -114,8 +114,8 @@ sealed interface GlassHighlight {
|
||||
highlightShader.apply {
|
||||
setFloatUniform("size", size.width, size.height)
|
||||
setFloatUniform("cornerRadius", cornerRadius)
|
||||
setFloatUniform("angle", angle * PI.toFloat() / 180f)
|
||||
setFloatUniform("decay", decay)
|
||||
setFloatUniform("angle", angle * (PI / 180f).toFloat())
|
||||
setFloatUniform("falloff", falloff)
|
||||
},
|
||||
"image"
|
||||
)
|
||||
|
||||
@@ -50,12 +50,11 @@ half4 main(float2 coord) {
|
||||
float2 halfSize = size * 0.5;
|
||||
float2 centeredCoord = coord - halfSize;
|
||||
float sd = sdRoundedRectangle(centeredCoord, halfSize, cornerRadius);
|
||||
|
||||
if (-sd >= refractionHeight) {
|
||||
return image.eval(coord);
|
||||
}
|
||||
|
||||
sd = min(sd, 0.0);
|
||||
|
||||
float maxGradRadius = max(min(halfSize.x, halfSize.y), cornerRadius);
|
||||
float gradRadius = min(cornerRadius * 1.5, maxGradRadius);
|
||||
float2 normal = gradSdRoundedRectangle(centeredCoord, halfSize, gradRadius);
|
||||
@@ -91,16 +90,10 @@ half4 main(float2 coord) {
|
||||
float2 halfSize = size * 0.5;
|
||||
float2 centeredCoord = coord - halfSize;
|
||||
float sd = sdRoundedRectangle(centeredCoord, halfSize, cornerRadius);
|
||||
|
||||
if (-sd >= dispersionHeight) {
|
||||
return image.eval(coord);
|
||||
}
|
||||
|
||||
sd = min(sd, 0.0);
|
||||
float maxGradRadius = max(min(halfSize.x, halfSize.y), cornerRadius);
|
||||
float gradRadius = min(cornerRadius * 1.5, maxGradRadius);
|
||||
float2 normal = gradSdRoundedRectangle(centeredCoord, halfSize, gradRadius);
|
||||
float2 tangent = normalToTangent(normal);
|
||||
|
||||
float dispersionDistance = circleMap(1.0 - -sd / dispersionHeight) * dispersionAmount;
|
||||
if (dispersionDistance < 2.0) {
|
||||
@@ -108,6 +101,11 @@ half4 main(float2 coord) {
|
||||
return color;
|
||||
}
|
||||
|
||||
float maxGradRadius = max(min(halfSize.x, halfSize.y), cornerRadius);
|
||||
float gradRadius = min(cornerRadius * 1.5, maxGradRadius);
|
||||
float2 normal = gradSdRoundedRectangle(centeredCoord, halfSize, gradRadius);
|
||||
float2 tangent = normalToTangent(normal);
|
||||
|
||||
half4 dispersedColor = half4(0.0);
|
||||
half4 weight = half4(0.0);
|
||||
float maxI = min(dispersionDistance, 20.0);
|
||||
@@ -123,6 +121,7 @@ half4 main(float2 coord) {
|
||||
weight += mask;
|
||||
}
|
||||
dispersedColor /= weight;
|
||||
|
||||
dispersedColor.a = image.eval(coord).a;
|
||||
return dispersedColor;
|
||||
}"""
|
||||
@@ -134,7 +133,7 @@ uniform shader image;
|
||||
uniform float2 size;
|
||||
uniform float cornerRadius;
|
||||
uniform float angle;
|
||||
uniform float decay;
|
||||
uniform float falloff;
|
||||
|
||||
$sdRectangleShaderUtils
|
||||
|
||||
@@ -146,7 +145,7 @@ half4 main(float2 coord) {
|
||||
float2 topLightNormal = float2(-cos(angle), -sin(angle));
|
||||
float topLightFraction = dot(topLightNormal, grad);
|
||||
float bottomLightFraction = -topLightFraction;
|
||||
float fraction = pow(max(topLightFraction, bottomLightFraction), decay);
|
||||
float fraction = pow(max(topLightFraction, bottomLightFraction), falloff);
|
||||
|
||||
return image.eval(coord) * fraction;
|
||||
}"""
|
||||
|
||||
Reference in new issue
Block a user