Viewmodel Blood Splatter Overlay: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Cleanup and QoL changes)
m (Replaced "WL_Feet" with "WL_Waist", so the blood doesn't instantly clean when touching *any* water. Now it only does it when fully underwater + added a note to the external links section.)
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
== Introduction ==
== Introduction ==


This is a tutorial for a simple material proxy system that we will use to switch viewmodel textures dependent on CQB.
In this tutorial, we'll be setting up a simple material proxy system that we can use to overlay a blood detail texture onto our viewmodel based on close-quarters combat.


Final results that could be made from this are, for example, hands and weapons covered in goo, acid, blood, water, animated raindrop water normals, and many more visual design elements.
The final results that can be made from this are, for example, hands and weapons covered in goo, acid, blood, water, animated raindrop water normals, and many more visual design elements.


== Requirements ==
== Requirements ==


* A Source SDK 2013 engine branch mod.
* Beginner/Intermediate knowledge of C++.
* Beginner/Intermediate knowledge of C++.
* Source SDK 2007 engine branch (2013 compatible version in process of writing).
* Knowledge and familiarity with textures and materials.
* Knowledge and familiarity with textures and materials.


== What You Will Learn ==


{{Note|This tutorial was written on the 2007 engine branch due to many issues within the port of this gameplay mechanic to the 2013 branch. I am seeking the community's help with this! '''Please update this page'''.}}
* Creating a new material proxy system using the [[$detail]] parameter.
* Creating an overlay of blood onto a viewmodel after shooting flesh or blood materials (NPCs included, of course) at close quarters.


== What You Will Learn ==
== The Implementation ==


* To create a new material proxy system using the [[$detail]] parameter.
Before starting, we'll need this file:
* To create an overlay of blood onto a viewmodel after shooting flesh or blood materials (NPCs included of course) at close quarters.
* [[Viewmodel Blood Splatter Overlay/bloodoverlayproxy.cpp|bloodoverlayproxy.cpp]]


== Known Bugs ==


Without using different names for materials and textures relating to multiple weapons, a blood overlay switch will happen to all weapons - Compile models with different material name associations.
Which you'll put into '''<src code directory>/src/game/client/'''


[[Image:Blood proxy System.jpg]]
=== c_baseplayer.h ===


== Tutorial ==
In '''c_baseplayer.h''', inside the <code>public:</code> section, add the following line:
=== Step 1: Programming ===
==== proxy_blood.cpp ====
On the client-side, create a file called '''proxy_blood.cpp''':
<source lang=cpp>
<source lang=cpp>
#include "cbase.h"
bool m_bShouldDrawBloodOverlay;
#include <KeyValues.h>
</source>
#include "materialsystem/IMaterialVar.h"
#include "materialsystem/IMaterial.h"
#include "materialsystem/ITexture.h"
#include "materialsystem/IMaterialSystem.h"
#include "FunctionProxy.h"
#include "toolframework_client.h"


// memdbgon must be the last include file in a .cpp file!!!
=== c_baseplayer.cpp ===
#include "tier0/memdbgon.h"


// Forward declarations
In '''c_baseplayer.cpp''', below <code>RecvPropString( RECVINFO(m_szLastPlaceName) ),</code> add:
void ToolFramework_RecordMaterialParams( IMaterial *pMaterial );
<source lang=cpp>
RecvPropBool( RECVINFO( m_bShouldDrawBloodOverlay ) ),
</source>


ConVar cl_viewmodel_blood( "cl_viewmodel_blood", "0" );
Then in the constructor, below <code>ListenForGameEvent( "base_player_teleported" );</code>, add:
<source lang=cpp>
m_bShouldDrawBloodOverlay = false;
</source>


//-----------------------------------------------------------------------------
=== player.h ===
// Purpose:
//-----------------------------------------------------------------------------
bool C_BaseEntity::IsCoveredInBlood()
{
return cl_viewmodel_blood.GetBool();
}


//-----------------------------------------------------------------------------
In '''player.h''', inside the <code>public:</code> section, add the following line:
// Purpose:  
<source lang=cpp>
//-----------------------------------------------------------------------------
CNetworkVar( bool, m_bShouldDrawBloodOverlay ); // Have we been hit or have blood splatted on us?
C_BaseEntity *C_BaseViewModel::GetCoveredBloodEntity()
</source>
{
C_BaseEntity *pWeapon = m_hWeapon.Get();
if ( pWeapon )
{
return pWeapon;
}


return NULL;
=== player.cpp ===
}


//-----------------------------------------------------------------------------
In '''player.cpp''', somewhere inside of <code>BEGIN_DATADESC( CBasePlayer )</code>, add:
// Purpose:  
<source lang=cpp>
//-----------------------------------------------------------------------------
DEFINE_FIELD( m_bShouldDrawBloodOverlay, FIELD_BOOLEAN ),
class CViewmodelBlood_Proxy : public IMaterialProxy
</source>
{
public:
CViewmodelBlood_Proxy();


bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
Inside the constructor and below <code>m_bitsDamageType = 0;</code>, add:
void OnBind( void *pC_BaseEntity );
<source lang=cpp>
IMaterial *GetMaterial();
m_bShouldDrawBloodOverlay = false;
virtual void Release() { delete this; }
</source>


private:
Then inside <code>IMPLEMENT_SERVERCLASS_ST( CBasePlayer, DT_BasePlayer )</code>, below <code>SendPropString (SENDINFO(m_szLastPlaceName) ),</code>, add:
IMaterialVar *m_pDetailBlendFactor;
<source lang=cpp>
SendPropBool( SENDINFO(m_bShouldDrawBloodOverlay) ),
</source>


IMaterial *m_pMaterial;
=== basecombatcharacter.cpp ===


bool m_bPlayerBlood;
In '''basecombatcharacter.cpp''', inside the <code>OnTakeDamage_Alive( const CTakeDamageInfo &info )</code> function above <code>return 1;</code>, add:
};
<source lang=cpp>
// Handle the viewmodel blood splatter overlay effect here:
if ( ( info.GetDamageType() & ( DMG_BULLET | DMG_SLASH | DMG_BLAST | DMG_CLUB | DMG_BUCKSHOT ) ) )
{
CBasePlayer *pPlayer = UTIL_GetLocalPlayer();
if ( pPlayer == this )
pPlayer->m_bShouldDrawBloodOverlay = true;


//-----------------------------------------------------------------------------
pPlayer = ToBasePlayer( info.GetAttacker() );
// Purpose:
if ( pPlayer && ( this->BloodColor() != BLOOD_COLOR_MECH ) )
//-----------------------------------------------------------------------------
{
CViewmodelBlood_Proxy::CViewmodelBlood_Proxy()
if ( pPlayer->GetAbsOrigin().DistTo( this->GetAbsOrigin() ) < 200.0f )
{
pPlayer->m_bShouldDrawBloodOverlay = true;
m_pMaterial = NULL;
}
m_pDetailBlendFactor = NULL;
}
 
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CViewmodelBlood_Proxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
{
m_pMaterial = pMaterial;
 
bool bFound;
m_pDetailBlendFactor = pMaterial->FindVar( "$detailblendfactor", &bFound );
if ( !bFound )
{
m_pDetailBlendFactor = NULL;
return false;
}
}
</source>


m_bPlayerBlood = pKeyValues->GetInt( "player", 0 ) > 0;
== The Materials ==


return true;
=== Viewmodel VMTs ===
}


//-----------------------------------------------------------------------------
Now you'll have to edit every single viewmodel '''.vmt''' to include the following snippet of code:
// Purpose:  
<source lang="text" highlight=5-15>
//-----------------------------------------------------------------------------
"VertexLitGeneric"
void CViewmodelBlood_Proxy::OnBind( void *pRenderable )
{
{
IMaterial *pMaterial = GetMaterial();
...
if ( pMaterial )
{
if ( pRenderable )
{
IClientRenderable *pRend = (IClientRenderable *)pRenderable;
C_BaseEntity *pEnt = pRend->GetIClientUnknown()->GetBaseEntity();
if ( pEnt )
{
C_BaseEntity *pWeapon = pEnt->GetCoveredBloodEntity();


if ( pWeapon )
"$detail" "detail/blood_detail"
{
"$detailblendmode" "2"
CBaseEntity *pTarget = m_bPlayerBlood ? pWeapon->GetOwnerEntity() : NULL;
"$detailblendfactor" "0.0"
if ( pTarget )
"$detailscale" "1.0"
{
pWeapon = pTarget;
}


if ( pWeapon->IsCoveredInBlood() )
"Proxies"
{
{
m_pDetailBlendFactor->SetFloatValue( 1.0f );
"BloodyTexture"
}
else
{
m_pDetailBlendFactor->SetFloatValue( 0.0f );
}
}
else
{
m_pDetailBlendFactor->SetFloatValue( 0.0f );
}
}
}
 
if ( ToolsEnabled() )
{
{
ToolFramework_RecordMaterialParams( GetMaterial() );
}
}
}
}
}
}
</source>
Notice how we're calling for '''materials/detail/blood_detail.vtf''', this is the blood detail texture that gets placed on top of our viewmodel.
== Conclusion ==
Now all that remains is making your blood detail texture for this. Still, for convenience/testing sake, you can download the included placeholder texture found in the "External links" section below.
And that's all there is to it. If set up correctly, you should now be able to see the blood splatter texture on your viewmodel based on the snippet of code we placed into '''basecombatcharacter.cpp'''.


//-----------------------------------------------------------------------------
== Additional Functionality (Optional) ==
// Purpose:
 
//-----------------------------------------------------------------------------
If you want to be able to clean off the blood by going into water, head over to '''player.cpp''' and find the <code>WaterMove()</code> function.
IMaterial *CViewmodelBlood_Proxy::GetMaterial()
{
return m_pMaterial;
}


EXPOSE_INTERFACE( CViewmodelBlood_Proxy, IMaterialProxy, "ViewmodelBloodProxy" IMATERIAL_PROXY_INTERFACE_VERSION );
Inside it, at the bottom, but before <code>UpdateUnderwaterState();</code> add this:
<source lang=cpp>
if ( GetWaterLevel() > WL_Waist )
m_bShouldDrawBloodOverlay = false;
</source>
</source>


==== c_baseentity.h ====
And if you want to be able to clean off the blood when consuming a health kit, find the <code>TakeHealth( float flHealth, int bitsDamageType )</code> function and add this inside of it:
In '''c_baseentity.h''', inside the '''public:''' section add the following lines:
<source lang=cpp>
<source lang=cpp>
virtual C_BaseEntity *GetCoveredBloodEntity() { return this; }
m_bShouldDrawBloodOverlay = false;
virtual bool IsCoveredInBlood();
</source>
</source>


Tutorial being made now...
== External links ==
 
[https://mega.nz/file/RsNm2DgK#qHysT50ZYbX_WyXg8FcnyO7Bwb88lfHE1qlXo__9FAI Download BloodDetailTexture.rar] - A placeholder blood detail texture by [[User:GamerDude27|Ian B.]]
{{Note|If you're gonna use this placeholder texture, make sure to rename "blood_hands" to "blood_detail".}}


[[Category: Programming]] [[Category: Tutorials]] [[Category: Weapons]] [[Category: Miscellaneous]]
[[Category:Programming]]
[[Category:Tutorials]]
[[Category:Free source code]]

Latest revision as of 14:15, 11 October 2023

Introduction

In this tutorial, we'll be setting up a simple material proxy system that we can use to overlay a blood detail texture onto our viewmodel based on close-quarters combat.

The final results that can be made from this are, for example, hands and weapons covered in goo, acid, blood, water, animated raindrop water normals, and many more visual design elements.

Requirements

  • A Source SDK 2013 engine branch mod.
  • Beginner/Intermediate knowledge of C++.
  • Knowledge and familiarity with textures and materials.

What You Will Learn

  • Creating a new material proxy system using the $detail parameter.
  • Creating an overlay of blood onto a viewmodel after shooting flesh or blood materials (NPCs included, of course) at close quarters.

The Implementation

Before starting, we'll need this file:


Which you'll put into <src code directory>/src/game/client/

c_baseplayer.h

In c_baseplayer.h, inside the public: section, add the following line:

	bool m_bShouldDrawBloodOverlay;

c_baseplayer.cpp

In c_baseplayer.cpp, below RecvPropString( RECVINFO(m_szLastPlaceName) ), add:

		RecvPropBool( RECVINFO( m_bShouldDrawBloodOverlay ) ),

Then in the constructor, below ListenForGameEvent( "base_player_teleported" );, add:

	m_bShouldDrawBloodOverlay = false;

player.h

In player.h, inside the public: section, add the following line:

	CNetworkVar( bool, m_bShouldDrawBloodOverlay ); // Have we been hit or have blood splatted on us?

player.cpp

In player.cpp, somewhere inside of BEGIN_DATADESC( CBasePlayer ), add:

	DEFINE_FIELD( m_bShouldDrawBloodOverlay, FIELD_BOOLEAN ),

Inside the constructor and below m_bitsDamageType = 0;, add:

	m_bShouldDrawBloodOverlay = false;

Then inside IMPLEMENT_SERVERCLASS_ST( CBasePlayer, DT_BasePlayer ), below SendPropString (SENDINFO(m_szLastPlaceName) ),, add:

	SendPropBool( SENDINFO(m_bShouldDrawBloodOverlay) ),

basecombatcharacter.cpp

In basecombatcharacter.cpp, inside the OnTakeDamage_Alive( const CTakeDamageInfo &info ) function above return 1;, add:

	// Handle the viewmodel blood splatter overlay effect here:
	if ( ( info.GetDamageType() & ( DMG_BULLET | DMG_SLASH | DMG_BLAST | DMG_CLUB | DMG_BUCKSHOT ) ) )
	{
		CBasePlayer *pPlayer = UTIL_GetLocalPlayer();
		if ( pPlayer == this )
			pPlayer->m_bShouldDrawBloodOverlay = true;

		pPlayer = ToBasePlayer( info.GetAttacker() );
		if ( pPlayer && ( this->BloodColor() != BLOOD_COLOR_MECH ) )
		{
			if ( pPlayer->GetAbsOrigin().DistTo( this->GetAbsOrigin() ) < 200.0f )
				pPlayer->m_bShouldDrawBloodOverlay = true;
		}
	}

The Materials

Viewmodel VMTs

Now you'll have to edit every single viewmodel .vmt to include the following snippet of code:

"VertexLitGeneric"
{
	...

	"$detail" "detail/blood_detail"
	"$detailblendmode" "2"
	"$detailblendfactor" "0.0"
	"$detailscale" "1.0"

	"Proxies"
	{
		"BloodyTexture"
		{
		}
	}
}

Notice how we're calling for materials/detail/blood_detail.vtf, this is the blood detail texture that gets placed on top of our viewmodel.

Conclusion

Now all that remains is making your blood detail texture for this. Still, for convenience/testing sake, you can download the included placeholder texture found in the "External links" section below.

And that's all there is to it. If set up correctly, you should now be able to see the blood splatter texture on your viewmodel based on the snippet of code we placed into basecombatcharacter.cpp.

Additional Functionality (Optional)

If you want to be able to clean off the blood by going into water, head over to player.cpp and find the WaterMove() function.

Inside it, at the bottom, but before UpdateUnderwaterState(); add this:

	if ( GetWaterLevel() > WL_Waist )
		m_bShouldDrawBloodOverlay = false;

And if you want to be able to clean off the blood when consuming a health kit, find the TakeHealth( float flHealth, int bitsDamageType ) function and add this inside of it:

	m_bShouldDrawBloodOverlay = false;

External links

Download BloodDetailTexture.rar - A placeholder blood detail texture by Ian B.

Note.pngNote:If you're gonna use this placeholder texture, make sure to rename "blood_hands" to "blood_detail".