34
26

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.

ARKitAdvent Calendar 2018

Day 8

ARオブジェクトの座標を操る方法チートシート

Last updated at Posted at 2018-12-08

カメラの50cm前に置く

0.gif

guard let camera = sceneView.pointOfView else {
    return
}
let cameraPos = SCNVector3Make(0, 0, -0.5)
let position = camera.convertPosition(cameraPos, to: nil)
boxNode.position = position

スクリーンのタップした位置の50cm前に置く

2.gif

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let point = touches.first?.location(in: sceneView) else {
        return
    }
    let infrontOfCamera = SCNVector3(x: 0, y: 0, z: -0.5)
    
    guard let cameraNode = sceneView.pointOfView else { return }
    let pointInWorld = cameraNode.convertPosition(infrontOfCamera, to: nil)
    
    var screenPos = sceneView.projectPoint(pointInWorld)
    
    screenPos.x = Float(point.x)
    screenPos.y = Float(point.y)
    
    let finalPosition = sceneView.unprojectPoint(screenPos)
    boxNode.position = finalPosition
}

タップして平面、垂直面に置く

水平面 垂直面
GIFイメージ-79E879DA7D94-1.gif GIFイメージ-B53791B867A3-1.gif

configuration

let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = [.horizontal, .vertical]

touchesBegan

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let location = touches.first?.location(in: sceneView),
        let horizontalHit = sceneView.hitTest(
            location,
            types: .existingPlane
            ).first else {
                return
    }
    let column3: simd_float4 = horizontalHit.worldTransform.columns.3
    let position = SCNVector3(column3.x, column3.y, column3.z)
    boxNode.position = position
    sceneView.scene.rootNode.addChildNode(boxNode)
}

任意の方向を向かせる

例:カメラの方向を向かせる

4.gif

guard let camera = sceneView.pointOfView else {
    return
}
boxNode.look(at: camera.position)

カメラと同じ方向を向かせる

5.gif

guard let camera = sceneView.pointOfView else {
    return
}
boxNode.eulerAngles = camera.eulerAngles

カメラと同じ高さに置く

1.gif

guard let camera = sceneView.pointOfView else {
    return
}
let cameraPos = SCNVector3Make(0, 0, -0.5)
var position = camera.convertPosition(cameraPos, to: nil)
position.y = camera.position.y // ここ
boxNode.position = position

カメラから発射する

6.gif

private func shoot(){
    guard let camera = sceneView.pointOfView else {
       return
    }
    boxNode.position = camera.position
    
    let targetPosCamera = SCNVector3Make(0, 0, -2)
    let target = camera.convertPosition(targetPosCamera, to: nil)
    let action = SCNAction.move(to: target, duration: 1)
    boxNode.runAction(action)
}

カメラの前に物体をホールド

7.gif

extension PracticeViewController: ARSCNViewDelegate {
    func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
        // このフラグをボタン等で切り替える
        if holding {
            guard let camera = sceneView.pointOfView else {
                return
            }
            let cameraPos = SCNVector3Make(0, 0, -0.5)
            let position = camera.convertPosition(cameraPos, to: nil)
            boxNode.position = position
        }
    }
}

サンプルコード

サンプルコードはARKit-EmperorのPracticeにあります!

34
26
1

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
34
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?