How can I get 4 vertices's coordinate of the virtual plane?

社会主义新天地 提交于 2019-12-23 06:31:32

问题


I made the virtual plane (SCNPlane) based on real table through Scenekit. Additionally, I want to know 4 vertices's coordinate of the virtual plane.

Click here to see image.


回答1:


You can get the coordinates of the bounding box, just as BlackMirrorz said.

let (min, max) = planeNode.boundingBox

This will give you opposite corners (min and max) as SCNVector3 objects. Those coordinates will be relative to the object, unrotated, untranslated, and unscaled. Derive the other two corner coordinates in the 2d plane. Don't worry about the Z axis. An SCNPlane is fundamentally a 2D object.

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)

Then call

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)

to convert each coordinate to world space. The system will do the rotation, translation, and scaling for you.




回答2:


An SCNNode has a boundingBox property which refers to the:

The minimum and maximum corner points of the object’s bounding box.

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

Whereby:

Scene Kit defines a bounding box in the local coordinate space using two points identifying its corners, which implicitly determine six axis-aligned planes marking its limits. For example, if a geometry’s bounding box has the minimum corner {-1, 0, 2} and the maximum corner {3, 4, 5}, all points in the geometry’s vertex data have an x-coordinate value between -1.0 and 3.0, inclusive. The coordinates provided when reading this property are valid only if the object has a volume to be measured. For a geometry containing no vertex data or a node containing no geometry, the values min and max are both zero.

As such it is quite easy to get the coordinates of an 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)
    """)

 }


来源:https://stackoverflow.com/questions/49790968/how-can-i-get-4-verticess-coordinate-of-the-virtual-plane

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!