Skip to content

Instantly share code, notes, and snippets.

@Codelaby
Forked from Lofionic/ContentView.swift
Created October 22, 2024 21:56
Show Gist options
  • Save Codelaby/7befb2aadbe6b6855bdf8960d52ebf4f to your computer and use it in GitHub Desktop.
Save Codelaby/7befb2aadbe6b6855bdf8960d52ebf4f to your computer and use it in GitHub Desktop.
Shader fun
import SwiftUI
struct ContentView: View {
@State private var start = Date.now
var body: some View {
VStack {
TimelineView(.animation) { timeline in
let time = start.distance(to: timeline.date)
Rectangle()
.frame(width: 300, height: 500)
.visualEffect { content, proxy in
content
.colorEffect(
ShaderLibrary.colorEffect(
.float2(proxy.size),
.float(time)
)
)
}
.colorEffect(ShaderLibrary.colorEffect())
.clipShape(RoundedRectangle(cornerRadius: 20))
}
}
.padding()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(.gray)
}
}
#include <metal_stdlib>
using namespace metal;
[[ stitchable ]] half4 colorEffect(float2 position, half4 color, float2 size, float time) {
float2 uv = (position / size) * 2 - 1;
half3 luma = half3(0.0);
for (int i = 0; i < 3; i++) {
float wave = cos(uv.x * 3.0 + time * 2.0) * sin(time * 3 + i * 2.0) * 5;
luma[i] = abs(1 / (50 * uv.y + 10 + wave)) + abs(1 / (50 * uv.y - 10 - wave));
}
return half4(luma * smoothstep(0, 1, (1 - abs(uv.x))) * smoothstep(0, 1, (1 - abs(uv.y))), 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment