这是我与添加SCNNode有关的代码:
void AddPlane(ARHitTestResult hitTestResult)
{
// Create a new scene
var scene = SCNScene.FromFile("art.scnassets/plane_banner.scn");
planeNode = scene.RootNode.FindChildNode("planeBanner", true);
planeNode.Position = new SCNVector3(hitTestResult.WorldTransform.Column3.X, hitTestResult.WorldTransform.Column3.Y, hitTestResult.WorldTransform.Column3.Z);
planeNode.Scale = new SCNVector3((float)0.005, (float)0.005, (float)0.005);
this.sceneView.Scene.RootNode.AddChildNode(planeNode);
// Pan gesture
tappedObjectNode = planeNode;
planeNode.Name = "planeBanner";
var panGestureRecognizer = new UIPanGestureRecognizer(move);
sceneView.AddGestureRecognizer(panGestureRecognizer);
}
这是我的“ 移动”方法:
void move(UIPanGestureRecognizer sender)
{
if (movingNow)
{
CGPoint translation;
translation = sender.TranslationInView(sceneView);
SCNVector3 result = CGPointToSCNVector3(sceneView, tappedObjectNode.Position.Z, translation);
tappedObjectNode.Position = result;
}
var test = new NSDictionary();
var tapPosition = sender.LocationInView(this.View);
var hitNode = sceneView.HitTest(tapPosition, test);
if (hitNode != null && hitNode.Length > 0)
{
movingNow = true;
if(hitNode[0].Node.Name == "planeBanner"){
tappedObjectNode = hitNode[0].Node;
}
}
if(sender.State == UIGestureRecognizerState.Ended) {
}
}
这是我在搜索从该点获取SCNVector3的答案时发现的一种方法。
SCNVector3 CGPointToSCNVector3(SCNView aView, float aDepth, CGPoint aPoint)
{
var projectOrigin = aView.ProjectPoint(new SCNVector3(0, 0, aDepth));
var locationWithZ = new SCNVector3((float)aPoint.X, (float)aPoint.Y, projectOrigin.Z);
return aView.UnprojectPoint(locationWithZ);
}
问题是,当我尝试移动对象时,它会逐渐缩小或从屏幕的最左侧开始。
我经历了各种解决方案,但在这种情况下没有找到任何有用的东西。
有人可以为此指导/帮助我吗?
如何使用Scenekit在AR中制作完美的可移动对象?