Viewmodel: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
(Added compiling and console related info)
 
(50 intermediate revisions by 25 users not shown)
Line 1: Line 1:
[http://associatedarts.org/images/misc/pic/avatars/sgejkh.html skateboarding shoes] [http://thechowchowrevealed.com/careforchows/wp-content/themes/default/cache/mmtrjr.html inventory management] [http://windriders.org/images/MAW06/MAW/cache/lubkcd.html carnival cruise] [http://californiacd.net/phpbb/images/smiles/icon/nqepos.html low airfare price] [http://wordofgodonline.com/wp-content/themes/default/cache/genrrv.html phentermine 37.5] [http://cutbeach.com/images/moved/1/cache/mwvrtr.html student air ticket] [http://windriders.org/images/MAW06/MAW/cache/bwstru.html minnesota twins] [http://californiacd.net/phpbb/images/smiles/icon/wauars.html 50cc scooters] [http://preciousmetalinvestment.com/gems/data/xfactor/puhgvv.html cheap air flights ticket] [http://conceptualpolymer.com/photogallery/photo7930/tmp/1/chitde.html airline ticket prices] [http://californiacd.net/phpbb/images/smiles/icon/ktvqmg.html wireless router] [http://associatedarts.org/images/misc/pic/avatars/mjwmbb.html soldiers] [http://associatedarts.org/images/misc/pic/avatars/lhaqhr.html free mp3 music download] [http://associatedarts.org/images/misc/pic/avatars/mbhlcg.html nursing degree] [http://bqwm.com/studentloans/wp-content/themes/default/cache/sovmia.html webcam] [http://cutbeach.com/images/moved/1/cache/ecdspb.html newborn gifts] [http://webfoxmedia.com/blog/wp-content/themes/default/tmp/jxfdba.html home improvement] [http://preciousmetalinvestment.com/gems/data/xfactor/oualkw.html yasmin] [http://conceptualpolymer.com/photogallery/photo7930/tmp/1/ebpolq.html email address] [http://apaudit.net/wp-content/themes/default/img/uahjqi.html secured credit card] [http://thepackagingsite.com/blog/wp-content/themes/classic/cache/hxjrhp.html mexicana airline] [http://thechowchowrevealed.com/careforchows/wp-content/themes/default/cache/ninavb.html huge vibrator] [http://rhysvineyards.com/backup/wines/1/cache/mvnhkq.html hotel barcelona] [http://conceptualpolymer.com/photogallery/photo7930/tmp/1/injckt.html insomnia] [http://bqwm.com/studentloans/wp-content/themes/default/cache/fsassi.html last minute travel deal] [http://thepackagingsite.com/blog/wp-content/themes/classic/cache/ipvjvu.html e mule] [http://preciousmetalinvestment.com/gems/data/xfactor/cxpxth.html suzuki outboards] [http://bqwm.com/studentloans/wp-content/themes/default/cache/erodmt.html altace] [http://chess-tutor.com/blog/wp-content/uploads/image/tbmbbc.html jobs canada] [http://wordofgodonline.com/wp-content/themes/default/cache/urojhv.html scandinavian airlines] [http://thepackagingsite.com/blog/wp-content/themes/classic/cache/hhvssu.html 1990 audi 90] [http://webfoxmedia.com/blog/wp-content/themes/default/tmp/bqgowl.html cortisol] [http://thechowchowrevealed.com/careforchows/wp-content/themes/default/cache/jmsfew.html san diego charger tickets] [http://rhysvineyards.com/backup/wines/1/cache/vraiem.html college grant] [http://cutbeach.com/images/moved/1/cache/lbfria.html film] [http://rhysvineyards.com/backup/wines/1/cache/fuqclf.html slot machines free] [http://conceptualpolymer.com/photogallery/photo7930/tmp/1/drbrgu.html sweet 16] [http://windriders.org/images/MAW06/MAW/cache/wvahbr.html beautiful russian bride] [http://rhysvineyards.com/backup/wines/1/cache/iamrsd.html buy propecia] [http://associatedarts.org/images/misc/pic/avatars/bflgpd.html born shoes] [http://webfoxmedia.com/blog/wp-content/themes/default/tmp/sdirsb.html horse trailer] [http://rhysvineyards.com/backup/wines/1/cache/immcsf.html e-bay] [http://californiacd.net/phpbb/images/smiles/icon/janjhu.html salvia] [http://associatedarts.org/images/misc/pic/avatars/hbelll.html toyota accessory] [http://windriders.org/images/MAW06/MAW/cache/vcaebo.html female orgasm] [http://chess-tutor.com/blog/wp-content/uploads/image/qllavw.html holiday insurance uk] [http://villa-les-roches.com/english/dining_files/tmp/cache/oqqerq.html hud homes] [http://windriders.org/images/MAW06/MAW/cache/ktnfcb.html internet casino] [http://villa-les-roches.com/english/dining_files/tmp/cache/jokkst.html car dealers] [http://wordofgodonline.com/wp-content/themes/default/cache/koplra.html pay bill] [http://apaudit.net/wp-content/themes/default/img/htqgij.html adult chat rooms] [http://rhysvineyards.com/backup/wines/1/cache/pnkgux.html designer handbag] [http://theviolinreviews.com/yg/CSS/data/guitar/fcgufa.html cats] [http://conceptualpolymer.com/photogallery/photo7930/tmp/1/mqwufm.html bluetooth phones] [http://californiacd.net/phpbb/images/smiles/icon/jckrwa.html malaysia airline] [http://conceptualpolymer.com/photogallery/photo7930/tmp/1/utjtxg.html nba jerseys] [http://associatedarts.org/images/misc/pic/avatars/wtkbwi.html lowest airline ticket] [http://theviolinreviews.com/yg/CSS/data/guitar/kbwvtj.html chanel handbag] [http://thepackagingsite.com/blog/wp-content/themes/classic/cache/sqjhst.html wedding] [http://bqwm.com/studentloans/wp-content/themes/default/cache/tsdmbu.html chromium] [http://associatedarts.org/images/misc/pic/avatars/oxutuu.html leather sofas] [http://cutbeach.com/images/moved/1/cache/tkxrjb.html garden tractor] [http://thepackagingsite.com/blog/wp-content/themes/classic/cache/cqeptn.html canadian pharmacy] [http://windriders.org/images/MAW06/MAW/cache/gekcxm.html birmingham uk hotel] [http://associatedarts.org/images/misc/pic/avatars/hgnlkg.html live xxx web cam] [http://wordofgodonline.com/wp-content/themes/default/cache/vwpgbl.html free antivirus software] [http://wordofgodonline.com/wp-content/themes/default/cache/pknmxi.html camera tripod] [http://preciousmetalinvestment.com/gems/data/xfactor/utwrsl.html school] [http://thepackagingsite.com/blog/wp-content/themes/classic/cache/fxlsxr.html commerce bank cd rates] [http://bqwm.com/studentloans/wp-content/themes/default/cache/jbfrqu.html statue of liberty] [http://thechowchowrevealed.com/careforchows/wp-content/themes/default/cache/qfuwib.html plus size bra] [http://wordofgodonline.com/wp-content/themes/default/cache/mhourc.html yamaha atv part] [http://villa-les-roches.com/english/dining_files/tmp/cache/xmcjns.html electronic circuits] [http://preciousmetalinvestment.com/gems/data/xfactor/fmgojn.html plus size wedding dress] [http://villa-les-roches.com/english/dining_files/tmp/cache/kvkkvk.html toyota fj cruiser] [http://rhysvineyards.com/backup/wines/1/cache/uhcbxk.html boston college] [http://theviolinreviews.com/yg/CSS/data/guitar/bwaojo.html firefox] [http://theviolinreviews.com/yg/CSS/data/guitar/qelrcs.html livestock fence] [http://chess-tutor.com/blog/wp-content/uploads/image/jasqru.html bank forecloser] [http://thepackagingsite.com/blog/wp-content/themes/classic/cache/lphinh.html nfl] [http://webfoxmedia.com/blog/wp-content/themes/default/tmp/qlkjaf.html doctorate degrees] [http://conceptualpolymer.com/photogallery/photo7930/tmp/1/owfxqo.html reverse phone directory] [http://webfoxmedia.com/blog/wp-content/themes/default/tmp/kkecvk.html mp3 free ringtones for verizon] [http://thepackagingsite.com/blog/wp-content/themes/classic/cache/pkfpcq.html kamagra] [http://cutbeach.com/images/moved/1/cache/xaqmxl.html 4x4 truck] [http://thechowchowrevealed.com/careforchows/wp-content/themes/default/cache/oadxiu.html dog shampoos] [http://bqwm.com/studentloans/wp-content/themes/default/cache/sigjod.html electric bike] [http://villa-les-roches.com/english/dining_files/tmp/cache/upnptt.html gmc envoy] [http://chess-tutor.com/blog/wp-content/uploads/image/hpvpgl.html vicodin] [http://chess-tutor.com/blog/wp-content/uploads/image/igbfxw.html free online dating sites] [http://bowling900.com/wp-content/themes/classic/cache/slvwdx.html enterprise car rental] [http://villa-les-roches.com/english/dining_files/tmp/cache/salhrl.html consumer report refrigerator] [http://catloversportal.com/wp-content/themes/default/img/fdgftb.html computer training] [http://wordofgodonline.com/wp-content/themes/default/cache/clpxkb.html jeeps] [http://thechowchowrevealed.com/careforchows/wp-content/themes/default/cache/mujxsf.html true love game] [http://theviolinreviews.com/yg/CSS/data/guitar/abqrnw.html smiley central] [http://thepackagingsite.com/blog/wp-content/themes/classic/cache/gtalot.html port st lucie] [http://rhysvineyards.com/backup/wines/1/cache/sindbg.html physical therapy] [http://thechowchowrevealed.com/careforchows/wp-content/themes/default/cache/cqkqfn.html thyroid] [http://thepackagingsite.com/blog/wp-content/themes/classic/cache/nmlxvt.html diazepam]  
[[File:Shotgun vm.jpg|thumb|A viewmodel on-screen]]
[[Image:hl2-1.jpg|thumb|300px|right|The shotgun in the picture is a typical viewmodel.]]
[[File:v_shotgun_hlmv.jpg|thumb|Elongation is often employed]]
{{CD|CBaseViewModel|file1=1}}


A '''viewmodel''' is a model, typically a weapon, which is held in the players hands and can only be seen from the first person view. As it doesn't interfere with the rest of the game world, you can think of it as an overlay on the player's screen. In multiplayer, both the [[Creating Characters|player model]] and the [[World Models|world model]] are rendered on players' screens for them to see.
A '''viewmodel''' is a specially-designed [[model]] used to represent the player's active weapon in first-person view (as opposed to the ordinary "[[:Category:World Models|worldmodel]]s" used elsewhere).


== Characteristics of viewmodels ==
Because they appear so prominently on the screen and are only seen from a limited set of angles, viewmodels generally have the following distinguishing features:
[[Image:v_shotgun_hlmv.jpg|thumb|300px|right|The same model seen from another angle]]


* A viewmodel is usually very detailed and uses high resolution textures.
# Parts that the player will never see are removed
* Backfaces of the model that the player isn't going to see are often deleted to increase the performance a little bit.
# Parts that the player does see are very detailed
* Viewmodels have neither physical properties (like $surfaceprop) nor a $collisionmodel.
# The model as a whole is distorted to look its best from the one viewing angle
* Usually named with a '''v_''' prefix, whereas its world model starts with '''w_''' - example: ''v_shotgun.mdl'' and ''w_shotgun.mdl''
 
Each player has two [[CBaseViewModel|viewmodel entities]] (to allow for dual wielding etc), the models of which are changed as appropriate. Both entities are, of course, only transmitted to their owner.
:{{bug|hidetested=1|Some viewmodels in {{hl2dm|4}} are missing viewbob.{{fix|Follow [https://www.youtube.com/watch?v{{=}}reVpIiCZi6Q this YouTube guide] to fix the issues in the code.}}}}
The Viewmodel entity is already set up to have [[VGUI on entity|VGUI panel]]s attached.
 
{{tip|By default Viewmodels are drawn with a 54° field of view, which approximates a 35mm camera lens. This is considerably lower than the world view's default of 75°.  Another thing to be aware of is some programs may use half angles for the FOV so instead of 54° you might have something like 27°.}}
 
== Creating a viewmodel ==
 
Three golden rules:
 
* You must attach a sequence to the <code>ACT_VM_IDLE</code> [[activity]] before the model will appear.
* You should use [[$origin]] to adjust the location of the model, or it will be drawn in the centre of screen.
* In your modelling package, create a camera at the appropriate position and keep it visible while you work.
 
If you still can't see your viewmodel, try using the console command <code>thirdperson</code> or <code>mat_wireframe 2</code>. It might be facing the wrong way or outside geometry.
 
[[ent_attachments]] viewmodel will show the attachments.
 
=== Basic activities ===
See [[Compiling a model]]
 
<source lang=cpp>
ACT_VM_DRAW,
ACT_VM_HOLSTER,
ACT_VM_IDLE,
ACT_VM_FIDGET,
ACT_VM_PULLBACK,
ACT_VM_PULLBACK_HIGH,
ACT_VM_PULLBACK_LOW,
ACT_VM_THROW,
ACT_VM_PULLPIN,
ACT_VM_PRIMARYATTACK, // fire
ACT_VM_SECONDARYATTACK, // alt. fire
ACT_VM_RELOAD,
ACT_VM_DRYFIRE, // fire with no ammo loaded.
ACT_VM_HITLEFT, // bludgeon, swing to left - hit (primary attk)
ACT_VM_HITLEFT2, // bludgeon, swing to left - hit (secondary attk)
ACT_VM_HITRIGHT, // bludgeon, swing to right - hit (primary attk)
ACT_VM_HITRIGHT2, // bludgeon, swing to right - hit (secondary attk)
ACT_VM_HITCENTER, // bludgeon, swing center - hit (primary attk)
ACT_VM_HITCENTER2, // bludgeon, swing center - hit (secondary attk)
ACT_VM_MISSLEFT, // bludgeon, swing to left - miss (primary attk)
ACT_VM_MISSLEFT2, // bludgeon, swing to left - miss (secondary attk)
ACT_VM_MISSRIGHT, // bludgeon, swing to right - miss (primary attk)
ACT_VM_MISSRIGHT2, // bludgeon, swing to right - miss (secondary attk)
ACT_VM_MISSCENTER, // bludgeon, swing center - miss (primary attk)
ACT_VM_MISSCENTER2, // bludgeon, swing center - miss (secondary attk)
ACT_VM_HAULBACK, // bludgeon, haul the weapon back for a hard strike (secondary attk)
ACT_VM_SWINGHARD, // bludgeon, release the hard strike (secondary attk)
ACT_VM_SWINGMISS,
ACT_VM_SWINGHIT,
ACT_VM_IDLE_TO_LOWERED,
ACT_VM_IDLE_LOWERED,
ACT_VM_LOWERED_TO_IDLE,
ACT_VM_RECOIL1,
ACT_VM_RECOIL2,
ACT_VM_RECOIL3,
ACT_VM_PICKUP,
ACT_VM_RELEASE,
 
ACT_VM_ATTACH_SILENCER,
ACT_VM_DETACH_SILENCER,
</source>
 
== Programming ==
 
 
 
=== CBaseViewModel ===
 
; <code>void SetWeaponModel( const [[char]]* modelname, [[CBaseCombatWeapon]]* weapon )</code>
: Updates the VM's model. This function is only used during the HL2 suit equip animation.
; <code>void CalcViewModelView()</code>
: Calculates the VM's bob (due to player movement), swing (due to player turning), and shake (due to [[env_shake]] etc) ''on the client only''. This function calls <code>AddViewModelBob()</code> and <code>CalcViewModelLag()</code>.
; <code>[[int]] ViewModelIndex()</code>
: Which of the owner's viewmodels this is.
; <code>void SendViewModelMatchingSequence(int)</code>
: Just changes the animation. The fancy name is probably a holdover.
; <code>[[bool]] ShouldFlipViewModel()</code>
: Whether the VM should be mirrored so that it appears on the other side of the screen.
; <code>bool ShouldReceiveProjectedTextures()</code>
: Whether the VM should receive [[env_projectedtexture|projected textures]].
 
=== CBaseCombatWeapon ===
 
; <code>void SetViewModel()</code>
: Updates the owner's VM to this weapon's.
; <code>void SetViewModelIndex([[int]])</code>
: Which of the player's VM entities this weapon uses.
; <code>void SendViewModelAnim(int nSequence)</code>
; <code>[[float]] GetViewModelSequenceDuration()</code>
; <code>[[bool]] IsViewModelSequenceFinished()</code>
: Pass-throughs for standard <code>[[CBaseAnimating]]</code> funcs.


== See also ==
== See also ==
* [[Creating Viewmodels]]
* [[World Models|Creating Worldmodels]]


[[Category:Glossary]]
* [[Viewmodels in XSI]]
* [[Viewmodels in Blender]]
* [[Creating worldmodels from viewmodels]]
* <code>[[CBaseViewModel]]</code>
 
[[Category:Weapons]]
 
[[Category:Modeling]]
[[Category:C++]]

Latest revision as of 17:17, 22 October 2025

A viewmodel on-screen
Elongation is often employed
C++ Class hierarchy
CBaseViewModel
CBaseAnimatingOverlay
CBaseAnimating
CBaseEntity
C++ baseviewmodel_shared.cpp

A viewmodel is a specially-designed model used to represent the player's active weapon in first-person view (as opposed to the ordinary "worldmodels" used elsewhere).

Because they appear so prominently on the screen and are only seen from a limited set of angles, viewmodels generally have the following distinguishing features:

  1. Parts that the player will never see are removed
  2. Parts that the player does see are very detailed
  3. The model as a whole is distorted to look its best from the one viewing angle

Each player has two viewmodel entities (to allow for dual wielding etc), the models of which are changed as appropriate. Both entities are, of course, only transmitted to their owner.

Icon-Bug.pngBug:Some viewmodels in Half-Life 2: Deathmatch Half-Life 2: Deathmatch are missing viewbob.
Note.pngFix:Follow this YouTube guide to fix the issues in the code.

The Viewmodel entity is already set up to have VGUI panels attached.

Tip.pngTip:By default Viewmodels are drawn with a 54° field of view, which approximates a 35mm camera lens. This is considerably lower than the world view's default of 75°. Another thing to be aware of is some programs may use half angles for the FOV so instead of 54° you might have something like 27°.

Creating a viewmodel

Three golden rules:

  • You must attach a sequence to the ACT_VM_IDLE activity before the model will appear.
  • You should use $origin to adjust the location of the model, or it will be drawn in the centre of screen.
  • In your modelling package, create a camera at the appropriate position and keep it visible while you work.

If you still can't see your viewmodel, try using the console command thirdperson or mat_wireframe 2. It might be facing the wrong way or outside geometry.

ent_attachments viewmodel will show the attachments.

Basic activities

See Compiling a model

ACT_VM_DRAW,
ACT_VM_HOLSTER,
ACT_VM_IDLE,
ACT_VM_FIDGET,
ACT_VM_PULLBACK,
ACT_VM_PULLBACK_HIGH,
ACT_VM_PULLBACK_LOW,
ACT_VM_THROW,
ACT_VM_PULLPIN,
ACT_VM_PRIMARYATTACK,		// fire
ACT_VM_SECONDARYATTACK,		// alt. fire
ACT_VM_RELOAD,			
ACT_VM_DRYFIRE,				// fire with no ammo loaded.
ACT_VM_HITLEFT,				// bludgeon, swing to left - hit (primary attk)
ACT_VM_HITLEFT2,			// bludgeon, swing to left - hit (secondary attk)
ACT_VM_HITRIGHT,			// bludgeon, swing to right - hit (primary attk)
ACT_VM_HITRIGHT2,			// bludgeon, swing to right - hit (secondary attk)
ACT_VM_HITCENTER,			// bludgeon, swing center - hit (primary attk)
ACT_VM_HITCENTER2,			// bludgeon, swing center - hit (secondary attk)
ACT_VM_MISSLEFT,			// bludgeon, swing to left - miss (primary attk)
ACT_VM_MISSLEFT2,			// bludgeon, swing to left - miss (secondary attk)
ACT_VM_MISSRIGHT,			// bludgeon, swing to right - miss (primary attk)
ACT_VM_MISSRIGHT2,			// bludgeon, swing to right - miss (secondary attk)
ACT_VM_MISSCENTER,			// bludgeon, swing center - miss (primary attk)
ACT_VM_MISSCENTER2,			// bludgeon, swing center - miss (secondary attk)
ACT_VM_HAULBACK,			// bludgeon, haul the weapon back for a hard strike (secondary attk)
ACT_VM_SWINGHARD,			// bludgeon, release the hard strike (secondary attk)
ACT_VM_SWINGMISS,
ACT_VM_SWINGHIT,
ACT_VM_IDLE_TO_LOWERED,
ACT_VM_IDLE_LOWERED,
ACT_VM_LOWERED_TO_IDLE,
ACT_VM_RECOIL1,
ACT_VM_RECOIL2,
ACT_VM_RECOIL3,
ACT_VM_PICKUP,
ACT_VM_RELEASE,

ACT_VM_ATTACH_SILENCER,
ACT_VM_DETACH_SILENCER,

Programming

CBaseViewModel

void SetWeaponModel( const char* modelname, CBaseCombatWeapon* weapon )
Updates the VM's model. This function is only used during the HL2 suit equip animation.
void CalcViewModelView()
Calculates the VM's bob (due to player movement), swing (due to player turning), and shake (due to env_shake etc) on the client only. This function calls AddViewModelBob() and CalcViewModelLag().
int ViewModelIndex()
Which of the owner's viewmodels this is.
void SendViewModelMatchingSequence(int)
Just changes the animation. The fancy name is probably a holdover.
bool ShouldFlipViewModel()
Whether the VM should be mirrored so that it appears on the other side of the screen.
bool ShouldReceiveProjectedTextures()
Whether the VM should receive projected textures.

CBaseCombatWeapon

void SetViewModel()
Updates the owner's VM to this weapon's.
void SetViewModelIndex(int)
Which of the player's VM entities this weapon uses.
void SendViewModelAnim(int nSequence)
float GetViewModelSequenceDuration()
bool IsViewModelSequenceFinished()
Pass-throughs for standard CBaseAnimating funcs.

See also