Created
April 29, 2022 09:06
-
-
Save wizard1066/88c099fe54125dc5177e4b3b0df9c5bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Polygon : Geometry { | |
func polyNode(in rect: CGRect) -> SCNGeometry { | |
var verticesP:[SCNVector3] = [] | |
var planeIndicesP:[Int32] = [] | |
let angle = 0 | |
let sides = 24 | |
var index:Int32 = 0 | |
planeIndicesP.append(Int32(sides)) | |
let center:CGPoint = CGPoint(x: rect.width / 2, y: rect.height / 2) | |
let radius:Double = Double(rect.width / 2) | |
for i in stride(from: angle, to: (360 + angle), by: 360/sides) { | |
let radians = Double(i) * Double.pi / 180.0 | |
let x = Double(center.x) + radius * cos(radians) | |
let y = Double(center.y) + radius * sin(radians) | |
verticesP.append(SCNVector3(x: Float(x), y: Float(y), z: 0)) | |
planeIndicesP.append(index) | |
index += 1 | |
} | |
let planesP:SCNGeometry = { | |
self.createGeometry( | |
vertices:verticesP, indices: planeIndicesP, | |
primitiveType: SCNGeometryPrimitiveType.polygon) | |
}() | |
return planesP | |
} | |
class Geometry : NSObject { | |
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: | |
return indices.count / 3 | |
case SCNGeometryPrimitiveType.triangleStrip: | |
return indices.count - 2 | |
case SCNGeometryPrimitiveType.polygon: | |
return 1 | |
default: | |
assert(false,"neverHappens \(primitiveType)") | |
} | |
} | |
} | |
// Create the source and elements in the appropriate format | |
let data = Data(bytes: vertices, count: MemoryLayout<SCNVector3>.size * vertices.count) | |
let vertexSource = SCNGeometrySource( | |
data: data as Data, semantic: SCNGeometrySource.Semantic.vertex, | |
vectorCount: vertices.count, usesFloatComponents: true, componentsPerVector: 3, | |
bytesPerComponent: MemoryLayout<Float>.size, dataOffset: 0, dataStride: MemoryLayout<SCNVector3>.size) | |
let indexData = Data(bytes: indices, count: MemoryLayout<Int32>.size * indices.count) | |
let element = SCNGeometryElement( | |
data: indexData as Data, primitiveType: primitiveType, | |
primitiveCount: primitiveCount, bytesPerIndex: MemoryLayout<Int32>.size) | |
var vertexColors = [SCNVector3]() | |
for _ in 0..<vertices.count { | |
let red = Float(arc4random() % 255) / 255.0 | |
let green = Float(arc4random() % 255) / 255.0 | |
let blue = Float(arc4random() % 255) / 255.0 | |
vertexColors.append(SCNVector3(red, green, blue)) | |
} | |
let colors = SCNGeometrySource(colors: vertexColors) | |
return SCNGeometry(sources: [vertexSource, colors], elements: [element]) | |
} | |
} | |
// Calling it in your SceneKit with the code | |
let polygon = Polygon() | |
let polyNode = SCNNode(geometry: polygon.polyNode(in: CGRect(x: 0, y: 0, width: 2, height: 2))) | |
scene.rootNode.addChildNode(polyNode) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment