A simple recreation of the MTKView from iOS/macOS, intended for use in visionOS. It works exactly the same, and allows for 2D Metal drawing within a (presumably) windowed application.
This was, for some reason, a missing feature. I'm assuming they will one day add it, at which point this can retire, but then again, the last time I did that was an Apple Watch web browser back in 2018, and for some reason that's still needed, so who knows.
- You can use this straight away in a visionOS app exactly as you would, normally, an MTKView in iOS or macOS.
- You can create a shared typelias, and bridge this across all 3 platforms with only a little extra work.
Just drop the Swift file into your project and compile.
There's also an included "ScreenAlias.swift" that typealiases NSScreen
for macOS, UIScreen
for iOS, and defines a basic struct for visionOS, allowing for cross-compatibility on screen scale checks. If you'd rather not use it, you will need to replace the reference to Screen.scale
in the main file. (Note: For visionOS, the value should only ever be 1.)
See tutorials that make use of Metal and MTKView.
I'm making use of this as-is in one of my projects currently, and it seems to be stable and complete, so I'm not expecting any additional changes from my end at this point.
If, however, you find a need for a particular change, a suggestion to make it better, then certainly feel free to open a pull request and I will review. Thanks in advance, if you do.
This project is available under The MPL-2.0 License.
Copyright © 2024, Eric Reedy. See LICENSE file.