Applying post-processing to SceneKit's Scene and saving it to a USDZ file

I am fairly new to 3D model rendering and do not know where to start.

I am trying to, ideally with ARKit & RealityKit or SceneKit, do a scan of an environment. This includes:

Applying realistic textures to the model. Being able to save it as a .usdz file (to be able to open it within the App itself) Once it is save do post-processing measurements within the model. I would prefer to accomplish this feature by using a mesh, instead of the pointCloud that is used in the sample project of apple. Would this be doable using Apple's APIs and on a mobile device or would it be necessary to use a third party program?

I have managed to create a USDZ file using SceneKit's .scene.write(to:,delegate:) method. However the saved file is a "single object" and it is not possible to use raycasting to do post-processing measurements in the model.

Answered by DTS Engineer in 920225022

Thanks for asking. Yes, you can capture, save, and measure a textured model on an iPhone or iPad with Apple's APIs. You can also measure a model that's saved as a single object.

Object Capture builds a textured mesh from photos on the device and saves it as a USDZ file. ObjectCaptureSession.isSupported tells you whether a device can run it. Object Capture's area mode, introduced in iOS 18, extends capture from objects to areas. Discover area mode for Object Capture describes it as a way to "capture uneven terrain outdoors, objects that you can't walk all the way around, or 2.5d surfaces." The video also notes that "Areas larger than 6 feet may have reduced mesh and texture quality unless processed at a higher detail level on Mac." Area mode needs no separate call or setting: calling startCapturing() without first calling startDetecting() starts it. The Scanning objects using Object Capture sample shows the whole workflow.

When the sample reconstructs an area capture, it sets isObjectMaskingEnabled to false in the session's configuration. With the default configuration, my area capture of a small meeting room stopped with processError. With masking turned off, it produced a model of the room in meters. I captured the room on an iPhone 18 Pro Max with iOS 27.2.1 and reconstructed it on a Mac with macOS 27.2.1. A round table measured 762 mm across and 724 mm tall with a tape, and about 760 mm and 750 mm in the model. That's one scan, and accuracy depends on the capture, so distances measured in a model are estimates.

Because Object Capture writes the USDZ file itself, you don't need SceneKit's write(to:delegate:). The SceneKit documentation also says "SceneKit is deprecated, use RealityKit instead."

You can measure by loading the file into a RealityKit view and hit-testing it. A hit test returns the point where it meets the model's surface, not just the object it hit. The hit test needs a collision shape, and the kind of shape matters.

The documentation for generateCollisionShapes(recursive:) doesn't say what shape it builds. In my tests on a Mac with macOS 27.2.1, it built a box around each mesh. A ray aimed between two separate cubes in one mesh hit the box around both. Rays that started inside a model reported a hit at their own starting point, so every measurement from inside a room was zero. generateStaticMesh(from:) "Creates a static collision mesh from a mesh resource," and with it, rays hit the triangles themselves. Inside a test mesh shaped like a 4 by 2.5 by 3 m room, rays returned the room's 4, 2.5, and 3 m dimensions.

This view loads a USDZ file and shows the distance between two tapped points:

import SwiftUI
import RealityKit

// Gives each mesh in a model a collision shape built from its triangles,
// so hit tests land on the model's surface.
@MainActor func addSurfaceCollision(to entity: Entity) async throws {
    if let mesh = entity.components[ModelComponent.self]?.mesh {
        let shape = try await ShapeResource.generateStaticMesh(from: mesh)
        entity.components.set([CollisionComponent(shapes: [shape]), InputTargetComponent()])
    }
    for child in entity.children {
        try await addSurfaceCollision(to: child)
    }
}

// Shows a USDZ model and reports the distance between two tapped points.
struct MeasureView: View {
    let modelURL: URL
    @State private var firstPoint: SIMD3<Float>?
    @State private var message = "Tap two points on the model."

    var body: some View {
        RealityView { content in
            if let model = try? await Entity(contentsOf: modelURL) {
                try? await addSurfaceCollision(to: model)
                content.add(model)
            }
        }
        .realityViewCameraControls(.orbit)
        .gesture(SpatialTapGesture().targetedToAnyEntity().onEnded { value in
            guard let hit = value.hitTest(point: value.location, in: .local).first else { return }
            if let firstPoint {
                message = String(format: "%.1f mm", distance(firstPoint, hit.position) * 1000)
                self.firstPoint = nil
            } else {
                firstPoint = hit.position
                message = "Tap a second point."
            }
        })
        .overlay(alignment: .bottom) { Text(message).padding() }
    }
}

The hitTest(point:in:query:mask:) call in the tap handler and generateStaticMesh(from:) both need iOS 18 or later. Object Capture models made from photos with depth data are in meters, as I described in Different measure units. The code multiplies the distance by 1,000 to report millimeters. On an iPhone 18 Pro Max with iOS 27.2.1, taps on an Object Capture model of a box landed on its front face.

Thanks for asking. Yes, you can capture, save, and measure a textured model on an iPhone or iPad with Apple's APIs. You can also measure a model that's saved as a single object.

Object Capture builds a textured mesh from photos on the device and saves it as a USDZ file. ObjectCaptureSession.isSupported tells you whether a device can run it. Object Capture's area mode, introduced in iOS 18, extends capture from objects to areas. Discover area mode for Object Capture describes it as a way to "capture uneven terrain outdoors, objects that you can't walk all the way around, or 2.5d surfaces." The video also notes that "Areas larger than 6 feet may have reduced mesh and texture quality unless processed at a higher detail level on Mac." Area mode needs no separate call or setting: calling startCapturing() without first calling startDetecting() starts it. The Scanning objects using Object Capture sample shows the whole workflow.

When the sample reconstructs an area capture, it sets isObjectMaskingEnabled to false in the session's configuration. With the default configuration, my area capture of a small meeting room stopped with processError. With masking turned off, it produced a model of the room in meters. I captured the room on an iPhone 18 Pro Max with iOS 27.2.1 and reconstructed it on a Mac with macOS 27.2.1. A round table measured 762 mm across and 724 mm tall with a tape, and about 760 mm and 750 mm in the model. That's one scan, and accuracy depends on the capture, so distances measured in a model are estimates.

Because Object Capture writes the USDZ file itself, you don't need SceneKit's write(to:delegate:). The SceneKit documentation also says "SceneKit is deprecated, use RealityKit instead."

You can measure by loading the file into a RealityKit view and hit-testing it. A hit test returns the point where it meets the model's surface, not just the object it hit. The hit test needs a collision shape, and the kind of shape matters.

The documentation for generateCollisionShapes(recursive:) doesn't say what shape it builds. In my tests on a Mac with macOS 27.2.1, it built a box around each mesh. A ray aimed between two separate cubes in one mesh hit the box around both. Rays that started inside a model reported a hit at their own starting point, so every measurement from inside a room was zero. generateStaticMesh(from:) "Creates a static collision mesh from a mesh resource," and with it, rays hit the triangles themselves. Inside a test mesh shaped like a 4 by 2.5 by 3 m room, rays returned the room's 4, 2.5, and 3 m dimensions.

This view loads a USDZ file and shows the distance between two tapped points:

import SwiftUI
import RealityKit

// Gives each mesh in a model a collision shape built from its triangles,
// so hit tests land on the model's surface.
@MainActor func addSurfaceCollision(to entity: Entity) async throws {
    if let mesh = entity.components[ModelComponent.self]?.mesh {
        let shape = try await ShapeResource.generateStaticMesh(from: mesh)
        entity.components.set([CollisionComponent(shapes: [shape]), InputTargetComponent()])
    }
    for child in entity.children {
        try await addSurfaceCollision(to: child)
    }
}

// Shows a USDZ model and reports the distance between two tapped points.
struct MeasureView: View {
    let modelURL: URL
    @State private var firstPoint: SIMD3<Float>?
    @State private var message = "Tap two points on the model."

    var body: some View {
        RealityView { content in
            if let model = try? await Entity(contentsOf: modelURL) {
                try? await addSurfaceCollision(to: model)
                content.add(model)
            }
        }
        .realityViewCameraControls(.orbit)
        .gesture(SpatialTapGesture().targetedToAnyEntity().onEnded { value in
            guard let hit = value.hitTest(point: value.location, in: .local).first else { return }
            if let firstPoint {
                message = String(format: "%.1f mm", distance(firstPoint, hit.position) * 1000)
                self.firstPoint = nil
            } else {
                firstPoint = hit.position
                message = "Tap a second point."
            }
        })
        .overlay(alignment: .bottom) { Text(message).padding() }
    }
}

The hitTest(point:in:query:mask:) call in the tap handler and generateStaticMesh(from:) both need iOS 18 or later. Object Capture models made from photos with depth data are in meters, as I described in Different measure units. The code multiplies the distance by 1,000 to report millimeters. On an iPhone 18 Pro Max with iOS 27.2.1, taps on an Object Capture model of a box landed on its front face.