参考
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") } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。