参考
https://www.jianshu.com/p/348678918783
隐私权限
https://www.cnblogs.com/shisishao/p/5872178.html
import UIKitimport ARKitstruct 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") }}// 子d类: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") } }总结
以上是内存溢出为你收集整理的swift 初探ARKit全部内容,希望文章能够帮你解决swift 初探ARKit所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)