0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

練習のためにSwiftをUdemyで勉強してみた(6日目)

Posted at

今日やったこと

  • ARアプリの続き
    • 平面を探す
    • 平面にグリッドを表示させる

DitectPlaneAR

平面を探し、見つけたらテキストを表示するアプリ
let configuration = ARWorldTrackingConfiguration()が平面を見つけてくれるそう。
以下での言及されている。
https://qiita.com/chino_tweet/items/2591648bea43dfccd898

ViewController.swift

        override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // Create a session configuration
        let configuration = ARWorldTrackingConfiguration()

        // Run the view's session
        sceneView.session.run(configuration)
    }

GridOverlay

上記Appの派生。
平面を見つけたら、そこにグリッド(十字線)を表示させる

GridPlane.swift
import Foundation
import SceneKit
import ARKit

class GridPlane: SCNNode{
    var anchor : ARPlaneAnchor
    var planeGeometry : SCNPlane!
    
    init(anchor: ARPlaneAnchor) {
        self.anchor = anchor
        super.init()
        setup()
    }
    
    func update(anchor: ARPlaneAnchor){
        self.planeGeometry.width = CGFloat(anchor.extent.x)
        self.planeGeometry.height = CGFloat(anchor.extent.z)
        self.position = SCNVector3Make(anchor.center.x, 0, anchor.center.z)
    }
    
    private func setup(){
        self.planeGeometry = SCNPlane(width: CGFloat(self.anchor.extent.x), height: CGFloat(self.anchor.extent.z))
        let material = SCNMaterial()
        material.diffuse.contents = UIImage(named: "overlay_grid.png")
         
        self.planeGeometry.materials = [material]
        
        let planenode = SCNNode(geometry: self.planeGeometry)
        planenode.position = SCNVector3Make(anchor.center.x,0,anchor.center.z)
        planenode.transform = SCNMatrix4MakeRotation(Float(-Double.pi / 2.0), 1.0, 0.0 , 0.0)
        
        self.addChildNode(planenode)
        
    }
    
    required init?(coder aDecoder: NSCoder){
        fatalError("Fatal Error!")
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?