Search code examples
androidarcore

ARCore - Displaying 3D Model over a plane


I've successfully displayed a 3D Model using ARCore, but how would I place the model directly on top of a plane?

enter image description here

     mArFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ar_fragment);
     mArFragment.setOnTapArPlaneListener((hitResult, plane, motionEvent) -> {
     Anchor anchor = hitResult.createAnchor();

     ModelRenderable.builder()
           .setSource(this, Uri.parse("ArcticFox_Posed.sfb"))
           .build()
           .thenAccept(modelRenderable -> addModelToScene(anchor, modelRenderable))
           .exceptionally(throwable -> {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage(throwable.getMessage())
                    .show();
                    return null;
               });
           });

Solution

  • You can make a plane renderable with ShapeFactory and make your fox renderable a child of a plane object like this :

    private fun addModelToScene(anchor: Anchor, renderable: Renderable) {    
        val anchorNode = AnchorNode(anchor)
        val node = TransformableNode(fragment.transformationSystem)
        node.renderable = renderablePlane //create plane renderable
        node.setParent(anchorNode)
        val nodeFox = Node()
        nodeFox.renderable = renderable
        //edit localPosition to fit on the plane with nodeFox.localPosition
        nodeFox.setParent(node)
        fragment.arSceneView.scene.addChild(anchorNode)
    }