java - Trying to achieve dynamic lighting in a tiled 2D isometric environment using Java2D -
i trying write lighting code java2d isometric game writing - have found few algorithms want try implementing - 1 of found here:
the problem sort of algorithm require optimal pixel-shading effect haven't found way of achieving via java2d. preferably method via graphics hardware if isn't possible - @ least method of achieving same effect in software.
if isn't possible, direct me more optimal algorithm java2d in mind? have considered per-tile lighting - find drawpolygon method isn't hardware accelerated , performs slowly.
i want try , avoid native dependencies or requirement elevated permissions in applet.
thanks
i did lot of research since posted question - there tons of alternatives , javafx intend (on later release) include own shader language interested. there ofcourse lwjgl allow load own shaders onto gpu.
however, if you're stuck in java2d (as am) still possible implement lighting in isometric game 'awkward' because cannot perform light shading on per-pixel level.
how looks:
i have achieved (highly unpolished - after polishing can assure great) effect casting shadows, depth sorting light map, , applying lighting without experiencing drop in frame-rate. here how looks:
you'll see in screen-shot diffuse light (not shaded in step i'd relatively easy in contrast steps there) casting shadows - areas behind entities obstructs light's passage but in bounds of light's maximum fall-out shaded in ambient lighting in reality area passed lights rendering routine factor in amount of obstruction has occurred light can apply prettier gradient (or fading effect of sort.)
the current implementation of diffuse lighting render obstructed regions ambient colour , render non-obstructed regions light's colour - though you'd apply fading effect got further light (that part of implementation haven't done yet - said relatively easy.)
how did it: don't guarantee optimal method, interested:
essentially, effect achieved using lot of java shape operations - rendering of light map accelerated using volatileimage.
when light map being generated, render routine following:
creates area object contains rectangle covers entirety of screen. area contain ambient lighting.
it iterates through lights asking them light-casting area if there no obstructions in way.
it takes area object , searches world actors\tiles contained within area light cast in.
for every tile finds obstructs view in light's casting area, calculate difference in light source's position , obstruction's position (essentially creating vector points at obstruction light source - direction want cast shadow) pointing vector (in world space) needs translated screen space.
once has been done, perpendicular vector taken , normalized. gives line can travel or down on multiplying given length travel given direction in. vector perpendicular direction want cast shadow over.
almost done, consturct polygon consists of 4 points. first 2 points @ the base of screen coordinate of obstruction's center point. first point, want travel perpendicular vector (calculated in 5) quantity of half tile's height [ relatively accurate approximation though think part of algorithm incorrect - has no noticable decay on visual effect] - ofcourse add obstructions origin. second, same instead travel down.
the remainder of 2 points calculated same way - these points need projected outward in direction of shadow's projection vector calculated in 4. - can choose large amount project outwards - long reaches @ least outside of light's casting area (so if want stupidly multiply shadow projection vector factor of 10 , should safe)
from polygon constructed, construct area, , invoke "intersect" method light's area first argument - assure shadows area doesn't reach outside of bounds of area light casts over.
subtract light's casting shadow area constructed above. @ point have 2 areas - area light casts unobstructed, , area light casts on obstructed - if actors have visibility obstruction factor used determine particular actor obstructing view - have grade @ obstructs view can apply later when drawing in light effect (this allow chose between darker\brighter shade depending on how light being obstructed
subtract ambient light area constructed in (1) both light area, , obstructed light area don't apply ambient light areas lighting effect take on , render into
now need merge light map depth-buffered world's render routine
now you've rendered you're light map , contained inside of volatile image, need throw world's render routine , depth-sorting algorithm. since back-buffer , light map both volatileimages, rendering light map on world relatively optimal.
you need construct polygon strip contains vertical strip of world tiles rendered (look @ screen shot, you'll see array of thin diagonal lines seperating these strips. these strips referring). can render parts of light map strip strip (render on strip after you've rendered last tile in strip since - - light map has applied over map). can use same image-map use strip clip graphics - need translate strip polygon down per render of strip.
anyway, said don't guarantee optimal way - far working fine me. light map applied p
Comments
Post a Comment