1
1

More than 3 years have passed since last update.

ARKit SceneKit でタップした場所のSCNVector3ポジションを取得する

Posted at

タップした先にSCNNodeがある場合は、タップ先とnodeの接点が取れます。

// sceneViewにtapRecognizerを与えておいてください。

func tap(_ sender: UITapGestureRecognizer) {
    let sceneView = sender.view as! ARSCNView
    let touchLocation = sender.location(in: sceneView)
    let hitResults = sceneView.hitTest(touchLocation, options: [:])
    if !hitResults.isEmpty {
        if let hitPosition = hitResults.first?.worldCoordinates {
            print(hitPosition)
    }

タップした先がARKitのカメラ背景など何もない場合、適当なPlaneを指定することでその平面とタップとの接点が取れます。

func tap(_ sender: UITapGestureRecognizer) {
    let sceneView = sender.view as! ARSCNView
    let touchLocation = sender.location(in: sceneView)
    if let unProjectPoint = sceneView.unprojectPoint(touchLocation, ontoPlane: simd_float4x4 (columns: (simd_float4(0,0,-5, 0), simd_float4(0,0,-5, 0), simd_float4(0,0,-5, 0), simd_float4(0,0,-5, 0)))) { 
    // 適当に5m先の平面との接点をとってみる。平面検出したアンカーなどでもいいですね。
       print(unProjectPoint)
    }

あとはタップポイントにノードおくなりビームを飛ばすなりなんなり。

🐣


フリーランスエンジニアです。
お仕事のご相談こちらまで
rockyshikoku@gmail.com

Core MLを使ったアプリを作っています。
機械学習関連の情報を発信しています。

Twitter
Medium

1
1
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
1
1