【问题标题】:ARKit get ARPlaneAnchor vertices world coordinatesARKit 获取 ARPlaneAnchor 顶点世界坐标
【发布时间】:2021-06-13 02:15:57
【问题描述】:

我正在尝试获取 4 个 ARPlaneAnchor 顶点/边世界坐标中的每一个。使用世界坐标的原因是,我已经有了从命中测试中放置节点的函数/代码,并且我想使用相同的逻辑放置节点。

基本上,ARPlaneAnchor 只有 2D 相对位置(相对于其 SCNNode),我试图将每个平面顶点的位置转换为世界坐标,然后使用这些坐标 (x,y,z)在那里放置一个新的 SCNNode/sphere。

我不想将子节点添加到锚的节点并使用锚的中心来放置它,因为这个节点会随着时间而改变。

【问题讨论】:

    标签: ios augmented-reality arkit scnnode


    【解决方案1】:

    经过一些工作后想通了。人们会期望 ARKit 完成所有这些数学运算...如果有人找到更好的实现,请告诉我!

    // helper to create a translation matrix
    func translateTransform(_ x: Float, _ y: Float, _ z: Float) -> float4x4 {
        var tf = float4x4(diagonal: SIMD4<Float>(repeating: 1))
        tf.columns.3 = SIMD4<Float>(x: x, y: y, z: z, w: 1)
        return tf
    }
    
    @available(iOS 13.0, *)
    extension ARPlaneAnchor {
        
        // returns all 4 world coordinates of the given plane
        // (topLeft, topRight, bottomLeft, bottomRight)
        func worldPoints() -> (SCNVector3, SCNVector3, SCNVector3, SCNVector3) {
            
            // Get world's updated center
            let worldTransform = transform * translateTransform(center.x, 0, center.z)
            
            let width = extent.x
            let height = extent.z
    
            let topLeft = worldTransform * translateTransform(-width / 2.0, 0, -height / 2.0)
            let topRight = worldTransform * translateTransform(width / 2.0, 0, -height / 2.0)
            let bottomLeft = worldTransform * translateTransform(-width / 2.0, 0, height / 2.0)
            let bottomRight = worldTransform * translateTransform(width / 2.0, 0, height / 2.0)
    
           
            let pointTopLeft = SCNVector3(
                x: topLeft.columns.3.x,
                y: topLeft.columns.3.y,
                z: topLeft.columns.3.z
            )
    
            let pointTopRight = SCNVector3(
                x: topRight.columns.3.x,
                y: topRight.columns.3.y,
                z: topRight.columns.3.z
            )
    
            let pointBottomLeft = SCNVector3(
                x: bottomLeft.columns.3.x,
                y: bottomLeft.columns.3.y,
                z: bottomLeft.columns.3.z
            )
    
            let pointBottomRight = SCNVector3(
                x: bottomRight.columns.3.x,
                y: bottomRight.columns.3.y,
                z: bottomRight.columns.3.z
            )
            
            return (
                pointTopLeft,
                pointTopRight,
                pointBottomLeft,
                pointBottomRight
            )
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多