Difference between revisions of "Adding Motion Blur"

From Valve Developer Community
Jump to: navigation, search
(Rendering the Motion Blur: Wrong variable name.. dog.)
(Rendering the Motion Blur: And Another!)
Line 123: Line 123:
 
   // Pretend we were never here, set everything back
 
   // Pretend we were never here, set everything back
 
   materials->[[SetRenderTarget]]( pOriginalRenderTarget );
 
   materials->[[SetRenderTarget]]( pOriginalRenderTarget );
   materials->DrawScreenSpaceQuad( pMatMBlur );
+
   materials->DrawScreenSpaceQuad( pMatScreen );
 
    
 
    
 
   materials->DepthRange( 0.0f, 1.0f );
 
   materials->DepthRange( 0.0f, 1.0f );

Revision as of 14:32, 18 August 2005

Garry motionblur.jpg

Motion Blur

Here's something that will be in an upcoming version of Garry's Mod. Motion Blur. This method doesn't use any shaders and will work on most graphics cards.

Summary

This effect is achieved by adding a transparent version of the front buffer to a seperate buffer then drawing that buffer on top of the original buffer. This could be achieved using only the front buffer - but that method is less versatile (which is one thing that the Garry's Mod fans love).

Creating a new Render Target

Open the file src/cl_dll/rendertexture.cpp and near the bottom add

 static CTextureReference s_pMoBlurTex0;
 
 ITexture *GetMoBlurTex0( void )
 {
 	if( !s_pMoBlurTex0 )
 	{
 		s_pMoBlurTex0.InitRenderTarget( 256, 256, RT_SIZE_FULL_FRAME_BUFFER, IMAGE_FORMAT_ARGB8888, MATERIAL_RT_DEPTH_NONE );
 		Assert( !IsErrorTexture( s_pMoBlurTex0 ) );
 	}
 
 	return s_pMoBlurTex0;
 }

This is the function we will use to get a pointer to the render target. The '256' numbers are meaningless, they get overridden by RT_SIZE_FULL_FRAME_BUFFER which forces the texture to be the size of the frame buffer.

So now in the file src/cl_dll/rendertexture.h add:

 ITexture *GetMoBlurTex0( void );

This will allow us to use this function.


Rendering the Motion Blur

First of all lets add some console commands that we're going to use. I make this console commands because people will want to change them in Garry's Mod. This code can go pretty much anywhere that it can be seen from CViewRender::RenderView( const CViewSetup &view, bool drawViewModel ) in view_scene.cpp.

 // To toggle the blur on and off
 ConVar pp_motionblur("pp_motionblur", "1", 0, "Motion Blur"); 
 // The amount of alpha to use when adding the FB to our custom buffer
 ConVar pp_motionblur_addalpha("pp_motionblur_addalpha", "0.1", 0, "Motion Blur Alpha");
 // The amount of alpha to use when adding our custom buffer to the FB
 ConVar pp_motionblur_drawalpha("pp_motionblur_drawalpha", "1", 0, "Motion Blur Draw Alpha");
 // Delay to add between capturing the FB
 ConVar pp_motionblur_time("pp_motionblur_time", "0.05", 0, "The amount of time to wait until updating the FB");

All pretty self explanatory.

 void CViewRender::DoMotionBlur( void )
 {
 	if ( pp_motionblur.GetInt() == 0 ) return;
 
 	static float fNextDrawTime = 0.0f;
 
 	bool found;
 	IMaterialVar* mv = NULL;
 	IMaterial *pMatScreen = NULL;
 	ITexture *pMoBlur = NULL;
 
 
 	// Get the front buffer material
 	pMatScreen = materials->FindMaterial( "frontbuffer", TEXTURE_GROUP_OTHER, true );
 	// Get our custom render target
 	pMoBlur = GetMoBlurTex0();
 	// Store the current render target	
 	ITexture *pOriginalRenderTarget = materials->GetRenderTarget();
 
 	// Set the camera up so we can draw the overlay
 	int oldX, oldY, oldW, oldH;
 	materials->GetViewport( oldX, oldY, oldW, oldH );
 
 	materials->MatrixMode( MATERIAL_PROJECTION );
 	materials->PushMatrix();
 	materials->LoadIdentity();	
 
 	materials->MatrixMode( MATERIAL_VIEW );
 	materials->PushMatrix();
 	materials->LoadIdentity();	
 
 
 	if ( fNextDrawTime < gpGlobals->curtime )
 	{
 		UpdateScreenEffectTexture( 0 );
 
 		// Set the alpha to whatever our console variable is
 		mv = pMatScreen->FindVar( "$alpha", &found, false );
 		if (found)
 		{
 			if ( fNextDrawTime == 0 )
 			{
 				mv->SetFloatValue( 1.0f );
 			}
 			else
 			{
 				mv->SetFloatValue( pp_motionblur_addalpha.GetFloat() );
 			}
 		}
 
 		materials->SetRenderTarget( pMoBlur );
 		materials->DrawScreenSpaceQuad( pMatScreen );
 
 		// Set the next draw time according to the convar
 		fNextDrawTime = gpGlobals->curtime + pp_motionblur_time.GetFloat();
 	}
 
 	// Set the alpha
 	mv = pMatScreen->FindVar( "$alpha", &found, false );
 	if (found)
 	{
 		mv->SetFloatValue( pp_motionblur_drawalpha.GetFloat() );
 	}
 
 	// Set the texture to our buffer
 	mv = pMatScreen->FindVar( "$basetexture", &found, false );
 	if (found)
 	{
 		mv->SetTextureValue( pMoBlur );
 	}
 
 	// Pretend we were never here, set everything back
 	materials->SetRenderTarget( pOriginalRenderTarget );
 	materials->DrawScreenSpaceQuad( pMatScreen );
 
 	materials->DepthRange( 0.0f, 1.0f );
 	materials->MatrixMode( MATERIAL_PROJECTION );
 	materials->PopMatrix();
 	materials->MatrixMode( MATERIAL_VIEW );
 	materials->PopMatrix();
 }

I hope that all makes sense.

Now in the CViewRender::RenderView( const CViewSetup &view, bool drawViewModel ) function which is located in view_scene.cpp, add this:

 DoMotionBlur();

Just before

 // Draw the 2D graphics

Material Files

That should do it. You need to add a material file called frontbuffer.vmt in your materials folder - although you could probably use an existing one. Here's what mine looks like:

 "UnlitGeneric"
 {
 	"$basetexture" "_rt_FullFrameFB"
 	"$ignorez"		1
 }

Finished

I chose this tutorial to do because it covered a few things that people continually ask me about. I hope it helps someone.

Garry Newman