swifty creatives

Creative coding framework for Swift. Built on Apple's Metal. Inspired by Processing. Supports visionOS.

210
8
Swift

SwiftyCreatives

Release
Swift Compatibility
Platform Compatibility
License

Creative coding framework for Swift.
Using Metal directly for rendering. Inspired by Processing. Supports visionOS.

outputFinalfinal

Requirements

  • Swift5.9

Supported Platforms

  • macOS v14
  • iOS v17
  • visionOS v1
  • tvOS v17

Key Features

Processing-like Syntax

You can easily create your graphics, using Swift Programming Language with the intuitive essence of Processing.
I like how push() and pop() became super simple using Swift’s trailing closure.

import SwiftyCreatives

final class MySketch: Sketch {
    override func draw(encoder: SCEncoder) {
        let count = 20
        for i in 0..<count {
            color(0.75, Float(i) / 40, 1, 0.5)
            push {
                rotateY(Float.pi * 2 / Float(count) * Float(i))
                translate(10, 0, 0)
                box(0, 0, 0, 1, 1, 1)
            }
        }
    }
}

struct ContentView: View {
    var body: some View {
        SketchView(MySketch())
    }
}

スクリーンショット 2024-02-04 5 38 56

Apple Vision Pro - Immersive Space

Supports visionOS! You can dive in to your sketch with Immersive Space rendering!

ImmersiveSpace(id: "ImmersiveSpace") {
    CompositorLayer(configuration: ContentStageConfiguration()) { layerRenderer in
        let renderer = RendererBase.BlendMode.normalBlend.getRenderer(sketch: SampleSketch(), layerRenderer: layerRenderer)
        renderer.startRenderLoop()
    }
}

ddd

xib to 3D Space!

Create UIView with xib, and place it in 3D scene!
UIButton can be connected with IBAction, and can be tapped in 3d space.

outout

Installation

Use Swift Package Manager.

dependencies: [
    .package(url: "https://github.com/yukiny0811/swifty-creatives.git", branch: "main")
]
.product(name: "SwiftyCreatives", package: "swifty-creatives")

Features

  • [x] Geometries
    • [x] Rectangle
    • [x] Circle
    • [x] Box
    • [x] Triangle
    • [x] Line
    • [x] BoldLine
    • [x] 3D Model (obj)
    • [x] Image
    • [x] Text
    • [x] 3D Text
    • [x] UIView Object (3d view created from xib, with interactive button)
    • [x] Mesh
    • [x] Vertex Buffer
    • [x] SVG
  • [x] Geometries with Hit Test (you can click or hover on it)
    • [x] HitTestableRect
    • [x] HitTestableBox
    • [x] HitTestableImg
  • [x] Effects
    • [x] Color
    • [x] Fog
    • [x] Bloom
    • [x] Post Process (you can create your own)
  • [x] Transforms
    • [x] Translate
    • [x] Rotate
    • [x] Scale
    • [x] Push & Pop
  • [x] Rendering
    • [x] Normal rendering with depth test
    • [x] Add blend rendering
    • [x] Transparent rendering with depth test
  • [x] Animation
    • [x] SCAnimatable property wrapper for animations
  • [x] Audio
    • [x] Audio Input
    • [x] FFT
  • [x] Camera
    • [x] Perspective Camera
    • [x] Orthographic Camera
    • [x] Customizable fov
  • [x] View
    • [x] SwiftUI View
    • [x] UIKit View
    • [x] visionOS Immersive Space
  • [x] Others
    • [x] Creating original geomery class
    • [x] Font Rendering

Events

open func setupCamera(camera: MainCamera) {}
open func preProcess(commandBuffer: MTLCommandBuffer) {}
open func update(camera: MainCamera) {}
open func draw(encoder: SCEncoder) {}
open func afterCommit(texture: MTLTexture?) {}
open func postProcess(texture: MTLTexture, commandBuffer: MTLCommandBuffer) {}

#if os(macOS)
open func mouseMoved(camera: MainCamera, location: f2) {}
open func mouseDown(camera: MainCamera, location: f2) {}
open func mouseDragged(camera: MainCamera, location: f2) {}
open func mouseUp(camera: MainCamera, location: f2) {}
open func mouseEntered(camera: MainCamera, location: f2) {}
open func mouseExited(camera: MainCamera, location: f2) {}
open func keyDown(with event: NSEvent, camera: MainCamera, viewFrame: CGRect) {}
open func keyUp(with event: NSEvent, camera: MainCamera, viewFrame: CGRect) {}
open func viewWillStartLiveResize(camera: MainCamera, viewFrame: CGRect) {}
open func resize(withOldSuperviewSize oldSize: NSSize, camera: MainCamera, viewFrame: CGRect) {}
open func viewDidEndLiveResize(camera: MainCamera, viewFrame: CGRect) {}
open func scrollWheel(with event: NSEvent, camera: MainCamera, viewFrame: CGRect) {}
#endif

#if os(iOS)
open func onScroll(delta: CGPoint, camera: MainCamera, view: UIView, gestureRecognizer: UIPanGestureRecognizer) {}
open func touchesBegan(camera: MainCamera, touchLocations: [f2]) {}
open func touchesMoved(camera: MainCamera, touchLocations: [f2]) {}
open func touchesEnded(camera: MainCamera, touchLocations: [f2]) {}
open func touchesCancelled(camera: MainCamera, touchLocations: [f2]) {}
#endif

Other Examples

ExampleMacOSApp 2023年-02月-24日 17 11 06

CheckMacOS 2023年-03月-01日 6 46 57

QuickTime Player - 画面収録 2023-02-10 1 53 14 mov 2023年-02月-10日 2 55 14

stable fluids

Credits

  • swifty-creatives library is created by Yuki Kuwashima
  • twitter: @yukiny_sfc
  • email