I'm loading a node from a .dae file with the following code:
func newNode() -> SCNNode {
var node = SCNNode()
let scene = SCNScene(named: "circle.dae")
var nodeArray = scene!.rootNode.childNodes
for childNode in nodeArray {
node.addChildNode(childNode as! SCNNode)
}
return node
}
I would now like to add some properties and methods to this specific node so that when I load it in a scene it gets a random color that I can then modify whenever I want. I had done something similar with a subclass of a SCNSphere (which is a geometry and not a node, though) using:
let NumberOfColors: UInt32 = 4
enum EllipsoidColor: Int, Printable {
case Red = 0, Blue, Green, White
var ellipsoidName: String {
switch self {
case .Red:
return "red"
case .Blue:
return "blue"
case .Green:
return "green"
case .White:
return "white"
}
}
var description: String {
return self.ellipsoidName
}
static func random() -> EllipsoidColor {
return EllipsoidColor(rawValue: Int(arc4random_uniform(NumberOfColors - 1)))!
}
}
class Ellipsoid: SCNNode {
func setMaterialColor(ellipsoidColor: EllipsoidColor) {
let color: UIColor
switch ellipsoidColor {
case .Red:
color = UIColor.redColor()
case .Blue:
color = UIColor.blueColor()
case .Green:
color = UIColor.greenColor()
case .White:
color = UIColor.whiteColor()
}
self.geometry?.firstMaterial!.diffuse.contents = color
}
var color : EllipsoidColor {
didSet {
self.setMaterialColor(color)
}
}
init(color: EllipsoidColor) {
self.color = color
super.init()
self.setMaterialColor(color)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
How can I "link" this subclass to the node that I obtain using newNode() ? I naively thought that using something like
let ellipsoid = newNode() as! Ellipsoid
would work, but it doesn't. Thanks for your time and help.