Skip to content

Commit 301b9e0

Browse files
committed
fixed pattern aspect ratio
1 parent 82447ca commit 301b9e0

File tree

6 files changed

+43
-40
lines changed

6 files changed

+43
-40
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ The following modifiers are available to customize the sticker effect:
4646
| `.stickerNoiseScale(_:)` | 100.0 | Adjusts the scale of the noise pattern |
4747
| `.stickerNoiseIntensity(_:)` | 1.2 | Controls the intensity of the noise effect |
4848
| `.stickerLightIntensity(_:)` | 0.3 | Adjusts the intensity of the light reflection |
49-
| `.stickerPatternType(_:)` | diamond | Modifies the checker pattern |
49+
| `.stickerPattern(_:)` | diamond | Modifies the checker pattern |
5050

5151
Example usage:
5252

‎Sources/Sticker/Resources/Shaders/FoilShader.metal‎

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,6 @@ float noisePattern(float2 uv) {
3737
return mix(a, b, u.x) + (c - a) * u.y * (1.0 - u.x) + (d - b) * u.x * u.y;
3838
}
3939

40-
// Checker pattern function to create a diamond (lozenge) effect
41-
float checkerPattern(float2 uv, float scale, float degreesAngle) {
42-
float radiansAngle = degreesAngle * M_PI_F / 180;
43-
44-
// Scale the UV coordinates
45-
uv *= scale;
46-
47-
// Rotate the UV coordinates by the specified angle
48-
float cosAngle = cos(radiansAngle);
49-
float sinAngle = sin(radiansAngle);
50-
float2 rotatedUV = float2(
51-
cosAngle * uv.x - sinAngle * uv.y,
52-
sinAngle * uv.x + cosAngle * uv.y
53-
);
54-
55-
// Determine if the current tile is black or white
56-
return fmod(floor(rotatedUV.x) + floor(rotatedUV.y), 2.0) == 0.0 ? 0.0 : 1.0;
57-
}
58-
5940
// Function to mix colors with more intensity on lighter colors
6041
half4 lightnessMix(half4 baseColor, half4 overlayColor, float intensity, float baselineFactor) {
6142
// Calculate brightness of the base color
@@ -92,9 +73,9 @@ float squarePattern(float2 uv, float scale, float degreesAngle) {
9273
float cosAngle = cos(radiansAngle);
9374
float sinAngle = sin(radiansAngle);
9475
float2 rotatedUV = float2(
95-
cosAngle * uv.x - sinAngle * uv.y,
96-
sinAngle * uv.x + cosAngle * uv.y
97-
);
76+
cosAngle * uv.x - sinAngle * uv.y,
77+
sinAngle * uv.x + cosAngle * uv.y
78+
);
9879

9980
// Determine if the current tile is black or white
10081
return fmod(floor(rotatedUV.x) + floor(rotatedUV.y), 2.0) == 0.0 ? 0.0 : 1.0;
@@ -116,17 +97,34 @@ float stickerPattern(int option, float2 uv, float scale) {
11697
}
11798
}
11899

100+
[[ stitchable ]] half4 foil(
101+
float2 position,
102+
half4 color,
103+
float2 offset,
104+
float2 size,
105+
float scale,
106+
float intensity,
107+
float contrast,
108+
float blendFactor,
109+
float checkerScale,
110+
float checkerIntensity,
111+
float noiseScale,
112+
float noiseIntensity,
113+
float patternType
114+
) {
115+
// Calculate aspect ratio (width / height)
116+
float aspectRatio = size.x / size.y;
119117

120-
[[ stitchable ]] half4 foil(float2 position, half4 color, float2 offset, float2 size, float scale, float intensity, float contrast, float blendFactor, float checkerScale, float checkerIntensity, float noiseScale, float noiseIntensity, float patternType) {
121118
// Normalize the offset by dividing by size to keep it consistent across different view sizes
122119
float2 normalizedOffset = (offset + size * 250) / (size * scale) * 0.01;
120+
float2 normalizedPosition = float2(position.x * aspectRatio, position.y);
123121

124122
// Adjust UV coordinates by adding the normalized offset, then apply scaling
125123
float2 uv = (position / (size * scale)) + normalizedOffset;
126124

127125
// Scale the noise based on the normalized position and noiseScale parameter
128126
float gradientNoise = random(position) * 0.1;
129-
float pattern = stickerPattern(patternType, position / size * checkerScale, checkerScale);
127+
float pattern = stickerPattern(patternType, normalizedPosition / size * checkerScale, checkerScale);
130128
float noise = noisePattern(position / size * noiseScale);
131129

132130
// Calculate less saturated color shifts for a metallic effect

‎Sources/Sticker/StickerEffect/StickerEffect.swift‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,11 @@ extension View {
9999
.shadow(radius: 20)
100100
.padding()
101101

102-
Circle()
103-
.fill(.white)
104-
.overlay {
105-
Circle()
106-
.stroke(.black, lineWidth: 16)
107-
.padding()
108-
}
102+
Image(systemName: "applelogo")
103+
.resizable()
104+
.aspectRatio(contentMode: .fit)
109105
.frame(height: 300)
106+
.foregroundStyle(.white)
110107
.animation(.snappy) { view in
111108
view
112109
.stickerEffect()

‎Sources/Sticker/StickerEffectParameter/StickerEffectParameter.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension EnvironmentValues {
1717
@Entry var stickerNoiseScale: Float = 100
1818
@Entry var stickerNoiseIntensity: Float = 1.2
1919
@Entry var stickerLightIntensity: Float = 0.3
20-
@Entry var stickerPattern: StickerPattern = .diamond
20+
@Entry var stickerPattern: StickerPatternType = .diamond
2121
}
2222

2323
extension View {
@@ -57,7 +57,7 @@ extension View {
5757
environment(\.stickerLightIntensity, intensity)
5858
}
5959

60-
public func stickerPatternType(_ type: StickerPattern) -> some View {
60+
public func stickerPattern(_ type: StickerPatternType) -> some View {
6161
environment(\.stickerPattern, type)
6262
}
6363
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// StickerPatternType.swift
3+
// Sticker
4+
//
5+
// Created by Benjamin Pisano on 26/01/2025.
6+
//
7+
8+
import Foundation
9+
10+
public enum StickerPatternType: Int, Hashable, Equatable, Sendable {
11+
case diamond = 0
12+
case square = 1
13+
}

‎Sources/Sticker/Utils/Extensions/ShaderLibraryExtension.swift‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extension ShaderLibrary {
2121
checkerIntensity: Float = 1.2,
2222
noiseScale: Float = 100,
2323
noiseIntensity: Float = 1.2,
24-
patternType: StickerPattern = .diamond
24+
patternType: StickerPatternType = .diamond
2525
) -> Shader {
2626
moduleLibrary.foil(
2727
.float2(offset),
@@ -60,8 +60,3 @@ public extension ShaderLibrary {
6060
try await reflectionShader().compile(as: .colorEffect)
6161
}
6262
}
63-
64-
public enum StickerPattern: Int, Hashable, Equatable, Sendable {
65-
case diamond = 0
66-
case square = 1
67-
}

0 commit comments

Comments
 (0)