0

I'm trying to create a custom SCNPlane class that could have the form of any polygon, as the regular SCNPlane can only be a rectangle.

I have attempted to accomplish this by extending SCNGeometry. Below is the code I use:

extension SCNGeometry {
    static func polygonPlane(vertices: [SCNVector3]) -> SCNGeometry {
        let src = SCNGeometrySource(vertices: vertices)

        var indices: [UInt32] = []        
        var index: UInt32 = 0
        for _ in vertices {
            indices.append(index)
            index += 1
        }

        let inds = SCNGeometryElement(indices: indices, primitiveType: .polygon)

        return SCNGeometry(sources: [src], elements: [inds])
    }
}

As you can see the method takes an array of vertices as input, and my wish is to connect these in order to make a plane with a specified geometry using the vertices provided.

When using SCNGeometryPrimitiveType.polygon as in the code above I get a weird error: "Thread 1: EXC_BREAKPOINT (code=1, subcode=0x106362e20)". It seems as the functionality for this case is not implemented in swift, but that it is for Objective-C? Is there any way for me to make use of this?

I also tried using the other options for the primitiveType-value, and there is no crash using those values, further strengthening my belief that there is something wrong with the .polygon case.

A. Claesson
  • 529
  • 4
  • 16
  • Solved the problem by following [this post](https://stackoverflow.com/questions/44922164/scngeometry-with-polygon-as-primitivetype) – A. Claesson Mar 24 '19 at 16:40

1 Answers1

1

When use polygon , you have to use

  init(data: Data?, primitiveType: SCNGeometryPrimitiveType, primitiveCount: Int, bytesPerIndex: Int)

to init the SCNGeometryElement. See apple doc for details.

E.Coms
  • 11,065
  • 2
  • 23
  • 35