Java2D radialgradient lighting

Hi JGO!

I am currently creating a game that required lighting.
Currently, I have something working but that only works with 1 light.
I tried adding more lamps using an ArrayList but that failed.
What has to be changed to support multiple lights?


ArrayList<Light> lights = new ArrayList<Light>();

public void addRandomLight() { // Called on mouse-click
	try {
		lights.add(new Light(mouse.getX(), mouse.getY(), 200));
	} catch (Exception e) {}
}

public void createLightMap(Graphics g) { // Called int render (fow now, gonna split it up eventially), after rendering the map
	Graphics2D g2d = (Graphics2D) g;
	RadialGradientPaint p = null;
	for (Light l : lights) {
	 java.awt.geom.Point2D center = new java.awt.geom.Point2D.Float(l.x, l.y);
	 float radius = l.r;
	 float[] dist = {0.0f, 1.0f};
	 Color[] colors = {new Color(0.0f, 0.0f, 0.0f, 0.0f), Color.BLACK};
	 p = new RadialGradientPaint(center, radius, dist, colors);
	 g2d.setPaint(p);
	 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
	 g2d.fillRect(0, 0, 1280, 736);
	}
}

Bump

This is the same problem as before, but then I must ask: What is really the problem? Does it not render, does it crash or does it only show one light? What is the result you don’t want from the code? I don’t know as I haven’t seen a picture of the problem, and the code you provided about how it worked earlier is identical, so why it shouldn’t work is beyond me with what information I currently have.

TL;DR: What is the problem, really?

Yes, this is the same problem as before.

Here is a gif showing my problem.

It only supports 1 light, multiple lights are in each other’s way.
light 1, 2, 3 etc… are just layers, but i think the best way is to subtract light 2 from light 1 because the black from light 1 blocks the others.

This explains why you’re going from transparent black to opaque black :slight_smile:

In the case of multiple lights though, you should begin thinking about darkness as the absence of light, instead of darkness as a result of light. I don’t know the process of doing it in java2D or if it’s possible, but I suppose you could render the scene completely transparent over a black background, and each light acts as an additive to the alpha channel; and thus the terrain becomes more opaque when you add lights.

Without checking the javadocs as I don’t know Java2D fully.

every time you draw a light, are you rendering everywhere else with a fully solid black colour?

if you are, that would mean you are restricted to one light as you would draw the light at one location, draw another light at another location which also sets everywhere else to black meaning your first light is now unlit.

Yes, I am. I know that that is the reason but I don’t have a solution yet!

Thanks! I will try this.
EDIT: @i91az Do you have some example code on how this could be done?

This is my current render function. (The background is already black)


public void render(GameCore gc, Render r) {
	Graphics2D g = (Graphics2D) r.getGraphics();

	//g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .2f));
	renderMap(gc, r); // Render checkerboard
	g.setFont(new Font("Arial", Font.PLAIN, 25));
		
	g.setColor(Color.RED);
	g.fillOval(ox, gc.getHeight() / 2, 200, 200);
	ox++;
	if (ox > gc.getWidth()) ox = -200;

	//g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));

	g.setColor(Color.RED);
	g.drawString("FPS: " + gc.getFPS(), 20, 20);
	g.drawString("TPS: " + gc.getTPS(), 20, 40);
}

Looking into the problem, I found a similar way to solve the problem that is much easier to accomplish. I don’t know why I didn’t think of this instead, but here it is:

You take an opaque black BufferedImage, and make it transparent where the light is. This means you simply render your light to the correct spot on the bufferedimage and then render the bufferedimage on top of the scene, this will create the illusion of light in the scene, and it’s almost like you have already done it :slight_smile:

I found a video on youtube about this, he can probably explain much better than I:

KjfbzDGrRfI

I don’t think it has to be a static image, but I don’t know how it will affect performance if you update it all the time.

Thanks! It’s working.
Now it’s time to optimize the performance.