1

我正在使用 Libgdx。我想在我的游戏中使用 pixmap 模拟雾,但是在生成"fogless"圆圈时遇到了问题。首先,我制作了一个像素图,用黑色填充(它有点透明)。填充后我想在上面画一个实心圆,但结果不是我预期的。

this.pixmap = new Pixmap(640, 640, Format.LuminanceAlpha);
Pixmap.setBlending(Blending.None); // disable Blending
this.pixmap.setColor(0, 0, 0, 0.9f);
this.pixmap.fill();
//this.pixmap.setColor(0, 0, 0, 0);
this.pixmap.fillCircle(200, 200, 100);
this.pixmapTexture = new Texture(pixmap, Format.LuminanceAlpha, false);

在程序render()

public void render() {
  mapRenderer.render();
  batch.begin();
  batch.draw(pixmapTexture, 0, 0);
  batch.end();
}

如果我使用格式。Alpha 在创建像素图和纹理时,我没有看到更半透明的圆圈。

这是我的问题:

在此处输入图像描述

问题

有人可以帮我吗?我应该怎么做,我应该在绘制一个完全透明的圆圈之前初始化什么?谢谢。

更新 我找到了我的问题的答案。我必须禁用混合以避免问题。


现在我的代码:

FrameBuffer fbo = new FrameBuffer(Format.RGBA8888, 620, 620, false);
Texture tex = EnemyOnRadar.assetManager.get("data/sugar.png", Texture.class);

batch.begin();
// others
batch.end();

fbo.begin();
batch.setColor(1,1,1,0.7f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw( tex, 100, 100);
batch.end();
fbo.end();

但我没有看到圆圈(这是一个 png 图像,代表透明 bg,白色填充圆圈)。

4

1 回答 1

2

我不确定这是否适合您,但我只是分享一下:您可以使用FrameBuffers 并执行以下操作:

  1. 在屏幕上绘制您想要绘制的所有内容。
  2. 结束你的 SpriteBatch 并开始你的 FrameBuffer,重新开始SpriteBatch
  3. 绘制雾,它用黑色、不透明的颜色填充整个“屏幕”(FrameBuffer)。
  4. 在要删除雾的位置绘制“无雾”圆圈为白色圆圈。
  5. FrameBuffers alpha 通道(透明度)设置为0.7或类似的东西。
  6. 结束SpriteBatch和 将FrameBuffer其绘制到屏幕上。

怎么了?您绘制正常场景,没有雾。您创建一个“虚拟屏幕”,用黑色填充它并用白色圆圈覆盖黑色。现在,您为这个“虚拟屏幕”设置了透明度,并用它透支了您的真实屏幕。白色圆圈下方的屏幕部分似乎很亮,而黑色的其余部分使您的场景更暗。阅读内容:带有 libgdx 的 2D Fire 效果,或多或少与雾相同。我的问题:没有box2d的Libgdx照明

编辑:另一个教程

让我知道它是否有帮助!

编辑:一些伪代码:在创建中:

fbo = new FrameBuffer(Format.RGBA8888, width, height, false);

在渲染中:

fbo.begin();
glClearColor(0f, 0f, 0f, 1f);    // Set the clear color to black, non transparent
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);  // Clear the "virtual screen" with the clear color
spriteBatch.begin();  // Start the SpriteBatch
// Draw the filled circles somehow // Draw your Circle Texture as a white, not transparent Texture
spriteBatch.end(); // End the spritebatch
fbo.end(); // End the FrameBuffer
spriteBatch.begin();   // start the spriteBatch, which now draws to the real screen
// draw your textures, sprites, whatever
spriteBatch.setColor(1f, 1f, 1f, 0.7f); // Sets a global alpha to the SpriteBatch, maybe it applies alo to the stuff you have allready drawn. If so just call spriteBatch.end() before and than spriteBatch.begin() again.
spriteBatch.draw(fbo, 0, 0);  // draws the FBO to the screen.
spriteBatch.end();

告诉我它是否有效

于 2014-02-06T13:28:33.447 回答