Skip to content

Instantly share code, notes, and snippets.

@Lofionic
Last active October 22, 2024 23:34
Show Gist options
  • Select an option

  • Save Lofionic/546554d84dcad121e77734ad821dc52c to your computer and use it in GitHub Desktop.

Select an option

Save Lofionic/546554d84dcad121e77734ad821dc52c to your computer and use it in GitHub Desktop.

Revisions

  1. Lofionic revised this gist Oct 22, 2024. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions ContentView.swift
    Original file line number Diff line number Diff line change
    @@ -9,9 +9,7 @@ struct ContentView: View {
    let time = start.distance(to: timeline.date)
    Rectangle()
    .frame(width: 300, height: 500)
    .visualEffect {
    content,
    proxy in
    .visualEffect { content, proxy in
    content
    .colorEffect(
    ShaderLibrary.colorEffect(
  2. Lofionic revised this gist Oct 22, 2024. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion ContentView.swift
    Original file line number Diff line number Diff line change
    @@ -28,5 +28,4 @@ struct ContentView: View {
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .background(.gray)
    }

    }
  3. Lofionic created this gist Oct 22, 2024.
    32 changes: 32 additions & 0 deletions ContentView.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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)
    }

    }
    13 changes: 13 additions & 0 deletions Shader.metal
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    #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);
    }