我在这样的纸板中获得了带有头部跟踪的 360 度视频。
使用RajawaliVRRenderer扩展您的课程
public class YoureVideoClass extends RajawaliVRRenderer {
接下来使用以下 initScene():
@Override
public void initScene() {
// setup sphere
Sphere sphere = new Sphere(1, 100, 100);
sphere.setPosition(0, 0, 0);
// invert the sphere normals
// factor "1" is two small and result in rendering glitches
sphere.setScaleX(100);
sphere.setScaleY(100);
sphere.setScaleZ(-100);
//initiate MediaPlayer
mediaPlayer = new MediaPlayer();
mediaPlayer.setLooping(true);
try {
Uri videoUrl = Uri.parse("android.resource://" + getContext().getPackageName() + "/"
+ R.raw.test);
mediaPlayer.setDataSource(getContext(),Uri.parse(videoUrl));
mediaPlayer.prepareAsync(); //prepare the player (asynchronous)
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start(); //start the player only when it is prepared
}
});
} catch (IOException e) {
e.printStackTrace();
}
// create texture from media player video
videoTexture = new StreamingTexture("video", mediaPlayer);
// set material with video texture
Material material = new Material();
material.setColorInfluence(0f);
try {
material.addTexture(videoTexture);
} catch (ATexture.TextureException e){
throw new RuntimeException(e);
}
sphere.setMaterial(material);
// add sphere to scene
getCurrentScene().addChild(sphere);
super.initScene();
}
启动场景后,您希望在每次渲染时更新 videoTexture,因此添加:
@Override
public void onRender(long elapsedRealTime, double deltaTime) {
super.onRender(elapsedRealTime, deltaTime);
if (videoTexture != null) {
// update texture from video content
videoTexture.update();
}
}
最后但并非最不重要的一点是,您要更新相机以跟踪纸板的运动,如下所示:
@Override
public void onDrawEye(Eye eye) {
// Rajawali camera
org.rajawali3d.cameras.Camera currentCamera = getCurrentCamera();
// cardboard field of view
FieldOfView fov = eye.getFov();
// update Rajawali camera from cardboard sdk
currentCamera.updatePerspective(fov.getLeft(), fov.getRight(), fov.getBottom(), fov.getTop());
eyeMatrix.setAll(eye.getEyeView());
// orientation
eyeOrientation.fromMatrix(eyeMatrix);
currentCamera.setOrientation(eyeOrientation);
// position
eyePosition = eyeMatrix.getTranslation().inverse();
currentCamera.setPosition(eyePosition);
// render with Rajawali
super.onRenderFrame(null);
}
此外,要将其他一些侦听器转发到 carboard sdk,您将需要以下函数:
@Override
public void onSurfaceChanged(int width, int height) {
// tell Rajawali that cardboard sdk detected a size change
super.onRenderSurfaceSizeChanged(null, width, height);
}
@Override
public void onSurfaceCreated(EGLConfig eglConfig) {
// pass opengl config to Rajawali
super.onRenderSurfaceCreated(eglConfig, null, -1, -1);
}
@Override
public void onRenderSurfaceDestroyed(SurfaceTexture surface) {
super.onRenderSurfaceDestroyed(surface);
}