Adding a Camera Bone to a Viewmodel

From Valve Developer Community
Jump to: navigation, search
Wikipedia - Letter.png
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
Dead End - Icon.png
This article has no links to other VDC articles. Please help improve this article by adding links that are relevant to the context within the existing text.
January 2024

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 your animation software, 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 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 animation software's "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