Muzzle Flash Lighting: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
mNo edit summary
 
(51 intermediate revisions by 23 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.
{{lang|title=Muzzle Flash Lighting|Muzzle Flash Lighting}}


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 the player, like in Counter-Strike: Source. These types of lights are called [[dlight]]s on brushes, and [[elight]]s on models.


{{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 they 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 | 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 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 28:
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)
Valve went a little crazy here and the result was a seemingly ugly muzzleflash. This tutorial is all about making things pretty, so lets 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
GetAttachment(1, vAttachment, angles); // set 1 instead of "attachment"
dl->decay = dl->radius / 0.2f; // original radius is 0.05f; **needed distance from a wall**
AngleVectors(angles, &vAng);
dl->die = gpGlobals->curtime + 0.05f;  // FIX ME: time causes somewhat weird lighting please adjust
vAttachment += vAng * 2;
dl->color.r = 255;
dl->color.g = 192;
dl->color.b = 64;
dl->color.exponent = 5;</pre>
 
 
== Expanding the idea of Dlights==
 
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)
dlight_t *dl = effects->CL_AllocDlight(index);
dl->origin = vAttachment;


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!)
// Original color values
int originalR = 231;
int originalG = 219;
int originalB = 14;


// Randomize color components within the range of +/- 20
dl->color.r = originalR + random->RandomInt(-20, 20);
dl->color.g = originalG + random->RandomInt(-20, 20);
dl->color.b = originalB + random->RandomInt(0, 0);


== Credits==
// Randomize the die value by +/- 0.01
This code is purely edited by myself "SHADOW-KILLER" with reference to the env_fire entity, please if you may keep my name in the code in respect to myself.
dl->die = gpGlobals->curtime + 0.05f + random->RandomFloat(-0.01f, 0.01f);
These ideas will be seen in my up and coming multilayer mod DEAD LIGHT
dl->radius = random->RandomFloat(245.0f, 256.0f);


So that's it, simple eh? well I hope you enjoy your Muzzle Flash lighting. BEST RESULTS SEEN IN THE DARK
// Randomize the decay value
dl->decay = random->RandomFloat(400.0f, 600.0f);
</source>
{{tip | You can try 252, 238, 128 for a realistic washed out yellow color.}}
{{idea | Write a console command to enable or disable dlights to shave off performance losses}}


[[Category:Programming]] --[[User:Shadow-killer|Shadow-killer]] 01:01, 14 December 2008 (EST)
[[Category:Weapons programming]]

Latest revision as of 20:46, 16 May 2024

English (en)Deutsch (de)Русский (ru)Translate (Translate)

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

This code was designed for any Orange Box based mod, singleplayer or multiplayer. It also works with NPCs, assuming they work properly.

Warning.pngWarning: Some dynamic props will not be lit when using dlights.
Warning.pngWarning: Too many dlights may result in severe performance losses on low-end systems. When coding for lighting, dlights should be used sparingly.
Note.pngNote: 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 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;

Valve went a little crazy here and the result was a seemingly ugly muzzleflash. This tutorial is all about making things pretty, so lets change that. Replace the entries above with this:

			Vector vAttachment, vAng;
			QAngle angles;
			GetAttachment(1, vAttachment, angles); // set 1 instead of "attachment"
			AngleVectors(angles, &vAng);
			vAttachment += vAng * 2;

			dlight_t *dl = effects->CL_AllocDlight(index);
			dl->origin = vAttachment;

			// Original color values
			int originalR = 231;
			int originalG = 219;
			int originalB = 14;

			// Randomize color components within the range of +/- 20
			dl->color.r = originalR + random->RandomInt(-20, 20);
			dl->color.g = originalG + random->RandomInt(-20, 20);
			dl->color.b = originalB + random->RandomInt(0, 0);

			// Randomize the die value by +/- 0.01
			dl->die = gpGlobals->curtime + 0.05f + random->RandomFloat(-0.01f, 0.01f);
			dl->radius = random->RandomFloat(245.0f, 256.0f);

			// Randomize the decay value
			dl->decay = random->RandomFloat(400.0f, 600.0f);
Tip.pngTip: You can try 252, 238, 128 for a realistic washed out yellow color.
Tip.pngIdea: Write a console command to enable or disable dlights to shave off performance losses