Difference between revisions of "Muzzle Flash Lighting"

From Valve Developer Community
Jump to: navigation, search
m (Note containing source 2013 info)
 
(34 intermediate revisions by 14 users not shown)
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.
+
{{pov}}
 +
{{otherlang2
 +
|title=Muzzle Flash Lighting
 +
|ru=Muzzle_Flash_Lighting:ru
 +
|de=Muzzle_Flash_Lighting:de
 +
}}
  
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 tutorial will show you how to create a muzzleflash that dynamically appears and lights up the world around you, like in Counter-Strike: Source. These types of lights are called "dlights".
  
{{warning| Some problems may still occur such as small amounts of light and fully lit props}}
+
This code was designed for any Orange Box based mod, singleplayer or multiplayer. It also works with NPCs, assuming that you've gotten them to work properly.
  
This code works perfectly for the EP2 engine on both single player and multiplayer, and does in fact work with NPC's.
+
{{warning| Some dynamic props will not be lit when using dlights.}}
this idea has not been tested with the EP1 engine, but feel free to try
+
{{warning| Too many dlights may result in severe performance losses on low-end systems. When coding for lighting, dlights should be used sparingly.}}
 +
{{Note | From Slam12f, even better if you can write a console command to enable or disable dlights. Is easy to implement with help use static ConVar.}}
 +
{{Note | The quality of dynamic lights depends on the lightmap scale.}}
  
== Dlight==
+
== Implementing Dlight-based Muzzleflashes ==
  
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 coding begins! Only a few minor changes are needed here to make your muzzleflashes look better than ever.
  
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.
+
First off, open up '''c_baseanimating.cpp''' and search for the function ''void C_BaseAnimating::ProcessMuzzleFlashEvent()''
 +
<source lang=cpp>
 +
Vector vAttachment;
 +
QAngle dummyAngles;
 +
GetAttachment( attachment, vAttachment, dummyAngles );
  
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.
+
// Make an elight
 
 
 
 
== 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
 
<pre>// Make an elight
 
 
dlight_t *el = effects->CL_AllocElight( LIGHT_INDEX_MUZZLEFLASH + index );
 
dlight_t *el = effects->CL_AllocElight( LIGHT_INDEX_MUZZLEFLASH + index );
 
el->origin = vAttachment;
 
el->origin = vAttachment;
Line 31: Line 34:
 
el->color.g = 192;
 
el->color.g = 192;
 
el->color.b = 64;
 
el->color.b = 64;
el->color.exponent = 5;</pre>
+
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
+
</source>
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)
+
As you may have noticed, Valve went a little crazy here and the result was 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:
<pre>// Muzzle Flash Lighting - SHADOW-KILLER
+
<source lang=cpp>
dlight_t *dl = effects->CL_AllocDlight ( index );
+
Vector vAttachment, vAng;
dl->origin = vAttachment;
+
QAngle angles;
dl->radius = random->RandomInt( 32, 64 ); // radius of flash
+
#ifdef HL2_EPISODIC
dl->decay = dl->radius / 0.2f;  // original radius is 0.05f; **needed distance from a wall**
+
                        GetAttachment( 1, vAttachment, angles ); // set 1 instead "attachment"
dl->die = gpGlobals->curtime + 0.05f; // FIX ME: time causes somewhat weird lighting please adjust
+
#else
dl->color.r = 255;
+
                        GetAttachment( attachment, vAttachment, angles );
dl->color.g = 192;
+
#endif
dl->color.b = 64;
+
AngleVectors( angles, &vAng );
dl->color.exponent = 5;</pre>
+
vAttachment += vAng * 2;
 
+
 
+
dlight_t *dl = effects->CL_AllocDlight ( index );
== Expanding the idea of Dlights==
+
dl->origin = vAttachment;
 
+
dl->color.r = 231;
{{warning| Large use of dlights will result in severe performance loss on low-end systems.}}
+
dl->color.g = 219;
 
+
dl->color.b = 14;
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.
+
dl->die = gpGlobals->curtime + 0.05f;
 
+
dl->radius = random->RandomFloat( 245.0f, 256.0f );
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!!)
+
dl->decay = 512.0f;
 
+
</source>
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)
+
{{Note | In Source 2013, the episodic code version is the only one that will work.}}
 
+
Edit: Changed the code for the colors, by default it was set to a magenta-ish color. A good muzzle flash color is closer to white, you can try 252, 238, 128 for a realistic washed out yellow.
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!)
 
  
 +
== Notes ==
 +
I am currently working further on this idea, the updates will add more visual effects to this idea to create a more realistic and cleaner look to the muzzle flash. the update will only work on the orange box engine
 +
A link will be provided HERE when the new muzzle flash is done
  
 +
A NOTE FROM GAMERMAN12: I had updated this code so that the lights decay after firing, so you have a flashing effect when shooting machine guns. And it fades so that it's alitle more realistic. Thanks.
  
[[Category:Programming]] --[[User:Shadow-killer|Shadow-killer]] 01:01, 14 December 2008 (EST)
+
[[Category:Weapons programming]]

Latest revision as of 12:33, 12 January 2015

Deutsch Русский

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

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

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

Note: From Slam12f, even better if you can write a console command to enable or disable dlights. Is easy to implement with help use static ConVar.
Note: The quality of dynamic lights depends on the lightmap scale.

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()

Vector vAttachment;
QAngle dummyAngles;
GetAttachment( attachment, vAttachment, dummyAngles );

// 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 was 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:

			Vector vAttachment, vAng;
			QAngle angles;
#ifdef HL2_EPISODIC
                        GetAttachment( 1, vAttachment, angles ); // set 1 instead "attachment"
#else
                        GetAttachment( attachment, vAttachment, angles );
#endif
			AngleVectors( angles, &vAng );
			vAttachment += vAng * 2;
		
			dlight_t *dl = effects->CL_AllocDlight ( index );
			dl->origin = vAttachment;
			dl->color.r = 231;
			dl->color.g = 219;
			dl->color.b = 14;
			dl->die = gpGlobals->curtime + 0.05f;
			dl->radius = random->RandomFloat( 245.0f, 256.0f );
			dl->decay = 512.0f;
Note: In Source 2013, the episodic code version is the only one that will work.

Edit: Changed the code for the colors, by default it was set to a magenta-ish color. A good muzzle flash color is closer to white, you can try 252, 238, 128 for a realistic washed out yellow.

Notes

I am currently working further on this idea, the updates will add more visual effects to this idea to create a more realistic and cleaner look to the muzzle flash. the update will only work on the orange box engine A link will be provided HERE when the new muzzle flash is done

A NOTE FROM GAMERMAN12: I had updated this code so that the lights decay after firing, so you have a flashing effect when shooting machine guns. And it fades so that it's alitle more realistic. Thanks.