- Home /

# 2D Tilebased Floodfill lightning

Hello Forum,

I think I have an easy question, but I can't seem to find any solution for it.

My game looks like this right now (I use Vertex Colors for lightning)

Lighting works perfect in realtime, with as many lights as I like. No performance issues.

The problem is that I want to introduce colors. Right now I'm just using a value between 0 and 255 for all R,G and B.

Okay so this is the method I use now (This is not the computation for realtime)

```
private void InitializeLightData() {
lightData = new byte[worldWidth, worldHeight];
for (int i = 0; i < worldWidth; i++) {
for (int j = 0; j < worldHeight; j++) {
if (BlockDescription.GetLightEmission(worldData[i, j, 0]) > 0) {
lightData[i, j] = BlockDescription.GetLightEmission(worldData[i, j, 0]);
} else {
lightData[i, j] = sunValue;
}
}
}
for (int i = 0; i < worldWidth; i++) {
for (int j = 0; j < worldHeight; j++) {
if (lightData[i, j] >= 17) {
PropagateLight(i, j - 1, (byte) (lightData[i, j] - 17));
PropagateLight(i + 1, j, (byte) (lightData[i, j] - 17));
PropagateLight(i, j + 1, (byte) (lightData[i, j] - 17));
PropagateLight(i - 1, j, (byte) (lightData[i, j] - 17));
}
}
}
}
private void PropagateLight(int x, int y, byte lightValue) {
if (lightValue == 0) {
return;
}
if (x < 0 || y < 0 || x >= worldWidth || y >= worldHeight) {
return;
}
if (BlockDescription.IsSolidToLight(worldData[x, y, 0])) {
return;
}
if (lightData[x, y] >= lightValue) {
return;
}
lightData[x, y] = lightValue;
lightValue -= 17;
PropagateLight(x, y - 1, lightValue);
PropagateLight(x + 1, y, lightValue);
PropagateLight(x, y + 1, lightValue);
PropagateLight(x - 1, y, lightValue);
}
```

So I thought about something like this

```
private void InitializeLightData() {
lightData = new Color32[worldWidth, worldHeight];
Color32 currentColor;
for (int i = 0; i < worldWidth; i++) {
for (int j = 0; j < worldHeight; j++) {
currentColor = BlockDescription.GetLightEmission(worldData[i, j, 0]);
if (currentColor.r != 0 || currentColor.g != 0 || currentColor.b != 0) {
lightData[i, j] = currentColor;
} else {
lightData[i, j] = sunColor;
}
}
}
for (int i = 0; i < World.current.worldWidth; i++) {
for (int j = 0; j < World.current.worldHeight; j++) {
Color32 newLight = new Color32(lightData[i, j].r, lightData[i, j].g, lightData[i, j].b, 0);
if (newLight.r >= 17) {
newLight.r -= 17;
}
if (newLight.g >= 17) {
newLight.g -= 17;
}
if (newLight.b >= 17) {
newLight.b -= 17;
}
PropagateLight(i, j - 1, newLight);
PropagateLight(i + 1, j, newLight);
PropagateLight(i, j + 1, newLight);
PropagateLight(i - 1, j, newLight);
}
}
}
private void PropagateLight(int x, int y, Color32 lightValue) {
if (lightValue.r == 0 && lightValue.g == 0 && lightValue.b == 0) {
return;
}
if (x < 0 || y < 0 || x >= worldWidth || y >= worldHeight) {
return;
}
if (BlockDescription.IsSolidToLight(worldData[x, y, 0])) {
return;
}
if (lightData[x, y].r >= lightValue.r && lightData[x, y].g >= lightValue.g && lightData[x, y].b >= lightValue.b) {
return;
}
lightData[x, y].r = lightValue.r;
lightData[x, y].g = lightValue.g;
lightData[x, y].b = lightValue.b;
if (lightValue.r >= 17) {
lightValue.r -= 17;
}
if (lightValue.g >= 17) {
lightValue.g -= 17;
}
if (lightValue.b >= 17) {
lightValue.b -= 17;
}
PropagateLight(x, y - 1, lightValue);
PropagateLight(x + 1, y, lightValue);
PropagateLight(x, y + 1, lightValue);
PropagateLight(x - 1, y, lightValue);
}
```

But this doesn't work. I have changed the code above many times, this is just the last state it was in. But hopefully you get the idea and can help me out.

Really appreciate it!

When you say that it doesn't work, what does it do exactly? Are the colors coming out as always white no matter what you do?

If yes, it's because in script colors are a float from 0 to 1. So, to use R 177, G 150, B 25 for examply, you should divide 177/255, 150/255, 25/255 and use the result.

I have a Color32[,]-Array where all Vertex-Colors are saved in. The methods above fill this Array, so at a later point the Render-Loop can use the information to update the Vertex-Colors.

What happens with the second method is this

I don't want that anybody tries to find a bug in my code. I think it's a logical error. So if somebody maybe knows how it could be done with colors and explain the idea behind it.

EDIT: At some point I had some code that actually generated Colors, but it was horribly slow. So maybe the idea is not that wrong.

**Answer** by tanoshimi
·
Aug 19, 2014 at 06:44 PM

That's `Color`

. The OP is using `Color32`

, which does use component ranges of 0-255. Though I agree it's unclear how the lightData value calculated is eventually assigned to the material (apologies if that's what you meant)

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

2D game level building best practice 1 Answer

Why do I get those green lines? 0 Answers

2D Platformer: Using Tiled? 1 Answer

How to set tile from ScriptedTile script? 1 Answer

Tiling 2D Planes 2 Answers