如何解决ARKitSample项目的子节点快照问题?

时间:2019-05-28 07:20:53

标签: c# xamarin xamarin.ios arkit

我已经从-https://github.com/xamarin/ios-samples/blob/master/ios11/ARKitSample/ARKitSample/GameViewController.cs

的船上下载了ARKitSample项目。

运行代码后,它会按预期运行,直到我点击屏幕,调用手势识别事件,并且第一次运行良好-我得到一个带有屏幕快照的平面节点。但是,当第二次调用此方法时,它没有在第二次点击的位置创建新节点,而是与初始子节点重叠。

该代码是ARKit主题演讲的原始Swift版本的端口-https://youtu.be/LLRweyZ1KpA?t=1380

private void HandleTap(UIGestureRecognizer gestureRecognize)
{
    // Get current frame
    var currentFrame = SceneView.Session.CurrentFrame;
    if (currentFrame == null) return;

    // Create an image plane using a snapshot of the view
    var imagePlane = SCNPlane.Create(SceneView.Bounds.Width / 6000, SceneView.Bounds.Height / 6000);
    imagePlane.FirstMaterial.Diffuse.Contents = SceneView.Snapshot();
    imagePlane.FirstMaterial.LightingModelName = SCNLightingModel.Constant;

    // Create a plane node and add it to the scene
    var planeNode = SCNNode.FromGeometry(imagePlane);
    SceneView.Scene.RootNode.AddChildNode(planeNode);

    // Set transform of node to be 10cm in front of the camera
    var translation = SCNMatrix4.CreateTranslation(0, 0, 0.1f);
    var cameraTranslation = currentFrame.Camera.Transform.ToSCNMatrix4();
    planeNode.Transform = SCNMatrix4.Mult(cameraTranslation, translation);
}

我已经在iPhone XS和7上尝试过此功能,我是否缺少明显的东西?

1 个答案:

答案 0 :(得分:0)

该示例为您更改了节点的变换。如果要更改节点的位置,可以尝试以下代码:

var tapLocation = gestureRecognize.LocationInView(SceneView);
var hitTestResults = SceneView.HitTest(tapLocation, ARHitTestResultType.EstimatedHorizontalPlane);

var hitTestResult = hitTestResults.FirstOrDefault();
if (hitTestResult == null) return;
var position = new SCNVector3(hitTestResult.WorldTransform.Column3.X, hitTestResult.WorldTransform.Column3.Y, hitTestResult.WorldTransform.Column3.Z);
planeNode.Position = position;