Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

181 lines (143 sloc) 6.72 KB
//
// ObjectGestureViewController.swift
// ARKit-beginner
//
// Created by Nobuhiro Harada on 2019/03/14.
// Copyright © 2019 Nobuhiro Harada. All rights reserved.
//
import UIKit
import ARKit
import MBProgressHUD
class GestureViewController: UIViewController {
@IBOutlet weak var sceneView: ARSCNView!
let configuration = ARWorldTrackingConfiguration()
private var mBProgressHUD: MBProgressHUD!
private var newAngleY :Float = 0.0
private var newAngleX :Float = 0.0
private var currentAngleX :Float = 0.0
private var currentAngleY :Float = 0.0
private var localTranslatePosition :CGPoint!
override func viewDidLoad() {
super.viewDidLoad()
self.mBProgressHUD = MBProgressHUD.showAdded(to: self.sceneView, animated: true)
self.mBProgressHUD.label.text = "平面検出中"
sceneView.delegate = self
// ARに内の座標軸と点群表示
self.sceneView.debugOptions = [.showWorldOrigin, .showFeaturePoints]
// fpsなどの表示
sceneView.showsStatistics = true
// 全方向光源ON
sceneView.autoenablesDefaultLighting = true
self.registerGestureRecognizer()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
configuration.planeDetection = .horizontal
sceneView.session.run(configuration)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView.session.pause()
}
func registerGestureRecognizer() {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapped))
self.sceneView.addGestureRecognizer(tapGestureRecognizer)
let pinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(pinched))
self.sceneView.addGestureRecognizer(pinchGestureRecognizer)
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panned))
self.sceneView.addGestureRecognizer(panGestureRecognizer)
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector
(longPressed))
self.sceneView.addGestureRecognizer(longPressGestureRecognizer)
}
@objc func tapped(recognizer: UITapGestureRecognizer) {
guard let tappedView = recognizer.view as? ARSCNView else { return }
let touchLocation = recognizer.location(in: tappedView)
let hitTestResults = tappedView.hitTest(touchLocation, types: .existingPlane)
if let hitTest = hitTestResults.first {
let roboScene = SCNScene(named: "art.scnassets/robo.scn")!
guard let roboNode = roboScene.rootNode.childNode(withName: "roboParentNode", recursively: true) else { return }
roboNode.name = "robo"
roboNode.position = SCNVector3(hitTest.worldTransform.columns.3.x,hitTest.worldTransform.columns.3.y,hitTest.worldTransform.columns.3.z)
self.sceneView.scene.rootNode.addChildNode(roboNode)
}
}
@objc func pinched(recognizer: UIPinchGestureRecognizer) {
if recognizer.state == .changed {
self.sceneView.scene.rootNode.enumerateChildNodes { (node, _) in
if node.name == "robo" {
let pinchScaleX = Float(recognizer.scale) * node.scale.x
let pinchScaleY = Float(recognizer.scale) * node.scale.y
let pinchScaleZ = Float(recognizer.scale) * node.scale.z
node.scale = SCNVector3(pinchScaleX,pinchScaleY,pinchScaleZ)
recognizer.scale = 1
}
}
}
}
@objc func panned(recognizer: UIPanGestureRecognizer) {
switch recognizer.state {
case .changed:
guard let pannedView = recognizer.view as? ARSCNView else { return }
let translation = recognizer.translation(in: pannedView)
self.sceneView.scene.rootNode.enumerateChildNodes { (node, _) in
if node.name == "robo" {
self.newAngleX = Float(translation.y) * (Float)(Double.pi)/180
self.newAngleX += self.currentAngleX
self.newAngleY = Float(translation.x) * (Float)(Double.pi)/180
self.newAngleY += self.currentAngleY
node.eulerAngles.x = self.newAngleX
node.eulerAngles.y = self.newAngleY
}
}
case .ended:
self.currentAngleX = self.newAngleX
self.currentAngleY = self.newAngleY
default:
break
}
}
@objc func longPressed(recognizer: UILongPressGestureRecognizer) {
guard let longPressedView = recognizer.view as? ARSCNView else { return }
let touch = recognizer.location(in: longPressedView)
self.sceneView.scene.rootNode.enumerateChildNodes { (node, _) in
if node.name == "robo" {
switch recognizer.state {
case .began:
localTranslatePosition = touch
case .changed:
let deltaX = Float(touch.x - self.localTranslatePosition.x)/700
let deltaY = Float(touch.y - self.localTranslatePosition.y)/700
print(deltaX)
print(deltaY)
node.localTranslate(by: SCNVector3(deltaX,0.0,deltaY))
self.localTranslatePosition = touch
default:
break
}
}
}
}
}
extension GestureViewController: ARSCNViewDelegate {
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
if anchor is ARPlaneAnchor {
DispatchQueue.main.async {
self.mBProgressHUD.label.text = "平面検出OK"
self.mBProgressHUD.hide(animated: true, afterDelay: 0.5)
}
}
guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
let width = CGFloat(planeAnchor.extent.x)
let height = CGFloat(planeAnchor.extent.z)
let plane = SCNPlane(width: width, height: height)
plane.materials.first?.diffuse.contents = UIColor.white.withAlphaComponent(0.5)
let planeNode = SCNNode(geometry: plane)
let x = CGFloat(planeAnchor.center.x)
let y = CGFloat(planeAnchor.center.y)
let z = CGFloat(planeAnchor.center.z)
planeNode.position = SCNVector3(x,y,z)
planeNode.eulerAngles.x = -.pi / 2
node.addChildNode(planeNode)
}
}
You can’t perform that action at this time.