I am trying to develop AR app with SwiftUI using FocusEntity to show placing object indicator. My objects are not in USDZ format they are in .scn and there is no way to convert them (at least I did not find). Now I am trying to load my objecst using SCNScene and I cannot find a way to load ARView's scene with SCNScene
Here is my code that handles tap gesture to place the object:
class ARCoordinator: NSObject, ARSessionDelegate {
weak var view: ARView?
var focusEntity: FocusEntity?
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
guard let view = self.view else { return }
debugPrint("Anchors added to the scene: ", anchors)
self.focusEntity = FocusEntity(on: view, style: .classic(color: .yellow))
}
@objc func handleTap() {
guard let view = self.view, let focusEntity = self.focusEntity else { return }
let scene = SCNScene(named: "art.scnassets/object.scn")!
let node = scene.rootNode.childNodes.first!
node.position = SCNVector3(focusEntity.position.x, focusEntity.position.y, focusEntity.position.z)
view.scene = scene //here is the problem
}
Preamble for others who stumble over this question: Depending on your use case, consider making your View an
ARSCNView, and adopt theARSCNViewDelegateprotocol, which has a scene of the correct type, see https://developer.apple.com/documentation/arkit/arscnviewConversion of Scenes
Regarding conversion of
.scntoUSDZ, a roundtrip through collada helps. Note, that the .scn format can contain several items that are specific to SceneKit, such as physics bodies and fields, constraints, and particle systems. These not transferrable that way.That said, on the Mac
SCNScene.write(to:options:delegate:progressHandler:)allows to export your scene to Collada/.dae format. Unfortunately, the Collada option is not available on iOS. For details, see https://developer.apple.com/documentation/scenekit/scnscene/1523577-writeConverting Collada to .usdz you have numerous viable options, depending on the content and the intended information workflow.