如何在LibGDX中绘制纹理?

时间:2017-05-11 01:40:14

标签: java libgdx textures

我正在用libgdx制作一个游戏,其中主要角色有一个灯光,而光线未到达它应该是黑暗的,我正在使用SpriteBash中的setBlendFunction来模拟灯光。我的问题是我不知道如何在光线质地周围做得很暗,我可以根据整个屏幕调整光线质量,但这样会很邋and并且在代码中不方便。有没有人有任何想法?谢谢你。

以下是显示我的问题的游戏图片

enter image description here

1 个答案:

答案 0 :(得分:0)

这通常使用FrameBuffer完成。

您希望在resize()方法中实例化并重新创建,以便它始终与屏幕/窗口的大小相匹配。在resize()

if (frameBuffer != null && (frameBuffer.getWidth() != width || frameBuffer.getHeight() != height)){
    frameBuffer.dispose();
    frameBuffer = null;
}
if (frameBuffer == null){
    try {
        frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
    } catch (GdxRuntimeException e){ // device doesn't support 8888
        frameBuffer = new FrameBuffer(Pixmap.Format.RGB565, width, height, false);
    }
}

render()中,在游戏中绘制任何内容之前,首先在frameBuffer上绘制阴影贴图:

frameBuffer.begin();
Gdx.gl.glClearColor(/* ... */); // This will be your ambient color, a dark color, like gray
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

spriteBatch.setProjectionMatrix(camera.combined); // same camera as your game scene
spriteBatch.setBlendFunction(GL20.GL_ONE, GL20.GL_ONE); //additive blending
spriteBatch.begin();

// draw light sprites, such as a white circle where your character is standing

spriteBatch.end();
frameBuffer.end();

然后正常绘制你的游戏场景。在绘制之前,不要忘记将混合函数更改回您通常使用的函数(默认为GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)。最后,在游戏中绘制阴影贴图:

// Use identity matrix so you don't have to worry about lining up the frame buffer texture
spriteBatch.setProjectionMatrix(spriteBatch.getProjectionMatrix().idt());

// Set multiplicative blending for shadows
spriteBatch.setBlendFunction(GL20.GL_ZERO, GL20.GL_SRC_COLOR); 

spriteBatch.begin();
spriteBatch.draw(frameBuffer.getColorBufferTexture(), -1, 1, 2, -2); //dimensions for full screen with identity matrix
spriteBatch.end();