gpt4 book ai didi

ios - ARKit 从屏幕平移创建节点

转载 作者:行者123 更新时间:2023-11-28 14:38:24 26 4
gpt4 key购买 nike

我目前正在开发一个应用程序,其目标是让用户能够在屏幕上滑动手指并在场景中看到一行 3D 对象,类似于 3D 绘图。我使用手势识别器设置了场景,但我不知道如何根据屏幕上的平移在 3D 空间中创建节点。这就是我现在所有的手势识别器。

`@IBAction func panRecognized(_ sender: UIPanGestureRecognizer) {
print("Panning")
print(sender.location(in: self.view).x)
print(sender.location(in: self.view).y)
}`

我想知道的是,如何使用这些 x 和 y 坐标在 3D 空间中创建节点,同时还使用设备的方向和位置?目标是放置在距设备约半米远的物体上。

最佳答案

您可以通过以下几种方式实现这一目标,但请注意,这只是一个起点,并未进行任何优化:

您要做的第一件事是在 viewDidLoad 中创建一个 UIPanGestureRecognizer 例如:

let panToDrawGesture = UIPanGestureRecognizer(target: self, action: #selector(createNodesFromPan(_:)))
self.view.addGestureRecognizer(panToDrawGesture)

为了让它更有趣一点,还要添加一个 [UIColor] 数组,例如:

let colours: [UIColor] = [.red, .green, .cyan, .orange, .purple]

然后使用您的 gestureRecognizer 进行绘制,您可以执行如下操作:

///Creates An SCNNode At The Touch Location Of The Gesture Recognizer
@objc func createNodesFromPan(_ gesture: UIPanGestureRecognizer){

//1. Get The Current Touch Location
let currentTouchPoint = gesture.location(in: self.augmentedRealityView)

//2. Perform An ARHitTest For Detected Feature Points
guard let featurePointHitTest = self.augmentedRealityView.hitTest(currentTouchPoint, types: .featurePoint).first else { return }

//3. Get The World Coordinates
let worldCoordinates = featurePointHitTest.worldTransform

//4. Create An SCNNode With An SCNSphere Geeomtery
let sphereNode = SCNNode()
let sphereNodeGeometry = SCNSphere(radius: 0.005)

//5. Generate A Random Colour For The Node's Geometry
let randomColour = colours[Int(arc4random_uniform(UInt32(colours.count)))]
sphereNodeGeometry.firstMaterial?.diffuse.contents = randomColour
sphereNode.geometry = sphereNodeGeometry

//6. Position & Add It To The Scene Hierachy
sphereNode.position = SCNVector3(worldCoordinates.columns.3.x, worldCoordinates.columns.3.y, worldCoordinates.columns.3.z)
self.augmentedRealityView.scene.rootNode.addChildNode(sphereNode)
}

或者,除了使用 gestureRecognizer,您还可以使用触摸来执行绘图:

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

//1. Get The Current Touch Location
guard let currentTouchPoint = touches.first?.location(in: self.augmentedRealityView),
//2. Perform An ARHitTest For Detected Feature Points
let featurePointHitTest = self.augmentedRealityView.hitTest(currentTouchPoint, types: .featurePoint).first else { return }

//3. Get The World Coordinates
let worldCoordinates = featurePointHitTest.worldTransform

//4. Create An SCNNode With An SCNSphere Geeomtery
let sphereNode = SCNNode()
let sphereNodeGeometry = SCNSphere(radius: 0.005)

//5. Generate A Random Colour For The Node's Geometry
let randomColour = colours[Int(arc4random_uniform(UInt32(colours.count)))]
sphereNodeGeometry.firstMaterial?.diffuse.contents = randomColour
sphereNode.geometry = sphereNodeGeometry

//6. Position & Add It To The Scene Hierachy
sphereNode.position = SCNVector3(worldCoordinates.columns.3.x, worldCoordinates.columns.3.y, worldCoordinates.columns.3.z)
self.augmentedRealityView.scene.rootNode.addChildNode(sphereNode)

}

在我的示例中,augmentedRealityView 指的是 ARSCNView,例如:

@IBOutlet weak var augmentedRealityView: ARSCNView!

希望对你有帮助

更新:

如果你想在设定的距离上绘制,并且只使用 ARCamera 的位置,你可以使用 ARSessionDelegate 做这样的事情(如果你想让你的绘图距离 1m然后摄像机更改第 3 部分中 sphereNode.position 的 Z 值):

func session(_ session: ARSession, didUpdate frame: ARFrame) {

//1. Create An SCNNode With An SCNSphere Geeomtery
let sphereNode = SCNNode()
let sphereNodeGeometry = SCNSphere(radius: 0.01)

//2. Generate A Random Colour For The Node's Geometry
let randomColour = colours[Int(arc4random_uniform(UInt32(colours.count)))]
sphereNodeGeometry.firstMaterial?.diffuse.contents = randomColour
sphereNode.geometry = sphereNodeGeometry

//3. Position & Add It To The Scene Hierachy
sphereNode.position = SCNVector3(0, 0, -0.5)
updatePositionAndOrientationOf(sphereNode, withPosition: sphereNode.position, relativeTo: self.augmentedRealityView.pointOfView!)
self.augmentedRealityView.scene.rootNode.addChildNode(sphereNode)
}


/// Updates The Position Of An SCNNode In Relation To The Camera Node
///
/// - Parameters:
/// - node: SCNNode
/// - position: SCNVector3
/// - referenceNode: SCNNode
func updatePositionAndOrientationOf(_ node: SCNNode, withPosition position: SCNVector3, relativeTo referenceNode: SCNNode) {

/* Full Credit To Pablo
https://stackoverflow.com/questions/42029347/position-a-scenekit-object-in-front-of-scncameras-current-orientation/42030679
*/

let referenceNodeTransform = matrix_float4x4(referenceNode.transform)

// Create A Translation Matrix With The Desired Position
var translationMatrix = matrix_identity_float4x4
translationMatrix.columns.3.x = position.x
translationMatrix.columns.3.y = position.y
translationMatrix.columns.3.z = position.z

// Multiply The Configured Translation With The ReferenceNode's Transform
let updatedTransform = matrix_multiply(referenceNodeTransform, translationMatrix)
node.transform = SCNMatrix4(updatedTransform)
}

关于ios - ARKit 从屏幕平移创建节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50710860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com