gpt4 book ai didi

ios - 使用 SceneKit 在 ARKit 中拖动 SCNNode

转载 作者:技术小花猫 更新时间:2023-10-29 10:52:22 26 4
gpt4 key购买 nike

我在 ARKit 中有一个简单的 SCNNode,我试图将它拖动到我在手机上移动手指的任何位置。这是我的代码。

 @objc func pan(recognizer :UIGestureRecognizer) {

guard let currentFrame = self.sceneView.session.currentFrame else {
return
}

var translation = matrix_identity_float4x4
translation.columns.3.z = -1.5

let sceneView = recognizer.view as! ARSCNView
let touchLocation = recognizer.location(in: sceneView)

let hitTestResult = sceneView.hitTest(touchLocation, options: [:])

if !hitTestResult.isEmpty {

print("hit result")

guard let hitResult = hitTestResult.first else {
return
}

let node = hitResult.node

node.simdTransform = matrix_multiply(currentFrame.camera.transform, translation)
}
}

问题是拖动很慢,不流畅。

最佳答案

我像这样用 PanGesture 处理翻译。除以700是为了平滑和调整运动速度,我试错了这个值,你可以试试看

@objc func onTranslate(_ sender: UIPanGestureRecognizer) {
let position = sender.location(in: scnView)
let state = sender.state

if (state == .failed || state == .cancelled) {
return
}

if (state == .began) {
// Check it's on a virtual object
if let objectNode = virtualObject(at: position) {
// virtualObject(at searches for root node if it's a subnode
targetNode = objectNode
latestTranslatePos = position
}

}
else if let _ = targetNode {

// Translate virtual object
let deltaX = Float(position.x - latestTranslatePos!.x)/700
let deltaY = Float(position.y - latestTranslatePos!.y)/700

targetNode!.localTranslate(by: SCNVector3Make(deltaX, 0.0, deltaY))

latestTranslatePos = position

if (state == .ended) {
targetNode = nil
}
}
}

关于ios - 使用 SceneKit 在 ARKit 中拖动 SCNNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44729610/

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