Muzzle Flash Lighting: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Complete revamp of this article.)
Line 1: Line 1:
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.
This tutorial will show you how to create a muzzleflash that dynamically appears and lights up the world around you, similar to the ones found in Counter-Strike: Source. These types of light are called "dlights".


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.
This code was designed for use with any Orange Box based mod, singleplayer or multiplayer. It also works with NPCs, assuming that you've gotten them to work properly.


{{warning| Some problems may still occur such as small amounts of light and fully lit props}}
{{warning| Some dynamic props will not be lit when using dlights.}}
{{warning| Too many may result in severe performance losses on low-end systems. When coding, dlights should only be used sparingly.}}


This code works perfectly for the EP2 engine on both single player and multiplayer, and does in fact work with NPC's.
== Implementing Dlight-based Muzzleflashes ==
this idea has not been tested with the EP1 engine, but feel free to try
 
== Dlight==
 
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==


The coding begins! Only a few minor changes are needed here to make your muzzleflashes look better than ever.
The coding begins! Only a few minor changes are needed here to make your muzzleflashes look better than ever.
Line 51: Line 40:
</pre>
</pre>
This two-light system works far better than most any other single dlight system and fixes Valves mistakes.
This two-light system works far better than most any other single dlight system and fixes Valves mistakes.
== Expanding the idea of Dlights==
{{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
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!
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


[[Category:Programming]]
[[Category:Programming]]

Revision as of 10:21, 22 February 2009

This tutorial will show you how to create a muzzleflash that dynamically appears and lights up the world around you, similar to the ones found in Counter-Strike: Source. These types of light are called "dlights".

This code was designed for use with any Orange Box based mod, singleplayer or multiplayer. It also works with NPCs, assuming that you've gotten them to work properly.

Warning.pngWarning: Some dynamic props will not be lit when using dlights.
Warning.pngWarning: Too many may result in severe performance losses on low-end systems. When coding, dlights should only be used sparingly.

Implementing Dlight-based Muzzleflashes

The coding begins! Only a few minor changes are needed here to make your muzzleflashes look better than ever.

First off, open up c_baseanimating.cpp and search for the function void C_BaseAnimating::ProcessMuzzleFlashEvent(). Then, 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 have noticed, Valve went a little crazy here and the result is a seemingly ugly muzzleflash. This tutorial is all about making things pretty, so we're going to change that. Replace the entries above with this:

			
dlight_t *dl = effects->CL_AllocDlight ( index );
dl->origin = vAttachment + Vector( 0, 0, 4 );
dl->color.r = 255;
dl->die = gpGlobals->curtime + 0.1f;
dl->radius = random->RandomFloat( 245.0f, 256.0f );
dl->color.g = dl->color.b = random->RandomInt( 95, 128 );

dlight_t *el= effects->CL_AllocElight( index );
el->origin = vAttachment;
el->color.r = 255;
el->color.g = dl->color.b = random->RandomInt( 95, 128 );
el->radius = random->RandomFloat( 260.0f, 290.0f );
el->die = gpGlobals->curtime + 0.1f;

This two-light system works far better than most any other single dlight system and fixes Valves mistakes.