Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java 2D Fake Lighting  (Read 10676 times)
0 Members and 1 Guest are viewing this topic.
Offline StumpyStrust
« Posted 2012-07-14 08:52:16 »

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

Here is a prog to test it

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.

Offline theagentd
« Reply #1 - Posted 2012-07-14 11:13:57 »

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?

Offline Mads

JGO Ninja

Medals: 26
Projects: 3
Exp: 6 years

One for all!

« Reply #2 - Posted 2012-07-14 11:49:53 »

Very nice! It worked for me. Anything open?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StumpyStrust
« Reply #3 - Posted 2012-07-14 14:29:29 »

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?

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.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_ATOP, all));
                      AlphaComposite.SRC_OVER, color));

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();
          for(int i = 0; i <= lights.size()-1;i++)
             lights.get(i).render(g, lightmapScale);
                      AlphaComposite.SRC_OVER, ambient));
          g.fillRect(0, 0, pane.getWidth(), pane.getHeight());

          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.

Offline The-Next

Senior Devvie

Medals: 7
Projects: 2

« Reply #4 - Posted 2012-08-19 22:04:45 »


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:

Offline jonjava
« Reply #5 - Posted 2012-08-19 22:17:55 »

For those wondering how to achieve the above check out

Pages: [1]
  ignore  |  Print  

EgonOlsen (44 views)
2018-06-10 19:43:48

EgonOlsen (24 views)
2018-06-10 19:43:44

EgonOlsen (46 views)
2018-06-10 19:43:20

DesertCoockie (201 views)
2018-05-13 18:23:11

nelsongames (126 views)
2018-04-24 18:15:36

nelsongames (125 views)
2018-04-24 18:14:32

ivj94 (866 views)
2018-03-24 14:47:39

ivj94 (127 views)
2018-03-24 14:46:31

ivj94 (770 views)
2018-03-24 14:43:53

Solater (142 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!