Viewmodel: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
mNo edit summary
m (Setting bug notice hidetested=1 param on page where the bug might not need tested in param specified)
 
(47 intermediate revisions by 24 users not shown)
Line 1: Line 1:
[http://chess-tutor.com/blog/wp-content/uploads/image/whtffm.html replica designer handbag] [http://apaudit.net/wp-content/themes/default/img/mgipxf.html bathroom vanities online] [http://catloversportal.com/wp-content/themes/default/img/cnafcb.html yamaha parts] [http://webfoxmedia.com/blog/wp-content/themes/default/tmp/vqmnmo.html cadillac escalade] [http://apaudit.net/wp-content/themes/default/img/tblmur.html commercial cleaning equipment] [http://preciousmetalinvestment.com/gems/data/xfactor/bnssva.html runescape money] [http://wordofgodonline.com/wp-content/themes/default/cache/liudtf.html photoshop] [http://catloversportal.com/wp-content/themes/default/img/gsxdif.html nero] [http://theviolinreviews.com/yg/CSS/data/guitar/gubjmx.html adult streaming video] [http://articlecash.net/content/wp-content/themes/default/cache/rploit.html wireless printer connection] [http://thepackagingsite.com/blog/wp-content/themes/classic/cache/vpgffh.html minority grants] [http://bqwm.com/studentloans/wp-content/themes/default/cache/xxktbg.html web cam chat] [http://chess-tutor.com/blog/wp-content/uploads/image/wttqie.html commercial cleaning equipment] [http://cutbeach.com/images/moved/1/cache/bwpidc.html tag heuer watch] [http://thepackagingsite.com/blog/wp-content/themes/classic/cache/bvndua.html montgomery county community college maryland] [http://cutbeach.com/images/moved/1/cache/qeqcja.html fishing boat] [http://bowling900.com/wp-content/themes/classic/cache/aqjshu.html volvo xc90 2005] [http://bqwm.com/studentloans/wp-content/themes/default/cache/rbgevl.html scrubs] [http://windriders.org/images/MAW06/MAW/cache/vxwlfx.html smiley central] [http://thechowchowrevealed.com/careforchows/wp-content/themes/default/cache/shsbps.html free wallpapers] [http://californiacd.net/phpbb/images/smiles/icon/pejtpu.html pet supplies] [http://villa-les-roches.com/english/dining_files/tmp/cache/fcbxxi.html coach] [http://chess-tutor.com/blog/wp-content/uploads/image/jxxxbo.html ford truck parts] [http://thepackagingsite.com/blog/wp-content/themes/classic/cache/opaafr.html uk holidays] [http://windriders.org/images/MAW06/MAW/cache/grhtbf.html reef sandal] [http://associatedarts.org/images/misc/pic/avatars/fobobp.html electric bike] [http://windriders.org/images/MAW06/MAW/cache/eqvmur.html speed dating ottawa] [http://windriders.org/images/MAW06/MAW/cache/efaurh.html free personal ad] [http://windriders.org/images/MAW06/MAW/cache/ibwqoq.html valtrex] [http://californiacd.net/phpbb/images/smiles/icon/gubqdg.html bankofamerica] [http://catloversportal.com/wp-content/themes/default/img/jmaxli.html beautiful russian bride] [http://wordofgodonline.com/wp-content/themes/default/cache/pimqpw.html magellan software] [http://villa-les-roches.com/english/dining_files/tmp/cache/dhtous.html xanax buy] [http://rhysvineyards.com/backup/wines/1/cache/atxhjh.html embroidery designs] [http://bowling900.com/wp-content/themes/classic/cache/erxiuh.html headset] [http://windriders.org/images/MAW06/MAW/cache/dbhotv.html atenolol] [http://articlecash.net/content/wp-content/themes/default/cache/ahqjfo.html key bank cleveland ohio] [http://rhysvineyards.com/backup/wines/1/cache/tnrphk.html dildo sex] [http://windriders.org/images/MAW06/MAW/cache/atpdbj.html honda crv] [http://californiacd.net/phpbb/images/smiles/icon/dlhmeu.html denon] [http://theviolinreviews.com/yg/CSS/data/guitar/rdpxxj.html coal] [http://windriders.org/images/MAW06/MAW/cache/vgbciq.html evo] [http://thechowchowrevealed.com/careforchows/wp-content/themes/default/cache/mmmiiq.html discount bedding online] [http://rhysvineyards.com/backup/wines/1/cache/xnpklj.html volvo xc90 2005] [http://chess-tutor.com/blog/wp-content/uploads/image/odunke.html muscle building] [http://conceptualpolymer.com/photogallery/photo7930/tmp/1/xarcgr.html office desks] [http://associatedarts.org/images/misc/pic/avatars/jdgrjq.html real estate in surfside beach south carolina] [http://thepackagingsite.com/blog/wp-content/themes/classic/cache/nwihaq.html replica designer handbag] [http://bqwm.com/studentloans/wp-content/themes/default/cache/cemmsp.html powerpoint tutorial] [http://cutbeach.com/images/moved/1/cache/nahmgd.html barebone notebook] [http://catloversportal.com/wp-content/themes/default/img/csjbjc.html clit vibrator] [http://windriders.org/images/MAW06/MAW/cache/onnise.html marlboro cigarettes] [http://conceptualpolymer.com/photogallery/photo7930/tmp/1/icsimr.html tmobile ringtones] [http://thechowchowrevealed.com/careforchows/wp-content/themes/default/cache/koxkxo.html adirondack chair] [http://windriders.org/images/MAW06/MAW/cache/tqigic.html philippine airline] [http://windriders.org/images/MAW06/MAW/cache/dtlwbx.html trade show booth] [http://villa-les-roches.com/english/dining_files/tmp/cache/jfbsdb.html online roulette] [http://bowling900.com/wp-content/themes/classic/cache/svigqx.html meridia] [http://preciousmetalinvestment.com/gems/data/xfactor/seoksv.html prepaid credit card] [http://associatedarts.org/images/misc/pic/avatars/nshcug.html psp games] [http://thepackagingsite.com/blog/wp-content/themes/classic/cache/khrvpx.html social security number] [http://articlecash.net/content/wp-content/themes/default/cache/ixauea.html bed and breakfast in london] [http://webfoxmedia.com/blog/wp-content/themes/default/tmp/cvacpk.html where to buy viagra] [http://associatedarts.org/images/misc/pic/avatars/ncwqld.html battery switch] [http://villa-les-roches.com/english/dining_files/tmp/cache/djokxn.html bargain coupon smart] [http://chess-tutor.com/blog/wp-content/uploads/image/rhkiwr.html adidas shoes] [http://windriders.org/images/MAW06/MAW/cache/qxqekq.html cork flooring] [http://theviolinreviews.com/yg/CSS/data/guitar/umkpvg.html cheap cigarette] [http://articlecash.net/content/wp-content/themes/default/cache/gxmkcc.html adjustable bed] [http://articlecash.net/content/wp-content/themes/default/cache/mpcokg.html pierre cardin] [http://associatedarts.org/images/misc/pic/avatars/dpfqag.html chocolate fountains] [http://webfoxmedia.com/blog/wp-content/themes/default/tmp/bqcwto.html depression symptoms] [http://wordofgodonline.com/wp-content/themes/default/cache/lgfxie.html adult chat rooms] [http://bqwm.com/studentloans/wp-content/themes/default/cache/tdsecp.html home equity loans] [http://apaudit.net/wp-content/themes/default/img/poaeha.html relationship help] [http://apaudit.net/wp-content/themes/default/img/orpqpo.html cool ringtones] [http://articlecash.net/content/wp-content/themes/default/cache/havdsg.html silk wedding flowers] [http://thechowchowrevealed.com/careforchows/wp-content/themes/default/cache/fjgbmi.html nutrisystem] [http://preciousmetalinvestment.com/gems/data/xfactor/slghmg.html salsa] [http://associatedarts.org/images/misc/pic/avatars/hxskgk.html photography] [http://conceptualpolymer.com/photogallery/photo7930/tmp/1/djsdde.html free sex chat] [http://bqwm.com/studentloans/wp-content/themes/default/cache/pbiurs.html debt] [http://bqwm.com/studentloans/wp-content/themes/default/cache/rkdule.html ata airlines] [http://associatedarts.org/images/misc/pic/avatars/eiahbh.html lufthansa ticket] [http://catloversportal.com/wp-content/themes/default/img/gfjpjj.html flooring] [http://chess-tutor.com/blog/wp-content/uploads/image/kdsema.html paralegal courses] [http://theviolinreviews.com/yg/CSS/data/guitar/pttoed.html timberland boot] [http://articlecash.net/content/wp-content/themes/default/cache/goilas.html helsinki] [http://rhysvineyards.com/backup/wines/1/cache/oewolf.html massage new jersey] [http://wordofgodonline.com/wp-content/themes/default/cache/tsmudm.html cheap cigars] [http://webfoxmedia.com/blog/wp-content/themes/default/tmp/bltpip.html hud homes] [http://catloversportal.com/wp-content/themes/default/img/wumwrd.html bag bed n] [http://conceptualpolymer.com/photogallery/photo7930/tmp/1/kqmbfn.html purses wholesale] [http://associatedarts.org/images/misc/pic/avatars/atrmst.html bronze sculpture] [http://chess-tutor.com/blog/wp-content/uploads/image/qcuqbg.html registration numbers] [http://apaudit.net/wp-content/themes/default/img/uljjdx.html football coaching] [http://articlecash.net/content/wp-content/themes/default/cache/etnbog.html discount scrubs] [http://associatedarts.org/images/misc/pic/avatars/ohhbnv.html package holiday] [http://wordofgodonline.com/wp-content/themes/default/cache/mvuqfm.html paper cutter] [http://wordofgodonline.com/wp-content/themes/default/cache/hioems.html boston college]  
[[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>. It might be facing the wrong way.
 
=== Basic activities ===
 
<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 07:12, 20 May 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. It might be facing the wrong way.

Basic activities

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