NoOneHere
NoOneHere

Reputation: 119

Creating custom geometry in Scenekit

I cannot get my custom geometry to show up. When I debug it, all the variables seem to be holding the correct data, but nothing renders. I just now set it to try and render a single square composed of two triangles for a very simple test, but still no luck. I feel like I'm building the SCNGeometry wrong. After doing all my logic/loops/magic, I have the following:

var verts = SCNGeometrySource(vertices: meshVertices)
var norms = SCNGeometrySource(normals: normals)
var element = SCNGeometryElement(indices: triangleIndices, primitiveType: .triangles)

I've tried both of the following to create the SCNGeometry. Neither render anything, though showStatistics does indicate there are two triangles. Strangely when I touch the screen to try and move the camera, the triangle total jumps to 2.74K...

let newGeo = SCNGeometry(sources: [verts, norms], elements: [element])

-

var sources = [SCNGeometrySource]()
sources.append(verts)
sources.append(norms)
let newGeo = SCNGeometrySource(sources: sources, elements: [element])

I'm really not sure what to try next unless I've completely misunderstood something.

Just to confirm, this is the data I have in my variables:

meshVertices = ([SCNVector3])
    [0]: x = (Float) 0, y = (Float) 0, z = (Float) 0
    [1]: x = (Float) 1, y = (Float) 0, z = (Float) 0
    [2]: x = (Float) 0, y = (Float) 1, z = (Float) 0
    [3]: x = (Float) 1, y = (Float) 1, z = (Float) 0

normals = ([SCNVector3])
    [0]: x = (Float) 0, y = (Float) 0, z = (Float) 1
    [1]: x = (Float) 0, y = (Float) 0, z = (Float) 1
    [2]: x = (Float) 0, y = (Float) 0, z = (Float) 1
    [3]: x = (Float) 0, y = (Float) 0, z = (Float) 1

triangleIndices = ([Int])
    [0]: 3
    [1]: 2
    [2]: 0
    [3]: 3
    [4]: 0
    [5]: 1

I have no problem getting SCNPlane, SCNBox, or other stuff to render, so I don't think the problem is there. The problem seems to be in how I'm creating the geometry. I'm pretty sure my vertices, normals, and indices are correct, so I must be doing something wrong when trying to create the final SCNGeometry object composed of SCNGeometrySource and SCNGeometryElement.

Upvotes: 2

Views: 818

Answers (1)

supc
supc

Reputation: 190

Though it may be a little bit too late. Hope this answer can help others, too.

When specifying indices, please do not use Int type. It seems that when using SceneKit (or maybe its is something even deeper like Metal/OpenGL? I am not sure), the Int type does not take into count. Please use UInt32 or UInt8 as indices here.

Upvotes: 3

Related Questions