Robbe Verhoest
Robbe Verhoest

Reputation: 481

Change pivot point of object in RealityKit

I want a cube to be scaled only over the positive z-axis. Now when I scale it, it's always scaled around its center. So for this I will have to change the anchor point of the object.

I know in SceneKit there was a pivot property you could use.

Is there a way to do this in RealityKit too?

Upvotes: 3

Views: 1961

Answers (2)

Andy Jazz
Andy Jazz

Reputation: 58093

iOS RealityKit

Pity, but in RealityKit framework you have no possibility to explicitly define model's pivot point's position. Hence, the best solution is to set pivot in 3D authoring app (like Maya or Blender).

However, when prototyping a scene in iOS Reality Composer, Orbit behavior allows you automatically position model's pivot in the center of another model.

Also, there's a similar implicit-pivot-positioning approach in RealityKit 2.0 when working with move(...) methods. Use relativeTo parameter to define pivot's position at the world origin (nil) or at a center of another model (entity), to commit a transform animation.

Or you can place a model inside an empty group (Entity) or AnchorEntity, and you'll get 2 independent pivots.


visionOS RealityKit

In visionOS, you can use SwiftUI's .scaleEffect(x:y:z:anchor:) modifier with anchor parameter to scale the whole view about chosen axis (pivot point position). The tap gesture works only when Collision and InputTarget components are assigned.

import SwiftUI
import RealityKit
import RealityKitContent

struct ContentView: View {
    @State private var scaleCoef: CGFloat = 1.0
    
    var body: some View {
        Model3D(named: "Scene", bundle: realityKitContentBundle)
            .onTapGesture {
                withAnimation(.linear(duration: 1.0)) {
                    scaleCoef += 1.0
                }
            }
            .scaleEffect(x: scaleCoef, 
                         y: scaleCoef, 
                         z: scaleCoef, anchor: .topFront)
    }
}
#Preview {
    ContentView()
}

Upvotes: 3

Paul Manson
Paul Manson

Reputation: 91

I found a fairly easy way to replicate the pivot functionality from SceneKit, in RealityKit.

All you need to do is wrap your ModelEntity within another outer "parent" Entity and offset the inner ModelEntity by the desired pivot value (SIMD3<Float>).

Once you've done that, you can position or rotate the outer "parent" entity and the inner entity will behave in essentially the same way as a SceneKit SCNNode with a pivot value applied.

Upvotes: 6

Related Questions