2D瓷砖照明瓷砖

2023-09-07 13:33:06 作者:吾喜我

我添加灯光到我的基于XNA 2D瓷砖游戏。

I'm adding lighting to my XNA 2D tile based game.

我发现这文章有用,但这样的做了不支持的碰撞。我想是一种方法做以下

I found this article useful, but the way its done it does not support collision. What I'd like is a method to do the following

让常亮点 冲突(如果光线击中块,然后调暗以任何数量的下一块,直到其黑暗模拟阴影)

我一直在寻找有很长一段时间,但没有运气(我没有找到克特林的教程,但它似乎有点提前对我来说,并不适用于瓷砖以及由于重绘整个游戏的每个点)

I've been searching around for quite a while but no luck (I did find Catalin's tutorial, but it seemed a bit advanced for me, and didn't apply to tiles well due to redrawing the entire game for each point)

推荐答案

我会申请一个柔和的灯光效果,一个2D的tile网格分享我的方法。 ClassicThunder的答案提供了阴影一个很好的链接。

I'll share my method for applying a smooth lighting effect to a 2D tile grid. ClassicThunder's answer provides a nice link for shadows.

首先,我们需要计算每一瓦片后面将模糊的照明值。让我说明这是如何工作之前,我进入了code。

First off, we will need to calculate the lighting values of each tile which will be blurred later. Let me illustrate how this works before I get into the code.

Basicly我们做的是遍历所有的瓷砖,从顶部开始,如果瓷砖是空的, CurrentLight 变量设置为最大亮度,如果发现实瓦,将其设置为 CurrentLight 变量,然后从 CurrentLight 减去一个absorbsion量。这样一来,我们的下一个坚实的砖,当我们设置平铺到 CurrentLight 的价值,这将是稍显不足。重复这一过程,直到该数组迭代

Basicly what we do is loop through all the tiles, starting from the top, if a tile is blank, set the CurrentLight variable to max brightness, if we find a solid tile, set it as the CurrentLight variable and subtract an "absorbsion" amount from the CurrentLight. This way, on our next solid tile, when we set the tile to the CurrentLight value, it will be slightly less. This process is repeated until the array is iterated.

现在将有一个很好的自上而下的照明效果,但不是很大。我们必须重复此过程3次以上,为下到上,从左到右,或者从右到左。它可以重复更多次的更好的质量。

Now there will be a nice top to bottom lighting effect, but it isn't that great. We must repeat this process 3 more times, for bottom to top, left to right, and right to left. And it can be repeated more times for better quality.

基本上运行在每瓦这个code在环

Basically running this code on every tile in the loop

if (tile.Light > CurrentLight) //If this tile is brighter than the last, set the current light to the tiles light
    CurrentLightR = tile.Light;
else if (CurrentLight != 0f) //If it is less, and isnt dark, then set the tile to the current light
    tile.Light = CurLightR;
if (tile.Light == CurLightR) //If it is the same, subtract absorb values
    CurrentLight -= tile.Absorb;

和你去那里,漂亮的瓷砖照明。但是,如果你想要一个不那么像素化的样子,你可以看看我的question在gamedev 为。

And there you go, nice tile lighting. However if you want a less "pixelized" look, you can check out my question on gamedev for that.