3D的screen,仿照2D来写。
准备部分
3D场景基类接口
package sence;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Game;
public interface Sence3D {
/** Called when this screen becomes the current screen for a {@link Game}. */
public void show ();
/** Called when the screen should render itself.
* @param delta The time in seconds since the last render. */
public void render (float delta);
/** @see ApplicationListener#resize(int, int) */
public void resize (int width, int height);
/** @see ApplicationListener#pause() */
public void pause ();
/** @see ApplicationListener#resume() */
public void resume ();
/** Called when this screen is no longer the current screen for a {@link Game}. */
public void hide ();
/** Called when this screen should release all resources. */
public void dispose ();
}
自己的是实现方式
public class BaseScreen implements Sence3D {
protected InputMultiplexer multiplexer;
private CubeSolveInputProcessor camController;
protected Stage3D stage3D;
private Stage stage;
private Label fps;
protected CubeSolve solve;
public BaseScreen(CubeSolve cubeSolve){
this.solve = cubeSolve;
multiplexer = new InputMultiplexer();
camController = new CubeSolveInputProcessor(cubeSolve.getViewport3D().getCamera());
stage3D = new Stage3D(cubeSolve.getViewport3D(),cubeSolve.getModelBatch());
stage = new Stage(cubeSolve.getViewport(),cubeSolve.getHudBatch());
multiplexer.addProcessor(stage);
multiplexer.addProcessor(camController);
Gdx.input.setInputProcessor(multiplexer);
fps = new Label("",new Label.LabelStyle(){
{
font = new BitmapFont(Gdx.files.internal("Bahnschrift-Regular_40_1.fnt"),
Gdx.files.internal("Bahnschrift-Regular_40_1.png"),false);
}
});
fps.setPosition(30,Constant.height - 30, Align.topLeft);
stage.addActor(fps);
}
@Override
public void show() {
}
@Override
public void render(float delta) {
fps.setText(Gdx.graphics.getFramesPerSecond()+" :fps");
stage.act();
stage.draw();
stage3D.act();
stage3D.draw();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
public void addActor(Actor actor){
stage.addActor(actor);
}
public void addActor3d(Actor3D actor3D){
stage3D.addActor(actor3D);
}
}
那么首先我们相机怎么使用
和2D一样在Stage中写
public class Stage3D extends InputAdapter implements Disposable {
private Viewport3D viewport3D;
private ModelBatch batch;
private Group3D group3D;
private GameController controller;
public Stage3D(Viewport3D viewport3D, ModelBatch batch){
this.viewport3D = viewport3D;
this.batch = batch;
this.group3D = new Group3D();
controller = new GameController((PerspectiveCamera) viewport3D.getCamera());
}
public void act(){
checkTileTouched();
group3D.act(Gdx.graphics.getDeltaTime());
}
public void draw(){
batch.begin(viewport3D.getCamera());
group3D.draw(batch, viewport3D.getEnvironment());
batch.end();
}
public void addActor(Actor3D actor3D) {
group3D.addActor3d(actor3D);
}
public Group3D getRoot() {
return group3D;
}
@Override
public void dispose() {
}
// final Vector3 intersection = new Vector3();
private void checkTileTouched() {
if (Gdx.input.justTouched()) {
System.out.println("touch down :"+Gdx.input.getX()+" "+Gdx.input.getY());
Ray pickRay = viewport3D.getCamera().getPickRay(Gdx.input.getX(), Gdx.input.getY());
float distance = -1;
Actor3D actor3D = null;
for (Actor3D child : getRoot().getChildren()) {
float dist2 = child.hit1(pickRay);
if (dist2 >= 0f && (distance < 0f || dist2 <= distance)) {
actor3D = child;
distance = dist2;
}
}
}
}