【问题标题】:How can I get 4 vertices's coordinate of the virtual plane?如何获得虚拟平面的 4 个顶点坐标?
【发布时间】:2018-04-12 07:56:06
【问题描述】:

我通过Scenekit制作了基于真实表的虚拟平面(SCNPlane)。另外,我想知道虚拟平面的4个顶点坐标。

Click here to see image.

【问题讨论】:

  • 你的虚拟平面是 SCNPlane 吗?
  • 是的。 SceneKit 的 SCNPlane

标签: swift arkit


【解决方案1】:

可以得到边界框的坐标,就像BlackMirrorz说的那样。

let (min, max) = planeNode.boundingBox

这将为您提供作为 SCNVector3 对象的对角(最小和最大)。这些坐标将相对于对象、未旋转、未平移和未缩放。导出二维平面中的其他两个角坐标。不用担心 Z 轴。 SCNPlane 本质上是一个 2D 对象。

let bottomLeft = SCNVector3(min.x, min.y, 0)
let topRight = SCNVector3(max.x, max.y, 0)

let topLeft = SCNVector3(min.x, max.y, 0)
let bottomRight = SCNVector3(max.x, min.y, 0)

然后调用

let worldBottomLeft = planeNode.convertPosition(bottomLeft, to: rootNode)
let worldTopRight = planeNode.convertPosition(topRight, to: rootNode)

let worldTopLeft = planeNode.convertPosition(topLeft, to: rootNode)
let worldBottomRight = planeNode.convertPosition(bottomRight, to: rootNode)

将每个坐标转换为世界空间。系统会为你做旋转、平移和缩放。

【讨论】:

    【解决方案2】:

    一个 SCNNode 有一个 boundingBox 属性,它指的是:

    物体边界框的最小和最大角点。

    var boundingBox: (min: SCNVector3, max: SCNVector3) { get set }
    

    在哪里:

    Scene Kit 在局部坐标空间中使用 两个点标识它的角,隐含地确定六个 标记其界限的轴对齐平面。例如,如果几何的 边界框具有最小角 {-1, 0, 2} 和​​最大角 {3, 4, 5},几何顶点数据中的所有点都有一个 x 坐标值介于 -1.0 和 3.0(含)之间。该坐标 读取此属性时提供的仅当对象具有 要测量的体积。对于不包含顶点数据的几何图形或 不包含几何的节点,最小值和最大值均为零。

    因此很容易获得SCNNode 的坐标:

    /// Returns The Size Of An SCNode
    ///
    /// - Parameter node: SCNNode
    func getSizeOfModel(_ node: SCNNode){
    
       //1. Get The Bouding Box Of The Node
       let (min, max) = node.boundingBox
    
       //2. Get It's Z Coordinate
       let zPosition = node.position.z
    
       //3. Get The Width & Height Of The Node
       let widthOfNode = max.x - min.x
       let heightOfNode = max.y - min.y
    
       //4. Get The Corners Of The Node
       let topLeftCorner = SCNVector3(min.x, max.y, zPosition)
       let bottomLeftCorner = SCNVector3(min.x, min.y, zPosition)
       let topRightCorner = SCNVector3(max.x, max.y, zPosition)
       let bottomRightCorner = SCNVector3(max.x, min.y, zPosition)
    
       print("""
            Width Of Node = \(widthOfNode)
            Height Of Node = \(heightOfNode)
            Bottom Left Coordinates = \(bottomLeftCorner)
            Top Left Coordinates = \(topLeftCorner)
            Bottom Right Coordinates = \(bottomRightCorner)
            Top Right Coordinates = \(topRightCorner)
        """)
    
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 2012-01-18
      • 2012-10-27
      相关资源
      最近更新 更多