初次接觸ARKit(1)

1.什么是AR

增強(qiáng)現(xiàn)實(shí)(Augmented Reality,簡(jiǎn)稱AR)

參考百度百科:AR

2.寫一個(gè)小demo

1.寫ARKit代碼之前我們要知道ARKit入門四大基礎(chǔ)知識(shí)點(diǎn):幾何,節(jié)點(diǎn),渲染,手勢(shì)

1.幾何。就是盛放我們AR元素的容器
2.節(jié)點(diǎn)。就是幾何要放置的位置
3.渲染。顧名思義就是對(duì)幾何進(jìn)行渲染
4.手勢(shì)。對(duì)AR元素的手勢(shì)操作,和UIView的手勢(shì)基本一樣

2.上代碼

首先創(chuàng)建一個(gè)AR工程,在viewdidload方法中添加如下代碼

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Set the view's delegate
    sceneView.delegate = self
    
    // Show statistics such as fps and timing information
    sceneView.showsStatistics = true
    
    // Create a new scene
    //方形幾何
    let scene = SCNScene()
    //1.創(chuàng)建幾何
    //這里的坐標(biāo)系為右手三維坐標(biāo),即伸出右手大拇指向上,食指向
    //右,中指指向自己,三根手指的指向分別為zxy的正方向
    let box = SCNBox(width:0.1, height:0.1, length:0.1,chamferRadius:0)
    //2.渲染
    let material = SCNMaterial()    //渲染器
    material.diffuse.contents = UIColor.red
    //box的渲染器為數(shù)組,可以添加多種渲染
    box.materials = [material]
    //3.創(chuàng)建節(jié)點(diǎn)
    let boxNode = SCNNode(geometry:box)
    boxNode.position = SCNVector3(0, 0, -0.2)
    //把節(jié)點(diǎn)放進(jìn)根節(jié)點(diǎn)
    scene.rootNode.addChildNode(boxNode)
    sceneView.scene = scene
}

這里的效果是在正前方0.2米的位置放置一個(gè)紅色邊長(zhǎng)為0.1米的立方體,這里可以移動(dòng)手機(jī)感受一下AR的效果是怎樣的。

換一種幾何,并給幾何添加手勢(shì)。

class ViewController: UIViewController, ARSCNViewDelegate {

@IBOutlet var sceneView: ARSCNView!

//texts是保存渲染圖片名的數(shù)組
let texts = ["1", "2", "3", "4"]
var index = 0

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Set the view's delegate
    sceneView.delegate = self
    
    // Show statistics such as fps and timing information
    sceneView.showsStatistics = true
    
    // Create a new scene
    /*圓形*/
    let scene = SCNScene()
    //1.幾何
    let spaere = SCNSphere(radius: 0.05)
    //2.渲染
    let material = SCNMaterial()
    material.diffuse.contents = UIImage(named:"")
    //3.節(jié)點(diǎn)
    let sohereNode = SCNNode(geometry: spaere)
    sohereNode.position = SCNVector3(0, 0, -1)
    scene.rootNode.addChildNode(sohereNode)
    sceneView.scene = scene
    //4.手勢(shì)
    self.registerGestureRecognizers()
}
//注冊(cè)手勢(shì)
func registerGestureRecognizers(){
    let tap = UITapGestureRecognizer (target: self, action: #selector(tapped))
    self.sceneView.addGestureRecognizer(tap)
}

@objc func tapped(rec:UIGestureRecognizer){
    //
    let scenView = rec.view as! ARSCNView
    //點(diǎn)擊事件
    let touchLocation = rec.location(in: scenView)
    let hitResults = scenView.hitTest(touchLocation, options:[:])
    if  !hitResults.isEmpty {
        if index == self.texts.count{
            index = 0
        }
        //守護(hù)  取hitResults的第一個(gè)元素
        guard let hitRersult = hitResults.first else {
            return
        }
        //節(jié)點(diǎn)
        let node = hitRersult.node
        //渲染
        node.geometry?.firstMaterial?.diffuse.contents = UIImage (named: texts[index])
        //渲染完令下標(biāo)+1
        index += 1
    }
    
}

那么這里點(diǎn)擊圓形幾何將會(huì)切換幾何的渲染效果。

總結(jié)起來(lái)就是

1.創(chuàng)建場(chǎng)景scene
2.創(chuàng)建幾何(box,sphere)
3.給幾何添加渲染效果
4.創(chuàng)建節(jié)點(diǎn)(誰(shuí)的節(jié)點(diǎn),節(jié)點(diǎn)的位置)
5.添加手勢(shì)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容