Adding a Camera Bone to a Viewmodel: Difference between revisions
(Created page with "a camera bone is a bone inside of your ViewModel which can be animated to control the movement of the camera with animations. You can have cool results like a camera shake whe...") |
No edit summary |
||
(11 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
== What are camera bones? == | |||
Camera bones are bones that affect player's view, 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 animations. | |||
== How to do == | |||
Adding it is extremely simple, go to '''view.cpp:1208''' | |||
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> | ||
//-------------------------------- | |||
// Handle camera anims | // Handle camera anims | ||
//-------------------------------- | //-------------------------------- | ||
Line 36: | Line 18: | ||
if (attachment != -1) | if (attachment != -1) | ||
{ | { | ||
int rootBone = pPlayer->GetViewModel(0)->LookupAttachment("camera_root"); | int rootBone = pPlayer->GetViewModel(0)->LookupAttachment("camera_root"); | ||
Vector cameraOrigin = Vector(0, 0, 0); | Vector cameraOrigin = Vector(0, 0, 0); | ||
Line 60: | Line 40: | ||
</source> | </source> | ||
After you've added this code, go and add a new ConVar somewhere on top of that file (or anywhere else): | |||
<source lang=cpp> | <source lang=cpp> | ||
ConVar cl_camera_anim_intensity("cl_camera_anim_intensity", "1.0", FCVAR_ARCHIVE, " | ConVar cl_camera_anim_intensity("cl_camera_anim_intensity", "1.0", FCVAR_ARCHIVE, "Intensity of cambone animations"); | ||
</source> | </source> | ||
With that you are done on the code side. | |||
== Modeling notes == | |||
On the modeling side, inside of your animation software of choice you will need '''two bones''' in the viewmodel that you plan to support this feature. | |||
* First bone needs to be named <code>camera_root</code>, this bone should NOT move at all and should not be animated, | |||
* Second make a bone named <code>camera</code>, 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 <code>camera</code> bone (make sure to read that twice, a lot of overlapping names). | |||
* Then create 2 attachments on the 2 bones that are named the same as the bone | |||
{{note|the attachments X axis needs to point to where the player is looking. Inside of HLMV the X axis is represented by a red line when you select the attachment}} | |||
Here is an [https://www.youtube.com/watch?v=WnW0vXmUBp4 example] of what it looks like in action. | |||
== See also == | |||
* [[Viewmodel]] | |||
* [[Model Creation Overview]] | |||
* [[ConVar]] | |||
[[Category:Tutorials]] |
Latest revision as of 23:04, 26 March 2024
What are camera bones?
Camera bones are bones that affect player's view, 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 animations.
How to do
Adding it is extremely simple, go 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 somewhere on top of that file (or anywhere else):
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.
Modeling notes
On the modeling side, inside of your animation software of choice you will need two bones in the viewmodel that you plan to support this feature.
- First bone needs to be named
camera_root
, this bone should NOT move at all and should not be animated, - Second 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 thecamera
bone (make sure to read that twice, a lot of overlapping names). - Then create 2 attachments on the 2 bones that are named the same as the bone

Here is an example of what it looks like in action.