-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add holo foil shader modifier (broken)
- Loading branch information
Showing
4 changed files
with
106 additions
and
5 deletions.
There are no files selected for viewing
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
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
83 changes: 83 additions & 0 deletions
83
...ugin/src/main/kotlin/org/jetbrains/jewel/samples/ideplugin/releasessample/FoilModifier.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.jetbrains.jewel.samples.ideplugin.releasessample | ||
|
||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.asComposeRenderEffect | ||
import androidx.compose.ui.graphics.graphicsLayer | ||
import org.intellij.lang.annotations.Language | ||
import org.jetbrains.skia.ImageFilter | ||
import org.jetbrains.skia.RuntimeEffect | ||
import org.jetbrains.skia.RuntimeShaderBuilder | ||
|
||
@Language("GLSL") // Technically, SkSL | ||
private const val FOIL_SHADER_CODE = """ | ||
const float STRENGTH = 0.4; // 0.0 = no effect, 1.0 = full effect | ||
const float SATURATION = 0.9; // Color saturation (0.0 = grayscale, 1.0 = full color) | ||
const float LIGHTNESS = 0.65; // Color lightness (0.0 = black, 1.0 = white) | ||
uniform shader content; // Input texture (the application canvas) | ||
uniform vec2 resolution; // Size of the canvas | ||
uniform vec2 offset; // Additional offset of the effect | ||
vec4 rainbowEffect(vec2 uv, vec2 offset) { | ||
vec4 srcColor = content.eval(uv); | ||
if (srcColor.a == 0.0) return srcColor; | ||
float hue = uv.x / (1.75 + abs(offset.x)) + offset.x / 3.0; | ||
float lightness = LIGHTNESS + 0.25 * (0.5 + offset.y * (0.5 - uv.y)); | ||
hue = fract(hue); | ||
float c = (1.0 - abs(2.0 * lightness - 1.0)) * SATURATION; | ||
float x = c * (1.0 - abs(mod(hue / (1.0 / 6.0), 2.0) - 1.0)); | ||
float m = LIGHTNESS - c / 2.0; | ||
vec3 rainbowPrime; | ||
if (hue < 1.0 / 6.0) { | ||
rainbowPrime = vec3(c, x, 0.0); | ||
} else if (hue < 1.0 / 3.0) { | ||
rainbowPrime = vec3(x, c, 0.0); | ||
} else if (hue < 0.5) { | ||
rainbowPrime = vec3(0.0, c, x); | ||
} else if (hue < 2.0 / 3.0) { | ||
rainbowPrime = vec3(0.0, x, c); | ||
} else if (hue < 5.0 / 6.0) { | ||
rainbowPrime = vec3(x, 0.0, c); | ||
} else { | ||
rainbowPrime = vec3(c, 0.0, x); | ||
} | ||
vec3 rainbow = rainbowPrime + m; | ||
return mix(srcColor, vec4(rainbow, srcColor.a), STRENGTH); | ||
} | ||
vec4 chromaticAberration(vec2 uv, vec2 offset) { | ||
vec4 srcColor = rainbowEffect(uv, offset); | ||
vec2 shift = offset * vec2(3.0, 5.0) / 1000.0; | ||
vec4 leftColor = rainbowEffect(uv - shift, offset); | ||
vec4 rightColor = rainbowEffect(uv + shift, offset); | ||
return vec4(rightColor.r, srcColor.g, leftColor.b, srcColor.a); | ||
} | ||
vec4 main(float2 fragCoord) { | ||
return chromaticAberration(fragCoord.xy, offset); | ||
} | ||
""" | ||
|
||
private val runtimeEffect = RuntimeEffect.makeForShader(FOIL_SHADER_CODE) | ||
private val shaderBuilder = RuntimeShaderBuilder(runtimeEffect) | ||
|
||
internal fun Modifier.holoFoil(offset: Float) = | ||
graphicsLayer { | ||
shaderBuilder.uniform("resolution", size.width, size.height) | ||
shaderBuilder.uniform("offset", 0f, offset) | ||
clip = true | ||
|
||
renderEffect = | ||
ImageFilter.makeRuntimeShader( | ||
runtimeShaderBuilder = shaderBuilder, | ||
shaderNames = arrayOf("content"), | ||
inputs = arrayOf(null), | ||
).asComposeRenderEffect() | ||
} |
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