swift 初探ARKit

 

参考

https://www.jianshu.com/p/348678918783

 

隐私权限

https://www.cnblogs.com/shisishao/p/5872178.html

 

 

import UIKit
import ARKit

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

class ViewController: UIViewController,ARSCNViewDelegate {
    
    var sceneView: ARSCNView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let showHeight : CGFloat = UIScreen.main.bounds.size.height - 100
        sceneView = ARSCNView.init(frame: CGRect.init(x: 0,y: 50,width: UIScreen.main.bounds.size.width,height:showHeight))
        
        // 整个场景的大小
        self.view.addSubview(sceneView)
        sceneView.automaticallyUpdatesLighting = true
        // 启动完成后添加飞船
        self.addShip()
    }
    
    func addShip() {
        let ship = Ship()
        let x : Double = 0
        let y : Double = 0.3
        let z : Double = -0.5
        ship.position = SCNVector3(x,y,z)
        self.sceneView.scene.rootNode.addChildNode(ship)
        
        //球形
        let bullet = Bullet()
        let x1 : Double = 0.1
        let y1 : Double = 0
        let z1 : Double = -0.2
        bullet.position = SCNVector3(x1,y1,z1)
        self.sceneView.scene.rootNode.addChildNode(bullet)
        // 有.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()
    }
}

// 飞船类
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 = UIImage.init(named: "battle")
        
        self.geometry?.materials = [material,material,material]
    }
    
    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)
        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 = UIImage.init(named: "sanlian")
        self.geometry?.materials = [material]
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...