如果你将光纹理与场景相乘,你会使该区域变暗,而不是变亮。
你可以尝试使用加法混合渲染;这样做看起来不太对,但很容易实现,可能是可以接受的。你需要使用相当低的 alpha 值来绘制光纹理,以避免过度饱和图像的那一部分。
另一种更复杂的照明方法是将所有光纹理(用于场景中所有灯光)以加法方式绘制到第二个渲染目标上,然后将此纹理与场景相乘。这应该会给出更加逼真的照明效果,但性能开销更大且更为复杂。
初始化:
RenderTarget2D lightBuffer = new RenderTarget2D(graphicsDevice, screenWidth, screenHeight, 1, SurfaceFormat.Color);
Color ambientLight = new Color(0.3f, 0.3f, 0.3f, 1.0f);
绘制:
graphicsDevice.SetRenderTarget(0, lightBuffer);
graphicsDevice.Clear(ambientLight)
spriteBatch.Begin(SpriteBlendMode.Additive);
foreach (light in lights)
spriteBatch.Draw(lightFadeOffTexture, light.Area, light.Color);
spriteBatch.End();
graphicsDevice.SetRenderTarget(0, null);
DrawScene();
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();