Se-eun Park
Se-eun Park

Reputation: 31

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

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.

Upvotes: 0

Views: 1856

Answers (2)

Bob Wakefield
Bob Wakefield

Reputation: 834

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.

Upvotes: 7

PongBongoSaurus
PongBongoSaurus

Reputation: 7385

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)
    """)

 }

Upvotes: 1

Related Questions