簡體   English   中英

如何在SpriteBatch Draw XNA(2D)中相乘兩個精靈

[英]How to multiply two sprites in SpriteBatch Draw XNA (2D)

我正在為XNA 3.1中的動作RPG編寫簡單的十六進制引擎。 我想在暗黑破壞神II中照亮英雄和火炬附近的地面。 我最好這樣做的方法是計算視野,隱藏任何瓷磚及其播放器無法看到的內容,並在任何光源上繪制特殊的“光”紋理:黑色和白色的紋理,模糊的圓圈在它的中心。

我想將這個紋理與背景相乘(如混合模式:乘法),但是 - 不幸的是 - 我沒有看到在SpriteBatch中這樣做的選項。 有人能指出我正確的方向嗎?

或者也許還有其他 - 更好的方式來實現暗黑破壞神II中的照明模型?

如果要將光照紋理與場景相乘,則會使區域變暗 ,而不會使區域變亮。

您可以嘗試使用添加劑混合渲染; 這看起來不太合適,但很容易接受。 您將不得不使用相當低的alpha繪制光線,以獲得輕盈的紋理,而不僅僅是過度飽和圖像的那部分。

另一種更復雜的照明方式是將所有光紋理(對於場景中的所有光源)添加到第二個渲染目標上,然后將此紋理與場景相乘。 這應該提供更真實的照明,但是具有更大的性能開銷並且更復雜。

初始化:

RenderTarget2D lightBuffer = new RenderTarget2D(graphicsDevice, screenWidth, screenHeight, 1, SurfaceFormat.Color);
Color ambientLight = new Color(0.3f, 0.3f, 0.3f, 1.0f);

畫:

// set the render target and clear it to the ambient lighting
graphicsDevice.SetRenderTarget(0, lightBuffer);
graphicsDevice.Clear(ambientLight)

// additively draw all of the lights onto this texture. The lights can be coloured etc.
spriteBatch.Begin(SpriteBlendMode.Additive);
foreach (light in lights)
    spriteBatch.Draw(lightFadeOffTexture, light.Area, light.Color);
spriteBatch.End();

// change render target back to the back buffer, so we are back to drawing onto the screen
graphicsDevice.SetRenderTarget(0, null);

// draw the old, non-lit, scene
DrawScene();

// multiply the light buffer texture with the scene
spriteBatch.Begin(SpriteBlendMode.Additive, SpriteSortMode.Immediate, SaveStateMode.None);
graphicsDevice.RenderState.SourceBlend = Blend.Zero;
graphicsDevice.RenderState.DestinationBlend = Blend.SourceColor;
spriteBatch.Draw(lightBuffer.GetTexture(), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
spriteBatch.End();

據我所知,沒有使用自己的自定義着色器就無法做到這一點。

一個自定義着色器可以這樣工作:

  1. 將場景渲染為紋理
  2. 將燈光渲染到另一個紋理
  3. 作為空白四邊形的后期處理,對兩個紋理進行采樣,結果是場景紋理*光紋理。

這將輸出一個亮起的場景,但它不會做任何陰影。 如果你想要陰影,我建議你遵循Catalin Zima的優秀樣本

也許使用與BloomEffect組件中相同的技術可能是一個想法。

基本上,效果的作用是抓住渲染的場景,從場景中最亮的區域計算綻放圖像,模糊並將兩者結合起來。 結果是根據顏色突出顯示區域。

這里可以使用相同的方法。 這將更簡單,因為您不必基於背景計算綻放圖像,僅基於角色的位置。

您甚至可以進一步重復使用它來為其他光源提供高亮顯示,例如火把,魔法效果等等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM