Skip to content

Commit 2812adf

Browse files
committed
Closes #2
1 parent efb5d8f commit 2812adf

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

Sources/DoomWipe/DoomWipeShader.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ public struct DoomWipeShader {
3838
/// - Parameter animationPosition: The current time position of the animation. 0 = start, 1 = end.`
3939
/// - Parameter seed: The random seed that affects the pattern of the wipe effect.
4040
/// - Parameter direction: Whether the wipe goes downwards (original), or upwards.
41-
public init(dimensions: CGSize, animationPosition: CGFloat, seed: Int = 0, direction: WipeDirection) {
41+
public init(progress: CGFloat, seed: Int = 0, direction: WipeDirection) {
4242
let offsets = Self.getOffset(seed: seed)
43+
let scale = CGSize(width: 2, height: 2)
4344

4445
shader = Shader(
4546
function: ShaderFunction(library: .bundle(.module), name: "doomWipe"),
4647
arguments: [
47-
.data(offsets),
48-
.float2(dimensions), // View dimensions.
49-
.float2(CGSize(width: 2, height: 2)), // Transition scale.
50-
.float(min(max(animationPosition, 0), 1)), // Animation position.
48+
.data(offsets), // Offset pattern.
49+
.float2(scale), // Transition scale.
50+
.float(min(max(progress, 0), 1)), // Animation position.
5151
.float(direction.rawValue), // Direction (1 = down (original), -1 = up).
5252
]
5353
)

Sources/DoomWipe/Resources/DoomWipe.metal

+13-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ using namespace metal;
1212
SwiftUI::Layer layer,
1313
device const float *offsets,
1414
int offsetsSize,
15-
float2 layerSize,
1615
float2 scale,
1716
float value,
1817
float direction
@@ -23,11 +22,20 @@ using namespace metal;
2322

2423
int column = int(position.x / (scale.x * 2)) & 0xFF;
2524
float offset = offsets[column] * offsetScale;
26-
offset = min((offset * layerSize.y) - (layerSize.y * value), 0.0);
27-
float y = position.y + offset * direction;
25+
offset = min(offset - value, 0.0);
26+
offset *= direction;
2827

29-
float2 uv = float2(position.x, y);
30-
half4 color = layer.sample(uv);
28+
// Attempt to get normalized 0..1 UV coordinates.
29+
// We're actually copying `SwiftUI::Layer`'s `sample` method implementation.
30+
float2 p = metal::fma(position.x, layer.info[0], metal::fma(position.y, layer.info[1], layer.info[2]));
31+
p = metal::clamp(p, layer.info[3], layer.info[4]);
32+
p.y += offset;
33+
half4 color = layer.tex.sample(metal::sampler(metal::filter::linear), p);
34+
35+
// Alpha = 0 when going out of bounds.
36+
if (p.y > layer.info[4].y) {
37+
color.a = 0.0;
38+
}
3139

3240
return color;
3341
}

Sources/DoomWipe/Transition.swift

+9-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Created by Freek (github.com/frzi).
44
*/
55

6+
import AVKit
67
import SwiftUI
78

89
/// The Doom Wipe transition modifier
@@ -16,36 +17,28 @@ struct DoomWipeTransitionModifier: ViewModifier {
1617
return globalRandomSeed
1718
}
1819

19-
@State private var viewDimensions: CGSize = .zero
2020
@State private var randomSeed = Self.globalAdvancedRandomSeed()
2121

22-
let animationPosition: CGFloat
22+
let progress: CGFloat
2323
let direction: DoomWipeShader.WipeDirection
2424

2525
private var shader: Shader {
2626
DoomWipeShader(
27-
dimensions: viewDimensions,
28-
animationPosition: animationPosition,
27+
progress: progress,
2928
seed: randomSeed,
3029
direction: direction
3130
).shader
3231
}
3332

34-
init(animationPosition: CGFloat, direction: DoomWipeShader.WipeDirection = .down) {
35-
self.animationPosition = animationPosition
33+
init(progress: CGFloat, direction: DoomWipeShader.WipeDirection = .down) {
34+
self.progress = progress
3635
self.direction = direction
3736
}
3837

3938
func body(content: Content) -> some View {
4039
content
4140
.compositingGroup()
4241
.layerEffect(shader, maxSampleOffset: .zero, isEnabled: true)
43-
.background(GeometryReader { reader in
44-
HStack {}
45-
.onAppear {
46-
viewDimensions = reader.size
47-
}
48-
})
4942
}
5043
}
5144

@@ -55,8 +48,8 @@ extension AnyTransition {
5548
.asymmetric(
5649
insertion: .identity,
5750
removal: .modifier(
58-
active: DoomWipeTransitionModifier(animationPosition: 1),
59-
identity: DoomWipeTransitionModifier(animationPosition: 0)
51+
active: DoomWipeTransitionModifier(progress: 1),
52+
identity: DoomWipeTransitionModifier(progress: 0)
6053
)
6154
)
6255
}
@@ -66,8 +59,8 @@ extension AnyTransition {
6659
.asymmetric(
6760
insertion: .identity,
6861
removal: .modifier(
69-
active: DoomWipeTransitionModifier(animationPosition: 1, direction: direction),
70-
identity: DoomWipeTransitionModifier(animationPosition: 0, direction: direction)
62+
active: DoomWipeTransitionModifier(progress: 1, direction: direction),
63+
identity: DoomWipeTransitionModifier(progress: 0, direction: direction)
7164
)
7265
)
7366
}

0 commit comments

Comments
 (0)