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);
}
}
}
}
}