Last active
October 20, 2023 09:14
-
-
Save touyou/72949ac2b30e2751e1a326aec70fafd6 to your computer and use it in GitHub Desktop.
雨粒シェーダー Metal / layerEffect版
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import Resources | |
@available(iOS 17.0, *) | |
public struct RaindropView: View { | |
@State var value: Double = 1.0 | |
@State var isSetting: Bool = false | |
let shaderFunction = ShaderFunction(library: .bundle(.module), name: "raindrop") | |
let start = Date() | |
public init() {} | |
public var body: some View { | |
TimelineView(.animation) { context in | |
ZStack { | |
Assets.image("shaderBackground") | |
.resizable() | |
.scaledToFill() | |
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height) | |
.blur(radius: 10.0) | |
VStack(spacing: 32) { | |
ZStack { | |
Assets.image("shaderBackground") | |
.resizable() | |
.scaledToFill() | |
.frame(width: 1534 / 5.0, height: 2300 / 5.0) | |
.rotation3DEffect(.degrees(180), axis: (x: 1, y: 0, z: 0)) | |
.rainDropEffect(function: shaderFunction, seconds: context.date.timeIntervalSince1970 - start.timeIntervalSince1970, alpha: 1.0, zoom: -0.4, value: value) | |
.clipShape(RoundedRectangle(cornerRadius: 16.0)) | |
.shadow(radius: 6.0) | |
Text("Hello iOS17") | |
.font(.title) | |
.bold() | |
.multilineTextAlignment(.center) | |
.foregroundStyle(.white) | |
.padding() | |
} | |
if isSetting { | |
HStack { | |
Text("\(value)") | |
Slider(value: $value, in: -3.0...3.0) | |
} | |
} | |
} | |
} | |
.onTapGesture { | |
isSetting.toggle() | |
} | |
} | |
.ignoresSafeArea(.all) | |
} | |
} | |
extension View { | |
@available(iOS 17.0, *) | |
func rainDropEffect(function: ShaderFunction, seconds: Double, alpha: Double, zoom: Double, value: Double) -> some View { | |
self.layerEffect( | |
Shader( | |
function: function, | |
arguments: [.boundingRect, .float(seconds), .float(alpha), .float(zoom), .float(value)] | |
), | |
maxSampleOffset: .zero | |
) | |
} | |
} | |
#Preview { | |
if #available(iOS 17.0, *) { | |
RaindropView() | |
} else { | |
EmptyView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment