27 February 2007

Lightmaps

For a long time I wanted to have two things in the game:
1. Night time shadows.
2. Different visibility based on your location, ambient light, nearby light sources, what you wear, what your attributes are. For example, someone sitting in a shadow at night time, with no armor on, would not be visible by someone sitting a few meters away. However, the person in the shadow would see someone that is in open space, under a light. Wearing shiny (metal) armors would make you more visible. Having a higher perception skill would make it easier to see people hiding in dark places.

Last night I spent about 8 hours (going to bed near 6 AM) to write a quick and dirty server side light map generator. The idea was to take each walking tile, see what the nearby lights are, trace a ray from the tile to the light, and if it isn't blocked add an attenuation base done the distance to the light, then store the info.

The generation was relatively easy an painless, due to the fact that I used the collision detection routines that were already there (same as for the arrows). It was fast too, it took only about 12 seconds to generate it for Whitestone, which is the biggest map size we have in the game.
I wasn't pleased at all with the results though. First problem was that most of the lights are placed inside lamppost or fireplaces and so on. Which meant that the ray collided with the object and the tile was marked as not illuminated. I fixed this by checking the end collision point, and if it was less than 30 cm away from where the light was, I assumed the collision was with the lamp post (or whatever other object is used to represent a light source) and marked it as illuminated.

The next problem was that even if a small tree branch is occluding the light source, a whole tile is marked as shadowed. Not good. This could be solved by adding about 10-20 rays with a small displacement, then computing an average value to see how many made it to the light source.
Then I realized we have two more problems that can't be solved with the current algorithm: The collision thing does not take the texture into account, so many trees obscure a light source even if the leaves would allow the light to pass. Now, this could be solved by taking the texture into account, but then some function would need to be severely modified. The second problem, which is far harder to solve, is that the client does not have this information, as we have no light maps. So even if a tree obscures the light, if a tile or object is close to a light, it will be fully illuminated. This would create a lot of confusion for the players, as they wouldn't know where are the good spots to hide, or where they can expect some hidden player to be.

There are quite a few other problems which I won't even go into, such as the fact that to do it right, we'd need like 6 bytes for each walking tile, to take the light intensity at different heights.

I talked with Schmurk about it, and we decided that the best solution to this problem would be to use lightmaps on the client.
Now, there are a few problems with the lightmaps, especially with high quality light maps:

a. They can take many GB of hard disk space, for one map alone.
b. They will take many MBs of RAM, possibly up to 512 or so per map.
c. They can take forever and ever to generate, possibly a day or two for a big map, on a fast computer.
d. Given their big size, they can't be included with the client download. Even compressed they might add up to 1GB or more for the whole game. So we will have to use p2p networks to distribute them, and maybe even sell a DVD with the prerendered maps.
e. They do not work with dynamic maps.
f. They do not work with moving things (players, animals, etc.)
g. If you make a change to a map, you will have to regenerate the whole lightmap for that map.

Now, for the advantages of using them:
1. Once created, the server can use them as well (with some modifications).
2. We can have true shadows during the night and in indoors areas, we can have as many light sources as we want, and a light can be visible from as far as we want it. Additionally, we can have soft shadows, and shadows from multiple lights (so there won't be only a shadow or no shadow).
3. The game will look so much better with them.

I already contacted a former developer, who is very good with math and OpenGL, and he will implement them for us (we'll pay him for it).

Until then, I am going to ignore all the collision with the lights, and use only a linear attenuation on the server, to determine how visible a player is. This is a good way to test the system, and when we have the light maps, we'll use them on the server too.

6 Comments:

Anonymous Anonymous said...

will the light maps HAVE to be downloaded to play the game or are they optional?

you mentioned that the maps could be up to 1GB even while compressed.. that would be a lot to d/l for players on modem or low bandwidth connections..

28/2/07 08:58  
Anonymous Anonymous said...

I dug up into my hard disk and found this screenshot of Eternal Lands:
http://img105.imageshack.us/img105/2333/85991883fn1.jpg

I guess this is what you want to work on.

28/2/07 13:26  
Blogger Radu said...

The maps will be totally optional, if they are not installed the game will look like it looks now.

Sistema: Yep, kind of like that.
That implementation looked great in screenshots, but looked totally bad in the game (when a light dissapeared, so did the shadows, which was terrible).

28/2/07 13:54  
Anonymous Anonymous said...

I've read carefully your post, and while I'd love a better looking EL I wonder if the disadvantages don't horribly outweigh the advantages, at least for the moment being. I could probably use the high quality light maps, but I suspect lots of people couldn't (too much resources involved: bandwidth, HD space, RAM). I also value gameplay more than appearance, but I understand that the latter is important for many players.

Nice to know that you're always looking to improve the game though.

Rehdon

2/3/07 04:51  
Anonymous Anonymous said...

In my very humble opinion far more effective would be to increase the texture quality and polygon amount of the 3D models. Useage of shader effects would furtherly improve the quality of the game, while keeping the download small and the client fast.

5/3/07 05:52  
Blogger Radu said...

Yes, actually we are workign at some new shaders, such as normal maps. We are also considering increasing some texture sizes, but not for the next update.

6/3/07 02:47  

Post a Comment

<< Home