Creating a custom polygon plane SCNGeometry error

Hello!


I have been using this piece of code in order to create a custom plane geometry using the SCNGeometryPrimitiveType option ".polygon".


extension SCNGeometry {
   
    static func polygonPlane(vertices: [SCNVector3]) -> SCNGeometry {
        var indices: [Int32] = [Int32(vertices.count)]
       
        var index: Int32 = 0
        for _ in vertices {
            indices.append(index)
            index += 1
        }
        let vertexSource = SCNGeometrySource(vertices: vertices)
        let indexData = Data(bytes: indices, count: indices.count * MemoryLayout.size)
        let element = SCNGeometryElement(data: indexData, primitiveType: .polygon, primitiveCount: 1, bytesPerIndex: MemoryLayout.size)
        let geometry = SCNGeometry(sources: [vertexSource], elements: [element])
        let material = SCNMaterial()
        material.diffuse.contents = UIColor.blue
        material.isDoubleSided = true
        geometry.firstMaterial = material
       
        return geometry
    }
}

This works by sending in vertex coordinates in an order which represent the outline of the desired plane. As an example I might make an array with vertices representing a rectangle geometry as follows: [lowerLeft, upperLeft, upperRight, lowerRight].

This method seems to work well for simpler shapes, but I sometimes get an error which I haven't been able to find the cause of when using more complex shapes or vertex coordinates which are randomly scattered in a plane (eg. when the method recieves an array where the vertices order is not outlining a shape. In the rectangle case it could look like this: [lowerLeft, upperRight, lowerRight, upperLeft] ). The error seems more likely to occur when the number of vertices used increases.


I'm using this method to allow the user of my app to "paint" an outline of the desired plane, and as I can't control how the user chooses to do so I want this method to be able to handle those cases as well.


This is the error print i recieve after calling this method:


-[MTLDebugDevice validateNewBufferArgs:options:]:467: failed assertion `Cannot create buffer of zero length.'

(lldb)


And this is what appears in the debug navigator:


libsystem_kernel.dylib`__pthread_kill:
    0x219cad0c4 <+0>:  mov    x16, #0x148
    0x219cad0c8 <+4>:  svc    #0x80
->  0x219cad0cc <+8>:  b.lo   0x219cad0e4               ; <+32>
    0x219cad0d0 <+12>: stp    x29, x30, [sp, #-0x10]!
    0x219cad0d4 <+16>: mov    x29, sp
    0x219cad0d8 <+20>: bl     0x219ca25d4               ; cerror_nocancel
    0x219cad0dc <+24>: mov    sp, x29
    0x219cad0e0 <+28>: ldp    x29, x30, [sp], #0x10
    0x219cad0e4 <+32>: ret   

Where line 4 has the error: com.apple.scenekit.scnview-renderer (16): signal SIGABRT

Any help or explanation for this error would be greatly appriciated!

Replies

I have no experience here.


But what you describe seems to be a problem when the polygon sides cross themselves, as with [A: lowerLeft, B: upperRight, C: lowerRight, D: upperLeft].

D______ B

\ /

\ /

/\ E

/ \

/______\

A C

It should wok for non convex, but maybe not these shapes.


I would suspect that replacing this form with 2 triangles (A C E and B D E) could make it work ?


May also look here :

https://stackoverflow.com/questions/54616093/swift-4-how-to-create-custom-scngeometry-as-polygon

[Quote] " I believe problem occurs when first position and last position has the same x,y,z values. Simply remove the the last position from the array of positions and the polygon should render as expected!"

Metal hates Zero lenght polygone Edges. if some succesive points in the contour of the poly are superimposed, that is have same XYZ vertices coords, you are dead ! RGDS

I have the same issue. But If I lauch the app with Debugger Mode (XCode) it always return the same issue above "[MTLDebugDevice validateNewBufferArgs:options:]:467: failed assertion `Cannot create buffer of zero length.'". But if I lauch without Debugger Mode (not include XCode) it's okay but in that case the shape is not correctly with my wishes, of course it doesn't crash my app. So Anyone can tell me

  1. Why in Debugger Mode, it will broken my app but in Release Mode it doesn't?
  2. In Release Mode, Why Sometimes the Sceneview can show my shape with "cross line" and sometimes it will not?

I really thank and appreciate any advice on my issue.