如何动态更改使用 RajawaliVR 渲染的图像
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;
}
}
目前在 RajawaliVR 库中预加载了一个固定图像。
用于设置图像的方法在开始时只调用一次。
我想随意更改图像。任何熟悉使用 rajawaliVR 库的人都会知道我在问什么,提前致谢。
得到解决方案,您可以动态更改对象的图像纹理,比如在某些外部触发器上,然后您可以使用此代码示例。
只要触发器被触发,您就可以调用 changeImage 方法。
不要忘记在 RajawaliCardboardRenderer 中声明方法 changeImage。
在 MyRenderer 对象上调用 changeImage 方法。
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();}
}
}
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;
}
}
目前在 RajawaliVR 库中预加载了一个固定图像。 用于设置图像的方法在开始时只调用一次。 我想随意更改图像。任何熟悉使用 rajawaliVR 库的人都会知道我在问什么,提前致谢。
得到解决方案,您可以动态更改对象的图像纹理,比如在某些外部触发器上,然后您可以使用此代码示例。
只要触发器被触发,您就可以调用 changeImage 方法。
不要忘记在 RajawaliCardboardRenderer 中声明方法 changeImage。
在 MyRenderer 对象上调用 changeImage 方法。
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();}
}
}