How to present a metal frame
Generated on 7/27/2024
1 search

This post is brought to you by Tailscan,
the best devtool for Tailwind CSS.
To present a Metal frame in visionOS, you can follow the steps outlined in the session "Render Metal with passthrough in visionOS" and "Bring your iOS or iPadOS game to visionOS" from WWDC 2024.
-
Create a Rendering Session:
- Use compositor services to create a rendering session.
- Utilize Metal APIs to render your frames.
- Integrate ARKit for world and hand tracking.
For more details, you can refer to the session Render Metal with passthrough in visionOS.
-
Render to a Texture:
- You can start with a
CAMetalLayer
for simplicity, but moving to a low-level texture API will give you more control. - Create a view that contains the
CAMetalLayer
and use aCADisplayLink
to get a callback every frame.
Here's a code snippet for rendering to a
CAMetalLayer
:class MetalView: UIView { override class var layerClass: AnyClass { return CAMetalLayer.self } var metalLayer: CAMetalLayer { return layer as! CAMetalLayer } private var displayLink: CADisplayLink? override init(frame: CGRect) { super.init(frame: frame) setupDisplayLink() } required init?(coder: NSCoder) { super.init(coder: coder) setupDisplayLink() } private func setupDisplayLink() { displayLink = CADisplayLink(target: self, selector: #selector(render)) displayLink?.add(to: .main, forMode: .default) } @objc private func render() { // Render to the metalLayer here } }
For more details, you can refer to the session Bring your iOS or iPadOS game to visionOS.
- You can start with a
-
Enhance the Rendering:
- Add stereoscopy to add depth to your game.
- Implement head tracking to make your game look like a physical window into another world.
- Add Variable Refresh Rate (VRR) to improve performance.
For more details, you can refer to the session Bring your iOS or iPadOS game to visionOS.
By following these steps, you can effectively present a Metal frame in visionOS, enhancing the immersive experience of your application.

Explore object tracking for visionOS
Find out how you can use object tracking to turn real-world objects into virtual anchors in your visionOS app. Learn how you can build spatial experiences with object tracking from start to finish. Find out how to create a reference object using machine learning in Create ML and attach content relative to your target object in Reality Composer Pro, RealityKit or ARKit APIs.

Render Metal with passthrough in visionOS
Get ready to extend your Metal experiences for visionOS. Learn best practices for integrating your rendered content with people’s physical environments with passthrough. Find out how to position rendered content to match the physical world, reduce latency with trackable anchor prediction, and more.

Port advanced games to Apple platforms
Discover how simple it can be to reach players on Apple platforms worldwide. We’ll show you how to evaluate your Windows executable on Apple silicon, start your game port with code samples, convert your shader code to Metal, and bring your game to Mac, iPhone, and iPad. Explore enhanced Metal tools that understand HLSL shaders to validate, debug, and profile your ported shaders on Metal.

Bring your iOS or iPadOS game to visionOS
Discover how to transform your iOS or iPadOS game into a uniquely visionOS experience. Increase the immersion (and fun factor!) with a 3D frame or an immersive background. And invite players further into your world by adding depth to the window with stereoscopy or head tracking.