Adding a Camera Bone to a Viewmodel: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(marked for cleanup.)
No edit summary
Line 1: Line 1:
{{cleanup}}
{{cleanup}}
a camera bone is a bone inside of your ViewModel which can be animated to control the movement of the camera with animations.
What are camera bones?
You can have cool results like a camera shake when reloading.


adding it is super easy all you will need to do is go inside of '''view.cpp'''
To put it simply camera bones are exactly what the name suggests, bone inside of your viewmodel that affect the camera, the in-game camera will copy rotation/transforms from your "camera" bone that you've created in Blender,  allowing animators to create good looking handmade camera effects for viewmodel animations such as reloads.


and right underneath this section of code inside of the '''void CViewRender::Render( vrect_t *rect )''' function
Adding it is extremely simple, we will start off by going to ```view.cpp:1208```
<source lang=cpp>
        int flags = 0;
if( eEye == STEREO_EYE_MONO || eEye == STEREO_EYE_LEFT || ( g_ClientVirtualReality.ShouldRenderHUDInWorld() ) )
{
flags = RENDERVIEW_DRAWHUD;
}
    if ( drawViewModel )
    {
    flags |= RENDERVIEW_DRAWVIEWMODEL;
    }
if( eEye == STEREO_EYE_RIGHT )
{
// we should use the monitor view from the left eye for both eyes
flags |= RENDERVIEW_SUPPRESSMONITORRENDERING;
}
</source>


add the following code
Inside of '''void CViewRender::Render( vrect_t *rect )''', scroll down to line 1408 where you should add the following code:


<source lang=cpp>
<source lang=cpp>
Line 61: Line 44:
</source>
</source>


next to fix the '''cl_camera_anim_intensity''' go to '''ConVar zoom_sensitivity_ratio''' inside of '''view.cpp'''
After you've added this code, go and add a new ConVar:
 
and add the following below it


<source lang=cpp>
<source lang=cpp>
ConVar cl_camera_anim_intensity("cl_camera_anim_intensity", "1.0", FCVAR_ARCHIVE, "intensity of the cammera animation");
ConVar cl_camera_anim_intensity("cl_camera_anim_intensity", "1.0", FCVAR_ARCHIVE, "Intensity of cambone animations");
</source>
</source>


and you are done!
With that you are done on the code side.
 
please remember that you need to have 2 camera bones inside of your ViewModel one is stationary the other is the one you will animate
the one that's stationary you will need to make an attachment inside of the ViewModel called '''"camera_root"''' and attach it to the stationary bone.
 
the other that you are going to animate should have an attachment called '''"camera"'''.
 
remember that the bones should be at the center of the ViewModel where the player is supposed to see the ViewModel with his camera
 
 
 


On the asset side, inside of Blender or your animation software of choice you will need two bones in the viewmodel that you plan to support this feature. One bone needs to be named ```camera_root```, this bone should NOT move at all and should not be animated, next make a bone named ```camera```, this is the bone we want to animate, when you keyframe and animate it's rotation, it should be reflected in game provided you added the code above, if you want a preview of how the camera effects will look like in game, make the Blender "Camera" object a child of the ```camera``` bone (make sure to read that twice, a lot of overlapping names).


here is an example of what it looks like in action: https://www.youtube.com/watch?v=WnW0vXmUBp4


have fun!
Here is an example of what it looks like in action: https://www.youtube.com/watch?v=WnW0vXmUBp4

Revision as of 12:15, 20 December 2022

Broom icon.png
This article or section needs to be cleaned up to conform to a higher standard of quality.
For help, see the VDC Editing Help and Wikipedia cleanup process. Also, remember to check for any notes left by the tagger at this article's talk page.

What are camera bones?

To put it simply camera bones are exactly what the name suggests, bone inside of your viewmodel that affect the camera, the in-game camera will copy rotation/transforms from your "camera" bone that you've created in Blender, allowing animators to create good looking handmade camera effects for viewmodel animations such as reloads.

Adding it is extremely simple, we will start off by going to ```view.cpp:1208```

Inside of void CViewRender::Render( vrect_t *rect ), scroll down to line 1408 where you should add the following code:

        //--------------------------------
		// Handle camera anims
		//--------------------------------

		if (!UseVR() && pPlayer && cl_camera_anim_intensity.GetFloat() > 0)
		{
			if (pPlayer->GetViewModel(0))
			{
				int attachment = pPlayer->GetViewModel(0)->LookupAttachment("camera");
				if (attachment != -1)
				{
					

					int rootBone = pPlayer->GetViewModel(0)->LookupAttachment("camera_root");
					Vector cameraOrigin = Vector(0, 0, 0);
					QAngle cameraAngles = QAngle(0, 0, 0);
					Vector rootOrigin = Vector(0, 0, 0);
					QAngle rootAngles = QAngle(0, 0, 0);

					pPlayer->GetViewModel(0)->GetAttachmentLocal(attachment, cameraOrigin, cameraAngles);
					if (rootBone != -1)
					{
						pPlayer->GetViewModel(0)->GetAttachmentLocal(rootBone, rootOrigin, rootAngles);
						cameraOrigin -= rootOrigin;
						cameraAngles -= rootAngles;

						DevMsg("camera attachment found\n");
					}
					view.angles += cameraAngles * cl_camera_anim_intensity.GetFloat();
					view.origin += cameraOrigin * cl_camera_anim_intensity.GetFloat();
				}
			}
		}

After you've added this code, go and add a new ConVar:

ConVar cl_camera_anim_intensity("cl_camera_anim_intensity", "1.0", FCVAR_ARCHIVE, "Intensity of cambone animations");

With that you are done on the code side.

On the asset side, inside of Blender or your animation software of choice you will need two bones in the viewmodel that you plan to support this feature. One bone needs to be named ```camera_root```, this bone should NOT move at all and should not be animated, next make a bone named ```camera```, this is the bone we want to animate, when you keyframe and animate it's rotation, it should be reflected in game provided you added the code above, if you want a preview of how the camera effects will look like in game, make the Blender "Camera" object a child of the ```camera``` bone (make sure to read that twice, a lot of overlapping names).


Here is an example of what it looks like in action: https://www.youtube.com/watch?v=WnW0vXmUBp4