简体   繁体   English

基于网格的2D照明问题

[英]Grid-based 2D lighting problems

I am aware this question has been asked before, but unfortunately I am new to the language, so the complicated explanations I've found do not help me in the least. 我知道之前曾有人问过这个问题,但是很遗憾,我是该语言的新手,所以我发现的复杂解释至少对我没有帮助。

I need a lighting engine for my game, and I've tried some procedural lighting systems. 我的游戏需要照明引擎,并且我尝试了一些程序照明系统。 This method works the best: 此方法效果最好:

if (light[xx - 1, yy] > light[xx, yy]) light[xx, yy] = light[xx - 1, yy] - lightPass;
if (light[xx, yy - 1] > light[xx, yy]) light[xx, yy] = light[xx, yy - 1] - lightPass;
if (light[xx + 1, yy] > light[xx, yy]) light[xx, yy] = light[xx + 1, yy] - lightPass;
if (light[xx, yy + 1] > light[xx, yy]) light[xx, yy] = light[xx, yy + 1] - lightPass;

(Subtracts adjacent values by 'lightPass' variable if they are more bright) (It's in a for() loop) (如果相邻的值更明亮,则通过“ lightPass”变量减去它们)(在for()循环中)

This is all fine and dandy except for a couple obvious reasons: 除了几个明显的原因,这一切都很好,很花哨:

  1. The system favors whatever comes first, in this case the value to the left (x - 1) 系统优先选择最先出现的值,在这种情况下,左边的值(x-1)
  2. The system cannot replace lighter values with darker values when a light source is deleted 删除光源后,系统无法将较暗的值替换为较浅的值

This is what the above code looks like applied to my game: 这就是上面的代码应用于我的游戏的样子: 光

If I could get some help on creating a new procedural or otherwise lighting system I would really appreciate it! 如果我能在创建新的程序或照明系统方面获得帮助,我将不胜感激!

First off, I love your art style :) 首先,我爱你的艺术风格:)

Grid based lighting was a tough one I was searching for, eventually I found a method of doing this. 基于网格的照明是我一直在寻找的难点,最终我找到了一种实现此目的的方法。 Basically each block has a Light value, how much light it emits. 基本上每个块都有一个Light值,即它发出多少光。 and an Absorb value, how much light it absorbs. Absorb值,吸收多少光。

A blank block, during day, may have: 白天,一个空白块可能具有:

Light = 1; (Or Color.White for color)
Absorb = 15; //Light flows for 15 blocks through blank tiles

A dirt block, will have: 一个污垢块将具有:

Light = 0; (Or Color.Black for color) //Emits nothing
Absorb = 6; //Light flows for 6 blocks

With this you can easily configure any block. 这样,您可以轻松配置任何块。 For this example I will assume you have a tile array of some sort of class, say Tile , which contains a Block , with Light and Absorb 对于此示例,我假设您具有某种类的tile数组,例如Tile ,其中包含具有LightAbsorbBlock

I will also say, that each frame you must RESET all of the lighting to its default values (Blank to 1, dirt to 0) before you recalculate the lighting (Unless nothing has changed of course, this skip the reset and lighting) 我还要说,在重新计算照明之前,必须在每个帧中将所有照明重置为默认值(从空白变为1,将污垢重置为0)(除非没有任何变化,否则请跳过重置和照明)

Before I show the code, let me explain the steps needed to do this: 在显示代码之前,让我解释一下执行此操作所需的步骤:

Basically 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 . 基本上,我们要做的是从顶部开始循环浏览所有图块,如果图块为空白,则将CurrentLight变量设置为最大亮度,如果找到实心图块,则将其设置为CurrentLight变量并减去“吸收”量来自CurrentLight This way, on our next solid tile, when we set the tile to the CurrentLight value, it will be slightly less. 这样,在下一个实心图块上,当我们将图块设置为CurrentLight值时,它会稍小一些。 This process is repeated until the array is iterated. 重复此过程,直到迭代数组为止。

演示

Now that will create a top to bottom lighting effect, the loop must be repeated, right to left, left to right, and bottom to top. 现在将创建从上到下的照明效果,必须从右到左,从左到右以及从下到上重复循环。

The code used for each tile on the loop is: 用于循环上每个图块的代码是:

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 isn't 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;

You can also see how I made "smooth" lighting, here : 您还可以在这里查看我是如何制作“平滑”照明的:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM