Skip to content

Instantly share code, notes, and snippets.

@radicalappdev
Last active December 12, 2024 12:45
Show Gist options
  • Save radicalappdev/88aa86d1aa23b4c302736620f782423c to your computer and use it in GitHub Desktop.
Save radicalappdev/88aa86d1aa23b4c302736620f782423c to your computer and use it in GitHub Desktop.
Draft of Example 018 - Using handAnchors(at:) for predicted hand positions
struct Example018: View {
let arSession = ARKitSession()
let handTrackingProvider = HandTrackingProvider()
let leftCollection = Entity()
let rightCollection = Entity()
let tipJoints: [HandSkeleton.JointName] = [
.thumbTip, .indexFingerTip, .middleFingerTip, .ringFingerTip, .littleFingerTip
]
var body: some View {
RealityView { content in
content.add(leftCollection)
content.add(rightCollection)
if let scene = try? await Entity(named: "HandTrackingLabs", in: realityKitContentBundle) {
content.add(scene)
if let leftHandSphere = scene.findEntity(named: "StepSphereBlue") {
// Create clones of the left hand sphere for each joint
for jointName in tipJoints {
let sphere = leftHandSphere.clone(recursive: true)
sphere.name = jointName.description
leftCollection.addChild(sphere)
}
leftHandSphere.isEnabled = false
}
if let rightHandSphere = scene.findEntity(named: "StepSphereGreen") {
// Create clones of the right hand sphere for each joint
for jointName in tipJoints {
let sphere = rightHandSphere.clone(recursive: true)
sphere.name = jointName.description
rightCollection.addChild(sphere)
}
rightHandSphere.isEnabled = false
}
}
}
.persistentSystemOverlays(.hidden)
.task { try! await arSession.run([handTrackingProvider]) }
// Left Hand: Receive updates from the provider and process them over time
.task {
for await update in handTrackingProvider.anchorUpdates where update.anchor.chirality == .left {
let handAnchor = update.anchor
for jointName in tipJoints {
if let joint = handAnchor.handSkeleton?.joint(jointName),
let sphere = leftCollection.findEntity(named: jointName.description) {
let transform = handAnchor.originFromAnchorTransform
let jointTransform = joint.anchorFromJointTransform
sphere.setTransformMatrix(transform * jointTransform, relativeTo: nil)
}
}
}
}
// Right Hand: Using handAnchors(at:) for predicted hand positions
.task {
while true {
let predictionTime = Date.now.timeIntervalSinceNow
// let predictionTime = Date.now.timeIntervalSinceNow + (1.0/15.0)
// let predictionTime = Date.now.timeIntervalSinceNow + (1.0/30.0)
if let handAnchor = handTrackingProvider.handAnchors(at: predictionTime).rightHand {
for jointName in tipJoints {
if let joint = handAnchor.handSkeleton?.joint(jointName),
let sphere = rightCollection.findEntity(named: jointName.description) {
let transform = handAnchor.originFromAnchorTransform
let jointTransform = joint.anchorFromJointTransform
sphere.setTransformMatrix(transform * jointTransform, relativeTo: nil)
}
}
}
try? await Task.sleep(for: .seconds(1/30))
// try? await Task.sleep(for: .seconds(1/60))
// try? await Task.sleep(for: .seconds(1/120))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment