ARKit 交互(手势)

ARKit interaction (gesture)

提问人:spoax 提问时间:1/9/2018 更新时间:1/10/2018 访问量:2999

问:

我正在尝试使用 ARKit 制作一个 iOS 应用程序。到目前为止,我已经使用了 Apple 的“放置对象”示例并对其进行了增强,使其具有自己的几何形状。这部分都很好用。

我有几个不同的物体可以放在地上。简单地说,它们是前面有门的盒子。

我遇到的问题是我现在想向应用程序添加手势,以便当门被点击时,它会旋转打开。然后,当它再次被敲击时,门就关上了。

我一直在寻找一些关于如何做到这一点的教程,但找不到任何东西。有人可以向我解释如何做到这一点,或者向我指出一个教程,展示如何实现这种交互性。

谢谢!:)

iOS Swift Xcode Swift4 ARKit

评论

0赞 Miket25 1/9/2018
您是否研究并注册了水龙头?UIGestureRecognizer

答:

4赞 Clay 1/10/2018 #1

下面是一个基本的 swift Playground,它在加载时会创建一个门。通过点击门,您可以旋转打开,再次点击将关闭门。我把代码分解成不同的函数,这样你就可以看到门是如何打开和关闭的。

import ARKit
import SceneKit
import PlaygroundSupport

class ViewController: NSObject {

var doorNode: SCNNode!
var doorisOpen: Bool!

var sceneView: ARSCNView
init(sceneView: ARSCNView) {
    self.sceneView = sceneView

    super.init()

    self.setupWorldTracking()
    self.sceneView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap(_:))))
        // place door
    self.sceneView.scene.rootNode.addChildNode(createDoor(position: SCNVector3(0,0,-1)))
}

private func setupWorldTracking() {
    if ARWorldTrackingConfiguration.isSupported {
        let configuration = ARWorldTrackingConfiguration()
        configuration.planeDetection = .horizontal
        configuration.isLightEstimationEnabled = true
        self.sceneView.session.run(configuration, options: [])
    }
}

@objc func handleTap(_ gesture: UITapGestureRecognizer) {
    let results = self.sceneView.hitTest(gesture.location(in: gesture.view), types: ARHitTestResult.ResultType.featurePoint)
    guard let result: ARHitTestResult = results.first else {
        return
    }

    let tappedNode = self.sceneView.hitTest(gesture.location(in: gesture.view), options: [:])

    if !tappedNode.isEmpty {
        let node = tappedNode[0].node 
        if doorisOpen == true {
            // rotate door 
            closeDoor()
        } else {
            // rotate door  
            openDoor()
        }

    } else { 

        return

    }

}

func createDoor(position: SCNVector3) -> SCNNode {

    let door = SCNBox(width: 0.3, height: 0.7, length: 0.025, chamferRadius: 0)
    doorNode = SCNNode(geometry: door)
    door.firstMaterial?.locksAmbientWithDiffuse = true
    door.firstMaterial?.diffuse.contents = UIColor.brown
    // place door 
    doorNode.position = position
    // Pivot door from the end
    endPivot(for: doorNode)

    return doorNode
}

func openDoor() {
    let rotate = SCNAction.rotateBy(x: 0, y: CGFloat(degToRadians(degrees: 90)), z: 0, duration: 1)
    doorNode.runAction(rotate)
    doorisOpen = true
}

func closeDoor() {
    let rotate = SCNAction.rotateBy(x: 0, y: CGFloat(degToRadians(degrees: -90)), z: 0, duration: 1)
    doorNode.runAction(rotate)
    doorisOpen = false
}

func endPivot(for node: SCNNode) {
    var min = SCNVector3Zero
    var max = SCNVector3Zero
    node.__getBoundingBoxMin(&min, max: &max)
    node.pivot = SCNMatrix4MakeTranslation(min.x, 0, 0)
}


func degToRadians(degrees:Double) -> Double
{
    return degrees * (M_PI / 180);
 }

}

let sceneView = ARSCNView()

let viewController = ViewController(sceneView: sceneView)
sceneView.autoenablesDefaultLighting = true
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = viewController.sceneView