S.E.M.M. / Garry's Mod: dynamic shadows tutorial

Uploaded by MaxOfS2D on 02.04.2012

In this video, we will be looking at integrating dynamic lighting into your Garry's Mod scenes.
In the current engine build, dynamic lighting is a very "rough" feature,
but it doesn't mean that you cannot make the most out of it, and that's what I'm going to show you.
We'll begin with the most important quality setting: the resolution.
r_flashlightdepthres is a console command that allows you to set the resolution of projected textures.
Here's the old default of 512.
If we tone down the blur filter...
...yeah, that's really bad.
We are going to gradually increase the resolution to the highest.
As I increase the resolution, you will notice how small objects (like the pen on the vest, and the glasses) get their own shadows.
But this also depends on another setting we will be looking at later on.
Most of the time, though, you will crash if you try to change the resolution while the engine is running a map.
So, for this reason, I would recommend to stick the resolution you want in your autoexec.cfg, or your command-line launch options.
The filter size is a variable that you'll be tweaking a lot,
because you have to make a compromise between getting somewhat "realistic" shadows
(as in not overly sharp or very blurry)
and managing to hide artefacts that you will probably dig up when increasing the accuracy of the shadows later.
It's fairly straightforward, though.
As you go higher, the shadows will become more blurry.
Here comes the tricky part, which (might) depend on your hardware.
These two variables define how biased the Source engine's renderer will be when tracing the shadow map.
By default, they're pretty big, and if you don't change them, small parts of the model might be ignored, or shaded incorrectly.
Here's what happens if I lower the first...
...the second...
...and both of them together.
That looks much better, don't you think?
But if you go too low, then say hello to your friends, the graphical artefacts. Yuck!
So yeah, this isn't exact science. You will most likely need to adjust them for a particular situation one day!
On a mid-range video card like mine (GeForce GTX 550 Ti), I'd set the first one to 2, and the second one to 0.000005.
It's a default that rarely needs to be adjusted, as far as I am concerned.
NearZ is a keyvalue on the env_projectedtexture entity.
Despite what you might think, it is not a cutoff value like its little brother, FarZ, but it offsets where the tracing begins.
Here, I placed this lamp far away from my scene, so it can imitate the sun. The sun is so far away from the Earth that it's basically an orthogonal projection.
Since my lamp is pretty far away, it covers the characters with a field-of-view of 10°, which is, well, close enough.
But still, this is looks pretty odd. I've set the bias values as low as they can be without noticeable artefacts,
yet shadows are still flickering in-and-out of existence, and, yeah, they're just being odd in general.
To keep it simple, the system used by the renderer to trace the shadows does not have enough precision in terms of depth.
However, if I go and tell the lamp to start tracing the shadows two thousand units after its location,
then you'll see there aren't any accuracy issues anymore.
I could've probably gotten even closer — and you should try to get as close as possible in all cases.
Tweak the NearZ values of your projected textures as best as you can.
Now that we've looked at the technical side of things of dynamic lighting in Source, let's integrate that into your scene.
Here's the actor for this example, the FemScout,
and we are going to create the lamp.
So, nothing too complicated here.
Note that I'm using an advanced lamp tool as, right now, Gmod's stock one doesn't allow you to adjust many of the settings discussed here,
most importantly power (which is separate from RGB).
The stock Gmod lamp tool spawns projected textures with a power of 200, which is way too weak for most uses.
But wait wait wait wait wait! She's so BRIGHT! Look at those hands, my GOD! Aw! What can I do about this?
Well, the thing is that the pre-compiled lighting from the map and the projected texture are "stacking" on top of each other,
So we have to remove one of these.
Spawn any prop and give it an entity name.
Then go back to your actor/ragdoll and use the setlightingorigin input,
and reference the prop you just spawned.
This can be used on both actors and ragdolls — as long as the entity has the setlightingorigin input, then yes, you can override the lighting.
If you move your prop around, the lighting will update on your actor accordingly.
So yes, that prop is now the lighting origin of your actor.
And if you remove that prop, the lighting will go back to being "owned" by your actor.
Well, here you go! You've got good-looking dynamic shadows
(as good-looking as we can possibly have, I guess)
I hope this small tutorial will have been helpful to you,
and I hope you can make good use of it. Thanks for watching!