2014-02-06 26 views
1

我正在使用Libgdx。 我想使用pixmap在我的遊戲中模擬霧,但在生成"fogless"圈時遇到問題。首先,我製作一個像素圖,充滿黑色(它透明一點點)。填充後,我想繪製一個實心的圓圈,但結果並不是我所期望的。在libgdx中使用filledcircle和pixmap

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); 

在程序渲染()

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

如果我使用的格式。 Alpha創建Pixmap和紋理時,我都看不到更加半透明的圓。

這裏是我的問題:

enter image description here

Problem

有人能幫助我嗎?我該怎麼做,我應該先畫出一個完整的透明圓圈?謝謝。

UPDATE 我找到了我的問題的答案。我必須禁用混合以避免該問題。現在


我的代碼:

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,白色實心圓)。

+0

我更新我的回答看看。如果您對此有任何疑問,請更新此問題,而不是發佈答案。如果你嘗試我給你的代碼,但它不起作用更新問題,粘貼代碼並讓我知道輸出是什麼。所以我和其他人可以幫助你。謝謝 – Springrbua

+0

另一個鏈接:http://stackoverflow.com/questions/14729961/ambiguous-results-with-frame-buffers-in-libgdx – Springrbua

+0

你現在怎麼做?你是否在使用PixMap的混合功能被禁用?或者你使用我建議的FBO解決方案嗎? – Springrbua

回答

2

我不知道這對你的作品,但我只是分享: 你可以使用FrameBuffer S和做到以下幾點:

  1. 畫你想在屏幕上繪製的一切。
  2. 結束您的SpriteBatch並開始您的FrameBuffer,再次開始SpriteBatch
  3. 繪製霧,填充整個「屏幕」(FrameBuffer)與黑色,不透明的顏色。
  4. 在您想要刪除霧的位置繪製「無霧」圓圈作爲白色圓圈。
  5. 設置FrameBuffer的alpha通道(透明度)爲0.7或類似的東西。
  6. 結束SpriteBatchFrameBuffer將其繪製到屏幕上。

會發生什麼情況?你畫出正常的場景,沒有霧。你創建一個「虛擬屏幕」,用黑色填充它,並用白色圓圈覆蓋黑色。現在你爲這個「虛擬屏幕」設置一個透明度,並用它透支你的真實屏幕。白色圓圈下方的屏幕部分看起來很亮,而黑色的休息會讓你的場景更暗。 看的東西:2D Fire effect with libgdx,或多或少像霧一樣。 我的問題是:Libgdx lighting without box2d

編輯:另一個Tutorial

讓我知道它是否有幫助!

編輯:一些僞代碼: 在創建:

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(); 

告訴我,如果它的工作原理

+0

你能給我寫一個示例代碼如何繪製到FrameBuffer上嗎?我以前從未使用過它。順便說一句,我可以忘記PixMap嗎? – user2498846

+0

其實我不得不說我也從來沒有使用過FrameBuffer因爲我也是新手,但是我閱讀了很多教程:P我更新了我的答案!我不知道pixmap是否有可能......讓我們希望別人有你的建議。 – Springrbua

+0

這是不行的。事實上,如果它可以工作,並且有兩個或更多個圓圈並且它們彼此重疊,那麼它們的阿爾法就是總結。所以我不知道,我該怎麼做。 – user2498846