编程开发笔记ios-SceneKit/ARKit/OpenGL/Unity3D

ARKit

2017-12-29  本文已影响218人  牛奈奈
简介

增强现实技术(Augmented Reality,简称 AR),是一种实时地计算摄影机影像的位置及角度并加上相应图像、视频、3D模型的技术,这种技术的目标是在屏幕上把虚拟世界套在现实世界并进行互动。ARKit框架提供了二种AR技术,分别基于三3D场景的SceneKit,以及基于2D场景的SpriktKit

原理

相机捕捉现实世界图像,计算3D模型的位置(ARKit负责)
呈现3D世界(SceneKit负责)
一般实现步骤:

三维坐标轴.png
SceneKit介绍
锚点.png
例子
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 添加SCNView
        let scnView = SCNView(frame: view.bounds)
        scnView.backgroundColor = UIColor.black
        //是否允许用户可以拖动屏幕来切换角度 默认false
        scnView.allowsCameraControl = true
        scnView.scene = SCNScene()
        view.addSubview(scnView)
        
        // 往scene中添加子节点(所有的子视图都以节点的方式添加)
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        // 自动调整相机zFar的值(何为zFar参考文中图片)
        cameraNode.camera?.automaticallyAdjustsZRange = true
        // 在三维中的坐标
        cameraNode.position = SCNVector3(0, 1000, 1000)
        // 默认相机视角是平行向前,此时绕X轴旋转可以看到低视角的节点,前三个参数是确定怎么旋转,绕哪个轴旋转,哪个轴的值为1
        cameraNode.rotation = SCNVector4(1, 0, 0, -Float(Double.pi / 4))
        scnView.scene?.rootNode.addChildNode(cameraNode)
        
        // 添加一个光源
        let lightNode = SCNNode()
        lightNode.light = SCNLight()
        lightNode.light?.type = .spot
        // 光源的几何形状,系统还有多种其他的基础几何体
        lightNode.geometry = SCNSphere(radius: 20)
        // 光源发射的内容 (本身是白色,发射的黄色的光)
        lightNode.geometry?.firstMaterial?.emission.contents = UIColor.yellow
        // 光源本身的材质内容 (本身是黄色)
//        lightNode.geometry?.firstMaterial?.diffuse.contents = UIColor.yellow
        lightNode.position = SCNVector3(0, 1000, 40)
        // 灯光左右移动
        let leftAction = SCNAction.move(to: SCNVector3(-100, 1000, 40), duration: 2)
        let rightAction = SCNAction.move(to: SCNVector3(100, 1000, 40), duration: 2)
        // 相当于UIKit中的组动画
        let sequence = SCNAction.sequence([leftAction,rightAction])
        lightNode.runAction(SCNAction.repeatForever(sequence))
        scnView.scene?.rootNode.addChildNode(lightNode)
        
        // 添加一个环境光
        let ambientNode = SCNNode()
        ambientNode.light = SCNLight()
        ambientNode.light?.type = .ambient
        scnView.scene?.rootNode.addChildNode(ambientNode)
        
        let floorNode = SCNNode()
        floorNode.geometry = SCNFloor()
        floorNode.geometry?.firstMaterial?.diffuse.contents = "floor.jpeg"
        scnView.scene?.rootNode.addChildNode(floorNode)
        
        let treeNode = SCNScene(named: "palm_tree.dae")?.rootNode
        treeNode?.rotation = SCNVector4(1, 0, 0, -Float(Double.pi / 2))
        scnView.scene?.rootNode.addChildNode(treeNode!)
        
        //当使用SCNLookAtConstraint时,Scene Kit不管被朝向的对象如何移动,旋转都会让相机对着他.
        let constaint = SCNLookAtConstraint(target: treeNode)
        lightNode.constraints = [constaint]
        
        // 是否投下阴影
        lightNode.light?.castsShadow = false
        //遮光布 透视下来的投影图像
        lightNode.light?.gobo?.contents = "mip.jpg"
        //值越大 遮光布的效果越明显,投下的图像越显
        lightNode.light?.gobo?.intensity = 0.5
        //阴影模式有三种:deferred(递延),forward:(向前),modulated:(调节)
        lightNode.light?.shadowMode = .modulated
        
    }
效果.gif

ARKit介绍

上一篇 下一篇

猜你喜欢

热点阅读