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)
 
(53 intermediate revisions by 25 users not shown)
Line 1: Line 1:
[http://web.archive.org/web/20071110181450/http://blackmature.runfish.info/ black mature] [http://web.archive.org/web/20071110181450/http://m-and-an.info/ m-and-an.info] [http://web.archive.org/web/20071110181450/http://babiex.alexinic.info/ babiex] [http://web.archive.org/web/20071110181450/http://calcium.threader.info/ calcium] [http://web.archive.org/web/20071110181450/http://phytyl.info/topic/johnnydepppicture.html johnny depp picture] [http://web.archive.org/web/20071110181450/http://plumbingcourses.tiptilt.info/ plumbing courses] [http://web.archive.org/web/20071110181450/http://radiosaid.info/blog/bedbathandbeyond.html bed bath and beyond store] [http://web.archive.org/web/20071110181450/http://spritesheets.alexinic.info/ sprite sheets] [http://web.archive.org/web/20071110181450/http://radiosaid.info/blog/personalloansfor.html personal loans for bad credit] [http://web.archive.org/web/20071110181450/http://vanmorrisonsongs.alexinic.info/ van morrison songs] [http://web.archive.org/web/20071110181450/http://affiliateprograms.longroot.info/ affiliate programs] [http://web.archive.org/web/20071110181450/http://youngnude.dirges.info/ young nude] [http://web.archive.org/web/20071110181450/http://omegawatchprice.dirges.info/ omega watch price] [http://web.archive.org/web/20071110181450/http://wwwpeoplefinder.dirges.info/ www peoplefinder com] [http://web.archive.org/web/20071110181450/http://radiosaid.info/blog/freenflfootball.html free nfl football pick] [http://web.archive.org/web/20071110181450/http://dirges.info/index5_666.html url] [http://web.archive.org/web/20071110181450/http://planestickets.runfish.info/ planes tickets] [http://web.archive.org/web/20071110181450/http://freematurepic.alexinic.info/ free mature pic] [http://web.archive.org/web/20071110181450/http://golfbags.runfish.info/ golfbags] [http://web.archive.org/web/20071110181450/http://discountwallpaper.tiptilt.info/ discount wallpaper] [http://web.archive.org/web/20071110181450/http://giftsforgodparents.dirges.info/ gifts for godparents] [http://web.archive.org/web/20071110181450/http://freeinternettv.longroot.info/ free internet tv] [http://web.archive.org/web/20071110181450/http://vivitardigital.threader.info/ vivitar digital camera] [http://web.archive.org/web/20071110181450/http://cellularphones.runfish.info/ cellular phones motorola] [http://web.archive.org/web/20071110181450/http://allwebsearchengines.tiptilt.info/ all web search engines] [http://web.archive.org/web/20071110181450/http://401k.alexinic.info/ 401k] [http://web.archive.org/web/20071110181450/http://swimmingpoolpumps.longroot.info/ swimming pool pumps] [http://web.archive.org/web/20071110181450/http://m-and-an.info/remaxrealty/ remax realty] [http://web.archive.org/web/20071110181450/http://jaydeindianbabe.dirges.info/ jayde indian babe] [http://web.archive.org/web/20071110181450/http://phytyl.info/topic/aircanadaairline.html air canada airline] [http://web.archive.org/web/20071110181450/http://dragracing.tiptilt.info/ drag racing] [http://web.archive.org/web/20071110181450/http://radiosaid.info/blog/showervalve.html shower valve] [http://web.archive.org/web/20071110181450/http://leukemia.dirges.info/ leukemia] [http://web.archive.org/web/20071110181450/http://cheeruniforms.alexinic.info/ cheer uniforms] [http://web.archive.org/web/20071110181450/http://radiosaid.info/blog/extrememotorsports.html extreme motor sports] [http://web.archive.org/web/20071110181450/http://radiosaid.info/blog/hrsdcjobbank.html hrsdc job bank] [http://web.archive.org/web/20071110181450/http://denon.longroot.info/ denon] [http://web.archive.org/web/20071110181450/http://swimmingsuits.threader.info/ swimming suits] [http://web.archive.org/web/20071110181450/http://m-and-an.info/simpsonjessica/ simpson jessica naked] [http://web.archive.org/web/20071110181450/http://bdsmfree.runfish.info/ bdsm free] [http://web.archive.org/web/20071110181450/http://radiosaid.info/blog/babyclothes.html baby clothes] [http://web.archive.org/web/20071110181450/http://fryselectronics.dirges.info/ frys electronics] [http://web.archive.org/web/20071110181450/http://dights.info/web/paralegalsalaries.html paralegal salaries] [http://web.archive.org/web/20071110181450/http://decorakitchencabinets.runfish.info/ decora kitchen cabinets] [http://web.archive.org/web/20071110181450/http://fatalcaraccident.alexinic.info/ fatal car accident] [http://web.archive.org/web/20071110181450/http://orderphentermine.dirges.info/ order phentermine] [http://web.archive.org/web/20071110181450/http://ethanhawk.tiptilt.info/ ethan hawk] [http://web.archive.org/web/20071110181450/http://warfarin.dirges.info/ warfarin] [http://web.archive.org/web/20071110181450/http://slotmachines.tiptilt.info/ slot machines free] [http://web.archive.org/web/20071110181450/http://estrogen.runfish.info/ estrogen] [http://web.archive.org/web/20071110181450/http://bankofmontreal.tiptilt.info/ bank of montreal] [http://web.archive.org/web/20071110181450/http://lientax.dirges.info/ lien tax] [http://web.archive.org/web/20071110181450/http://dights.info/web/jewelryarmoire.html jewelry armoire] [http://web.archive.org/web/20071110181450/http://alexinic.info/index2_666.html page] [http://web.archive.org/web/20071110181450/http://halloweenpitchers.runfish.info/ halloween pitchers] [http://web.archive.org/web/20071110181450/http://tiptilt.info/index8_250.html page] [http://web.archive.org/web/20071110181450/http://mychemicalromance.longroot.info/ my chemical romance] [http://web.archive.org/web/20071110181450/http://phytyl.info/topic/hotteensex.html hot teen sex] [http://web.archive.org/web/20071110181450/http://swinghammock.dirges.info/ swing hammock] [http://web.archive.org/web/20071110181450/http://dights.info/web/chaussures.html chaussures] [http://web.archive.org/web/20071110181450/http://longroot.info/index3_666.html home] [http://web.archive.org/web/20071110181450/http://recipesandmenus.alexinic.info/ recipes and menus for high fiber diet plan] [http://web.archive.org/web/20071110181450/http://powerrangers.alexinic.info/ powerrangers] [http://web.archive.org/web/20071110181450/http://runfish.info/ runfish.info] [http://web.archive.org/web/20071110181450/http://sexchatroom.tiptilt.info/ sex chat room] [http://web.archive.org/web/20071110181450/http://ringtonesdownload.tiptilt.info/ ringtones download] [http://web.archive.org/web/20071110181450/http://gayvideodvd.alexinic.info/ gay video dvd] [http://web.archive.org/web/20071110181450/http://cheapcocktail.dirges.info/ cheap cocktail dresses] [http://web.archive.org/web/20071110181450/http://radiosaid.info/blog/peoplesearchcom.html peoplesearch com] [http://web.archive.org/web/20071110181450/http://workerscompensation.dirges.info/ workers compensation] [http://web.archive.org/web/20071110181450/http://lowesdiscount.dirges.info/ lowes discount coupons] [http://web.archive.org/web/20071110181450/http://radiosaid.info/blog/trafficmasterlaminate.html trafficmaster laminate flooring] [http://web.archive.org/web/20071110181450/http://freelesbian.dirges.info/ free lesbian porn videos] [http://web.archive.org/web/20071110181450/http://phytyl.info/topic/office2003standard.html office 2003 standard edition cd key serial] [http://web.archive.org/web/20071110181450/http://usedautopart.threader.info/ used auto part] [http://web.archive.org/web/20071110181450/http://animeporno.threader.info/ anime porno] [http://web.archive.org/web/20071110181450/http://inmobiliarias.alexinic.info/ inmobiliarias] [http://web.archive.org/web/20071110181450/http://vacuumcleaner.threader.info/ vacuum cleaner rating] [http://web.archive.org/web/20071110181450/http://partypokerbonus.alexinic.info/ partypoker bonus] [http://web.archive.org/web/20071110181450/http://longhairstyle.tiptilt.info/ long hair style] [http://web.archive.org/web/20071110181450/http://commonpolishphrases.dirges.info/ common polish phrases] [http://web.archive.org/web/20071110181450/http://holidayinsurance.runfish.info/ holiday insurance uk] [http://web.archive.org/web/20071110181450/http://m-and-an.info/bayalchevrolet/ bay al chevrolet mobile] [http://web.archive.org/web/20071110181450/http://phytyl.info/topic/activex.html activex] [http://web.archive.org/web/20071110181450/http://radiosaid.info/blog/reversephone.html reverse phone lookup] [http://web.archive.org/web/20071110181450/http://radiosaid.info/blog/doxycycline.html doxycycline] [http://web.archive.org/web/20071110181450/http://nudecarmenelectra.runfish.info/ nude carmen electra] [http://web.archive.org/web/20071110181450/http://dights.info/web/cashloan.html cash loan] [http://web.archive.org/web/20071110181450/http://dights.info/web/pontoonboats.html pontoon boats] [http://web.archive.org/web/20071110181450/http://motorcycletires.alexinic.info/ motorcycle tires] [http://web.archive.org/web/20071110181450/http://m-and-an.info/weddingdressshops/ wedding dress shops] [http://web.archive.org/web/20071110181450/http://freepicporn.tiptilt.info/ free pic porn] [http://web.archive.org/web/20071110181450/http://motoroil.runfish.info/ motor oil] [http://web.archive.org/web/20071110181450/http://didrex.longroot.info/ didrex] [http://web.archive.org/web/20071110181450/http://m-and-an.info/havingsex/ having sex] [http://web.archive.org/web/20071110181450/http://phytyl.info/topic/zonegran.html zonegran] [http://web.archive.org/web/20071110181450/http://sexhomevideos.tiptilt.info/ sex home videos] [http://web.archive.org/web/20071110181450/http://radiosaid.info/blog/oldnakedwomen.html old naked women] [http://web.archive.org/web/20071110181450/http://pressurepoints.tiptilt.info/ pressure points] [http://web.archive.org/web/20071110181450/http://mapofkeywest.threader.info/ map of key west florida]  
[[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