-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRain.swift
65 lines (55 loc) · 1.74 KB
/
Rain.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// Rain.swift
//
//
// Created by Ben Myers on 3/20/24.
//
import SwiftUI
import Particles
import Foundation
public extension Preset {
struct Rain: Entity, PresetEntry {
private var parameters: Parameters
public init(lifetime: TimeInterval = 1.0, intensity: Int = 20, wind: CGFloat = 0.5) {
self.parameters = .init(intensity: intensity, rainLifetime: lifetime, windVelocity: wind)
}
public var body: some Entity {
Emitter(every: 1.0 / Double(parameters.intensity)) {
Drop(parameters: parameters)
}
.emitAll()
.initialPosition(.top)
}
public struct Drop: Entity {
internal var parameters: Rain.Parameters
public var body: some Entity {
Particle {
Rectangle().frame(width: 3.0, height: 12.0)
.foregroundColor(.blue)
}
.initialOffset(withX: { c in
let w = c.system.size.width * 0.5
return .random(in: -w ... w)
})
.initialPosition(.top)
.initialVelocity(xIn: parameters.windVelocity +/- 1, yIn: 13 ... 15)
.initialAcceleration(y: 0.1)
.opacity(in: 0.5 ... 1.0)
.transition(.opacity)
.lifetime(parameters.rainLifetime)
.initialRotation(.degrees(-5.0 * parameters.windVelocity))
.hueRotation(angleIn: .degrees(-10.0) ... .degrees(10.0))
.blendMode(.plusLighter)
.scale { c in
let s = CGFloat.random(in: 0.3 ... 1.0, seed: c.proxy.seed.0)
return CGSize(width: s /** cos(0.1 * c.system.time + c.proxy.seed.1)*/, height: s)
}
}
}
internal struct Parameters {
var intensity: Int
var rainLifetime: TimeInterval
var windVelocity: CGFloat
}
}
}