简体   繁体   中英

How to dynamically change the image rendered using RajawaliVR

public class MyRenderer extends RajawaliCardboardRenderer 
{
    public MyRenderer(Context context) 
    {
        super(context);
    }

    @Override
    public void initScene() {
    Log.d("debug1","initScene()");
    Sphere sphere = createPhotoSphereWithTexture(new Texture("photo",R.drawable.image));
    getCurrentScene().addChild(sphere);
    getCurrentCamera().setPosition(Vector3.ZERO);
    getCurrentCamera().setFieldOfView(75);
}

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;
  }
}

Currently there is a fixed image that is preloaded in the RajawaliVR library. The method that is used to set the image in called just once at the beginning. I want to change the image on will. Anyone who is familiar with using the rajawaliVR library will know what I am asking ,Thanks in advance.

Got the solution, you can dynamically change the image texture of your object say on some external trigger then you can use this code sample.
You can call the changeImage method whenever a triggered is fired. Dont forget to declare the method changeImage in RajawaliCardboardRenderer. Call the changeImage method on MyRenderer object.

public class MyRenderer extends RajawaliCardboardRenderer 
    {
       public MyRenderer(Context context) 
       {
          super(context);
       }  

    @Override
    public void initScene() {
    Log.d("debug1","initScene()");
    Sphere sphere = createPhotoSphereWithTexture(new Texture("photo",R.drawable.image));
    getCurrentScene().addChild(sphere);
    getCurrentCamera().setPosition(Vector3.ZERO);
    getCurrentCamera().setFieldOfView(75);
    }

    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;
    }

    public void changeImage()
    {
       Log.d("debug1", "" + getCurrentScene().getNumChildren());
        ArrayList<Object3D> objectList = getCurrentScene().getChildrenCopy();
        Material material = objectList.get(0).getMaterial();
        for (ATexture texture : material.getTextureList())
        {
            material.removeTexture(texture);
            texture = null;
        }

        Texture t = new Texture("sphereTexture",R.drawable.newImage);
        t.shouldRecycle(true);
              try {
                  material.addTexture(t);
              }
              catch (Exception e){e.printStackTrace();}

    }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM