Muzzle Flash Lighting

From Valve Developer Community
Revision as of 03:07, 14 December 2008 by Mflux (talk | contribs)
Jump to: navigation, search

This tutorial will lay out how to create a light that appears and lights up the world around you caused by the muzzle flash of the gun, like in counter-strike source.

First we will look at the Dynamics of this type of lighting known as a "Dlight" and then we will move on how to add the Dlight to the muzzle flash and then how you can implement this into other things.

Warning.png Warning:  Some problems may still occur such as small amounts of light and fully lit props

This code works perfectly for the EP2 engine on both single player and multiplayer, and does in fact work with NPC's. this idea has not been tested with the EP1 engine, but feel free to try


The Dlight, can be seen in its, i find, most popular way known as the "fire glow" this glow is what you find underneath the fire lighting up the ground, you can achieve this effect by simply ticking off the "Glow" in the flags of env_fire.

the Dligth can also be raised off the ground to light up not just the ground but the surrounding walls, but the best part of the Dlight i find is that it can be attached to a moving origin as used in this muzzle flash tutorial.

Almost every aspect of the Dlight can be edited, its color (RGB scale), radius of light, radius of distance from a wall and it's attachment point.

Muzzle Flash

Well lets begin the coding. The coding is a quick simple fix with some minor changes, and then some quick changes based on your likings.

first off open up c_baseanimating.cpp and head down to line 2993 or simply search for void C_BaseAnimating::ProcessMuzzleFlashEvent() and look at this little section of code

// Make an elight
dlight_t *el = effects->CL_AllocElight( LIGHT_INDEX_MUZZLEFLASH + index );
el->origin = vAttachment;
el->radius = random->RandomInt( 32, 64 ); 
el->decay = el->radius / 0.05f;
el->die = gpGlobals->curtime + 0.05f;
el->color.r = 255;
el->color.g = 192;
el->color.b = 64;
el->color.exponent = 5;

as you may notice the comment sais that we are creating an "Elight" but the beginning of the code calls for a Dlight so what your going to very simply do is scratch out all the pointless code and fix the fact that were calling an Elights and make them Dlights. (basically copy paste code for you)

// Muzzle Flash Lighting - SHADOW-KILLER 
dlight_t *dl = effects->CL_AllocDlight ( index );
dl->origin = vAttachment;
dl->radius = random->RandomInt( 32, 64 ); // radius of flash
dl->decay = dl->radius / 0.2f;  // original radius is 0.05f; **needed distance from a wall**
dl->die = gpGlobals->curtime + 0.05f;  // FIX ME: time causes somewhat weird lighting please adjust
dl->color.r = 255;
dl->color.g = 192;
dl->color.b = 64;
dl->color.exponent = 5;

Expanding the idea of Dlights

Warning.png Warning:  Large use of dlights will result in severe performance loss on low-end systems.

Here are a few things you can do that can add that little extra bit of realism that can effect the atmosphere of some level or mods drastically.

1. Raise the Dlight of the fire, that's right raise it!, right now the fires dlight is sitting on the floor raise it up to the center of the fire and make light up the room with it (LOOKS GREAT!!)

2. Add a dlight to the explosion, why? because the light u see coming from the explosion is a particle effect and doesn't effect the world around it, add a dligth effect everything! (IN TESTING)

3. Want to make your mod like the grand theft auto series? add a Dligh to the missile of the RPG and light up the surrounding walls as it flies down the hall way to its target (EXPERIENCE IT YOURSELF!) --Shadow-killer 01:01, 14 December 2008 (EST)