Created
May 13, 2023 03:42
-
-
Save hakanai/affcbc88a995d35b31ae81582f8f6dd6 to your computer and use it in GitHub Desktop.
Fragment shaders in Jetpack Compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.foundation.Canvas | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.material.MaterialTheme | |
import androidx.compose.material.Surface | |
import androidx.compose.runtime.derivedStateOf | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.remember | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.geometry.Size | |
import androidx.compose.ui.graphics.ShaderBrush | |
import androidx.compose.ui.window.singleWindowApplication | |
import org.jetbrains.skia.RuntimeEffect | |
import org.jetbrains.skia.RuntimeShaderBuilder | |
fun main() = singleWindowApplication(title = "Shaaaderrrs") { | |
val runtimeEffect = remember { | |
RuntimeEffect.makeForShader( | |
""" | |
uniform vec3 iResolution; | |
vec4 main(in vec2 fragCoord) { | |
return vec4(fragCoord / iResolution.xy, 0, 1); | |
} | |
""".trimIndent() | |
) | |
} | |
val shader by derivedStateOf { | |
val builder = RuntimeShaderBuilder(runtimeEffect) | |
builder.uniform("iResolution", 800.0f, 600.0f, 1.0f) | |
builder.makeShader() | |
} | |
val brush by derivedStateOf { | |
ShaderBrush(shader) | |
} | |
Surface(modifier = Modifier.background(MaterialTheme.colors.surface)) { | |
Canvas(modifier = Modifier.fillMaxSize()) { | |
drawRect(brush = brush, size = Size(800.0f, 600.0f)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment