Skip to content

Instantly share code, notes, and snippets.

@Koshimizu-Takehito
Created February 11, 2025 04:35
Show Gist options
  • Save Koshimizu-Takehito/5ce2a862edd0553bc991354b01561e1f to your computer and use it in GitHub Desktop.
Save Koshimizu-Takehito/5ce2a862edd0553bc991354b01561e1f to your computer and use it in GitHub Desktop.
MSLでパーティクル
import SwiftUI
struct ShaderView: View {
let name: String
private let start = Date()
var body: some View {
let shader = ShaderFunction(library: .default, name: name)
TimelineView(.animation) { context in
let seconds = context.date.timeIntervalSince(start)
Rectangle()
.colorEffect(shader(.boundingRect, .float(seconds)))
}
.ignoresSafeArea()
}
}
#Preview("Sample") {
ShaderView(name: "Sample::main")
}
#include <metal_stdlib>
using namespace metal;
#define PI2 6.28318530718
namespace Sample {
[[stitchable]] half4 main(float2 position, half4 color, float4 box, float sec) {
color = 0;
half2 origin = half2(box.x, box.y);
half2 size = half2(box.z, box.w);
half2 localCoord = half2(position) - origin;
half2 p = ( localCoord - 0.5 * size ) / size.y * 0.8;
half3 baseColor = half3(color.r, color.g, color.b) * 0.9 - half3(0.002);
half N = 200.0;
for (half i = 0.0; i < N; i++) {
half freq = fmod(sec / 9.0 + tan(i / N) + 1.0, 0.35);
half2 angle = half2(i, i)
+ PI2 * half2(0.0, 0.25)
+ 0.1 * normalize(half2(i, 0.1 * sec + 1.0));
half2 s = half2(sin(angle.x), sin(angle.y));
half2 q = freq * s;
half d = exp(0.47 - length(q));
half distFactor = 4 * exp(-100.0 * (d + 0.04) * length(p - q));
baseColor += half3(0.0, 0.5, 0.8) * distFactor;
}
return half4(clamp(baseColor, 0.0, 1.0), 1.0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment