From e064508e830ece9e3c31a9c1254dc0ec515f320b Mon Sep 17 00:00:00 2001 From: Rebecca Franks Date: Mon, 4 Dec 2023 12:22:34 +0000 Subject: [PATCH 1/6] background node migration --- .../java/com/example/jetlagged/Background.kt | 85 +++++++++++++++---- 1 file changed, 69 insertions(+), 16 deletions(-) diff --git a/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt b/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt index 0f4fc3ecda..a7d13a4795 100644 --- a/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt +++ b/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt @@ -19,19 +19,71 @@ package com.example.jetlagged import android.graphics.Color import android.graphics.RuntimeShader import android.os.Build -import androidx.compose.animation.core.withInfiniteAnimationFrameMillis -import androidx.compose.runtime.getValue -import androidx.compose.runtime.produceState +import androidx.annotation.RequiresApi +import androidx.compose.runtime.mutableFloatStateOf +import androidx.compose.runtime.mutableLongStateOf +import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.Modifier -import androidx.compose.ui.composed import androidx.compose.ui.draw.drawWithCache import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.ShaderBrush +import androidx.compose.ui.graphics.drawscope.ContentDrawScope +import androidx.compose.ui.node.DrawModifierNode +import androidx.compose.ui.node.ModifierNodeElement import com.example.jetlagged.ui.theme.White import com.example.jetlagged.ui.theme.Yellow import com.example.jetlagged.ui.theme.YellowVariant +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import org.intellij.lang.annotations.Language +@RequiresApi(Build.VERSION_CODES.TIRAMISU) +private class YellowBackgroundNode : DrawModifierNode, Modifier.Node() { + + private val shader = RuntimeShader(SHADER) + private val shaderBrush = ShaderBrush(shader) + private var time = mutableLongStateOf(0L) + init { + shader.setColorUniform( + "color", + Color.valueOf(Yellow.red, Yellow.green, Yellow.blue, Yellow.alpha) + ) + } + override fun ContentDrawScope.draw() { + shader.setFloatUniform("resolution", size.width, size.height) + shader.setFloatUniform("time", time.longValue.toFloat()) + drawRect(shaderBrush) + drawContent() + } + override fun onAttach() { + coroutineScope.launch { + while (true) { + time.longValue = System.nanoTime() + delay(1) + } + } + } +} +private data object YellowBackgroundElement : ModifierNodeElement() { + @RequiresApi(Build.VERSION_CODES.TIRAMISU) + override fun create() = YellowBackgroundNode() + override fun update(node: YellowBackgroundNode) { + + } +} +fun Modifier.yellowBackground() : Modifier = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + this.then(YellowBackgroundElement) +} else { + this.then(drawWithCache { + + val gradientBrush = Brush.verticalGradient(listOf(Yellow, YellowVariant, White)) + onDrawBehind { + drawRect(gradientBrush) + } + }) +} +/* + fun Modifier.yellowBackground(): Modifier = this.composed { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { // produce updating time in seconds variable to pass into shader @@ -45,14 +97,14 @@ fun Modifier.yellowBackground(): Modifier = this.composed { Modifier.drawWithCache { val shader = RuntimeShader(SHADER) val shaderBrush = ShaderBrush(shader) - shader.setFloatUniform("iResolution", size.width, size.height) + shader.setFloatUniform("resolution", size.width, size.height) // Pass the color to support color space automatically shader.setColorUniform( - "iColor", + "color", Color.valueOf(Yellow.red, Yellow.green, Yellow.blue, Yellow.alpha) ) onDrawBehind { - shader.setFloatUniform("iTime", time) + shader.setFloatUniform("time", time) drawRect(shaderBrush) } } @@ -65,12 +117,13 @@ fun Modifier.yellowBackground(): Modifier = this.composed { } } } +*/ @Language("AGSL") val SHADER = """ - uniform float2 iResolution; - uniform float iTime; - layout(color) uniform half4 iColor; + uniform float2 resolution; + uniform float time; + layout(color) uniform half4 color; float calculateColorMultiplier(float yCoord, float factor) { return step(yCoord, 1.0 + factor * 2.0) - step(yCoord, factor - 0.1); @@ -84,23 +137,23 @@ val SHADER = """ const float energy = 0.6; // Calculated values - float2 uv = fragCoord / iResolution.xy; - float3 color = iColor.rgb; - float timeOffset = iTime * speedMultiplier; + float2 uv = fragCoord / resolution.xy; + float3 rgbColor = color.rgb; + float timeOffset = time * speedMultiplier; float hAdjustment = uv.x * 4.3; - float3 loopColor = vec3(1.0 - color.r, 1.0 - color.g, 1.0 - color.b) / loops; + float3 loopColor = vec3(1.0 - rgbColor.r, 1.0 - rgbColor.g, 1.0 - rgbColor.b) / loops; for (float i = 1.0; i <= loops; i += 1.0) { float loopFactor = i * 0.1; float sinInput = (timeOffset + hAdjustment) * energy; float curve = sin(sinInput) * (1.0 - loopFactor) * 0.05; float colorMultiplier = calculateColorMultiplier(uv.y, loopFactor); - color += loopColor * colorMultiplier; + rgbColor += loopColor * colorMultiplier; // Offset for next loop uv.y += curve; } - return float4(color, 1.0); + return float4(rgbColor, 1.0); } """.trimIndent() From 1339018f22f20192cc9adcba9a59b4ed23e350f2 Mon Sep 17 00:00:00 2001 From: Rebecca Franks Date: Wed, 6 Dec 2023 10:30:17 +0000 Subject: [PATCH 2/6] Migrate background to Modifier.Node --- .../java/com/example/jetlagged/Background.kt | 51 +++---------------- 1 file changed, 8 insertions(+), 43 deletions(-) diff --git a/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt b/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt index a7d13a4795..31a33c987b 100644 --- a/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt +++ b/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt @@ -20,9 +20,9 @@ import android.graphics.Color import android.graphics.RuntimeShader import android.os.Build import androidx.annotation.RequiresApi +import androidx.compose.animation.core.withInfiniteAnimationFrameMillis import androidx.compose.runtime.mutableFloatStateOf import androidx.compose.runtime.mutableLongStateOf -import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.Modifier import androidx.compose.ui.draw.drawWithCache import androidx.compose.ui.graphics.Brush @@ -30,10 +30,10 @@ import androidx.compose.ui.graphics.ShaderBrush import androidx.compose.ui.graphics.drawscope.ContentDrawScope import androidx.compose.ui.node.DrawModifierNode import androidx.compose.ui.node.ModifierNodeElement +import androidx.compose.ui.node.invalidateDraw import com.example.jetlagged.ui.theme.White import com.example.jetlagged.ui.theme.Yellow import com.example.jetlagged.ui.theme.YellowVariant -import kotlinx.coroutines.delay import kotlinx.coroutines.launch import org.intellij.lang.annotations.Language @@ -42,24 +42,25 @@ private class YellowBackgroundNode : DrawModifierNode, Modifier.Node() { private val shader = RuntimeShader(SHADER) private val shaderBrush = ShaderBrush(shader) - private var time = mutableLongStateOf(0L) init { shader.setColorUniform( "color", Color.valueOf(Yellow.red, Yellow.green, Yellow.blue, Yellow.alpha) ) } + private val time = mutableFloatStateOf(0f) override fun ContentDrawScope.draw() { shader.setFloatUniform("resolution", size.width, size.height) - shader.setFloatUniform("time", time.longValue.toFloat()) + shader.setFloatUniform("time", time.floatValue) drawRect(shaderBrush) drawContent() } override fun onAttach() { coroutineScope.launch { while (true) { - time.longValue = System.nanoTime() - delay(1) + withInfiniteAnimationFrameMillis { + time.floatValue = it/1000f + } } } } @@ -74,50 +75,14 @@ private data object YellowBackgroundElement : ModifierNodeElement= Build.VERSION_CODES.TIRAMISU) { this.then(YellowBackgroundElement) } else { - this.then(drawWithCache { + drawWithCache { val gradientBrush = Brush.verticalGradient(listOf(Yellow, YellowVariant, White)) onDrawBehind { drawRect(gradientBrush) } - }) -} -/* - -fun Modifier.yellowBackground(): Modifier = this.composed { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - // produce updating time in seconds variable to pass into shader - val time by produceState(0f) { - while (true) { - withInfiniteAnimationFrameMillis { - value = it / 1000f - } - } - } - Modifier.drawWithCache { - val shader = RuntimeShader(SHADER) - val shaderBrush = ShaderBrush(shader) - shader.setFloatUniform("resolution", size.width, size.height) - // Pass the color to support color space automatically - shader.setColorUniform( - "color", - Color.valueOf(Yellow.red, Yellow.green, Yellow.blue, Yellow.alpha) - ) - onDrawBehind { - shader.setFloatUniform("time", time) - drawRect(shaderBrush) - } - } - } else { - Modifier.drawWithCache { - val gradientBrush = Brush.verticalGradient(listOf(Yellow, YellowVariant, White)) - onDrawBehind { - drawRect(gradientBrush) - } - } } } -*/ @Language("AGSL") val SHADER = """ From 10ee3c7ecf2ee0aac9ab65faccfc339c6601018e Mon Sep 17 00:00:00 2001 From: Rebecca Franks Date: Wed, 6 Dec 2023 13:21:22 +0000 Subject: [PATCH 3/6] Update background.kt --- .../java/com/example/jetlagged/Background.kt | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt b/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt index 31a33c987b..ebd09f6995 100644 --- a/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt +++ b/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt @@ -22,7 +22,6 @@ import android.os.Build import androidx.annotation.RequiresApi import androidx.compose.animation.core.withInfiniteAnimationFrameMillis import androidx.compose.runtime.mutableFloatStateOf -import androidx.compose.runtime.mutableLongStateOf import androidx.compose.ui.Modifier import androidx.compose.ui.draw.drawWithCache import androidx.compose.ui.graphics.Brush @@ -30,7 +29,6 @@ import androidx.compose.ui.graphics.ShaderBrush import androidx.compose.ui.graphics.drawscope.ContentDrawScope import androidx.compose.ui.node.DrawModifierNode import androidx.compose.ui.node.ModifierNodeElement -import androidx.compose.ui.node.invalidateDraw import com.example.jetlagged.ui.theme.White import com.example.jetlagged.ui.theme.Yellow import com.example.jetlagged.ui.theme.YellowVariant @@ -42,12 +40,14 @@ private class YellowBackgroundNode : DrawModifierNode, Modifier.Node() { private val shader = RuntimeShader(SHADER) private val shaderBrush = ShaderBrush(shader) + init { shader.setColorUniform( "color", Color.valueOf(Yellow.red, Yellow.green, Yellow.blue, Yellow.alpha) ) } + private val time = mutableFloatStateOf(0f) override fun ContentDrawScope.draw() { shader.setFloatUniform("resolution", size.width, size.height) @@ -55,16 +55,18 @@ private class YellowBackgroundNode : DrawModifierNode, Modifier.Node() { drawRect(shaderBrush) drawContent() } + override fun onAttach() { coroutineScope.launch { while (true) { withInfiniteAnimationFrameMillis { - time.floatValue = it/1000f + time.floatValue = it / 1000f } } } } } + private data object YellowBackgroundElement : ModifierNodeElement() { @RequiresApi(Build.VERSION_CODES.TIRAMISU) override fun create() = YellowBackgroundNode() @@ -72,17 +74,19 @@ private data object YellowBackgroundElement : ModifierNodeElement= Build.VERSION_CODES.TIRAMISU) { - this.then(YellowBackgroundElement) -} else { - drawWithCache { - val gradientBrush = Brush.verticalGradient(listOf(Yellow, YellowVariant, White)) - onDrawBehind { - drawRect(gradientBrush) +fun Modifier.yellowBackground(): Modifier = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + this.then(YellowBackgroundElement) + } else { + drawWithCache { + + val gradientBrush = Brush.verticalGradient(listOf(Yellow, YellowVariant, White)) + onDrawBehind { + drawRect(gradientBrush) + } } } -} @Language("AGSL") val SHADER = """ From 97831daa732fd29fe890e7e4ecb943b89d944444 Mon Sep 17 00:00:00 2001 From: riggaroo Date: Wed, 6 Dec 2023 13:23:33 +0000 Subject: [PATCH 4/6] =?UTF-8?q?=F0=9F=A4=96=20Apply=20Spotless?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JetLagged/app/src/main/java/com/example/jetlagged/Background.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt b/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt index ebd09f6995..1d75fa0e02 100644 --- a/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt +++ b/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt @@ -71,7 +71,6 @@ private data object YellowBackgroundElement : ModifierNodeElement Date: Wed, 6 Dec 2023 13:56:52 +0000 Subject: [PATCH 5/6] code cleanup --- .../java/com/example/jetlagged/Background.kt | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt b/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt index ebd09f6995..01890fcf47 100644 --- a/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt +++ b/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt @@ -35,11 +35,20 @@ import com.example.jetlagged.ui.theme.YellowVariant import kotlinx.coroutines.launch import org.intellij.lang.annotations.Language +private data object YellowBackgroundElement : ModifierNodeElement() { + @RequiresApi(Build.VERSION_CODES.TIRAMISU) + override fun create() = YellowBackgroundNode() + override fun update(node: YellowBackgroundNode) { + + } +} + @RequiresApi(Build.VERSION_CODES.TIRAMISU) private class YellowBackgroundNode : DrawModifierNode, Modifier.Node() { private val shader = RuntimeShader(SHADER) private val shaderBrush = ShaderBrush(shader) + private val time = mutableFloatStateOf(0f) init { shader.setColorUniform( @@ -48,7 +57,7 @@ private class YellowBackgroundNode : DrawModifierNode, Modifier.Node() { ) } - private val time = mutableFloatStateOf(0f) + override fun ContentDrawScope.draw() { shader.setFloatUniform("resolution", size.width, size.height) shader.setFloatUniform("time", time.floatValue) @@ -67,13 +76,6 @@ private class YellowBackgroundNode : DrawModifierNode, Modifier.Node() { } } -private data object YellowBackgroundElement : ModifierNodeElement() { - @RequiresApi(Build.VERSION_CODES.TIRAMISU) - override fun create() = YellowBackgroundNode() - override fun update(node: YellowBackgroundNode) { - - } -} fun Modifier.yellowBackground(): Modifier = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { From 73755dcd32339c20b367f499ef12091b368c0231 Mon Sep 17 00:00:00 2001 From: riggaroo Date: Wed, 6 Dec 2023 15:20:56 +0000 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=A4=96=20Apply=20Spotless?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/src/main/java/com/example/jetlagged/Background.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt b/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt index 01890fcf47..fd6c42c320 100644 --- a/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt +++ b/JetLagged/app/src/main/java/com/example/jetlagged/Background.kt @@ -39,7 +39,6 @@ private data object YellowBackgroundElement : ModifierNodeElement= Build.VERSION_CODES.TIRAMISU) { this.then(YellowBackgroundElement)