Skip to content

Instantly share code, notes, and snippets.

@jeremyfromearth
Created January 28, 2016 20:58
Show Gist options
  • Save jeremyfromearth/e1b10d156133efd1b20a to your computer and use it in GitHub Desktop.
Save jeremyfromearth/e1b10d156133efd1b20a to your computer and use it in GitHub Desktop.
import SceneKit
class Geometry : NSObject {
// Creates a geometry object from given vertex, index and type data
internal func createGeometry(vertices:[SCNVector3], indices:[Int32], primitiveType:SCNGeometryPrimitiveType) -> SCNGeometry {
// Computed property that indicates the number of primitives to create based on primitive type
var primitiveCount:Int {
get {
switch primitiveType {
case SCNGeometryPrimitiveType.Line:
return indices.count / 2
case SCNGeometryPrimitiveType.Point:
return indices.count
case SCNGeometryPrimitiveType.Triangles,
SCNGeometryPrimitiveType.TriangleStrip:
return indices.count / 3
}
}
}
// Create the source and elements in the appropriate format
let data = NSData(bytes: vertices, length: sizeof(SCNVector3) * vertices.count)
let vertexSource = SCNGeometrySource(
data: data, semantic: SCNGeometrySourceSemanticVertex,
vectorCount: vertices.count, floatComponents: true, componentsPerVector: 3,
bytesPerComponent: sizeof(Float), dataOffset: 0, dataStride: sizeof(SCNVector3))
let indexData = NSData(bytes: indices, length: sizeof(Int32) * indices.count)
let element = SCNGeometryElement(
data: indexData, primitiveType: primitiveType,
primitiveCount: primitiveCount, bytesPerIndex: sizeof(Int32))
return SCNGeometry(sources: [vertexSource], elements: [element])
}
}
@pkclsoft
Copy link

This is great, and it works fine so long as you're not interested in seeing any light shading on the rendered object. It unfortunately lacks the calculation of normals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment