|
| 1 | +/* |
| 2 | + * Copyright 2024 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 | + * https://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.example.jetlagged.backgrounds |
| 18 | + |
| 19 | +import android.graphics.RuntimeShader |
| 20 | +import android.os.Build |
| 21 | +import androidx.annotation.RequiresApi |
| 22 | +import androidx.compose.animation.core.withInfiniteAnimationFrameMillis |
| 23 | +import androidx.compose.runtime.mutableFloatStateOf |
| 24 | +import androidx.compose.ui.Modifier |
| 25 | +import androidx.compose.ui.graphics.ShaderBrush |
| 26 | +import androidx.compose.ui.graphics.drawscope.ContentDrawScope |
| 27 | +import androidx.compose.ui.node.DrawModifierNode |
| 28 | +import androidx.compose.ui.node.ModifierNodeElement |
| 29 | +import com.example.jetlagged.ui.theme.Yellow |
| 30 | +import kotlinx.coroutines.launch |
| 31 | +import org.intellij.lang.annotations.Language |
| 32 | + |
| 33 | +/** |
| 34 | + * Background modifier that displays a custom shader for Android T and above and a linear gradient |
| 35 | + * for older versions of Android |
| 36 | + */ |
| 37 | +fun Modifier.solarFlareShaderBackground(): Modifier = |
| 38 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
| 39 | + this.then(SolarFlareShaderBackgroundElement) |
| 40 | + } else { |
| 41 | + this.then(Modifier.simpleGradient()) |
| 42 | + } |
| 43 | + |
| 44 | +@RequiresApi(Build.VERSION_CODES.TIRAMISU) |
| 45 | +private data object SolarFlareShaderBackgroundElement : |
| 46 | + ModifierNodeElement<SolarFlairShaderBackgroundNode>() { |
| 47 | + override fun create() = SolarFlairShaderBackgroundNode() |
| 48 | + override fun update(node: SolarFlairShaderBackgroundNode) { |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +@RequiresApi(Build.VERSION_CODES.TIRAMISU) |
| 53 | +private class SolarFlairShaderBackgroundNode : DrawModifierNode, Modifier.Node() { |
| 54 | + private val shader = RuntimeShader(SHADER) |
| 55 | + private val shaderBrush = ShaderBrush(shader) |
| 56 | + private val time = mutableFloatStateOf(0f) |
| 57 | + |
| 58 | + init { |
| 59 | + shader.setColorUniform( |
| 60 | + "baseColor", |
| 61 | + android.graphics.Color.valueOf(Yellow.red, Yellow.green, Yellow.blue, Yellow.alpha) |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + override fun ContentDrawScope.draw() { |
| 66 | + shader.setFloatUniform("resolution", size.width, size.height) |
| 67 | + shader.setFloatUniform("time", time.floatValue) |
| 68 | + |
| 69 | + drawRect(shaderBrush) |
| 70 | + drawContent() |
| 71 | + } |
| 72 | + |
| 73 | + override fun onAttach() { |
| 74 | + coroutineScope.launch { |
| 75 | + while (isAttached) { |
| 76 | + withInfiniteAnimationFrameMillis { |
| 77 | + time.floatValue = it / 1000f |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +@Language("AGSL") |
| 85 | +private val SHADER = """ |
| 86 | + uniform float2 resolution; |
| 87 | + uniform float time; |
| 88 | + layout(color) uniform half4 baseColor; |
| 89 | + |
| 90 | + const int ITERATIONS = 2; |
| 91 | + const float INTENSITY = 100.0; |
| 92 | + const float TIME_MULTIPLIER = 0.25; |
| 93 | + |
| 94 | + float4 main(in float2 fragCoord) { |
| 95 | + // Slow down the animation to be more soothing |
| 96 | + float calculatedTime = time * TIME_MULTIPLIER; |
| 97 | + |
| 98 | + // Coords |
| 99 | + float2 uv = fragCoord / resolution.xy; |
| 100 | + float2 uvCalc = (uv * 5.0) - (INTENSITY * 2.0); |
| 101 | + |
| 102 | + // Values to adjust per iteration |
| 103 | + float2 iterationChange = float2(uvCalc); |
| 104 | + float colorPart = 1.0; |
| 105 | + |
| 106 | + for (int i = 0; i < ITERATIONS; i++) { |
| 107 | + iterationChange = uvCalc + float2( |
| 108 | + cos(calculatedTime + iterationChange.x) + |
| 109 | + sin(calculatedTime - iterationChange.y), |
| 110 | + cos(calculatedTime - iterationChange.x) + |
| 111 | + sin(calculatedTime + iterationChange.y) |
| 112 | + ); |
| 113 | + colorPart += 0.8 / length( |
| 114 | + float2(uvCalc.x / (cos(iterationChange.x + calculatedTime) * INTENSITY), |
| 115 | + uvCalc.y / (sin(iterationChange.y + calculatedTime) * INTENSITY) |
| 116 | + ) |
| 117 | + ); |
| 118 | + } |
| 119 | + colorPart = 1.6 - (colorPart / float(ITERATIONS)); |
| 120 | + |
| 121 | + // Fade out the bottom on a curve |
| 122 | + float alpha = 1.0 - (uv.y * uv.y); |
| 123 | + // Mix calculated color with the incoming base color |
| 124 | + float4 color = float4(colorPart * baseColor.r, colorPart * baseColor.g, colorPart * baseColor.b, alpha); |
| 125 | + // Keep all channels within valid bounds of 0.0 and 1.0 |
| 126 | + return clamp(color, 0.0, 1.0); |
| 127 | + } |
| 128 | +""".trimIndent() |
0 commit comments