Thursday, July 21, 2011

Lighting Tricks

With the Second Life viewer, dynamic lighting is done by using "vertex lighting".  If you enable wireframe mode (CTRL-SHIFT-R), you'll see the vertex points which make up an object, as spots where the wireframe lines intersect.

Now, vertex lighting has some problems in the area of accuracy.  It is done by calculating the brightness of a light based on the lights parameters (intensity, radius, falloff), and then applying a gradient to the prim surface.  With smaller objects, this isn't a problem.  Larger objects, such as megaprims, will have a problem.

With megaprims, the vertex points are spread much further apart.  If a light with a smaller radius is too far from one of these points, it will produce little or no light.  Meanwhile, if it is close to a vertex point, it'll produce too much light.  The white light in the snapshot below has a radius of just one meter.  But because it is right on top of a vertex point, its lighting radius is far greater than one meter!

Now, take a look at the two snapshots below.  In the before snapshot, you can see that 1) the torch on the left is casting a lot of light onto the floor, while the one on the right is casting none.  The four torches in the background are casting little or no light against the floor.  So, what's going on?

The lefthand torch is right near a vertex point, over-representing its light.  The torch on the righthand side is centered between two different vertex points, under-representing its light.  On the After image, the lights are all emitting light at the desired levels.  What is difference between the two versions?

The lefthand image has a megaprim cube for the floor.  In the second image, the floor is represented by a sculpty in the shape of a flat square.  The cube prim has a vertex resolution of 4x4 per face.  The flat square sculpty, however, has a resolution of 32x32 vertices.  Because of this, the vertex points are much closer together, allowing for a more even level of lighting.

The disadvantage of this method, however, is that the floor has 64x as many vertex points (16 points vs 1024).  So, this method will incur a higher rendering cost.  This isn't much of a problem if you use this method sparingly, but using it all over the place will result in more lag.

Below is a copy of the sculpt map that I used to create the floor with.  I'm releasing it into the public domain, so feel free to use it however you wish :-)

Flexible Meshes: The Sword That Cuts Both Ways

So, Linden Lab has decided to cut out flexible meshes, due to lag concerns.  I think Linden Lab is wrong on this.  Because, while it would mean more lag, it would also mean an opportunity to reduce it, by replacing the older flexiprims with a more efficient design!

Flexiprims have always been a major source of lag, and they are most commonly used for the creation of dresses, skirts, and hair.  But, if you were to create a skirt from a mesh and use it as clothing, you could reduce the lag generated by the skirt for these reasons:
  • A flexiprim skirt uses a lot of flexiprims.  One friend was wearing one with 18 prims, while another was wearing one using 51 prims!
  • Skirts use flexiprim cylinders.  Each cylinder uses 98 vertices at maximum LOD, and 14 vertices at minimum.
  • By making them flexible, their LOD increases a lot.  At minimum, a flexible mesh will have 56 vertices.  At maximum LOD, it will have 216 vertices.  And if you make them hollow, then the vertex count will roughly double.
As you can see, flexible prims on their own are rather inefficient.  A few of them scattered about won't do much to affect lag, but they will increase lag when there are ton of them all over.

So, I've made the point that flexible objects are laggy.  Why would sculpted meshes allow you to reduce lag?

The answer is simple: optimization.  A skirt maker can create a flexible mesh skirt as one single object, but rather than having the vertex count of a few dozen flexible prims, it would have a vertex count equivalent to just 4-8 flexible prims, depending on how ornate it is.  Mesh hair would also benefit from such improvements to LOD.

There will, of course, be people who would overuse this functionality and mesh itself, resulting in heavy lag-inducing objects with lots of needless and unnoticeable detail.  My personal hope is that such "detail gluttons" will receive a very quick and very harsh lesson for their mistakes.