ARKit SceneKit 利用ARKit实现虚拟现实

利用ARKit实现虚拟现实的步骤:

一:添加OptionSet结构体

struct Masks: OptionSet {
    let rawValue: Int
    static let ship = Masks(rawValue: 1 << 0)
    static let bullet = Masks(rawValue: 1 << 1)
}

二:添加SCNNode类


// 飞船类
import UIKit
import SceneKit

class Ship: SCNNode {

    override init() {
        super.init()
        
        let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
        self.geometry = box
        
        let shape = SCNPhysicsShape(geometry: box)
        self.physicsBody = SCNPhysicsBody(type: .dynamic, shape: shape)
        self.physicsBody?.isAffectedByGravity = false
        self.physicsBody?.categoryBitMask = Masks.ship.rawValue
        self.physicsBody?.contactTestBitMask = Masks.bullet.rawValue
        
        let material = SCNMaterial()
        material.diffuse.contents = #imageLiteral(resourceName: "boss")
        
        self.geometry?.materials = [material,material,material,material,material,material]
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// 子弹类:
import UIKit
import SceneKit

class Bullet: SCNNode {

    override init() {
        super.init()
        // 形状:球形
        let sphere = SCNSphere(radius: 0.025)
        self.geometry = sphere
        // 物理实体
        let shape = SCNPhysicsShape(geometry: sphere)
        self.physicsBody = SCNPhysicsBody(type: .dynamic, shape: shape)
        
        self.physicsBody?.isAffectedByGravity = false // 不受引力影响
        self.physicsBody?.categoryBitMask = Masks.bullet.rawValue // 自身是识别码
        self.physicsBody?.contactTestBitMask = Masks.ship.rawValue // 碰撞的掩码
        
        // 添加节点的图片
        let material = SCNMaterial()
        material.diffuse.contents = #imageLiteral(resourceName: "bullet2")
        self.geometry?.materials = [material]
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

三:在viewController里面添加ARSCNView

import UIKit
import SceneKit
import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

    var sceneView: ARSCNView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        sceneView = ARSCNView.init(frame: CGRect.init(x: 0, y: 0, width: 320, height: 480)) // 整个场景的大小
        self.view.addSubview(sceneView)
        sceneView.automaticallyUpdatesLighting = true
        // 启动完成后添加飞船
        self.addShip()
        
    }
    
    func addShip() {
        let ship = Ship()
        let x = 0.2
        let y = 0.3
        let z = -1.0
        ship.position = SCNVector3(x,y,z)
        self.sceneView.scene.rootNode.addChildNode(ship)

// 有.scn 类型的资源就更方便了。
        // Create a new scene
//        let scene = SCNScene(named: "art.scnassets/ship.scn")!

    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let configuration = ARWorldTrackingConfiguration()
        // Run the view's session
        sceneView.session.run(configuration)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        // Pause the view's session
        sceneView.session.pause()
    }
}

上面的方法可以实现把一个虚拟的物体添加到现实中去,想要实现更绚丽的效果,只需要添加相应动画就OK了。

你可能感兴趣的:(ARKit SceneKit 利用ARKit实现虚拟现实)