Tile by tile lighting light wont change from white?

Hello i made my lighting system not long ago and i have a problem with changing the colour of the light it is always staying white and it doesn’t matter what color i give it?

Can somebody help/advice?

Colour Mixing

private static Color color = new Color(1,1,1,1);
	
	public static Color mixColours(Color color1,Color color2,float amount){
		color.set((color1.r * color2.r) * amount, (color1.g * color2.g) * amount, (color1.b * color2.b) * amount, (color1.a * color2.a)*amount);
		return color;
	}
    public static Vector2 unprojectCoordinates(OrthographicCamera camera,float x, float y) {
    Vector3 rawtouch = new Vector3(x, y,0);
    camera.unproject(rawtouch);
    return new Vector2(rawtouch.x, rawtouch.y);
    }

Light Engine

public static final int MAX_LIGHTS = 20;
	private static final float MAX_DISTANCE = 5f;
	public static boolean ENABLED = true;

	private Array<MasterTile> lights;
	private SpriteBatch spriteBatch;
	private Texture pixelTexture;
	private OrthographicCamera camera;

	private Color sunlight;

	public LightEngine(OrthographicCamera camera, SpriteBatch spriteBatch) {
		Pixmap tilePixel = new Pixmap(1, 1, Format.RGBA8888);
		tilePixel.setColor(new Color(1,1,1,1));
		tilePixel.fill();
		pixelTexture = new Texture(tilePixel);

		sunlight = new Color(1,1,1,1);
		this.camera = camera;
		this.spriteBatch = spriteBatch;
	}

	public static void setEnabled(boolean e) {
		ENABLED = e;
	}

	public void setSunBrightness(float b) {
		sunlight.a = b;
	}
	public float getSunBrightness() {
		return sunlight.a;
	}
	public void addSunBrightness(float a) {
		sunlight.a += a;
	}
	

	public void setLights(Array<MasterTile> lights) {
		this.lights = lights;
	}

	public Array<MasterTile> getLights() {
		return lights;
	}

	public void render(float delta) {

		if (ENABLED) {
			int camX = (int) (camera.position.x + (MasterTile.TILE_WIDTH / 2) - camera.viewportWidth / 2) / MasterTile.TILE_WIDTH;
			int camY = (int) (camera.position.y + (MasterTile.TILE_HEIGHT / 2) - camera.viewportHeight / 2) / MasterTile.TILE_HEIGHT;
			int camZoomFixX = (int) (((camera.zoom - 1) * 100) / (MasterTile.TILE_WIDTH / 10));
			int camZoomFixY = (int) (((camera.zoom - 1) * 100) / (MasterTile.TILE_WIDTH / 10));
			int offset = 1;

			if (camZoomFixX < 0)
				camZoomFixX = 0;
			if (camZoomFixY < 0)
				camZoomFixY = 0;

			int startX = camX - (camZoomFixX + offset);
			int toX = (camX + TileRenderer.AMOUNT_WIDTH) + (camZoomFixX + offset);
			int startY = camY - (camZoomFixY + offset);
			int toY = (camY + TileRenderer.AMOUNT_HEIGHT) + (camZoomFixY + offset);

			// Lights
			{

				for (int x = startX; x < toX; x++) {
					for (int y = startY; y < toY; y++) {
						if (x < 0 || x > TileRenderer.map.getWidth() - 1 || y < 0 || y > TileRenderer.map.getHeight() - 1)
							continue;

						float tileBrightness = sunlight.a;

						for (int i = 0; i < lights.size; i++) {

							MasterTile tile = lights.get(i);
							if (tile == null)
								continue;
							float distance = Vector2.dst(tile.getX() / MasterTile.TILE_WIDTH, tile.getY() / MasterTile.TILE_HEIGHT, x, y);
							tileBrightness -= Math.max(0, 1 - distance / MAX_DISTANCE);
							

						}
						
						Color tileColor = Color.WHITE;
						tileColor = Utils.mixColours(tileColor, Color.WHITE, tileBrightness);
						tileColor = Utils.mixColours(tileColor, Color.BLACK, sunlight.a);
		                	                
						spriteBatch.setColor(tileColor);
						spriteBatch.draw(pixelTexture, x * MasterTile.TILE_WIDTH, y * MasterTile.TILE_HEIGHT, MasterTile.TILE_WIDTH, MasterTile.TILE_HEIGHT);
						spriteBatch.setColor(Color.WHITE);

					}
				}
			}
		}
	}

I think it’s with your colour mixer method. It doesn’t seem like interpolation. If you want linear interpolation you can use the libgdx Color.lerp method.

From what I can tell from your posting history, you’re overlaying each tile with a “shadow” to imitate lighting, which is totally fine (I do it). This is how my colour mixing goes:

I don’t use the alpha part of colour (RGB888). Sunlight is black. All tiles are by default “sunlight”.
A block will check its adjacent neighbours (above, below, left, right) and mix its neighbours with its current colour. The mixing is a 50% linear interpolation from the source to the destination colour (it works in the other direction as well because it’s 50%). I use this: colorOfNeighbour.lerp(currentBlockColour, 0.5f).

Brightness is in its own category and is handled differently. It is NOT the alpha of the block colour.

Try using black for sunlight and lerping it rather than your multiplicative blending.

I am not sure how i would add this… can you give an example using methods from my code?

I just don’t understand why when i change the light color to red it still is white??
I just don’t know what to do… plus i like the way this method works other then checking the tiles around

It’s buggy, that’s why it’s not working. Sometimes the easy ways, or the ways we are comfortable with solving a problem, are not the best solutions to a problem. My suggestion; use the information chrislo just gave you and go back to the drawing board. You’ll learn more that way, and you’ll have working lighting!

I really have no idea where to start with this :frowning: :frowning: :frowning:
Why can’t i use “Math.max(0, 1 - distance / MAX_DISTANCE);”

All i am getting is a black screen :frowning:

I am not sure how to do this because you guys are saying that i don’t need to touch alpha but if i don’t touch alpha then it will just a draw a box over my tiles without transparency… sooo confused right now

I think previous posts (here and in other threads) have probably pretty much covered everything, but I’ll throw in a couple things here. First, regarding this function:

   public static Color mixColours(Color color1,Color color2,float amount){
      color.set((color1.r * color2.r) * amount, (color1.g * color2.g) * amount, (color1.b * color2.b) * amount, (color1.a * color2.a)*amount);
      return color;
   }

Just out of curiosity, is this based on a particular example or tutorial? Or is this just something of your own?

I notice you’re using a static ‘scratch’ color for mixColours(). This is perfectly understandable, but be aware that mutable static/global state of this sort can lead to difficult-to-diagnose bugs (insert hope for value types in Java here…). So, just watch out for that - this sort of pooling (if you’ll grant that a static ‘scratch’ object counts as simple, ad hoc pooling) has to be handled somewhat carefully.

Looking at this:

                  Color tileColor = Color.WHITE;
                  tileColor = Utils.mixColours(tileColor, Color.WHITE, tileBrightness);
                  tileColor = Utils.mixColours(tileColor, Color.BLACK, sunlight.a);

I’ll assume the alpha for your color constants is 1. Unless I’m making a mistake somewhere, the first mix leaves you with [ tileBrightness, tileBrightness, tileBrightness, tileBrightness ]. The second should leave you with [ 0, 0, 0, tileBrightness * sunlight.a ]. That may be exactly what you want, but since there seems to be some unneeded work here (that is, work that either doesn’t do anything or ends up being overwritten), it might be worth double-checking to make sure it’s really what you intend.

I don’t know if/how this relates to the ‘color is always white’ issue, but I’m not seeing in your post where another color would be introduced.

That’s not what chrislo is saying, if you read his reply again carefully you’ll understand.

I’m not saying to not touch alpha. I’m suggesting that you move brightness away from the colour object’s alpha and use your own system (I used bytes which gets me 127 levels of precision which is good enough; in this case I don’t use the negative side of a byte).

This was a part of my system, I thought i did it right… whoops

So what do i need to do? all i want is to change the colour of the light instead of white but the lights look perfect


<------ the lighting is fine other then it won’t change colour

Er… but it is changing color? Seems like some tiles are darker than others. I don’t see the issue.

I mean I would like a different colored light other then white when i try to do this it doesn’t work

Does the code you posted earlier show where you’re trying to use a color other than white?

Dont multiply your light values try adding them.

						Color tileColor = Color.PURPLE;
		                tileColor = Utils.mixColours(tileColor, Color.PURPLE, tileBrightness);
		                tileColor = Utils.mixColours(tileColor, sunlight, sunlight.a);

Just tried that, messed up the lighting ???

Are your light values from 0 to 1 or from 0 to 255? because if it is from 0 to 1, going over 1 may mess up the color.

I am using Libgdx which is Open Gl so 0-1