Sceneform - How to add a Sphere Renderable?

Viewed 200

I have a ModelRenderable (The main node) created and I'm trying to attach Sphere Renderables (Child nodes) using LocalPosition on the Main node to ensure that they maintain their position relative to the Main node.

The problem I'm coming across is that the child nodes are being placed in the wrong position (from what I'm expecting) when getting the HitTestResult from clicking on the back of the Main node.

Here is a gif demonstration:

enter image description here

Creating the Main Renderable:

private void createAndyRenderable(Node node) {
        ModelRenderable.builder()
                .setSource(this, Uri.parse(OBJ_NAME))
                .build()
                .thenAccept(modelRenderable -> {
                    node.setRenderable(modelRenderable);
                    node.setLocalPosition(NODE_STARTING_LOCAL_POSITION);
                })
                .exceptionally(throwable -> {
                    Toast toast = Toast.makeText(this, "Unable to load renderable" 
                            + throwable, Toast.LENGTH_LONG);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                    return null;
                });
    }

Attaching an on Tap Handler:

private void addOnTapHandler() {
        mPersonNode.setOnTapListener((HitTestResult hitTestResult, MotionEvent motionEvent) -> {

            Log.d(TAG, "addOnTapHandler: " + hitTestResult.getPoint());

            Color color = new Color(android.graphics.Color.parseColor("#FF7390"));

            MaterialFactory.makeOpaqueWithColor(this, color)
                    .thenAccept(material -> {
                        Renderable sphere = ShapeFactory.makeSphere(0.05f, Vector3.zero(), material);

                        Node node = new Node();
                        node.setParent(hitTestResult.getNode());
                        node.setLocalPosition(hitTestResult.getPoint());
                        node.setRenderable(sphere);

                    });
        });
    }

Using the code in OnCreate:

mScene = sceneView.getScene();
mPersonNode = new Node();
createAndyRenderable(mPersonNode);
mScene.addChild(mPersonNode);
addOnTapHandler();

Edit: Code for rotate button

@OnClick(R.id.rotate)
    public void rotate() {
        if (mCurrentNodeRotatedAngle == 180f) {
            Log.d(TAG, "rotate: Called 1 " + mCurrentNodeRotatedAngle);
            mCurrentNodeRotatedAngle = 0f;
            mPersonNode.setLocalRotation(Quaternion.axisAngle(new Vector3(0f, 1f, 0f), mCurrentNodeRotatedAngle));
        } else {
            Log.d(TAG, "rotate: Called 2 " + mCurrentNodeRotatedAngle);
            mCurrentNodeRotatedAngle = 180f;
            mPersonNode.setLocalRotation(Quaternion.axisAngle(new Vector3(0f, 1f, 0f), mCurrentNodeRotatedAngle));

        }

    }
mCurrentPosition = camera.getWorldPosition();
0 Answers
Related