转换" SCNScene"到" SCNNode"

时间:2017-10-21 12:00:09

标签: ios swift xcode scenekit arkit

我正在为我的IT任务做一个ARKit应用程序,我遵循了比特屏蔽和碰撞的指南,我让它工作,但它只适用于一个简单的盒子而不是我的3D模型所以有一种方法转换这个代码到底部的那个或我做错了什么?因为顶部代码不会出现,但是底部代码会出现:

class Monster: SCNNode {

    override init () {
        super.init()

    guard let virtualObjectScene = SCNScene(named: "Monster.scn", inDirectory: "art.scnassets" ) else {
        return
    }

    let wrapperNode = SCNNode()
    for child in virtualObjectScene.rootNode.childNodes {
        child.geometry?.firstMaterial?.diffuse.contents = UIColor.black
        wrapperNode.addChildNode(child)
    }
    wrapperNode.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.dynamic, shape: nil)
    wrapperNode.physicsBody?.isAffectedByGravity = false
    wrapperNode.physicsBody?.categoryBitMask = CollisionCategory.ship.rawValue
    wrapperNode.physicsBody?.collisionBitMask = CollisionCategory.bullet.rawValue
    wrapperNode.physicsBody?.contactTestBitMask = CollisionCategory.bullet.rawValue
    wrapperNode.pivot = SCNMatrix4MakeRotation(Float(CGFloat(Double.pi/2)), 1, 0, 0)

    print("Placing Monster")
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

对于这样的事情,因为出于某种原因,这段代码有效,而最高层代码没有,我做错了什么?

class Bullet: SCNNode {
    override init () {
        super.init()
        let sphere = SCNSphere(radius: 0.025)
        self.geometry = sphere
        let shape = SCNPhysicsShape(geometry: sphere, options: nil)
        self.physicsBody = SCNPhysicsBody(type: .dynamic, shape: shape)
        self.physicsBody?.isAffectedByGravity = false
        self.physicsBody?.categoryBitMask = CollisionCategory.bullet.rawValue
        self.physicsBody?.collisionBitMask = CollisionCategory.ship.rawValue

        // add texture
        let material = SCNMaterial()
        material.diffuse.contents = UIImage(named: "bullet_texture.jpg")
        self.geometry?.materials  = [material]
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

由于

1 个答案:

答案 0 :(得分:0)

使用:SCNReferenceNode

网站:[1]:https://developer.apple.com/documentation/scenekit/scnreferencenode/

用法:定义路径并将其用作参考节点