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)

alt text

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!

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)