I am trying t instantiate my arview controller from appdelegate. Code is as below.
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?)
-> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "StartViewMain") as! ViewController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
This doesn't give me the ARview. However if I comment the code in appdelegate for instantiation and make the Main.storyboard as an Initial View Controller it works fine. What could be the problem with this code. The end objective I have to be able to show the ARview controller on need. Below is the code for the viewController,swift file Thanks
/* See LICENSE folder for this sample’s licensing information.
Abstract: The sample app's main view controller. */
import UIKit import RealityKit import ARKit import Combine
class ViewController: UIViewController, ARSessionDelegate {
@IBOutlet var arView: ARView!
// The 3D character to display.
var character: BodyTrackedEntity?
let characterOffset: SIMD3<Float> = [-1.0, 0, 0] // Offset the character by one meter to the left
let characterAnchor = AnchorEntity()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
arView.session.delegate = self
arView.debugOptions.insert(.showStatistics)
// If the iOS device doesn't support body tracking, raise a developer error for
// this unhandled case.
guard ARBodyTrackingConfiguration.isSupported else {
fatalError("This feature is only supported on devices with an A12 chip")
}
// Run a body tracking configration.
let configuration = ARBodyTrackingConfiguration()
arView.session.run(configuration)
arView.scene.addAnchor(characterAnchor)
// Asynchronously load the 3D character.
var cancellable: AnyCancellable? = nil
cancellable = Entity.loadBodyTrackedAsync(named: "character/robot").sink(
receiveCompletion: { completion in
if case let .failure(error) = completion {
print("Error: Unable to load model: \(error.localizedDescription)")
}
cancellable?.cancel()
}, receiveValue: { (character: Entity) in
if let character = character as? BodyTrackedEntity {
// Scale the character to human size
character.scale = [1.0, 1.0, 1.0]
self.character = character
cancellable?.cancel()
} else {
print("Error: Unable to load model as BodyTrackedEntity")
}
})
}
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
for anchor in anchors {
guard let bodyAnchor = anchor as? ARBodyAnchor else { continue }
// Update the position of the character anchor's position.
let bodyPosition = simd_make_float3(bodyAnchor.transform.columns.3)
characterAnchor.position = bodyPosition + characterOffset
// Also copy over the rotation of the body anchor, because the skeleton's pose
// in the world is relative to the body anchor's rotation.
characterAnchor.orientation = Transform(matrix: bodyAnchor.transform).rotation
if let character = character, character.parent == nil {
// Attach the character to its anchor as soon as
// 1. the body anchor was detected and
// 2. the character was loaded.
characterAnchor.addChild(character)
}
//////////////////////////////////////////////////////////
///Anchor transform in 3D
///
// Access the position of the root node.
let hipWorldPosition = bodyAnchor.transform
// Access the skeleton geometry.
let skeleton = bodyAnchor.skeleton
// Access the list of transforms of all joints relative to the root.
let jointTransforms = skeleton.jointModelTransforms
// Iterate over all the joints.
for (i, jointTransform) in jointTransforms.enumerated() {
// Extract parent index from definition.
let parentIndex = skeleton.definition.parentIndices[i]
// Check if it's not root (the root doesn't have a parent).
guard parentIndex != -1 else { continue }
// Find position of parent joint
let parentJointTransform = jointTransforms[parentIndex]
// Use this however you want...
}
}
}