Search code examples
androidandroid-studiorajawali

Adding clickable 3DObject Rajawali Cardboard


I'm trying to add a simple Sphere Object to my VR app using Rajawali Libraries. My app should show a simple View ( with RajawaliCardboardRenderer, a sphere with the texture of an images ). The question is : can I simply add a sphere or a 3DObject to that sphere which is gonna be clickable?

Here's my code :

    public class MyRenderer extends RajawaliCardboardRenderer {
    private Sphere sphere,sphere2;
    public MyRenderer(Context context) {
        super(context);
    }

    @Override
    protected void initScene() {

        sphere = createPhotoSphereWithTexture(new Texture("photo", R.drawable.panorama));

        sphere2 = new Sphere(24,24,24);

        Material m = new Material();
        m.setColor(0);
        try {
            m.addTexture(new Texture("photo",R.drawable.zvirbloniu_parkas));
        } catch ( ATexture.TextureException e) {
            e.printStackTrace();
        }
        sphere2.setMaterial(m);


        getCurrentScene().addChild(sphere);
        getCurrentScene().addChild(sphere2);

        getCurrentCamera().setPosition(Vector3.ZERO);
        getCurrentCamera().setFieldOfView(100);
    }

    @Override
    protected void onRender(long ellapsedRealtime, double deltaTime) {
        super.onRender(ellapsedRealtime, deltaTime);
        sphere2.rotate(Vector3.Axis.Y, 1.0);
    }

    private static Sphere createPhotoSphereWithTexture(ATexture texture) {

        Material material = new Material();
        material.setColor(0);

        try {
            material.addTexture(texture);
        } catch (ATexture.TextureException e) {
            throw new RuntimeException(e);
        }

        Sphere sphere = new Sphere(50, 64, 32);
        sphere.setScaleX(-1);
        sphere.setMaterial(material);

        return sphere;
    }
}

Don't look at the form of the code, it is bad-written, it doesn't follow any pattern!


Solution

  • You need to attach the sphere2 as a child of the "scenario" sphere, in order to display it inside the scenario, something like this:

     sphere = createPhotoSphereWithTexture(new Texture("photo", .drawable.panorama));
    
     sphere2 = new Sphere(24,24,24);
    
     ...
    
     sphere.addChild(sphere2);
     getCurrentScene().addChild(sphere);