手指按下的ARKit放置节点 [英] ARKit place node where finger press is

查看:98
本文介绍了手指按下的ARKit放置节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ARKit开发一个ios应用程序,我想知道如何在我的手指按在屏幕上但距屏幕约0.5m的位置创建一个节点.我知道我将不得不考虑设备的方向,但是对于从何处开始我仍然很困惑.任何帮助将是巨大的!谢谢.

I am working on an ios app with ARKit and I am wondering how to create a node exactly where I press my finger on the screen but approximately 0.5m into the screen. I know I will have to take into account the orientation of the device but I am still very confused on where to begin. Any help would be great! Thanks.

推荐答案

解决方案很简单.

使用位置矢量(0,0,1)将所需的节点作为子节点添加到摄像机.现在,保存您创建的该节点的世界位置,并立即从相机中删除该子节点.

Add a your required node as a child node to the camera with position vector (0,0,1). Now, save the world position of that node you created and immediately remove the child node from the camera.

再次将您的节点添加为具有保存的世界位置的rootNode的子节点.

Add your node again as the child of the rootNode with the saved world position.

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

    let boxNode = SCNNode.init()
    let boxGeometry = SCNBox.init(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
    boxGeometry.materials.first?.diffuse.contents = UIColor.red
    boxNode.geometry = boxGeometry
    boxNode.position = SCNVector3.init(0, 0, -0.5)
    self.sceneView.pointOfView?.addChildNode(boxNode)


    let boxPosition = boxNode.worldPosition
    boxNode.removeFromParentNode()
    boxNode.worldPosition = boxPosition
    self.sceneView.scene.rootNode.addChildNode(boxNode)

}

该解决方案将需要进行一些小的修改.要获得世界位置,请在相机之前创建一个planeNode(可以在不需要时将其删除).然后在sceneView上执行hitTestResult来获取worldPosition.

The solution will need a small modification. To get the world position, create a planeNode before your camera (you may remove it when not required). And do a hitTestResult on the sceneView to fetch the worldPosition.

    let planeGeometry = SCNPlane.init(width: 10, height: 10)
    planeGeometry.materials.first?.transparency = 0
    planeNode.geometry = planeGeometry
    planeNode.position = SCNVector3.init(0, 0, -1)
    planeNode.name = "HitTestPlane"
    self.sceneView.pointOfView?.addChildNode(planeNode)



    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {


    let loc = touches.first?.location(in: self.sceneView)
    let hitTextResult = self.sceneView.hitTest(loc!, options: [:])

    for result in hitTextResult {
        if result.node.name == "HitTestPlane" {

            let hitPosition = SCNVector3.init(result.worldCoordinates.x, result.worldCoordinates.y, result.worldCoordinates.z)
            let boxNode = SCNNode.init()
            let boxGeometry = SCNBox.init(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
            boxGeometry.materials.first?.diffuse.contents = UIColor.red
            boxNode.geometry = boxGeometry

            boxNode.position = hitPosition
            self.sceneView.scene.rootNode.addChildNode(boxNode)

        }
    }

}

这篇关于手指按下的ARKit放置节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆