Post news RSS K.I.S.A Devlog 010: Lighting II

Happy new year, everyone! I spent the past few days updating K.I.S.A's lighting system to incorporate proper character shadows.

Posted by on

Happy new year, everyone! Compare the image on the left to the image on the right:

I spent the past few days updating the lighting system to make K.I.S.A look like the right image, rather than the left. Lighting in K.I.S.A, as you may already know, is accomplished by drawing a screen-sized surface on top of the rest of the scene. There's not really any other options if you want an adaptable and realistic lighting system, but sometimes it takes a bit of work to make it all behave correctly.

The lighting system used to be drawn in the following order:

1. (Draw scene)
2. Ambient lights
3. Background lights
4. Object shadows
5. Foreground lights
6. Tile occlusions
7. Unseen area occlusions
8. (Draw lighting)

This works well as long as the characters are behind the objects, but if they're in front of them you get the gnarly results seen on the left side of the picture. The solution that I found - and here I gloss over five or six failed attempts involving multiple lighting passes and overly complex calculations - was very simple: If a character is closer to the camera than objects, then draw a shadow in their position. The new drawing order:

1. (Draw scene)
2. Ambient lights
3. Background lights
4. Object shadows
5. Character shadows
6. Foreground lights
7. Tile occlusions
8. Unseen area occlusions
9. (Draw lighting)

This method worked, but wound up making the game run on the wrong side of 60 FPS. It takes several different sprites and meshes to draw a character, and drawing them all twice was too slow. After a few other failed attempts to avoid drawing the character twice, I eventually realized there was no way around it.

When I examined each component of the character drawing code, I realized it wasn't the drawing itself that was computationally expensive, but the multiple accesses of variables needed for drawing: The position and angle of each limb, as well as the points for the skirt and hair polygons.

The great thing is that this can be easily avoided; surfaces can be drawn to at anytime during the drawing process, not just directly before the surface is rendered to the screen. So, while it wounded my sense of modularity, I combined the character drawing code and the character shadow drawing code into a single script that only accessed the necessary variables once. The drawing order now looked like this:

1. Ambient lights
2. Background lights
3. Object shadows
4. (Draw scene)/Character shadows
5. Foreground lights
6. Tile occlusions
7. Unseen area occlusions
8. (Draw lighting)

With that change, I was able to welcome back 60 FPS and bid farewell to improperly lit characters.

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.