【问题标题】:iOS RealityKit : get world position of placed object from raycast center of screeniOS RealityKit:从屏幕的光线投射中心获取放置对象的世界位置
【发布时间】:2021-05-11 22:30:47
【问题描述】:

使用光线投射和点击手势,我已成功在我的arview 中放置了多个对象(实体和锚点)。

现在,我正在尝试使用屏幕中心获取离我们最近的entity,以便在它附近放置一个对象。所以我们可以想象,每当一个锚点靠近手机屏幕的中心时,就会“生成”一个新的对象

为此,我尝试使用raycast,但我的代码:


    func session(_ session: ARSession, didUpdate frame: ARFrame) {
     // 
        if let hitEntity = arView.entity(
                at: self.arView.center
        ) {
            print("hitEntity IS FOUND !")
            print(hitEntity.name) // this is the object I previously placed

            guard let result = arView.raycast(from: self.arView.center, allowing: .estimatedPlane, alignment: .any).first else { return }

           // here result is the surface behind/below the object, and not the object I want 
            return;

         }
     
    }

result 在表面上触发,但我无法获得 object(实体)而不是背后表面的世界变换。

你有什么想法吗?

谢谢

【问题讨论】:

    标签: swift arkit realitykit


    【解决方案1】:

    要获取实体的worldTransform,可以调用hitEntity.position(relativeTo: nil)


    如果我没有正确理解您的问题,这也可能有所帮助:

    要获取result相对于实体本地空间的位置,可以调用hitEntity.convert(position: result.worldTransform, from: nil)

    Convert 是 RealityKit 中一个超级有用的方法。有几种不同的方法,但这里是我在上面添加的那个的文档:
    https://developer.apple.com/documentation/realitykit/hastransform/3244194-convert/


    我希望其中一个对你有帮助!

    【讨论】:

      猜你喜欢
      • 2021-03-17
      • 2020-06-18
      • 1970-01-01
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 2014-07-24
      • 2019-07-17
      相关资源
      最近更新 更多