quickest most effecient way to darken/lighten image on the fly?

Good morning.

I have a shading system in my game that currently I do it in an old school way. I have the tiles basically 4 times (in three different shades.)

Is there an efficient way to shade (or lighten) tiles in 2D on the fly in java? The one method I found seems quite expensive.

Java2D isn’t so great at doing things “on the fly” – for that you should be using OpenGL and GLSL.

The easiest and most efficient thing would be to construct the four texture atlases at creation/loading time. I say texture atlases because they should have the potential for more hardware acceleration than individual buffered images… :slight_smile:

Of course, if you want your high performance active rendering, you shouldn’t be using Java2D in the first place.

You could always draw a slightly transparent white (lighten) or black (darken) rectangle over a tile if the light is uniform

In my opinion, an efficient way of doing this in java2D/windows is using the drawImage with a LookupOp operation

LookupOp LU;

g.drawImage(image,LU,posX,posY);

by modifying the LookupOp you can change the image brightness:

e.g. Darken image, half of the brightness:

        brightnessBloq=new byte[256];
    for(int i=0;i<256;i++){brightnessBloq[i]=(byte)(i/2);
    LU = new LookupOp( new ByteLookupTable(0,brightnessBloq), null);

later, by just modifying values of brightnessBloq array, it automatically change the brigtness fo the next painted image.

Also, I agree that if you want good performance with high resolutions, java2D can be a little bit problematic :stuck_out_tongue:

BR

I advise you to stay away from Java2D when creating games. Java2D spend more time rendering than for example Slick2D does and, having fps such as 60 or higher is going to cause lag because Java2D can not render so many times per frame. So what is going to happen that it is going to skip rendering some frames, making the game look laggy, but in reality it is just rendering operation that are being aborted. Note that this do not happen on all computers, but you wanna make a game that looks the same on all computer, right?

I agree that, for a serious game, Java2D is not the best option, as it has several serious flaws. But it is perfectly able to handle 60 frames per second when the graphics are not very complicated.

wfh_jN23jfA

In this video I use two techniques for changing object brightness on the fly. The orange region has on top a layer wich alpha is changed dynamically. The gray part uses the technique I mentioned before.

whot are the two techniques that you mentioned???

In my experiences with Java2D, working with buffere images operation like LookupOp to perform process of the immage on the fly
is extremely heavy!

looking at the Garbage collection log, my games was using more that 10-15 time the memory with the effect!!!
for sure, depend from the use that u did of the effect, but it is without doubt a heavy operation

One is using LookUpOp operations. The other is just draw something on top (like a rectangle, or initialized VolatileImage) filled with a certain color with a certain alpha. You fill it each time so the alpha can be changed anytime.

Interesting what you are saying. TBH I just look at the rendering time of my loops for evaluating the algorithms. I didn’t got any noticeable FPS drop with the lookupOp. Drawing a layer over caused more FPS drops, specially in low end GPUs like an intel HD3000. My desktop uses a mid/high-range GPU: a GTX460.

ok now is clear, thanks
in your example you use on the top of your image a rectangle filled with a RadialGradientPaint
and you change just the alpha channel of the color,

now comparing the two solutions:

    1. LookUpOp (that works at the pixel level so is for any pixel the operation must be performed)
    1. rectangle filled with same Paint (like Gradient)

what is the best solution?

your experience say the first one, without your response my natural answer instead is the second, because I imagine that at low level the second one must use same low level directive to draw the rect.
but i don’t know,i never tryed the second solution.
Do you know how works Graphics2D.fillRect method?

regarding the verify of the Garbage collection try to pass this parameter to your java command
-verbose:gc

this is the official link
http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html

if u works with eclipse in the console you will see the output of the GC

Have you tested this on a variety of machines? And without the “OpenGL” VM flag?

I ask this because Java2D performance varies a lot from system to system. What might get 100 FPS on one will lead to 2 FPS on another. I’d love to test it out and see how it fares. :slight_smile:

I just lowing the r,g,b channel value together. It turns darker.