Skip to content

Instantly share code, notes, and snippets.

@Koshimizu-Takehito
Last active February 11, 2025 23:43
Show Gist options
  • Save Koshimizu-Takehito/6823c17ee460797154bf99cf2d8f751a to your computer and use it in GitHub Desktop.
Save Koshimizu-Takehito/6823c17ee460797154bf99cf2d8f751a to your computer and use it in GitHub Desktop.
シェーダー関数でモザイク
#include <metal_stdlib>
#include <SwiftUI/SwiftUI.h>
using namespace metal;
[[ stitchable ]] half4 mosaic(
float2 position,
SwiftUI::Layer layer,
float scale
) {
position = floor(position / scale) * scale;
return layer.sample(position);
}
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 + 30 * (sin(time) + 1)
Image("waterwheel")
.layerEffect(
mosaic(scale: scale),
maxSampleOffset: .zero
)
}
}
func mosaic(scale: Double) -> Shader {
ShaderLibrary
.mosaic(.float(scale))
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment