Scaling model from its center
Rotation and scaling of models is performed relative to their pivot point. If the pivot is located on the lower border of the Bounding Box (for a robot
model, this is the correct location of the pivot), then if scaling it up, the model will "grow up" from the "floor". If you want to scale the model from its center, then create a new parent Entity (since RealityKit still doesn't have simdPivot property, like in SceneKit), translate it to model's center (however, do not forget to compensate robot's position), and use its pivot point as the scale's origin.
Here's a code:
import SwiftUI
import RealityKit
struct ContentView : View {
var body: some View {
ARViewContainer().ignoresSafeArea()
}
}
struct ARViewContainer: UIViewRepresentable {
let arView = ARView(frame: .zero)
func makeUIView(context: Context) -> ARView {
let robot = try! ModelEntity.load(named: "toy_robot.usdz")
let scalingPivot = Entity()
scalingPivot.position.y = robot.visualBounds(relativeTo: nil).center.y
scalingPivot.addChild(robot)
// compensating a robot position
robot.position.y -= scalingPivot.position.y
let anchor = AnchorEntity()
anchor.addChild(scalingPivot)
arView.scene.addAnchor(anchor)
let newTransform = Transform(scale: .one * 7)
scalingPivot.move(to: newTransform,
relativeTo: scalingPivot.parent,
duration: 5.0)
return arView
}
func updateUIView(_ view: ARView, context: Context) { }
}
There's also a visionOS solution.
