sky*_*guy 3 augmented-reality scenekit swift arkit
好的,像其他所有人一样,我在ARKit /世界空间中拖动/转换SCNNode时遇到麻烦。我已经看过使用SceneKit在ARKit中拖动SCNNode以及所有流行的问题,以及苹果公司的代码https://github.com/gao0122/ARKit-Example-by-Apple/blob/master/ARKitExample/VirtualObject.swift
Ive试图尽可能简化,并且做到了我在普通场景工具包游戏中所做的事情-http: //dayoftheindie.com/tutorials/3d-games-graphics/t-scenekit-3d-picking-dragging/
我可以获取被点击的对象并存储当前的手指pos没问题:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let results = gameView.hitTest(touch.location(in: gameView), types: [ARHitTestResult.ResultType.featurePoint])
//TAP Test
let hits = gameView.hitTest(touch.location(in: gameView), options: nil)
currentTapPos = getARPos(hitFeature: hitFeature) //TAP POSITION
if let tappedNode = hits.first?.node {
Run Code Online (Sandbox Code Playgroud)
但是,我的问题是在更新中执行此操作-没有动画。该对象仅出现在我轻按的位置,并且总体上不起作用-
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let results = gameView.hitTest(touch.location(in: gameView), types: [ARHitTestResult.ResultType.featurePoint])
guard let hitFeature = results.last else { return }
testNode.position = currentTapPos
Run Code Online (Sandbox Code Playgroud)
我使用以下函数将其转换为SCNVector3:
func getARPos(hitFeature: ARHitTestResult)->SCNVector3
{
let hitTransform = SCNMatrix4.init(hitFeature.worldTransform)
let hitPosition = SCNVector3Make(hitTransform.m41,
hitTransform.m42,
hitTransform.m43)
return hitPosition
}
Run Code Online (Sandbox Code Playgroud)
我试过了:
-通过向量平移手势-平移手势(这会破坏其他功能)-SCNAction.moveTo
我在这里可以做什么?怎么了?
我同意@Rickster的观点,它Apple Code提供了一个更强大的示例,但是,我有此建议,并且它似乎运行得很顺利(在使用PlaneDetection时更是如此(因为特征点更为零星):
我不使用,touchesBegan而只是做工作touchesMoved:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
//1. Get The Current Touch Point
guard let currentTouchPoint = touches.first?.location(in: self.augmentedRealityView),
//2. Get The Next Feature Point Etc
let hitTest = augmentedRealityView.hitTest(currentTouchPoint, types: .existingPlane).first else { return }
//3. Convert To World Coordinates
let worldTransform = hitTest.worldTransform
//4. Set The New Position
let newPosition = SCNVector3(worldTransform.columns.3.x, worldTransform.columns.3.y, worldTransform.columns.3.z)
//5. Apply To The Node
nodeToDrag.simdPosition = float3(newPosition.x, newPosition.y, newPosition.z)
}
Run Code Online (Sandbox Code Playgroud)
我可能会以完全错误的方式进行此操作(如果我愿意,我希望获得反馈)。
无论如何,我希望它能对您有所帮助...您可以在此处查看其运行中的快速视频:拖动SCNNode
| 归档时间: |
|
| 查看次数: |
734 次 |
| 最近记录: |