├── .gitignore
├── .swiftpm
└── xcode
│ ├── package.xcworkspace
│ └── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ └── xcshareddata
│ └── xcschemes
│ └── GlassEffect.xcscheme
├── Example
├── .gitignore
├── GlassEffectExample.xcodeproj
│ ├── project.pbxproj
│ └── project.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
└── GlassEffectExample
│ ├── Assets.xcassets
│ ├── 90sNormal.imageset
│ │ ├── Contents.json
│ │ └── Glass_Vintage_001_normal.png
│ ├── AccentColor.colorset
│ │ └── Contents.json
│ ├── AppIcon.appiconset
│ │ └── Contents.json
│ ├── BlurredNormal.imageset
│ │ ├── Contents.json
│ │ └── everytexture.com-stock-misc-texture-00047-normal-1024.jpg
│ ├── BrokenNormal.imageset
│ │ ├── Contents.json
│ │ └── Stone_Tiles_003_NORM.png
│ └── Contents.json
│ ├── ContentView.swift
│ ├── GlassEffectExample.entitlements
│ ├── GlassEffectExampleApp.swift
│ ├── Preview Content
│ └── Preview Assets.xcassets
│ │ └── Contents.json
│ ├── Slider+.swift
│ └── View+Parallax.swift
├── LICENSE
├── Package.swift
├── README.md
└── Sources
└── GlassEffect
├── GlassEffect.swift
└── Shaders
└── GlassEffect.metal
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | /.build
3 | /Packages
4 | xcuserdata/
5 | DerivedData/
6 | .swiftpm/configuration/registries.json
7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8 | .netrc
9 |
--------------------------------------------------------------------------------
/.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.swiftpm/xcode/xcshareddata/xcschemes/GlassEffect.xcscheme:
--------------------------------------------------------------------------------
1 |
2 |
5 |
8 |
9 |
15 |
21 |
22 |
23 |
24 |
25 |
31 |
32 |
42 |
43 |
49 |
50 |
56 |
57 |
58 |
59 |
61 |
62 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/Example/.gitignore:
--------------------------------------------------------------------------------
1 | # Xcode
2 | #
3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4 |
5 | ## User settings
6 | xcuserdata/
7 |
8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
9 | *.xcscmblueprint
10 | *.xccheckout
11 |
12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
13 | build/
14 | DerivedData/
15 | *.moved-aside
16 | *.pbxuser
17 | !default.pbxuser
18 | *.mode1v3
19 | !default.mode1v3
20 | *.mode2v3
21 | !default.mode2v3
22 | *.perspectivev3
23 | !default.perspectivev3
24 |
25 | ## Obj-C/Swift specific
26 | *.hmap
27 |
28 | ## App packaging
29 | *.ipa
30 | *.dSYM.zip
31 | *.dSYM
32 |
33 | ## Playgrounds
34 | timeline.xctimeline
35 | playground.xcworkspace
36 |
37 | # Swift Package Manager
38 | #
39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
40 | # Packages/
41 | # Package.pins
42 | # Package.resolved
43 | # *.xcodeproj
44 | #
45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
46 | # hence it is not needed unless you have added a package configuration file to your project
47 | # .swiftpm
48 |
49 | .build/
50 |
51 | # CocoaPods
52 | #
53 | # We recommend against adding the Pods directory to your .gitignore. However
54 | # you should judge for yourself, the pros and cons are mentioned at:
55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
56 | #
57 | # Pods/
58 | #
59 | # Add this line if you want to avoid checking in source code from the Xcode workspace
60 | # *.xcworkspace
61 |
62 | # Carthage
63 | #
64 | # Add this line if you want to avoid checking in source code from Carthage dependencies.
65 | # Carthage/Checkouts
66 |
67 | Carthage/Build/
68 |
69 | # Accio dependency management
70 | Dependencies/
71 | .accio/
72 |
73 | # fastlane
74 | #
75 | # It is recommended to not store the screenshots in the git repo.
76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed.
77 | # For more information about the recommended setup visit:
78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control
79 |
80 | fastlane/report.xml
81 | fastlane/Preview.html
82 | fastlane/screenshots/**/*.png
83 | fastlane/test_output
84 |
85 | # Code Injection
86 | #
87 | # After new code Injection tools there's a generated folder /iOSInjectionProject
88 | # https://github.com/johnno1962/injectionforxcode
89 |
90 | iOSInjectionProject/
91 |
--------------------------------------------------------------------------------
/Example/GlassEffectExample.xcodeproj/project.pbxproj:
--------------------------------------------------------------------------------
1 | // !$*UTF8*$!
2 | {
3 | archiveVersion = 1;
4 | classes = {
5 | };
6 | objectVersion = 56;
7 | objects = {
8 |
9 | /* Begin PBXBuildFile section */
10 | AE007E1E2B922A3F00F6F24A /* View+Parallax.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE007E1D2B922A3F00F6F24A /* View+Parallax.swift */; };
11 | AE007E202B922ABD00F6F24A /* Slider+.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE007E1F2B922ABD00F6F24A /* Slider+.swift */; };
12 | AE312BF82B8F5EFB007C1000 /* GlassEffect in Frameworks */ = {isa = PBXBuildFile; productRef = AE312BF72B8F5EFB007C1000 /* GlassEffect */; };
13 | AE3C3C042B8F5D9B003F0B6A /* GlassEffectExampleApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE3C3C032B8F5D9B003F0B6A /* GlassEffectExampleApp.swift */; };
14 | AE3C3C062B8F5D9B003F0B6A /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE3C3C052B8F5D9B003F0B6A /* ContentView.swift */; };
15 | AE3C3C082B8F5D9E003F0B6A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AE3C3C072B8F5D9E003F0B6A /* Assets.xcassets */; };
16 | AE3C3C0C2B8F5D9E003F0B6A /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AE3C3C0B2B8F5D9E003F0B6A /* Preview Assets.xcassets */; };
17 | /* End PBXBuildFile section */
18 |
19 | /* Begin PBXFileReference section */
20 | AE007E1D2B922A3F00F6F24A /* View+Parallax.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+Parallax.swift"; sourceTree = ""; };
21 | AE007E1F2B922ABD00F6F24A /* Slider+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Slider+.swift"; sourceTree = ""; };
22 | AE312BF52B8F5E2D007C1000 /* GlassEffect */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = GlassEffect; path = ..; sourceTree = ""; };
23 | AE3C3C002B8F5D9B003F0B6A /* GlassEffectExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = GlassEffectExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
24 | AE3C3C032B8F5D9B003F0B6A /* GlassEffectExampleApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlassEffectExampleApp.swift; sourceTree = ""; };
25 | AE3C3C052B8F5D9B003F0B6A /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; };
26 | AE3C3C072B8F5D9E003F0B6A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; };
27 | AE3C3C092B8F5D9E003F0B6A /* GlassEffectExample.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = GlassEffectExample.entitlements; sourceTree = ""; };
28 | AE3C3C0B2B8F5D9E003F0B6A /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; };
29 | /* End PBXFileReference section */
30 |
31 | /* Begin PBXFrameworksBuildPhase section */
32 | AE3C3BFD2B8F5D9B003F0B6A /* Frameworks */ = {
33 | isa = PBXFrameworksBuildPhase;
34 | buildActionMask = 2147483647;
35 | files = (
36 | AE312BF82B8F5EFB007C1000 /* GlassEffect in Frameworks */,
37 | );
38 | runOnlyForDeploymentPostprocessing = 0;
39 | };
40 | /* End PBXFrameworksBuildPhase section */
41 |
42 | /* Begin PBXGroup section */
43 | AE312BF62B8F5EFB007C1000 /* Frameworks */ = {
44 | isa = PBXGroup;
45 | children = (
46 | );
47 | name = Frameworks;
48 | sourceTree = "";
49 | };
50 | AE3C3BF72B8F5D9B003F0B6A = {
51 | isa = PBXGroup;
52 | children = (
53 | AE312BF52B8F5E2D007C1000 /* GlassEffect */,
54 | AE3C3C022B8F5D9B003F0B6A /* GlassEffectExample */,
55 | AE3C3C012B8F5D9B003F0B6A /* Products */,
56 | AE312BF62B8F5EFB007C1000 /* Frameworks */,
57 | );
58 | sourceTree = "";
59 | };
60 | AE3C3C012B8F5D9B003F0B6A /* Products */ = {
61 | isa = PBXGroup;
62 | children = (
63 | AE3C3C002B8F5D9B003F0B6A /* GlassEffectExample.app */,
64 | );
65 | name = Products;
66 | sourceTree = "";
67 | };
68 | AE3C3C022B8F5D9B003F0B6A /* GlassEffectExample */ = {
69 | isa = PBXGroup;
70 | children = (
71 | AE3C3C032B8F5D9B003F0B6A /* GlassEffectExampleApp.swift */,
72 | AE3C3C052B8F5D9B003F0B6A /* ContentView.swift */,
73 | AE3C3C072B8F5D9E003F0B6A /* Assets.xcassets */,
74 | AE3C3C092B8F5D9E003F0B6A /* GlassEffectExample.entitlements */,
75 | AE3C3C0A2B8F5D9E003F0B6A /* Preview Content */,
76 | AE007E1D2B922A3F00F6F24A /* View+Parallax.swift */,
77 | AE007E1F2B922ABD00F6F24A /* Slider+.swift */,
78 | );
79 | path = GlassEffectExample;
80 | sourceTree = "";
81 | };
82 | AE3C3C0A2B8F5D9E003F0B6A /* Preview Content */ = {
83 | isa = PBXGroup;
84 | children = (
85 | AE3C3C0B2B8F5D9E003F0B6A /* Preview Assets.xcassets */,
86 | );
87 | path = "Preview Content";
88 | sourceTree = "";
89 | };
90 | /* End PBXGroup section */
91 |
92 | /* Begin PBXNativeTarget section */
93 | AE3C3BFF2B8F5D9B003F0B6A /* GlassEffectExample */ = {
94 | isa = PBXNativeTarget;
95 | buildConfigurationList = AE3C3C0F2B8F5D9E003F0B6A /* Build configuration list for PBXNativeTarget "GlassEffectExample" */;
96 | buildPhases = (
97 | AE3C3BFC2B8F5D9B003F0B6A /* Sources */,
98 | AE3C3BFD2B8F5D9B003F0B6A /* Frameworks */,
99 | AE3C3BFE2B8F5D9B003F0B6A /* Resources */,
100 | );
101 | buildRules = (
102 | );
103 | dependencies = (
104 | );
105 | name = GlassEffectExample;
106 | packageProductDependencies = (
107 | AE312BF72B8F5EFB007C1000 /* GlassEffect */,
108 | );
109 | productName = GlassEffectExample;
110 | productReference = AE3C3C002B8F5D9B003F0B6A /* GlassEffectExample.app */;
111 | productType = "com.apple.product-type.application";
112 | };
113 | /* End PBXNativeTarget section */
114 |
115 | /* Begin PBXProject section */
116 | AE3C3BF82B8F5D9B003F0B6A /* Project object */ = {
117 | isa = PBXProject;
118 | attributes = {
119 | BuildIndependentTargetsInParallel = 1;
120 | LastSwiftUpdateCheck = 1520;
121 | LastUpgradeCheck = 1520;
122 | TargetAttributes = {
123 | AE3C3BFF2B8F5D9B003F0B6A = {
124 | CreatedOnToolsVersion = 15.2;
125 | };
126 | };
127 | };
128 | buildConfigurationList = AE3C3BFB2B8F5D9B003F0B6A /* Build configuration list for PBXProject "GlassEffectExample" */;
129 | compatibilityVersion = "Xcode 14.0";
130 | developmentRegion = en;
131 | hasScannedForEncodings = 0;
132 | knownRegions = (
133 | en,
134 | Base,
135 | );
136 | mainGroup = AE3C3BF72B8F5D9B003F0B6A;
137 | productRefGroup = AE3C3C012B8F5D9B003F0B6A /* Products */;
138 | projectDirPath = "";
139 | projectRoot = "";
140 | targets = (
141 | AE3C3BFF2B8F5D9B003F0B6A /* GlassEffectExample */,
142 | );
143 | };
144 | /* End PBXProject section */
145 |
146 | /* Begin PBXResourcesBuildPhase section */
147 | AE3C3BFE2B8F5D9B003F0B6A /* Resources */ = {
148 | isa = PBXResourcesBuildPhase;
149 | buildActionMask = 2147483647;
150 | files = (
151 | AE3C3C0C2B8F5D9E003F0B6A /* Preview Assets.xcassets in Resources */,
152 | AE3C3C082B8F5D9E003F0B6A /* Assets.xcassets in Resources */,
153 | );
154 | runOnlyForDeploymentPostprocessing = 0;
155 | };
156 | /* End PBXResourcesBuildPhase section */
157 |
158 | /* Begin PBXSourcesBuildPhase section */
159 | AE3C3BFC2B8F5D9B003F0B6A /* Sources */ = {
160 | isa = PBXSourcesBuildPhase;
161 | buildActionMask = 2147483647;
162 | files = (
163 | AE007E202B922ABD00F6F24A /* Slider+.swift in Sources */,
164 | AE007E1E2B922A3F00F6F24A /* View+Parallax.swift in Sources */,
165 | AE3C3C062B8F5D9B003F0B6A /* ContentView.swift in Sources */,
166 | AE3C3C042B8F5D9B003F0B6A /* GlassEffectExampleApp.swift in Sources */,
167 | );
168 | runOnlyForDeploymentPostprocessing = 0;
169 | };
170 | /* End PBXSourcesBuildPhase section */
171 |
172 | /* Begin XCBuildConfiguration section */
173 | AE3C3C0D2B8F5D9E003F0B6A /* Debug */ = {
174 | isa = XCBuildConfiguration;
175 | buildSettings = {
176 | ALWAYS_SEARCH_USER_PATHS = NO;
177 | ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
178 | CLANG_ANALYZER_NONNULL = YES;
179 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
180 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
181 | CLANG_ENABLE_MODULES = YES;
182 | CLANG_ENABLE_OBJC_ARC = YES;
183 | CLANG_ENABLE_OBJC_WEAK = YES;
184 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
185 | CLANG_WARN_BOOL_CONVERSION = YES;
186 | CLANG_WARN_COMMA = YES;
187 | CLANG_WARN_CONSTANT_CONVERSION = YES;
188 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
189 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
190 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
191 | CLANG_WARN_EMPTY_BODY = YES;
192 | CLANG_WARN_ENUM_CONVERSION = YES;
193 | CLANG_WARN_INFINITE_RECURSION = YES;
194 | CLANG_WARN_INT_CONVERSION = YES;
195 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
196 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
197 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
198 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
199 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
200 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
201 | CLANG_WARN_STRICT_PROTOTYPES = YES;
202 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
203 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
204 | CLANG_WARN_UNREACHABLE_CODE = YES;
205 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
206 | COPY_PHASE_STRIP = NO;
207 | DEBUG_INFORMATION_FORMAT = dwarf;
208 | ENABLE_STRICT_OBJC_MSGSEND = YES;
209 | ENABLE_TESTABILITY = YES;
210 | ENABLE_USER_SCRIPT_SANDBOXING = YES;
211 | GCC_C_LANGUAGE_STANDARD = gnu17;
212 | GCC_DYNAMIC_NO_PIC = NO;
213 | GCC_NO_COMMON_BLOCKS = YES;
214 | GCC_OPTIMIZATION_LEVEL = 0;
215 | GCC_PREPROCESSOR_DEFINITIONS = (
216 | "DEBUG=1",
217 | "$(inherited)",
218 | );
219 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
220 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
221 | GCC_WARN_UNDECLARED_SELECTOR = YES;
222 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
223 | GCC_WARN_UNUSED_FUNCTION = YES;
224 | GCC_WARN_UNUSED_VARIABLE = YES;
225 | LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
226 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
227 | MTL_FAST_MATH = YES;
228 | ONLY_ACTIVE_ARCH = YES;
229 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)";
230 | SWIFT_OPTIMIZATION_LEVEL = "-Onone";
231 | };
232 | name = Debug;
233 | };
234 | AE3C3C0E2B8F5D9E003F0B6A /* Release */ = {
235 | isa = XCBuildConfiguration;
236 | buildSettings = {
237 | ALWAYS_SEARCH_USER_PATHS = NO;
238 | ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
239 | CLANG_ANALYZER_NONNULL = YES;
240 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
241 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
242 | CLANG_ENABLE_MODULES = YES;
243 | CLANG_ENABLE_OBJC_ARC = YES;
244 | CLANG_ENABLE_OBJC_WEAK = YES;
245 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
246 | CLANG_WARN_BOOL_CONVERSION = YES;
247 | CLANG_WARN_COMMA = YES;
248 | CLANG_WARN_CONSTANT_CONVERSION = YES;
249 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
250 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
251 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
252 | CLANG_WARN_EMPTY_BODY = YES;
253 | CLANG_WARN_ENUM_CONVERSION = YES;
254 | CLANG_WARN_INFINITE_RECURSION = YES;
255 | CLANG_WARN_INT_CONVERSION = YES;
256 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
257 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
258 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
259 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
260 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
261 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
262 | CLANG_WARN_STRICT_PROTOTYPES = YES;
263 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
264 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
265 | CLANG_WARN_UNREACHABLE_CODE = YES;
266 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
267 | COPY_PHASE_STRIP = NO;
268 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
269 | ENABLE_NS_ASSERTIONS = NO;
270 | ENABLE_STRICT_OBJC_MSGSEND = YES;
271 | ENABLE_USER_SCRIPT_SANDBOXING = YES;
272 | GCC_C_LANGUAGE_STANDARD = gnu17;
273 | GCC_NO_COMMON_BLOCKS = YES;
274 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
275 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
276 | GCC_WARN_UNDECLARED_SELECTOR = YES;
277 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
278 | GCC_WARN_UNUSED_FUNCTION = YES;
279 | GCC_WARN_UNUSED_VARIABLE = YES;
280 | LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
281 | MTL_ENABLE_DEBUG_INFO = NO;
282 | MTL_FAST_MATH = YES;
283 | SWIFT_COMPILATION_MODE = wholemodule;
284 | };
285 | name = Release;
286 | };
287 | AE3C3C102B8F5D9E003F0B6A /* Debug */ = {
288 | isa = XCBuildConfiguration;
289 | buildSettings = {
290 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
291 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
292 | CODE_SIGN_ENTITLEMENTS = GlassEffectExample/GlassEffectExample.entitlements;
293 | CODE_SIGN_STYLE = Automatic;
294 | CURRENT_PROJECT_VERSION = 1;
295 | DEVELOPMENT_ASSET_PATHS = "\"GlassEffectExample/Preview Content\"";
296 | DEVELOPMENT_TEAM = S4YLM87KXX;
297 | ENABLE_HARDENED_RUNTIME = YES;
298 | ENABLE_PREVIEWS = YES;
299 | GENERATE_INFOPLIST_FILE = YES;
300 | "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;
301 | "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphonesimulator*]" = YES;
302 | "INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphoneos*]" = YES;
303 | "INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphonesimulator*]" = YES;
304 | "INFOPLIST_KEY_UILaunchScreen_Generation[sdk=iphoneos*]" = YES;
305 | "INFOPLIST_KEY_UILaunchScreen_Generation[sdk=iphonesimulator*]" = YES;
306 | "INFOPLIST_KEY_UIStatusBarStyle[sdk=iphoneos*]" = UIStatusBarStyleDefault;
307 | "INFOPLIST_KEY_UIStatusBarStyle[sdk=iphonesimulator*]" = UIStatusBarStyleDefault;
308 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
309 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
310 | IPHONEOS_DEPLOYMENT_TARGET = 17.2;
311 | LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
312 | "LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
313 | MACOSX_DEPLOYMENT_TARGET = 14.2;
314 | MARKETING_VERSION = 1.0;
315 | PRODUCT_BUNDLE_IDENTIFIER = com.mczarnik.GlassEffectExample;
316 | PRODUCT_NAME = "$(TARGET_NAME)";
317 | SDKROOT = auto;
318 | SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx";
319 | SWIFT_EMIT_LOC_STRINGS = YES;
320 | SWIFT_VERSION = 5.0;
321 | TARGETED_DEVICE_FAMILY = "1,2";
322 | };
323 | name = Debug;
324 | };
325 | AE3C3C112B8F5D9E003F0B6A /* Release */ = {
326 | isa = XCBuildConfiguration;
327 | buildSettings = {
328 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
329 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
330 | CODE_SIGN_ENTITLEMENTS = GlassEffectExample/GlassEffectExample.entitlements;
331 | CODE_SIGN_STYLE = Automatic;
332 | CURRENT_PROJECT_VERSION = 1;
333 | DEVELOPMENT_ASSET_PATHS = "\"GlassEffectExample/Preview Content\"";
334 | DEVELOPMENT_TEAM = S4YLM87KXX;
335 | ENABLE_HARDENED_RUNTIME = YES;
336 | ENABLE_PREVIEWS = YES;
337 | GENERATE_INFOPLIST_FILE = YES;
338 | "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;
339 | "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphonesimulator*]" = YES;
340 | "INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphoneos*]" = YES;
341 | "INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphonesimulator*]" = YES;
342 | "INFOPLIST_KEY_UILaunchScreen_Generation[sdk=iphoneos*]" = YES;
343 | "INFOPLIST_KEY_UILaunchScreen_Generation[sdk=iphonesimulator*]" = YES;
344 | "INFOPLIST_KEY_UIStatusBarStyle[sdk=iphoneos*]" = UIStatusBarStyleDefault;
345 | "INFOPLIST_KEY_UIStatusBarStyle[sdk=iphonesimulator*]" = UIStatusBarStyleDefault;
346 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
347 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
348 | IPHONEOS_DEPLOYMENT_TARGET = 17.2;
349 | LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
350 | "LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
351 | MACOSX_DEPLOYMENT_TARGET = 14.2;
352 | MARKETING_VERSION = 1.0;
353 | PRODUCT_BUNDLE_IDENTIFIER = com.mczarnik.GlassEffectExample;
354 | PRODUCT_NAME = "$(TARGET_NAME)";
355 | SDKROOT = auto;
356 | SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx";
357 | SWIFT_EMIT_LOC_STRINGS = YES;
358 | SWIFT_VERSION = 5.0;
359 | TARGETED_DEVICE_FAMILY = "1,2";
360 | };
361 | name = Release;
362 | };
363 | /* End XCBuildConfiguration section */
364 |
365 | /* Begin XCConfigurationList section */
366 | AE3C3BFB2B8F5D9B003F0B6A /* Build configuration list for PBXProject "GlassEffectExample" */ = {
367 | isa = XCConfigurationList;
368 | buildConfigurations = (
369 | AE3C3C0D2B8F5D9E003F0B6A /* Debug */,
370 | AE3C3C0E2B8F5D9E003F0B6A /* Release */,
371 | );
372 | defaultConfigurationIsVisible = 0;
373 | defaultConfigurationName = Release;
374 | };
375 | AE3C3C0F2B8F5D9E003F0B6A /* Build configuration list for PBXNativeTarget "GlassEffectExample" */ = {
376 | isa = XCConfigurationList;
377 | buildConfigurations = (
378 | AE3C3C102B8F5D9E003F0B6A /* Debug */,
379 | AE3C3C112B8F5D9E003F0B6A /* Release */,
380 | );
381 | defaultConfigurationIsVisible = 0;
382 | defaultConfigurationName = Release;
383 | };
384 | /* End XCConfigurationList section */
385 |
386 | /* Begin XCSwiftPackageProductDependency section */
387 | AE312BF72B8F5EFB007C1000 /* GlassEffect */ = {
388 | isa = XCSwiftPackageProductDependency;
389 | productName = GlassEffect;
390 | };
391 | /* End XCSwiftPackageProductDependency section */
392 | };
393 | rootObject = AE3C3BF82B8F5D9B003F0B6A /* Project object */;
394 | }
395 |
--------------------------------------------------------------------------------
/Example/GlassEffectExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Example/GlassEffectExample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Example/GlassEffectExample/Assets.xcassets/90sNormal.imageset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "filename" : "Glass_Vintage_001_normal.png",
5 | "idiom" : "universal"
6 | }
7 | ],
8 | "info" : {
9 | "author" : "xcode",
10 | "version" : 1
11 | },
12 | "properties" : {
13 | "compression-type" : "lossless"
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Example/GlassEffectExample/Assets.xcassets/90sNormal.imageset/Glass_Vintage_001_normal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Czajnikowski/GlassEffect/ea49838d901b9ef050c9af431304b74fe89f67cc/Example/GlassEffectExample/Assets.xcassets/90sNormal.imageset/Glass_Vintage_001_normal.png
--------------------------------------------------------------------------------
/Example/GlassEffectExample/Assets.xcassets/AccentColor.colorset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "colors" : [
3 | {
4 | "idiom" : "universal"
5 | }
6 | ],
7 | "info" : {
8 | "author" : "xcode",
9 | "version" : 1
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Example/GlassEffectExample/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "universal",
5 | "platform" : "ios",
6 | "size" : "1024x1024"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "scale" : "1x",
11 | "size" : "16x16"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "scale" : "2x",
16 | "size" : "16x16"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "scale" : "1x",
21 | "size" : "32x32"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "scale" : "2x",
26 | "size" : "32x32"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "scale" : "1x",
31 | "size" : "128x128"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "scale" : "2x",
36 | "size" : "128x128"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "scale" : "1x",
41 | "size" : "256x256"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "scale" : "2x",
46 | "size" : "256x256"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "scale" : "1x",
51 | "size" : "512x512"
52 | },
53 | {
54 | "idiom" : "mac",
55 | "scale" : "2x",
56 | "size" : "512x512"
57 | }
58 | ],
59 | "info" : {
60 | "author" : "xcode",
61 | "version" : 1
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/Example/GlassEffectExample/Assets.xcassets/BlurredNormal.imageset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "filename" : "everytexture.com-stock-misc-texture-00047-normal-1024.jpg",
5 | "idiom" : "universal"
6 | }
7 | ],
8 | "info" : {
9 | "author" : "xcode",
10 | "version" : 1
11 | },
12 | "properties" : {
13 | "compression-type" : "lossless"
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Example/GlassEffectExample/Assets.xcassets/BlurredNormal.imageset/everytexture.com-stock-misc-texture-00047-normal-1024.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Czajnikowski/GlassEffect/ea49838d901b9ef050c9af431304b74fe89f67cc/Example/GlassEffectExample/Assets.xcassets/BlurredNormal.imageset/everytexture.com-stock-misc-texture-00047-normal-1024.jpg
--------------------------------------------------------------------------------
/Example/GlassEffectExample/Assets.xcassets/BrokenNormal.imageset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "filename" : "Stone_Tiles_003_NORM.png",
5 | "idiom" : "universal"
6 | }
7 | ],
8 | "info" : {
9 | "author" : "xcode",
10 | "version" : 1
11 | },
12 | "properties" : {
13 | "compression-type" : "lossless"
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Example/GlassEffectExample/Assets.xcassets/BrokenNormal.imageset/Stone_Tiles_003_NORM.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Czajnikowski/GlassEffect/ea49838d901b9ef050c9af431304b74fe89f67cc/Example/GlassEffectExample/Assets.xcassets/BrokenNormal.imageset/Stone_Tiles_003_NORM.png
--------------------------------------------------------------------------------
/Example/GlassEffectExample/Assets.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "author" : "xcode",
4 | "version" : 1
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/Example/GlassEffectExample/ContentView.swift:
--------------------------------------------------------------------------------
1 | //
2 | // ContentView.swift
3 | // GlassEffectExample
4 | //
5 | // Created by Maciek Czarnik on 28/02/2024.
6 | //
7 |
8 | import SwiftUI
9 | import GlassEffect
10 |
11 | struct ContentView: View {
12 | private var normalMapNames = ["90s", "Broken", "Blurred"]
13 | @State private var selectedNormalMapName = "90s"
14 | private var normalImageResource: ImageResource {
15 | .init(name: selectedNormalMapName + "Normal", bundle: .main)
16 | }
17 |
18 | @State private var distance: Float = 200
19 | @State private var transparency: Float = 0.85
20 | @State private var reflection: Float = 0.75
21 | @State private var detail: Float = 0.5
22 | @State private var lightPosition: CGPoint = .init(x: 0.1, y: 0.5)
23 |
24 | @State private var isConfigurationPresented = false
25 |
26 | var body: some View {
27 | Rectangle()
28 | .fill(.background.opacity(0.001))
29 | .background {
30 | Text(
31 | """
32 | 🥹
33 | 🏙️
34 | ♥️
35 | """
36 | )
37 | .font(.system(size: 100))
38 | #if os(iOS)
39 | .withParallaxOffset(magnitude: distance / 2)
40 | #endif
41 | }
42 | .ignoresSafeArea()
43 | .glassEffect(
44 | normal: .init(normalImageResource),
45 | distance: distance,
46 | transparency: transparency,
47 | reflection: reflection,
48 | lightPosition: lightPosition,
49 | detail: detail
50 | )
51 | .padding(.all, -50)
52 | #if os(iOS)
53 | .withParallaxOffset(magnitude: 10)
54 | #endif
55 | .overlay(alignment: .top) {
56 | Picker("Select map:", selection: $selectedNormalMapName) {
57 | ForEach(normalMapNames, id: \.self) {
58 | Text($0)
59 | }
60 | }
61 | // A change to `selectedNormalMapName` does not get picked up as a change otherwise... SwiftUI issue 🐛
62 | .onChange(of: selectedNormalMapName) { transparency += 0.0001 }
63 | }
64 | .overlay(alignment: .bottomTrailing) {
65 | Button(
66 | action: { isConfigurationPresented.toggle() },
67 | label: {
68 | Image(systemName: "slider.horizontal.3")
69 | .padding()
70 | }
71 | )
72 | }
73 | .font(.largeTitle)
74 | .sheet(isPresented: $isConfigurationPresented) {
75 | HStack {
76 | VStack {
77 | Slider(value: $distance, in: 0 ... 600).withText("distance")
78 | Slider(value: $transparency, in: 0 ... 1).withText("transparency")
79 | Slider(value: $detail, in: 0 ... 2).withText("detail")
80 | }
81 | VStack {
82 | Slider(value: $reflection, in: 0 ... 5).withText("reflection")
83 | Slider(value: $lightPosition.x, in: -1 ... 1).withText("x")
84 | Slider(value: $lightPosition.y, in: -1 ... 1).withText("y")
85 | }
86 | }
87 | .presentationDetents([.fraction(0.3)])
88 | }
89 | }
90 | }
91 |
92 | #Preview {
93 | ContentView()
94 | }
95 |
--------------------------------------------------------------------------------
/Example/GlassEffectExample/GlassEffectExample.entitlements:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | com.apple.security.app-sandbox
6 |
7 | com.apple.security.files.user-selected.read-only
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Example/GlassEffectExample/GlassEffectExampleApp.swift:
--------------------------------------------------------------------------------
1 | //
2 | // GlassEffectExampleApp.swift
3 | // GlassEffectExample
4 | //
5 | // Created by Maciek Czarnik on 28/02/2024.
6 | //
7 |
8 | import SwiftUI
9 |
10 | @main
11 | struct GlassEffectExampleApp: App {
12 | var body: some Scene {
13 | WindowGroup {
14 | ContentView()
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Example/GlassEffectExample/Preview Content/Preview Assets.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "author" : "xcode",
4 | "version" : 1
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/Example/GlassEffectExample/Slider+.swift:
--------------------------------------------------------------------------------
1 | //
2 | // Slider+.swift
3 | // GlassEffectExample
4 | //
5 | // Created by Maciek Czarnik on 01/03/2024.
6 | //
7 |
8 | import SwiftUI
9 |
10 | extension Slider {
11 | func withText(_ text: String) -> some View {
12 | VStack {
13 | Text(text)
14 | self
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Example/GlassEffectExample/View+Parallax.swift:
--------------------------------------------------------------------------------
1 | //
2 | // View+Parallax.swift
3 | // GlassEffectExample
4 | //
5 | // Created by Maciek Czarnik on 01/03/2024.
6 | //
7 |
8 | import SwiftUI
9 | import CoreMotion
10 |
11 | #if os(iOS)
12 | extension View {
13 | func withParallaxOffset(magnitude: Float) -> some View {
14 | modifier(ParallaxMotionModifier(magnitude: magnitude))
15 | }
16 | }
17 |
18 | private struct ParallaxMotionModifier: ViewModifier {
19 | @State private var pitch: Float = 0
20 | @State private var roll: Float = 0
21 |
22 | @StateObject var manager: MotionManager = MotionManager()
23 |
24 | var magnitude: Float
25 |
26 | func body(content: Content) -> some View {
27 | content
28 | .offset(
29 | x: CGFloat(roll * magnitude),
30 | y: CGFloat(-pitch * magnitude)
31 | )
32 | .onAppear {
33 | manager.setBindings(pitch: $pitch, roll: $roll)
34 | manager.initialize()
35 | }
36 | }
37 | }
38 |
39 | private class MotionManager: ObservableObject {
40 | @Binding var pitch: Float
41 | @Binding var roll: Float
42 |
43 | init() {
44 | _pitch = .constant(0)
45 | _roll = .constant(0)
46 | }
47 |
48 | func setBindings(pitch: Binding, roll: Binding) {
49 | _pitch = pitch
50 | _roll = roll
51 | }
52 |
53 | private let manager: CMMotionManager = CMMotionManager()
54 |
55 | func initialize() {
56 | self.manager.deviceMotionUpdateInterval = 1/60
57 | self.manager.startDeviceMotionUpdates(to: .main) { (motionData, error) in
58 | guard error == nil else {
59 | print(error!)
60 | return
61 | }
62 |
63 | if let motionData = motionData {
64 | self.pitch = Float(motionData.attitude.pitch)
65 | self.roll = Float(motionData.attitude.roll)
66 | }
67 | }
68 | }
69 | }
70 | #endif
71 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2024 Maciek Czarnik
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version: 5.9
2 | // The swift-tools-version declares the minimum version of Swift required to build this package.
3 |
4 | import PackageDescription
5 |
6 | let package = Package(
7 | name: "GlassEffect",
8 | platforms: [
9 | .iOS(.v17),
10 | .macOS(.v14),
11 | ],
12 | products: [
13 | .library(
14 | name: "GlassEffect",
15 | targets: ["GlassEffect"]
16 | ),
17 | ],
18 | targets: [
19 | .target(
20 | name: "GlassEffect",
21 | resources: [.process("Shaders/GlassEffect.metal")]
22 | ),
23 | ]
24 | )
25 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # GlassEffect
2 |
3 | You can use this effect to produce a decorative/ornamental glass-like effect on top of your SwiftUI `View`s 🔍. The package features an example app that can be used to play with some input parameters and showcases a bunch of normal textures.
4 |
5 | It models refraction and reflection of light taking the fresnel effect into account. It also allows one to calculate a "detail" that serves as a visual clue about the shape of the glass.
6 |
7 | 
8 |
9 | Be sure to check it in both light and dark mode (especially if you're after reflections ✨).
10 |
11 | ## How To Use It?
12 |
13 | Use as a Swift Package.
14 |
15 | Use a [`View.glassEffect` modifier](https://github.com/Czajnikowski/GlassEffect/blob/main/Sources/GlassEffect/GlassEffect.swift#L5-L24). At a minimum, you should be able to run the effect by supplying a normal map texture image. For best results, be sure to use high-quality normal maps.
16 |
17 | ## Credits
18 |
19 | I used a bunch of resources from [3dtextures.me](https://3dtextures.me) and [everytexture.com](https://everytexture.com/) in the example app.
20 |
21 | ## Why?
22 |
23 | I made it mostly for myself as an exercise in my recent `SwiftUI` + `Metal` research.
24 |
25 | Feel free to use it, feel free to contribute (fix issues, share ideas), and feel free to hit me up [@czajnikowski](https://twitter.com/czajnikowski) 👋
26 |
--------------------------------------------------------------------------------
/Sources/GlassEffect/GlassEffect.swift:
--------------------------------------------------------------------------------
1 | import SwiftUI
2 |
3 | extension View {
4 |
5 | /// Applies a glass effect.
6 | ///
7 | /// - Parameters:
8 | /// - normal: a normal map used to generate effect.
9 | /// - distance: distance between the content (modified view) and the glass. The higher - the more refracted the output.
10 | /// - color: glass color.
11 | /// - transparency: 0.0 results in non-transparent glass of `color` color. 1.0 results in fully transparent glass.
12 | /// - reflection: amount of reflections in the output
13 | /// - lightPosition: the light, positioned at `lightPosition.x, lightPosition.y, -1` is used to provide the reflection on the glass.
14 | /// - detail: corresponds to the amount of structural detail visible in the output.
15 | /// - Returns: A view with applied effect.
16 | public func glassEffect(
17 | normal: Image,
18 | distance: Float = 50,
19 | color: Color = Color(#colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)),
20 | transparency: Float = 0.8,
21 | reflection: Float = 0.5,
22 | lightPosition: CGPoint = .init(x: 0.1, y: 0.5),
23 | detail: Float = 0.5
24 | ) -> some View {
25 | compositingGroup()
26 | .layerEffect(
27 | .init(
28 | function: .init(
29 | library: .bundle(.module),
30 | name: "glassEffect"
31 | ),
32 | arguments: [
33 | .image(normal),
34 | .float(distance),
35 | .color(color),
36 | .float(transparency),
37 | .float(reflection),
38 | .float2(lightPosition),
39 | .float(detail)
40 | ]
41 | ),
42 | maxSampleOffset: .zero
43 | )
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/Sources/GlassEffect/Shaders/GlassEffect.metal:
--------------------------------------------------------------------------------
1 | //
2 | // GlassEffect.metal
3 | // GlassEffect
4 | //
5 | // Created by Maciek Czarnik on 28/02/2024.
6 | //
7 |
8 | #include
9 | #include
10 |
11 | using namespace metal;
12 |
13 | constexpr sampler s(address::repeat, filter::bicubic);
14 |
15 | uint2 getResolution(texture2d t) {
16 | return (t.get_width(), t.get_height());
17 | }
18 |
19 | float2 texturePosition(float2 position, texture2d texture) {
20 | uint2 textureResolution = getResolution(texture);
21 | return position / float2(textureResolution);
22 | }
23 |
24 | float fresnel(
25 | half3 eye,
26 | half3 normal,
27 | half power
28 | ) {
29 | half fresnelFactor = max(half(0.), dot(eye, normal));
30 | half inverseFresnelFactor = 1.0 - fresnelFactor;
31 |
32 | return pow(inverseFresnelFactor, power);
33 | }
34 |
35 | [[ stitchable ]] half4 glassEffect(
36 | float2 position,
37 | SwiftUI::Layer layer,
38 | texture2d normalMap,
39 | float distance,
40 | half4 baseColor,
41 | float transparencyFactor,
42 | float reflectionFactor,
43 | float2 lightPosition,
44 | float detailFactor
45 | ) {
46 | half3 incident = normalize(half3(0, 0, 1));
47 | half3 eye = -incident;
48 | half3 light = half3(half2(lightPosition), -1);
49 |
50 | float2 normalPosition = texturePosition(position, normalMap);
51 | half3 normal = normalize((normalMap.sample(s, normalPosition) - 0.5).xyz);
52 | normal.z *= -1;
53 |
54 | half fresnelFactor = fresnel(eye, normal, 0.5);
55 |
56 | half3 refraction = refract(incident, normal, 1 / 1.49);
57 | half4 refractedLayerColor = layer.sample(position + float2(refraction.xy * refraction.z) * distance);
58 | refractedLayerColor.xyz *= 1 - fresnelFactor;
59 |
60 | half3 eyeLight = eye + light;
61 | half3 eyeLightHalf = eyeLight / length(eyeLight);
62 | half k = pow(max(half(0.), dot(normal, eyeLightHalf)), half(1000.));
63 | half E = max(half(0.), dot(normal, light));
64 | half3 lightReflectionColor = fresnelFactor * k * E * reflectionFactor;
65 |
66 | half3 detail = reflect(incident, normal);
67 | half3 detailColor = half3(detail.x * detail.y * detail.z) * detailFactor;
68 |
69 | return mix(
70 | baseColor,
71 | refractedLayerColor,
72 | transparencyFactor
73 | )
74 | + half4(lightReflectionColor + detailColor, 0);
75 | }
76 |
--------------------------------------------------------------------------------