SwiftyCreatives

1.6.3

Creative coding framework for Swift. Built on Apple's Metal.
yukiny0811/swifty-creatives

What's New

v1.6.3

2022-12-30T15:40:15Z

fix: setup() called more than once.

SwiftyCreatives

Release Swift Compatibility Platform Compatibility License

Creative coding library for Swift.
Using Metal directly for rendering.

ExampleMacOSApp 2022年-12月-16日 18 08 41

Sample Code

Main sketch process

final class MySketch: SketchBase {
    var boxes: [Box] = []
    var elapsed: Float = 0.0
    func setup() {
        for _ in 0...100 {
            let box = Box()
            box.setColor(f4.randomPoint(0...1))
            box.setPos(f3.randomPoint(-10...10))
            box.setScale(f3.one * Float.random(in: 0.3...3))
            boxes.append(box)
        }
    }
    func update() {
        for b in boxes {
            b.setColor(f4(sin(elapsed), b.color.y, b.color.z, b.color.w))
        }
        elapsed += 0.01
    }
    func cameraProcess(camera: MainCamera<some CameraConfigBase>) {
        camera.rotateAroundY(0.01)
    }
    func draw(encoder: MTLRenderCommandEncoder) {
        for b in boxes {
            b.draw(encoder)
        }
    }
}

Configs (optional)

final class MyDrawConfigNormal: DrawConfigBase {
    static var contentScaleFactor: Int = 3
    static var blendMode: SwiftyCreatives.BlendMode = .normalBlend
}
final class MyDrawConfigAdd: DrawConfigBase {
    static var contentScaleFactor: Int = 3
    static var blendMode: SwiftyCreatives.BlendMode = .add
}
final class MyDrawConfigAlpha: DrawConfigBase {
    static var contentScaleFactor: Int = 3
    static var blendMode: SwiftyCreatives.BlendMode = .alphaBlend
}

You can use SketchView as SwiftUI View

ZStack {
    Text("Swifty-Creatives Example")
        .font(.largeTitle)
    VStack {
        HStack {
            SketchView<MySketch, MainCameraConfig, MainDrawConfig>()
            SketchView<MySketch, MainCameraConfig, MainDrawConfig>()
            SketchView<MySketch, MainCameraConfig, MainDrawConfig>()
        }
        HStack {
            SketchView<MySketch, MainCameraConfig, MyDrawConfigNormal>()
            SketchView<MySketch, MainCameraConfig, MyDrawConfigAdd>()
            SketchView<MySketch, MainCameraConfig, MyDrawConfigAlpha>()
        }
    }
}
.background(.black)

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Jan 27 2023 18:25:44 GMT-0500 (GMT-05:00)