0

Làm một ứng dụng đơn giản với ARKit

Tạo 1 ARKit SceneKit View đơn giản

Tạo mới 1 project kéo ARKit SceneKit vào view của bạn

rồi kéo reference với file ViewController.swift

tiếp theo thì viết code để config ARKit SceneKit

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    let configuration = ARWorldTrackingConfiguration()
    sceneView.session.run(configuration)
}

và ở viewWillDisappear


override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    sceneView.session.pause()
}

thêm vào file info.plist để app sử dụng Camera

Thêm 3D Object vào ARKit SceneKit View

func addBox() {
    let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
    
    let boxNode = SCNNode()
    boxNode.geometry = box
    boxNode.position = SCNVector3(0, 0, -0.2)
    
    let scene = SCNScene()
    scene.rootNode.addChildNode(boxNode)
    sceneView.scene = scene
}

rồi thêm vào viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()
    addBox()
}

ok, bây giờ hãy chạy app và xem thành quả của mình nào

cũng rất đơn giản như thêm 1 view bình thường đúng ko nào

bây giờ hãy làm một chút nâng cao hơn nào 😃

Nâng cao với ARKit SceneKit View

thêm Gesture Recognizer vào ARSCNView

func addTapGestureToSceneView() {
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.didTap(withGestureRecognizer:)))
    sceneView.addGestureRecognizer(tapGestureRecognizer)
}

rồi để vào viewDidLoad để thực thi chúng

override func viewDidLoad() {
   super.viewDidLoad()
   
   addBox()
   addTapGestureToSceneView()
}

viết thêm cả function để Removing Object khỏi ARSCNView

@objc func didTap(withGestureRecognizer recognizer: UIGestureRecognizer) {
    let tapLocation = recognizer.location(in: sceneView)
    let hitTestResults = sceneView.hitTest(tapLocation)
    guard let node = hitTestResults.first?.node else { return }
    node.removeFromParentNode()
}

thêm 1 extension để chuyển về float 3

extension float4x4 {
    var translation: float3 {
        let translation = self.columns.3
        return float3(translation.x, translation.y, translation.z)
    }
}

rồi sửa 1 chút phương thức addBox

func addBox(x: Float = 0, y: Float = 0, z: Float = -0.2) {
    let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
    
    let boxNode = SCNNode()
    boxNode.geometry = box
    boxNode.position = SCNVector3(x, y, z)
    
    sceneView.scene.rootNode.addChildNode(boxNode)
}

sửa thêm 1 chút ở hàm didTap(withGestureRecognizer:) thành

@objc func didTap(withGestureRecognizer recognizer: UIGestureRecognizer) {
    let tapLocation = recognizer.location(in: sceneView)
    let hitTestResults = sceneView.hitTest(tapLocation)
    guard let node = hitTestResults.first?.node else {
        let hitTestResultsWithFeaturePoints = sceneView.hitTest(tapLocation, types: .featurePoint)
        if let hitTestResultWithFeaturePoints = hitTestResultsWithFeaturePoints.first {
            let translation = hitTestResultWithFeaturePoints.worldTransform.translation
            addBox(x: translation.x, y: translation.y, z: translation.z)
        }
        return
    }
    node.removeFromParentNode()
}

Tất cả mọi thứ đã xong, chạy project và cùng xem thành quả của mình nào

bài viết dựa trên hướng dẫn từ Appcoda


All Rights Reserved

Viblo
Let's register a Viblo Account to get more interesting posts.