RealityKit – 从中心缩放模型

问题描述 投票:0回答:1

我想在原地缩放一个 3D 模型(一个高大的玩具机器人),即从它的中心开始:它应该在所有维度上增长和收缩,而不改变位置。

我可以缩放玩具机器人模型,但是模型从它的脚开始向上或向下缩放,而不是它的适当中心。

我试过通过

model.scale = ...
.

进行缩放

我也尝试过使用

model.move
方法,如this answer中所推荐的那样。

我的代码:

let newTransform = Transform(scale: .init(x: myScale.x, 
                                          y: myScale.y, 
                                          z: myScale.z))

modelEntity.move(to: newTransform, relativeTo: modelEntity, duration: 1.0)

arAnchor.addChild(modelEntity)
swift augmented-reality realitykit
1个回答
0
投票

从中心缩放模型

模型的旋转和缩放是相对于它们的枢轴点执行的。如果枢轴位于边界框的下边界(对于

robot
模型,这是枢轴的正确位置),那么如果将其放大,模型将从“地板”“长大”。如果你想从它的中心缩放模型,然后创建一个新的父实体,将它平移到模型的中心(但是,不要忘记补偿机器人的位置),并使用它的轴心点作为缩放的原点。

这是一个代码:

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) { }
}

© www.soinside.com 2019 - 2024. All rights reserved.