RealityKit – move(to:) methods work only without duration parameter - swift

Using move(to:) method to update Entity's position works only if I don't use the the initialiser with duration parameter.
sphere.move(to: newTransform, relativeTo: nil, duration: 0.75) // Absolutely no effect
sphere.move(to: newTransform, relativeTo: nil) // Instant effect
Both called from the Main thread. I don't understand what may cause this strange behaviour.

Implement move(...) method after arView.scene.anchors.append(scene), not before. When you run the following code you'll see that model moves along the +X axis during 2 sec, as expected.
let scene = try! Experience.loadBox()
guard let model = scene.steelBox?.children[0] as? ModelEntity
else { return }
var transform = Transform()
transform.translation.x = 0.5
arView.scene.anchors.append(scene)
DispatchQueue.main.async {
model.move(to: transform, relativeTo: nil, duration: 2.0)
}
However, if you do not use the duration parameter (i.e. duration = 0.0 sec), the model will instantly move 0.5 m along the +X axis.
DispatchQueue.main.async {
model.move(to: transform, relativeTo: nil)
}

I've just realised that marking the init method with #MainActor did not cause move(to:relativeTo:duration:) to be called on the main thread, executing it within DispatchQueue.main.async{} closure did the trick.

Related

Animation doesn't repeat correctly in Swift

#objc private func updateCountdownLabel(_ notification: NSNotification) {
self.progressWidthAnchor.constant = 0
if let timeRemaining = notification.userInfo?["timeRemaining"] as? Int {
self.secondsRemaining = timeRemaining
self.animateProgress(width: 100, duration: 10)
}
}
private func animateProgress(width: CGFloat, duration: Int) {
self.layoutIfNeeded()
UIView.animate(withDuration: TimeInterval(duration),
delay: TimeInterval(LocalConstants.animationDelayDuration),
options: .curveLinear) {
self.progressWidthAnchor.constant = width
self.layoutIfNeeded()
}
}
I'm having a weird issue with my animation. At the moment I am trying to animate a bar decreasing/increasing but I need it to repeat every second.
For some reason it doesn't repeat the animation, the constraint jumps outside the view.
It works the first time, but the second time it doesn't work as you'd expect.
No constraint warnings are thrown.
Moving code around,
updating the layoutIfNeeded to various locations
The cause of my animation not working correctly was due to
removeAllAnimations() not being called prior to the next UIView.animate code blo
Figured it out
Calling removeAllAnimations before executing the code again fixed my issue

RealityKit – Rotating an Entity affects its Scale

I am loading an entity using the USDZ file. I want after loading the entity, I want to rotate is forever. I am using the following code.
cancellable = ModelEntity.loadAsync(named: "toy_drummer").sink { [weak self] completion in
if case let .failure(error) = completion {
print("Unable to load model \(error)")
}
self?.cancellable?.cancel()
} receiveValue: { entity in
anchor.addChild(entity)
arView.scene.addAnchor(anchor)
let rotation = Transform(pitch: 0, yaw: .pi, roll: 0)
entity.move(to: rotation,
relativeTo: nil,
duration: 15.0,
timingFunction: .linear)
}
Instead of rotating correctly, the entity is scaling and getting bigger and bigger. Any ideas?
You need a starting transform "point" and ending transform "point". If a value of referenceEntity (relativeTo) argument equal to nil it means relative to world space. Since the same 4x4 matrix slots are used for rotation values ​​as for scaling, when the model is rotated, its scale also changes at the same time, if there is a difference in scale.
For perpetual transform animation use some of RealityKit 2.0 tricks.
And, of course, there is a Trigonometry that was really conceived for perpetual orbiting.
Here's a correct version of your code:
import UIKit
import RealityKit
import Combine
class ViewController: UIViewController {
#IBOutlet var arView: ARView!
var cancellable: Cancellable? = nil
let anchor = AnchorEntity()
override func viewDidLoad() {
super.viewDidLoad()
cancellable = ModelEntity.loadAsync(named: "drummer.usdz").sink { _ in
self.cancellable?.cancel()
} receiveValue: { entity in
self.anchor.addChild(entity)
self.arView.scene.addAnchor(self.anchor)
let rotation = Transform(pitch: 0, yaw: .pi, roll: 0)
entity.move(to: rotation,
relativeTo: entity,
duration: 5.0,
timingFunction: .linear)
}
}
}
I made a swift package a couple of years ago, RealityUI, which does include animations like a continuous rotation:
https://github.com/maxxfrazer/RealityUI/wiki/Animations#spin
You'd just need to include the package, and call:
entity.ruiSpin(by: [0, 1, 0], period: 1)
docs here:
https://maxxfrazer.github.io/RealityUI/Extensions/Entity.html#/s:10RealityKit6EntityC0A2UIE7ruiSpin2by6period5times10completionys5SIMD3VySfG_SdSiyycSgtF

How to perform action on RealityKit animation completion

In WWDC19 talk they showed how to animate Entity and perform action on animation completion (go to minute 12 for reference) like this:
let animationController = move(to: flipUpTransform, relativeTo: parent, duration: 0.25, timingFunction: .easeInOut)
animationController.completionHandler {
// Perform some action
}
But it looks like that they removed or never added this completionHandler. Instead they have isCompleted boolean property.
I am new to swift so I have no idea to use this isCompleted to perform some actions upon animation completion. How can I solve this?
The documentation says:
Look for one of the events in AnimationEvents if you want to be alerted when certain aspects of animation occur.
So you can use Combine to get notified when the animation finishes. so
let animation = entity.move(to: targetTransform, relativeTo: nil, duration: 1)
arView.scene.publisher(for: AnimationEvents.PlaybackCompleted.self)
.filter { $0.playbackController == animation }
.sink(receiveValue: { event in
/ * your completion handler */
}).store(in: &subscriptions)

Swift Dynamic animation followed by property animator

I have an animation where I use a push animation, then a snap animation using UIDynamicBehavior, and then I finish with a property behavior:
for card in selectedCards {
removeCard(card: card)
}
private func removeCard(card: Card) {
guard let subView = cardsContainer.subviews.first(where: { ($0 as? PlayingCardView)?.card == card }) else {
return
}
if let card = subView as? PlayingCardView { card.selected = false }
let matchedCardsFrame = matchedCards.convert(matchedCards.frame, to: view)
view.addSubview(subView)
cardBehavior.addItem(subView) // here I add the push behavior
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.cardBehavior.removeItem(subView) // here I remove the push behavior
UIViewPropertyAnimator.runningPropertyAnimator(
withDuration: 0.3,
delay: 0,
options: [],
animations: {
self.cardBehavior.addSnapBehavior(subView, frame: matchedCardsFrame) // here I add the snap behavior
}, completion: { finished in
self.animator.removeAllBehaviors()
subView.frame.size = CGSize(width: matchedCardsFrame.height, height: matchedCardsFrame.width)
subView.transform = CGAffineTransform.identity.rotated(by: CGFloat.pi / 2)
subView.setNeedsDisplay()
})
}
}
Essentially the above code does the following:
Add push behavior
Remove push behavior
Add snap behavior
Remove all behaviors
Add property transform
What I want is for the push action to execute, then after a second or so, have the snap behavior execute, and after the snap execution is finished, to perform a transform. However, if I removeAllBehaviors() before I execute the property transform then the snap behavior doesn't finish. But if I leave the snap behavior and try to execute the property transform then it has no effect since it appears that the snap behavior acts on the object indefinitely, putting it at odds with the property transform.
How can I programmatically say finish the snap behavior and then perform the transform?

Dispatch queue to animate SCNNode in ARKit

I'm facing an issue when trying to periodically animate my nodes on an ARSession. I'm fetching data from Internet every 5 seconds and then with that data I update this nodes (shrink or enlarge).
My code looks something like this:
Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { timer in
fetchDataFromServer() {
let fetchedData = $0
DispatchQueue.main.async {
node1.update(fetchedData)
node2.update(fetchedData)
node3.update(fetchedData)
}
if stopCondition { timer.invalidate() }
}
}
Problem is that when calling the updates I'm seeing a glitch in which the camera seems to freeze for a fraction of second and I see the following message in the console: [Technique] World tracking performance is being affected by resource constraints [1]
Update happens correctly, but the UX is really clumpsy if every 5 seconds I get these "short freezes"
I've tried creating a concurrent queue too:
let animationQueue = DispatchQueue(label: "animationQueue", attributes: DispatchQueue.Attributes.concurrent)
and call animationQueue.async instead of main queue but problem persists.
I'd appreciate any suggestions.
EDIT: Each of the subnodes on it's update method looks like this
private func growingGeometryAnimation(newHeight height: Float) -> CAAnimation{
// Change height
let grow = CABasicAnimation(keyPath: "geometry.height")
grow.toValue = height
grow.fromValue = prevValue
// .... and the position
let move = CABasicAnimation(keyPath: "position.y")
let newPosition = getNewPosition(height: height)
move.toValue = newPosition.y + (yOffset ?? 0)
let growGroup = CAAnimationGroup()
growGroup.animations = [grow, move]
growGroup.duration = 0.5
growGroup.beginTime = CACurrentMediaTime()
growGroup.timingFunction = CAMediaTimingFunction(
name: kCAMediaTimingFunctionEaseInEaseOut)
growGroup.fillMode = kCAFillModeForwards
growGroup.isRemovedOnCompletion = false
growGroup.delegate = self
return growGroup
}
self.addAnimation(growingGeometryAnimation(newHeight: self.value), forKey: "bar_grow_animation")
To make any updates to the scene use SCNTransaction, it makes sure all of the changes are made on the appropriate thread.
Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { timer in
fetchDataFromServer() {
let fetchedData = $0
SCNTransaction.begin()
node1.update(fetchedData)
node2.update(fetchedData)
node3.update(fetchedData)
SCNTransaction.commit()
if stopCondition { timer.invalidate() }
}
}