ARCORE:通过单击特定可渲染对象来删除该可渲染对象 [英] ARCORE: remove a specific renderable by clicking on this renderable
问题描述
我正在使用ARCore的Sceneform开发一个项目.我基于ARCore提供的HelloSceneform示例进行开发. 我想做的是按点击添加可渲染对象,然后在单击屏幕上的特定可渲染对象时将其删除. 我已经尝试过如下方法AnchorNode.setOnTapListener,但是它没有用(无响应):
I'm working on a project using Sceneform from ARCore. I develop it base on HelloSceneform example provided by ARCore. What I wanna do is adding a renderable object by a hit and then delete it when I click on the specific renderable on the screen. I've tried method AnchorNode.setOnTapListener as following, but it didn't work(no response):
anchorNode.setOnTapListener(new Node.OnTapListener() {
@Override
public void onTap(HitTestResult hitTestResult, MotionEvent motionEvent) {
if(anchorNode.getAnchor()!=null){
arFragment.getArSceneView().getScene().removeChild(anchorNode);
anchorNode.getAnchor().detach();
anchorNode.setParent(null);
}
}
});
我还尝试了以下方法,该方法会导致意外关闭:
I also tried the following method, which causes unexpected close:
Scene scene = arFragment.getArSceneView().getScene();
scene.addOnPeekTouchListener(new Scene.OnPeekTouchListener() {
@Override
public void onPeekTouch(HitTestResult hitTestResult, MotionEvent motionEvent) {
Node node = hitTestResult.getNode();
node.setParent(null);
}
});
是否有任何方法可以实现此功能?
Is there any method could achieve this feature?
推荐答案
下面的代码应该可以检测到触摸并删除该节点.
The code below should detect the touch and delete the node.
如果您希望有一个单独的按钮来删除所选节点,则可以添加常规按钮和侦听器,只需使用'touch'事件选择要删除的节点即可.
If you want to have a separate button to delete a selected node you can add a regular button and listener and just use the 'touch' event to select the node you want to delete.
private void handleOnTouch(HitTestResult hitTestResult, MotionEvent motionEvent) {
Log.d(TAG,"handleOnTouch");
// First call ArFragment's listener to handle TransformableNodes.
arFragment.onPeekTouch(hitTestResult, motionEvent);
//We are only interested in the ACTION_UP events - anything else just return
if (motionEvent.getAction() != MotionEvent.ACTION_UP) {
return;
}
// Check for touching a Sceneform node
if (hitTestResult.getNode() != null) {
Log.d(TAG,"handleOnTouch hitTestResult.getNode() != null");
Node hitNode = hitTestResult.getNode();
if (hitNode.getRenderable() == andyRenderable) {
Toast.makeText(LineViewMainActivity.this, "We've hit Andy!!", Toast.LENGTH_SHORT).show();
arFragment.getArSceneView().getScene().removeChild(hitNode);
AnchorNode hitNodeAnchor = (AnchorNode) hitNode;
if (hitNodeAnchor != null) {
hitNode.getAnchor().detach();
}
hitNode.setParent(null);
hitNode = null;
}
}
}
以上内容摘自VR测试应用程序的各个部分,并在此处组合成一个简洁的示例-完整的应用程序源可在此处找到: https://github.com/mickod/LineView
The above is extracted from various parts of a VR test application and combined here for a concise example - the full working application source is available here: https://github.com/mickod/LineView
更新-Kotlin版本(于2020年4月测试):
Update - Kotlin version (tested April 2020):
private fun removeAnchorNode(nodeToRemove: AnchorNode) {
//Remove an Anchor node
arFragment.getArSceneView().getScene().removeChild(nodeToRemove);
nodeToRemove.getAnchor()?.detach();
nodeToRemove.setParent(null);
nodeToRemove.renderable = null
}
这篇关于ARCORE:通过单击特定可渲染对象来删除该可渲染对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!