A lightweight 3D renderer for SwiftUI.

Overview

Prism

A lightweight 3D renderer for SwiftUI.

  • Works with any SwiftUI View.
  • Fully interactive and animatable.
  • Compatible with all SwiftUI modifiers.
  • Will not affect sizing (unlike GeometryReader...)
  • 100% SwiftUI. No SceneKit or other weird stuff.
  • Powered by perspective transforms, so it's fast.
  • Supports sizing, extrusion, levitation, and more.
Logo cube Square that animates into a cube, with border 3 green cubes that bounce on click Thin, levitating blue prism Prism with an image on each side

Installation

Requires iOS 13+ or macOS Catalina and up. Prism can be installed through the Swift Package Manager.

https://github.com/aheze/Prism

Usage

Prism is designed to be easy to use. First, define a PrismConfiguration for customization. Then add a PrismCanvas, which can contain as many PrismViews as you'd like.

struct ContentView: View {
    @State var configuration = PrismConfiguration(
        tilt: 0.5, /// A value from 0 to 1, representing the perspective.
        size: CGSize(width: 200, height: 200), /// How big the prism is.
        extrusion: 100, /// The z height.
        levitation: 20, /// How far the prism is from the ground.
        shadowColor: Color.black, /// A dynamic shadow that's rendered underneath the prism.
        shadowOpacity: 0.25 /// The strength of the shadow.
    )

    var body: some View {
        PrismCanvas(tilt: configuration.tilt) {
            PrismView(configuration: configuration) {
                Color.blue
            } left: {
                Color.red
            } right: {
                Color.green
            }
        }
    }
}

Result, a multicolored cube

Example

The example app includes a bunch of samples to play with. Download it here!

Screenshot of example app

Customization

Prism supports extensive customization through the PrismConfiguration struct. Check out the example app for an editor!

Customization.mp4

Community

Author

Prism is made by aheze.

Contributing

All contributions are welcome. Just fork the repo, then make a pull request.

Need Help?

Open an issue or join the Discord server. You can also ping me on Twitter. Or read the source code — there's lots of comments.

Apps Using Prism

Find is an app for browsing photos. Prism is used in the onboarding screen — download to check it out!

Find.mp4

If you have an app that uses Prism, just make a PR or message me.

License

MIT License

Copyright (c) 2022 A. Zheng

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
You might also like...
Lightweight and customisable async image loading in SwiftUI. Supports on-disk storage, placeholders and more!
Lightweight and customisable async image loading in SwiftUI. Supports on-disk storage, placeholders and more!

Asyncrounously download and display images in Swift UI. Supports progress indicators, placeholders and image transitions. RemoteImageView Asyncrounous

📖   A lightweight, paging view solution for SwiftUI
📖 A lightweight, paging view solution for SwiftUI

Getting Started | Customization | Installation Getting Started Basic usage Using Pages is as easy as: import Pages struct WelcomeView: View { @S

SSToastMessage is written purely in SwiftUI. It will add toast, alert, and floating message view over the top of any view. It is intended to be simple, lightweight, and easy to use. It will be a popup with a single line of code.
SSToastMessage is written purely in SwiftUI. It will add toast, alert, and floating message view over the top of any view. It is intended to be simple, lightweight, and easy to use. It will be a popup with a single line of code.

SSToastMessage SSToastMessage is written in SwiftUI. It will add toast, alert, and floating message view over the top of any view. It is intended to b

A simple and lightweight Swift package which provides a SwiftUI view for interactive geo coordinates input!
A simple and lightweight Swift package which provides a SwiftUI view for interactive geo coordinates input!

LocationPicker for SwiftUI LocationPicker for SwiftUI is a very simple and lightweight Swift package which provides you a SwiftUI view for interactive

A lightweight iOS mini framework that enables programmatic navigation with SwiftUI, by using UIKit under the hood.

RouteLinkKit A lightweight iOS mini framework that enables programmatic navigation with SwiftUI. RouteLinkKit is fully compatible with native Navigati

SwiftUINavigator: a lightweight, flexible, and super easy library which makes SwiftUI navigation a trivial task
SwiftUINavigator: a lightweight, flexible, and super easy library which makes SwiftUI navigation a trivial task

The logo is contributed with ❤️ by Mahmoud Hussein SwiftUINavigator is a lightwe

A powerful lightweight theme 🎨 manager for SwiftUI
A powerful lightweight theme 🎨 manager for SwiftUI

SwiftTheming 🎨 is a handy lightweight handy theme manager which handles multiple themes based on system-wide appearances - light and dark appearances

A lightweight Elm-like Store for SwiftUI

ObservableStore A simple Elm-like Store for SwiftUI, based on ObservableObject. ObservableStore helps you craft more reliable apps by centralizing all

A lightweight loading animation that can be applied to any SwiftUI view with 1 line of code.

SimpleAFLoader A lightweight loading animation that can be applied to any SwiftUI view with 1 line of code. All animations are built using the SwiftUI

🎯Linker  Lightweight way to handle internal and external deeplinks in Swift for iOS
🎯Linker Lightweight way to handle internal and external deeplinks in Swift for iOS

Linker Lightweight way to handle internal and external deeplinks in Swift for iOS. Installation Dependency Managers CocoaPods CocoaPods is a dependenc

Apphud SDK is a lightweight open-source Swift library to manage auto-renewable subscriptions and other in-app purchases in your iOS app.
Apphud SDK is a lightweight open-source Swift library to manage auto-renewable subscriptions and other in-app purchases in your iOS app.

Apphud SDK Apphud SDK is a lightweight open-source Swift library to manage auto-renewable subscriptions and other in-app purchases in your iOS app. No

 InAppPurchase - A Simple and Lightweight framework for In App Purchase
InAppPurchase - A Simple and Lightweight framework for In App Purchase

InAppPurchase A Simple, Lightweight and Safe framework for In App Purchase Feature Simple and Light 👍 Support Promoting In-App Purchases 💰 No need t

SwiftyStoreKit is a lightweight In App Purchases framework for iOS, tvOS, watchOS, macOS, and Mac Catalyst ⛺
SwiftyStoreKit is a lightweight In App Purchases framework for iOS, tvOS, watchOS, macOS, and Mac Catalyst ⛺

SwiftyStoreKit is a lightweight In App Purchases framework for iOS, tvOS, watchOS, macOS, and Mac Catalyst. Features Super easy-to-use block-based API

 ARVoxelKit - Lightweight Framework for Voxel graphic using AR + SceneKit
ARVoxelKit - Lightweight Framework for Voxel graphic using AR + SceneKit

ARVoxelKit Lightweight Framework for Voxel graphic using AR + SceneKit Requirements ARVoxelKit requires iOS 11 and devices, which support ARKit Usage

Chatto is a Swift lightweight framework to build chat applications.
Chatto is a Swift lightweight framework to build chat applications.

Chatto is a Swift lightweight framework to build chat applications. It's been designed to be extensible and performant. Along with Chatto there is ChattoAdditions, a companion framework which includes cells for messages and an extensible input component. You can find more details about how it was implemented in our blog. See them in action!

SwiftyTextTable - A lightweight Swift library for generating text tables
SwiftyTextTable - A lightweight Swift library for generating text tables

SwiftyTextTable A lightweight Swift library for generating text tables. Swift Language Support SwiftyTextTable is now Swift 4.0 compatible! The last r

Hydra ⚡️ Lightweight full-featured Promises, Async & Await Library in Swift
Hydra ⚡️ Lightweight full-featured Promises, Async & Await Library in Swift

Lightweight full-featured Promises, Async & Await Library in Swift What's this? Hydra is full-featured lightweight library which allows you to write b

🛶Shallows is a generic abstraction layer over lightweight data storage and persistence.

Shallows Shallows is a generic abstraction layer over lightweight data storage and persistence. It provides a StorageKey, Value type, instances of w

Super lightweight DB written in Swift.
Super lightweight DB written in Swift.

Use of value types is recommended and we define standard values, simple structured data, application state and etc. as struct or enum. Pencil makes us store these values more easily.

Comments
  • Sometimes there's a gap between the left and right sides

    Sometimes there's a gap between the left and right sides

    Sometimes a gap appears:

    Normal | Gap --- | --- Screen Shot 2022-09-27 at 9 59 12 AM | Screen Shot 2022-09-27 at 9 59 09 AM

    The gap sometimes gets more pronounced when animating the tilt.

    This is probably due this rotationEffect here:

    Screen Shot 2022-09-27 at 10 07 16 AM

    We need to find a way for the tiltRight function to apply that rotation effect too, instead of splitting it into two.

    https://github.com/aheze/Prism/blob/6879fddd60d41ec6012501f07bb38963cb596fd2/Sources/PrismEffect.swift#L61

    bug help wanted 
    opened by aheze 1
  • Edge cases when `tilt` is set to `0`

    Edge cases when `tilt` is set to `0`

    When the tilt is set to 0, this gets printed in the console:

    ignoring singular matrix: ProjectionTransform(m11: 0.0, m12: 1.0, m13: 0.0, m21: 0.0, m22: 1.0, m23: 0.0, m31: 0.0, m32: 0.0, m33: 1.0)
    ignoring singular matrix: ProjectionTransform(m11: 0.0, m12: 1.0, m13: 0.0, m21: 0.0, m22: 1.0, m23: 0.0, m31: 0.0, m32: 0.0, m33: 1.0)
    ignoring singular matrix: ProjectionTransform(m11: 0.0, m12: 1.0, m13: 0.0, m21: 0.0, m22: 1.0, m23: 0.0, m31: 0.0, m32: 0.0, m33: 1.0)
    ignoring singular matrix: ProjectionTransform(m11: 0.0, m12: 1.0, m13: 0.0, m21: 0.0, m22: 1.0, m23: 0.0, m31: 0.0, m32: 0.0, m33: 1.0)
    ignoring singular matrix: ProjectionTransform(m11: 0.0, m12: 1.0, m13: 0.0, m21: 0.0, m22: 1.0, m23: 0.0, m31: 0.0, m32: 0.0, m33: 1.0)
    

    Also, the shadow gets significantly darker:

    https://user-images.githubusercontent.com/49819455/192589013-f6b2d63a-f914-4db8-877e-7b170601201c.mov

    Could this be due to CGAffineTransform edge cases when the value is 0? Maybe we should have an if check for 0 and adjust as needed.

    Current workaround

    Set the shadow opacity to 0 when settings the shadow to 0.

    bug help wanted 
    opened by aheze 0
Releases(1.0.0)
  • 1.0.0(Sep 27, 2022)

    Thanks for checking out Prism! For some more animations with Prism, check out Find v3 — DM me on Twitter for the invite link.

    https://user-images.githubusercontent.com/49819455/192443083-a826b35b-d226-4f09-9cb5-d1bf21932222.mp4

    Source code(tar.gz)
    Source code(zip)
Owner
Andrew Zheng
I like coding with Swift and watching anime. WWDC21 Scholar.
Andrew Zheng
Awesome SwiftUI Libraries 🚀 🚀 🚀

Awesome SwiftUI Libraries ?? ?? ?? Do you know a cool SwiftUI library? Welcome to contribute! Content Alert Animation Button Calendar Card Chart Clock

Anton Paliakov 348 Dec 29, 2022
SwiftUI Cookbook (2nd Edition), published by Packt

SwiftUI Cookbook - Second Edition This is the code repository for SwiftUI Cookbook - Second Edition , published by Packt. A guide to solving the most

Packt 55 Dec 24, 2022
SwiftUIDay49To61 - Includes CoreData, Networking learning and my custom MVVM CoreData implementation from 100 days of SwiftUI

Includes CoreData, Networking learning and my custom MVVM CoreData implementation from 100 days of SwiftUI by "Hacking with Swift"

Vong Nyuk Soon 3 Feb 11, 2022
SwiftUI Apple Developer Tutorial

SwiftUI Apple Developer Tutorial Apple Developer Documentation - SwiftUI Tutorial Chapter 1 SwiftUI Essentials Creating and Combining Views Apple Deve

Hyungyu Kim 4 Feb 16, 2022
SVG parser and renderer written in SwiftUI

SVGView SVG parser written in SwiftUI We are a development agency building phenomenal apps. Overview The goal of this project is to bring the full pow

Exyte 269 Jan 4, 2023
A powerful and easy to use live mesh gradient renderer for iOS.

MeshKit A powerful and easy to use live mesh gradient renderer for iOS. This project wouldn't be possible without the awesome work from Moving Parts a

Ethan Lipnik 51 Jan 1, 2023
A proof-of-concept WebURL domain renderer, using a port of Chromium's IDN spoof-checking logic to protect against confusable domains

WebURLSpoofChecking A proof-of-concept WebURL.Domain renderer which uses a port of Chromium's IDN spoof-checking logic (Overview, Implementation) to p

Karl 3 Aug 6, 2022
Easily show HUDs with SwiftUI! Lightweight SwiftUI wrapper for JGProgressHUD for iOS, tvOS, Catalyst.

JGProgressHUD-SwiftUI This is a lightweight and easy-to-use SwiftUI wrapper for JGProgressHUD, giving you access to the large and proven feature set o

Jonas Gessner 78 Dec 21, 2022
sRouting - The lightweight navigation framework for SwiftUI.

sRouting The lightweight navigation framework for SwiftUI. Overview sRouting using the native navigation mechanism in SwiftUI. It's easy to handle nav

Shiro 8 Aug 15, 2022
AsyncImage before iOS 15. Lightweight, pure SwiftUI Image view, that displays an image downloaded from URL, with auxiliary views and local cache.

URLImage URLImage is a SwiftUI view that displays an image downloaded from provided URL. URLImage manages downloading remote image and caching it loca

Dmytro Anokhin 1k Jan 4, 2023