Skip to content

colinrford/BasicSurfaces

Repository files navigation

BasicSurfaces

Rotating Sphere Demo

This repo serves as a proof-of-concept of using Metal to render graphics inside a SwiftUI View, which among other things allows for Metal Views (see MetalUI, either in this repository, or here) to work inside Xcode previews.

As of now (1/22/25) this should just be git clone-able and build just fine right out of the box. If you try BasicSurfaces and find a build bug - or any kinda bug - please inform me via Github issues or email or anything along those lines.

What to expect of BasicSurfaces in its current state:

  • while viewing the file Basic3DView.swift in Xcode, an Xcode Preview shows a multicolored rotating sphere
  • building for macOS, iOS, and iPadOS targets is successful and a very simple application is produced which displays the same multicolored rotating sphere in Basic3DView.swift
  • you will have to alter the build settings to enable Mac Catalyst, or to alter older OS compatibility. This should work for any device that can run SwiftUI, perhaps within constraints this author is ignorant of
  • there is depth to the rotating sphere
  • the resolution is not as good as it could be
  • changing line 32 of Basic3DPresenter.swift, which reads
    surface = Sphere(device: device)//, radius: 2, vertexCount: 10240)
    
    changes whether the sphere vertices are generated on the CPU (as-is) or the GPU (un-comment out the commented portion and the additional right-parenthesis)
    • In both cases, the time it takes to generate the vertices is printed to the console along with where they were generated, the CPU or GPU
    • For the unfamiliar: with Xcode Previews, there is a separate tab for prints to the console, and the console may be hidden from view. In the console, the Executables tab will show anything printed during runtime, while the Previews tab will show what's printed within an Xcode Preview
    • A sphere generated by the kernel shader in sphere_generator.metal may have some erroneous vertices and if so will render with a few additional lines diverging from the shape of a sphere
    • The first Sphere initializer, init(device: MTLDevice), generates vertices on the CPU and should do so without error; the second initializer init?(device: MTLDevice, radius: Float, vertexCount: UInt) generates vertices on the GPU and may fail (probably won't), and you'll need to add ! or ? in a few spots
  • un-commenting lines 96-97 will cause the sphere to continuously change its brightness between 0.0 (none) and 1.0 (full)

For now I intend to keep this minimal, and really just keep it a proof-of-concept. Perhaps if there is interest I will expand upon it in the future.

About

Proof-of-concept of using Metal in SwiftUI

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published