创建 SCNNode 的子类 [英] Creating a subclass of a SCNNode

查看:45
本文介绍了创建 SCNNode 的子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从 .dae 文件加载节点:

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
}

我现在想向这个特定节点添加一些属性和方法,这样当我在场景中加载它时,它会获得随机颜色,然后我可以随时修改.我使用以下方法对 SCNSphere 的子类(虽然它是几何体而不是节点)做了类似的事情:

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")
    }
}

如何将这个子类链接"到我使用 newNode() 获得的节点?我天真地认为使用类似

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.

推荐答案

好吧,您的代码无法运行.

Well, your code cannot work.

newNode中创建一个SCNNode:

func newNode() -> SCNNode {
   var node = SCNNode()
   //...
   return node
}

然后你告诉编译器这个 SCNNode 是一个 Ellipsoid

And later on you tell the compiler that this SCNNode is a Ellipsoid

let ellipsoid = newNode() as! Ellipsoid

但事实并非如此!这是一个 SCNNode.你的程序当然会崩溃.

But it isn't! It’s a SCNNode. Of course your program will crash.

如果你想要一个 Ellipsoid 创建一个:

If you want an Ellipsoid create one:

func newEllipsoid() -> Ellipsoid {
   var node = Ellipsoid()
   //...
   return node
}

(或任何您需要创建的地方)

(or wherever you need to create it)

这篇关于创建 SCNNode 的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆