Skip to content

Instantly share code, notes, and snippets.

@Koshimizu-Takehito
Created January 12, 2025 06:10
Show Gist options
  • Save Koshimizu-Takehito/4314dc875d4e0fcc8ae70447f827177a to your computer and use it in GitHub Desktop.
Save Koshimizu-Takehito/4314dc875d4e0fcc8ae70447f827177a to your computer and use it in GitHub Desktop.
シェーダー関数でタイル
#include <metal_stdlib>
using namespace metal;
[[ stitchable ]] half4 tile(float2 position, half4 color, float4 box, float scale) {
position = position - box.zw/2;
position = position / (10 * scale);
int2 xy = int2(floor(position)) % 2;
if ((xy.x + xy.y) % 2) {
return half4(0.0, 0.0, 0.0, 1.0);
} else {
return half4(1.0, 1.0, 1.0, 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 = 1 + 10 * (sin(time) + 1)
Rectangle()
.colorEffect(shader(scale: scale))
.foregroundStyle(.white)
.ignoresSafeArea()
}
}
func shader(scale: Double) -> Shader {
ShaderLibrary
.tile(.boundingRect, .float(scale))
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment