Reputation: 29
i want to show only one object at a time on an achor. Currently i'm getting my AR model from a server at runtime and showing my model. when i tap on anchor pin points every time i got the new AR model so i have to many model after some taps(equals to number of taps). i want restrict the model at once. every time when user taps on the screen ar model always shows only one object at a time.
public class ARObjectActivity extends AppCompatActivity {
private ArFragment arFragment;
private String Asset_3D = "";
String imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a_r_object);
arFragment=(ArFragment) getSupportFragmentManager().findFragmentById(R.id.arFragment);
Intent intent = getIntent();
imageUri = intent.getStringExtra("arImageUri");
arFragment.setOnTapArPlaneListener((hitResult, plane, motionEvent) ->
placeModel(hitResult.createAnchor()));
}
private void placeModel(Anchor anchor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
ModelRenderable.builder()
.setSource(ARObjectActivity.this, RenderableSource.builder()
.setSource(ARObjectActivity.this, Uri.parse(imageUri),
RenderableSource.SourceType.GLTF2)
.setScale(0.5f)
.setRecenterMode(RenderableSource.RecenterMode.ROOT)
.build()
)
.setRegistryId(Asset_3D)
.build()
.thenAccept(modelRenderable -> addNoteToScene(modelRenderable,anchor))
.exceptionally(throwable -> {
AlertDialog.Builder builder = new AlertDialog.Builder(ARObjectActivity.this);
builder.setMessage(throwable.getMessage()).show();
return null;
});
}
}
private void addNoteToScene(ModelRenderable modelRenderable, Anchor anchor) {
AnchorNode anchorNode = new AnchorNode(anchor);
//anchorNode.setRenderable(modelRenderable);
anchorNode.setParent(arFragment.getArSceneView().getScene());
// arFragment.getArSceneView().getScene().addChild(anchorNode);
// Create the transformable andy and add it to the anchor.
TransformableNode node = new TransformableNode(arFragment.getTransformationSystem());
//set rotation in direction (x,y,z) in degrees 90
node.setLocalRotation(Quaternion.axisAngle(new Vector3(1f, 1f, 1f), 90f));
node.setParent(anchorNode);
node.setRenderable(modelRenderable);
node.select();
}
}
Upvotes: 0
Views: 225
Reputation: 542
Add this new method
private void clearAllModel() {
List<Node> children = new ArrayList<Node>(arFragment.getArSceneView().getScene().getChildren());
for (Node node : children) {
if (node instanceof AnchorNode) {
AnchorNode tmp = (AnchorNode) node;
if (tmp.getAnchor() != null) {
tmp.getAnchor().detach();
}
}
}
}
Then call it at
arFragment.setOnTapArPlaneListener((hitResult, plane, motionEvent) -> {
clearAllModel();
placeModel(hitResult.createAnchor()));
}
Explanation:
as you can see, everytime you tap and placeModel
into addNoteToScene
is called, there is this line of code
anchorNode.setParent(arFragment.getArSceneView().getScene());
all your existing model (which is on anchorNode) is under this arFragment.getArSceneView().getScene()
as a child
with my method suggestion, the first time there is 0 child (null). So it will move on and just do placeModel
and then it has 1 child.
On second time it remove that 1 child, and placeModel
is called again, and make it 1 child again. And so on ... so, there will be only 1 child (model)
Upvotes: 2