You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RealityKit技术问题:如何从模型中心缩放3D模型

问题

希望在原位从中心缩放一个3D玩具机器人模型,要求放大或缩小时模型位置保持不变。目前已实现缩放,但缩放是从模型脚部而非自身中心开始的。尝试过直接设置model.scale = ...,也用了model.move方法,代码如下:

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)
解决方案

问题核心是模型的轴心点不在自身几何中心,导致缩放时围绕脚部(默认轴心)变化。解决分两步:

  • 先调整模型的轴心到几何中心
    加载模型后,先计算模型的包围盒,再通过父容器节点抵消轴心偏差:

    // 假设modelEntity是已加载好的模型实体
    let boundingBox = modelEntity.model?.boundingBox ?? .zero
    let centerOffset = SIMD3<Float>(
        x: -boundingBox.center.x,
        y: -boundingBox.center.y,
        z: -boundingBox.center.z
    )
    // 创建容器节点作为缩放操作的载体
    let containerEntity = Entity()
    modelEntity.position = centerOffset
    containerEntity.addChild(modelEntity)
    // 将容器节点添加到锚点,后续操作容器即可
    arAnchor.addChild(containerEntity)
    
  • 正确应用中心缩放动画
    现在对容器实体执行缩放动画,就能实现模型围绕自身中心缩放且位置不变:

    let targetScale = SIMD3<Float>(x: myScale.x, y: myScale.y, z: myScale.z)
    let targetTransform = Transform(scale: targetScale)
    containerEntity.move(to: targetTransform, relativeTo: containerEntity, duration: 1.0)
    

如果是直接修改缩放属性,直接操作containerEntity.scale = targetScale即可,效果一致。

内容的提问来源于stack exchange,提问作者Christian Clampitt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 11:44:57