yogaboy
05/17/2023, 7:11 AMJakub Syty
05/17/2023, 8:01 AMyogaboy
05/17/2023, 8:08 AMJakub Syty
05/17/2023, 9:38 AMyogaboy
05/17/2023, 10:17 AMStylianos Gakis
05/17/2023, 12:43 PM<https://cs.android.com/>
, so not sure if you’d find that in there too.yogaboy
05/17/2023, 1:16 PMStylianos Gakis
05/17/2023, 1:18 PMyogaboy
05/17/2023, 1:21 PMStylianos Gakis
05/17/2023, 1:23 PMyogaboy
05/17/2023, 1:24 PMandrew
05/17/2023, 4:25 PMclass TurbulenceNoiseShader : RuntimeShader(TURBULENCE_NOISE_SHADER) {
// language=AGSL
companion object {
private const val UNIFORMS =
"""
uniform float in_gridNum;
uniform vec3 in_noiseMove;
uniform vec2 in_size;
uniform float in_aspectRatio;
uniform float in_opacity;
uniform float in_pixelDensity;
layout(color) uniform vec4 in_color;
layout(color) uniform vec4 in_backgroundColor;
"""
private const val SHADER_LIB =
"""
float getLuminosity(vec3 c) {
return 0.3*c.r + 0.59*c.g + 0.11*c.b;
}
vec3 maskLuminosity(vec3 dest, float lum) {
dest.rgb *= vec3(lum);
// Clip back into the legal range
dest = clamp(dest, vec3(0.), vec3(1.0));
return dest;
}
"""
private const val MAIN_SHADER =
"""
vec4 main(vec2 p) {
vec2 uv = p / in_size.xy;
uv.x *= in_aspectRatio;
vec3 noiseP = vec3(uv + in_noiseMove.xy, in_noiseMove.z) * in_gridNum;
float luma = simplex3d(noiseP) * in_opacity;
vec3 mask = maskLuminosity(in_color.rgb, luma);
vec3 color = in_backgroundColor.rgb + mask * 0.6;
// Add dither with triangle distribution to avoid color banding. Ok to dither in the
// shader here as we are in gamma space.
float dither = triangleNoise(p * in_pixelDensity) / 255.;
// The result color should be pre-multiplied, i.e. [R*A, G*A, B*A, A], thus need to
// multiply rgb with a to get the correct result.
color = (color + dither.rrr) * in_color.a;
return vec4(color, in_color.a);
}
"""
private const val TURBULENCE_NOISE_SHADER =
ShaderUtilLibrary.SHADER_LIB + UNIFORMS + SHADER_LIB + MAIN_SHADER
}
/** Sets the number of grid for generating noise. */
fun setGridCount(gridNumber: Float = 1.0f) {
setFloatUniform("in_gridNum", gridNumber)
}
/**
* Sets the pixel density of the screen.
*
* Used it for noise dithering.
*/
fun setPixelDensity(pixelDensity: Float) {
setFloatUniform("in_pixelDensity", pixelDensity)
}
/** Sets the noise color of the effect. */
fun setColor(color: Int) {
setColorUniform("in_color", color)
}
/** Sets the background color of the effect. */
fun setBackgroundColor(color: Int) {
setColorUniform("in_backgroundColor", color)
}
/**
* Sets the opacity to achieve fade in/ out of the animation.
*
* Expected value range is [1, 0].
*/
fun setOpacity(opacity: Float) {
setFloatUniform("in_opacity", opacity)
}
/** Sets the size of the shader. */
fun setSize(width: Float, height: Float) {
setFloatUniform("in_size", width, height)
setFloatUniform("in_aspectRatio", width / max(height, 0.001f))
}
/** Current noise movements in x, y, and z axes. */
var noiseOffsetX: Float = 0f
private set
var noiseOffsetY: Float = 0f
private set
var noiseOffsetZ: Float = 0f
private set
/** Sets noise move offset in x, y, and z direction. */
fun setNoiseMove(x: Float, y: Float, z: Float) {
noiseOffsetX = x
noiseOffsetY = y
noiseOffsetZ = z
setFloatUniform("in_noiseMove", noiseOffsetX, noiseOffsetY, noiseOffsetZ)
}
}
Rebecca Franks
05/17/2023, 4:37 PMImage(
vectorPainter, contentDescription = "",
modifier = Modifier
.fillMaxSize()
.background(largeRadialGradient)
.onSizeChanged { size ->
shader.setFloatUniform(
"resolution",
size.width.toFloat(),
size.height.toFloat()
)
}
.graphicsLayer {
shader.setFloatUniform("time", time)
renderEffect = android.graphics.RenderEffect
.createRuntimeShaderEffect(
shader,
"contents"
)
.asComposeRenderEffect()
}
)
Kirill Grouchnikov
05/17/2023, 4:56 PMandrew
05/17/2023, 6:46 PMyogaboy
05/18/2023, 8:43 AM