Skip to content

Instantly share code, notes, and snippets.

@Koshimizu-Takehito
Last active February 11, 2025 23:41
Show Gist options
  • Save Koshimizu-Takehito/2317e4b6f7dd9a50d15df5c83f2ed2d2 to your computer and use it in GitHub Desktop.
Save Koshimizu-Takehito/2317e4b6f7dd9a50d15df5c83f2ed2d2 to your computer and use it in GitHub Desktop.
シェーダー関数で螺旋
#include <metal_stdlib>
using namespace metal;
[[ stitchable ]] half4 spiral(
float2 position,
half4 color,
float4 box,
float scale
) {
position = position - box.zw/2;
position = position / (10 * scale);
float r = length(position);
float t = atan2(position.y, position.x);
float offset = int(10 * t) % 2 ? 0 : M_PI_F;
half3 value = int((sin((r + t + offset)) + 1)) % 2;
return half4(value * color.xyz, 1.0);
}
import SwiftUI
struct ContentView: View {
@State private var start = Date()
var body: some View {
TimelineView(.animation) { context in
let time = context.date
.timeIntervalSince(start)
let scale = 0.3 + 6 * (sin(time) + 1) / 2
Rectangle()
.colorEffect(shader(scale: scale))
.foregroundStyle(.white)
.ignoresSafeArea()
}
}
func shader(scale: Double) -> Shader {
ShaderLibrary
.spiral(.boundingRect, .float(scale))
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment