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

查看:16
本文介绍了手指按压所在的 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天全站免登陆