gpt4 book ai didi

ios - 在 ARKit 中放置、拖动和删除 SCNNode

转载 作者:可可西里 更新时间:2023-11-01 04:55:25 25 4
gpt4 key购买 nike

我正在使用 ARKit 开发一个小项目。我希望能够在点击时将对象添加到我的 AR SceneView,通过双击删除它们,并通过平移或拖动来拖动主题。

放置对象的初始点击工作正常,但我在节点移除和拖动方面遇到了一些问题。

删除和拖动的主要问题是很难真正“按住”或单击 SCNNode。大多数结果最终不在我添加的 SCNNode 上。

第二个问题是拖动有点问题,SCNNode 并没有像我的手指在拖动上移动那么多。

我决定在 github 上创建一个项目,链接在这里:https://github.com/theraad/ARAttempt

但我还将在此处发布删除对象和拖动对象的代码:

-(void)handleRemoveObject:(UITapGestureRecognizer *)recognizer {
NSLog(@"Long Press Fired");
CGPoint tapPoint = [recognizer locationInView:_sceneView];

NSArray <SCNHitTestResult *> *result = [self.sceneView hitTest:tapPoint options:nil];

if ([result count] == 0) {
return;
}
SCNHitTestResult *hitResult = [result firstObject];
if (hitResult.node) {
[[hitResult.node parentNode] removeFromParentNode];
}
}

-(void)moveObject:(UIPanGestureRecognizer *)recognizer {
NSLog(@"Move object");
if (recognizer.state == UIGestureRecognizerStateBegan) {
NSLog(@"Pan state began");
CGPoint tapPoint = [recognizer locationInView:_sceneView];
NSArray <SCNHitTestResult *> *result = [self.sceneView hitTest:tapPoint options:nil];

if ([result count] == 0) {
return;
}
SCNHitTestResult *hitResult = [result firstObject];
if ([hitResult.node.name isEqualToString:@"candle"]) {
movedObject = [hitResult node];
} else if ([[hitResult.node parentNode].name isEqualToString:@"candle"]) {
movedObject = [[[hitResult node] parentNode] parentNode] parentNode];
}
if (movedObject){
NSLog(@"Holding an Object");
}
}
if (recognizer.state == UIGestureRecognizerStateChanged) {
NSLog(@"Pan State Changed");
if (movedObject) {

CGPoint tapPoint = [recognizer locationInView:_sceneView];
NSArray <ARHitTestResult *> *hitResults = [_sceneView hitTest:tapPoint types:ARHitTestResultTypeFeaturePoint];
ARHitTestResult *result = [hitResults lastObject];

SCNMatrix4 matrix = SCNMatrix4FromMat4(result.worldTransform);
SCNVector3 vector = SCNVector3Make(matrix.m41, matrix.m42, matrix.m43);

[movedObject setPosition:vector];
NSLog(@"Moving object position");
}
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
NSLog(@"Done moving object homeie");
movedObject = nil;
}
}

如有任何帮助,我们将不胜感激。

谢谢。

更新:

所以我发现抓取物体的困难是因为我在使用:self.sceneView.debugOptions = ARSCNDebugOptionShowFeaturePoints;
当我试图点击一个对象时,大多数时候它会捕获这些特征点之一。

-(void)moveObject:(UIPanGestureRecognizer *)recognizer {
NSLog(@"Move object");
if (recognizer.state == UIGestureRecognizerStateBegan) {
NSLog(@"Pan state began");
CGPoint tapPoint = [recognizer locationInView:_sceneView];
NSArray <SCNHitTestResult *> *result = [self.sceneView hitTest:tapPoint options:nil];

if ([result count] == 0) {
return;
}
SCNHitTestResult *hitResult = [result firstObject];
movedObject = [[[hitResult node] parentNode] parentNode] parentNode]; //This aspect varies based on the type of .SCN file that you have
}
if (movedObject){
NSLog(@"Holding an Object");
}
}
if (recognizer.state == UIGestureRecognizerStateChanged) {
NSLog(@"Pan State Changed");
if (movedObject) {

CGPoint tapPoint = [recognizer locationInView:_sceneView];
NSArray <ARHitTestResult *> *hitResults = [_sceneView hitTest:tapPoint types:ARHitTestResultTypeFeaturePoint];
ARHitTestResult *result = [hitResults lastObject];

SCNMatrix4 matrix = SCNMatrix4FromMat4(result.worldTransform);
SCNVector3 vector = SCNVector3Make(matrix.m41, matrix.m42, matrix.m43);

[movedObject setPosition:vector];
NSLog(@"Moving object position");
}
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
NSLog(@"Done moving object homeie");
movedObject = nil;
}
}

所以问题似乎是我之前没有捕获整个对象,而是仍然捕获了这个对象的子项,当你试图拖动一个子项时,由于某种原因它迫使运动变得迟缓。所以我不得不做一些试验和错误来意识到我必须提升父级别来解决这个问题。

希望这对您有所帮助。

最佳答案

拖动对象的解决方案是将 movedObject 设置为 [[[hitResult node] parentNode] parentNode] parentNode] 并且拖动变得更平滑。

关于ios - 在 ARKit 中放置、拖动和删除 SCNNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45509973/

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