Search code examples
iosswiftscenekitarkit

How to calculate normal vector of a SCNPlane?


Goal: Define the normal vector for a plane represented by SCNPlane.

As mentioned in Apple's documentation Working with Vectors - Calculate the Normal of a Triangle, I can calculate the normal vector based on 3 points from the plane. The issue here is that I don't know how I can get 3 points that they make proper triangle. I've noticed that SCNPlane has a property boundingBox and it can represent 2 of the triangle vertices (min and max). How can I find the 3rd vertex that is placed on the SCNPlane? I cannot use the center of the SCNPlane, because it creates a line along with min and max points from boundingBox.

Is there any other way that can help me to retrieve a normal vector for the SCNPlane?


Solution

  • From the documentation we learn that

    The surface is one-sided. Its surface normal vectors point in the positive z-axis direction of its local coordinate space, so it is only visible from that direction by default.

    The normal of a SCNPlane is always (0, 0, 1) in local space and that cannot change.

    When it is attached to a node, the orientation of that node determines the normal in an another coordinate system. You can use simdConvertVector:toNode: to convert between coordinate spaces:

    // normal expressed in world space
    let normal = simd_normalize(node.simdConvertVector(simd_float3(0, 0, 1), to: nil))