Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'll share my method for applying a smooth lighting effect to a 2D tile grid. ClassicThunder's <a href="https://stackoverflow.com/a/9877259/1218281">answer</a> provides a nice link for shadows.</p> <p>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.</p> <p><img src="https://i.stack.imgur.com/Q2FJ8.gif" alt="Lighting"></p> <p>Basicly what we do is loop through all the tiles, starting from the top, if a tile is blank, set the <code>CurrentLight</code> variable to max brightness, if we find a solid tile, set it as the <code>CurrentLight</code> variable and subtract an "absorbsion" amount from the <code>CurrentLight</code>. This way, on our next solid tile, when we set the tile to the <code>CurrentLight</code> value, it will be slightly less. This process is repeated until the array is iterated.</p> <p>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.</p> <p>Basically running this code on every tile in the loop</p> <pre><code>if (tile.Light &gt; 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; </code></pre> <p>And there you go, nice tile lighting. However if you want a less "pixelized" look, you can check out my <a href="https://gamedev.stackexchange.com/questions/40563/how-can-i-acheive-a-smooth-2d-lighting-effect">question on gamedev</a> for that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload