Muzzle Flash Lighting: Difference between revisions
m (Removed Ep2 fix, and included the ifdef. Less confusing) |
SirYodaJedi (talk | contribs) mNo edit summary |
||
(16 intermediate revisions by 9 users not shown) | |||
Line 1: | Line 1: | ||
{{ | {{lang|title=Muzzle Flash Lighting|Muzzle Flash Lighting}} | ||
|title=Muzzle Flash Lighting | |||
| | |||
}} | |||
This tutorial will show you how to create a muzzleflash that dynamically appears and lights up the world around | 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. | ||
This code was designed for any Orange Box based mod, singleplayer or multiplayer. It also works with NPCs, assuming | This code was designed for any Orange Box based mod, singleplayer or multiplayer. It also works with NPCs, assuming they work properly. | ||
{{warning| Some dynamic props will not be lit when using dlights.}} | {{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.}} | {{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.}} | {{Note | The quality of dynamic lights depends on the lightmap scale.}} | ||
== Implementing | == Implementing DLight-based Muzzleflashes == | ||
The coding begins! Only a few minor changes are needed here to make | 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()'' | First off, open up '''c_baseanimating.cpp''' and search for the function ''void C_BaseAnimating::ProcessMuzzleFlashEvent()'' | ||
Line 35: | Line 30: | ||
el->color.exponent = 5; | el->color.exponent = 5; | ||
</source> | </source> | ||
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: | |||
<source lang=cpp> | <source lang=cpp> | ||
Vector vAttachment, vAng; | Vector vAttachment, vAng; | ||
QAngle angles; | QAngle angles; | ||
GetAttachment(1, vAttachment, angles); // set 1 instead of "attachment" | |||
AngleVectors(angles, &vAng); | |||
AngleVectors( angles, &vAng ); | |||
vAttachment += vAng * 2; | vAttachment += vAng * 2; | ||
dlight_t *dl = effects->CL_AllocDlight ( index ); | dlight_t *dl = effects->CL_AllocDlight(index); | ||
dl->origin = vAttachment; | 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); | |||
</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:Weapons programming]] | [[Category:Weapons programming]] |
Latest revision as of 20:46, 16 May 2024
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.



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

