我将分享我将平滑光照效果应用于 2D 平铺网格的方法。ClassicThunder的答案为阴影提供了一个很好的链接。
首先,我们需要计算每个图块的光照值,稍后将对其进行模糊处理。在进入代码之前,让我先说明一下它是如何工作的。

基本上我们所做的是循环遍历所有图块,从顶部开始,如果图块是空白的,则将变量设置CurrentLight为最大亮度,如果我们找到实心图块,则将其设置为CurrentLight变量并从“吸收”量中减去CurrentLight。这样,在我们的下一个实体图块上,当我们将图块设置为该CurrentLight值时,它会稍微小一些。重复这个过程直到数组被迭代。
现在将会有一个很好的从上到下的照明效果,但不是那么好。我们必须再重复此过程 3 次,从下到上、从左到右、从右到左。并且可以重复多次以获得更好的质量。
基本上在循环中的每个图块上运行此代码
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;
Run Code Online (Sandbox Code Playgroud)
就这样,漂亮的瓷砖照明。但是,如果您想要不太“像素化”的外观,您可以查看我在 gamedev 上的问题。