我正在使用Libgdx创建一个RPG风格的游戏。我正在尝试调整教程中的代码 - " Lightmap来实现很酷的2D火焰效果" (这是我正在关注的tutorial的链接)。我遇到的问题是" light"周围有一个黑色矩形。如果有人能够指出我的错误,那将非常有帮助。
如果你指的是我所包含的链接教程,我只对lastShader感兴趣,它包含一个着色器中的夜间效果和灯光效果。
public class WorldRenderer {
public enum ShaderSelection{
Default,
Ambiant,
Light,
Final
};
//variables
private OrthogonalTiledMapRenderer renderer;
public OrthographicCamera camera;
ShapeRenderer debugRenderer = new ShapeRenderer();
private Texture light;
private FrameBuffer fbo;
//entities
private Player player;
private monsterSmall sMonster;
//Tilemap
private TiledMapTileLayer collision;
private TiledMap map;
private int tileMapWidth;
private int tileMapHeight;
private int tileSize;
//shading
private ShaderSelection shaderSelection = ShaderSelection.Final;
private ShaderProgram currentShader;
private ShaderProgram defaultShader;
private ShaderProgram finalShader;
//values passed to the shader
public static final float ambientIntensity = .7f;
public static final Vector3 ambientColor = new Vector3(0.3f, 0.3f, 0.7f);
//read our shader files
FileHandle vtxShaderFile = Gdx.files.internal("shaders/vertexShader.glsl");
FileHandle finalShaderFile = Gdx.files.internal("shaders/pixelShader.glsl");
FileHandle defaultFile = Gdx.files.internal("shaders/defaultPixelShader.glsl");
//set the shader files
private final String vertexShader = vtxShaderFile.readString();
private final String finalPixelShader = finalShaderFile.readString();
private final String defaultPixelShader = defaultFile.readString();
public WorldRenderer() {
map = new TmxMapLoader().load("maps/map1.tmx");
tileMapWidth = (Integer) map.getProperties().get("width");
tileMapHeight = (Integer) map.getProperties().get("height");
tileSize = (Integer) map.getProperties().get("tilewidth");
renderer = new OrthogonalTiledMapRenderer(map);
player = new Player(new Vector2(200, 100));
sMonster = new monsterSmall(new Vector2(300, 100));
camera = new OrthographicCamera();
camera.viewportWidth = Gdx.graphics.getWidth()/2;
camera.viewportHeight = Gdx.graphics.getHeight()/2;
//setting up the night shader
ShaderProgram.pedantic = false;
defaultShader = new ShaderProgram(vertexShader, defaultPixelShader);
finalShader = new ShaderProgram(vertexShader, finalPixelShader);
setShader(shaderSelection);
finalShader.begin();
finalShader.setUniformi("u_lightmap", 1);
finalShader.setUniformf("ambientColor", ambientColor.x, ambientColor.y,
ambientColor.z, ambientIntensity);
finalShader.end();
fbo = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
finalShader.begin();
finalShader.setUniformf("resolution", Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
finalShader.end();
light = new Texture("sfx/light.png");
}
public void render (float delta) {
camera.update();
renderer.setView(camera);
player.render(delta);
sMonster.render(delta);
handleInput();
cameraSetUp();
fbo.begin();
renderer.getSpriteBatch().setProjectionMatrix(camera.combined);
renderer.getSpriteBatch().setShader(defaultShader);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.getSpriteBatch().begin();
float lightSize = 200f;
renderer.getSpriteBatch().draw(light,
player.position.x +16 - lightSize*0.5f + 0.5f ,
player.position.y +16 + 0.5f - lightSize*0.5f,
lightSize,
lightSize);
renderer.getSpriteBatch().end();
fbo.end();
//draw the actual scene
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.getSpriteBatch().setProjectionMatrix(camera.combined);
renderer.getSpriteBatch().setShader(currentShader);
renderer.getSpriteBatch().begin();
fbo.getColorBufferTexture().bind(1); //this is important! bind the FBO to the 2nd texture unit
light.bind(0);
renderer.getSpriteBatch().end();
renderer.render();
renderer.getSpriteBatch().setProjectionMatrix(camera.combined);
//set the night shader to the sprite batch
renderer.getSpriteBatch().begin();
renderer.getSpriteBatch().draw(player.currentFrame, player.position.x, player.position.y);
renderer.getSpriteBatch().end();
}
/**
* To set the current shader that is being used.
* @param ss
*/
public void setShader(ShaderSelection ss){
shaderSelection = ss;
if(ss == ShaderSelection.Final){
currentShader = finalShader;
}else{
ss = ShaderSelection.Default;
currentShader = defaultShader;
}
}
//我调用渲染器的屏幕
public class GameScreen implements Screen {
private WorldRenderer renderer;
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 0);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
renderer.render(delta);
}
@Override
public void resize(int width, int height) {
}
@Override
public void show() {
renderer = new WorldRenderer();
}
@Override
public void hide() {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
}