Java 2D Fake Lighting

So here is meh hacky lighting. It is very hacky and you don’t get “real” shadows say but it is super fast and you can choose the shadow map quality which makes it very scalable for different resolutions.

Here are some screens

http://s12.postimage.org/5bdpl9vrv/sshot3.png

Here is a prog to test it

http://www.mediafire.com/download.php?8ccknsm60bjycz5

Controls
Q lowers the lighting map quality
W increases it
s lowers the amount of color on lights
a increases color
x lowers ambient light
z increases ambient light

v spawns some lights
click to spawn static larger lights
K kills all lights

I want to know if anyone would be interested in some fake 2d lighting in java 2D. I know that lighting is not that hard with opengl using slick or libgdx but it is still fun trying to do something in plain old java2D.

Tried it, but I’m getting terrible flickering here. Might be some driver setting if it’s just me.

Works fine otherwise, never drops below 60 FPS. Any chance of more info of how you did it? Does it support any shadows?

Very nice! It worked for me. Anything open?

[quote]Tried it, but I’m getting terrible flickering here. Might be some driver setting if it’s just me.

Works fine otherwise, never drops below 60 FPS. Any chance of more info of how you did it? Does it support any shadows?
[/quote]
It is not a driver thing i get is sometimes but I think is because I threw this together really fast and may have done the buffering wrong or something.

Anyways when I say no shadows I mean I am basically drawing images with a specific alpha interpolation that takes the sources alpha and uses that for the destination. So you are not ever casting any rays or gradients or checking to see if you hit some wall.

The pipeline goes like this: Draw everything effected by light, draw lights to an image or lightmap (this is very slow on high resolutions), draw lightmap, draw anything else not effect by light. You can do this any number of times layering lighting effects.

Here is the core of the trick


Image light = drawLights();
				g2d.setClip(light.getGraphics().getClip());
				g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_ATOP, all));
				g2d.drawImage(light,0,0,frame.getWidth(),frame.getHeight(),null);
				
				g2d.setComposite(AlphaComposite.getInstance(
		                AlphaComposite.SRC_OVER, color));
				g2d.drawImage(light,0,0,frame.getWidth(),frame.getHeight(),null);

The first draw call sets the light and should always be 1. You could lower it to lower all light values but that should not be done here. The second call is how much of the images color you are going to put on top of the light. You could use none but it would look a little strange.

The light map or image I use when I call drawLights() is a smaller resolution then the window/frame/canvas I am drawing on. This reduces the quality of the lights but greatly improves performance to the point where rendering lights cost almost nothing. ( I also like the pixelated effect at low resolutions) After the lights are drawn onto the image I render the image but stretch it to fit the window.

Here is the drawLights() method


public Image drawLights()
	    {
	    	BufferedImage buff = BufferUtils.getCompatibleImage(pane.getWidth()/lightmapScale, pane.getHeight()/lightmapScale);
	    	Graphics2D g = (Graphics2D) buff.getGraphics();
	    	g.setClip(0,0,buff.getWidth(),buff.getHeight());
	    	for(int i = 0; i <= lights.size()-1;i++)
	    	{
	    		lights.get(i).render(g, lightmapScale);
	    	}
	    	g.setColor(Color.black);
	    	g.setComposite(AlphaComposite.getInstance(
		                AlphaComposite.SRC_OVER, ambient));
	    	g.fillRect(0, 0, pane.getWidth(), pane.getHeight());

    		g.dispose();
    		return buff;
	    }

There are a lot of optimizations you could do through all of the example but I just wanted to see if it would even work at some form of fps.

The problem is you really cant get cool looking shadows because if you say have a square block the light would go right through it or below it. I think I could come up with something so when the light hits those objects that block light it simply stops. Wont be really cool sharp shadows but it would be much more useable.

Even with this down side, the performance is great and it still can work in many games i think.

Hi!

Thank you very much for your idea! I was looking for a very fast way of performing lightning an shadows. I managed to do dynamic shadows by clipping the light sprite:

For those wondering how to achieve the above check out http://www.java-gaming.org/index.php?topic=27012.0