我正在尝试编写代码,以获取ARKit 3动作捕捉中所有关节的XYZ坐标,以便通过这些坐标计算身体的所有角度。但是我找不到XYZ坐标,因此无法进行下一步。
我正试图使用character!.jointTransforms
来获取关节数据的位置(可能就是XYZ坐标),但是我尝试跟踪了几个动作,数字没有变化。
这是我的代码:
import UIKit
import RealityKit
import ARKit
import Combine
class ViewController: UIViewController, ARSessionDelegate {
@IBOutlet var arView: ARView!
var character: BodyTrackedEntity?
let characterOffset: SIMD3<Float> = [0.0, 0, 0]
let characterAnchor = AnchorEntity()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
arView.session.delegate = self
guard ARBodyTrackingConfiguration.isSupported else {
fatalError("This feature is only supported on devices with an A12 chip")
}
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 }
let bodyPosition = simd_make_float3(bodyAnchor.transform.columns.3)
characterAnchor.position = bodyPosition + characterOffset
characterAnchor.orientation = Transform(matrix: bodyAnchor.transform).rotation
if let character = character, character.parent == nil {
characterAnchor.addChild(character)
}
let XYZArray = character!.jointTransforms.map({ (transform: RealityKit.Transform) -> SIMD3<Float> in
transform.translation
})
print(XYZArray)
}
}
}
这段代码是参考以下文档:https://developer.apple.com/documentation/arkit/capturing_body_motion_in_3d