Viewmodel: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
mNo edit summary
(Added compiling and console related info)
 
(39 intermediate revisions by 20 users not shown)
Line 1: Line 1:
[http://renatabraatz.talk4fun.net/edr.html edr] [http://meganmulvaney.ifrance.com/laetitia-casta-nuda.html laetitia casta nuda] [http://mitsuespann.talk4fun.net/generac-generators.html generac generators] [http://huepinder.talk4fun.net/blog/purple-flowers.html purple flowers] [http://emileebe.w8w.pl/kristin-kreuk.html kristin kreuk fake] [http://mariannel.w8w.pl/assembly-hall.html assembly hall champaign tickets] [http://emileebe.w8w.pl/kauffmans.html 14 8 kauffmans] [http://mildredg.w8w.pl/q/intestines.html disease intestine] [http://markettacaouett.ifrance.com/directx.html 9.0 direct download free x] [http://emileebe.w8w.pl/star-wars-episode.html star war episode ii soundtrack] [http://amelialinck.ifrance.com/new/jamocha-almond.html jamocha almond fudge] [http://catheyhoopes.talk4fun.net/content/toxoplasmosis.html toxoplasmosis incidence] [http://purple.freewww.info/scott-gertner.html scott gertner sky bar] [http://blakerivet.ifrance.com/water-softner.html water softner] [http://poise.lflinkup.net/princess-bride.html princess bride marriage scene] [http://mildredg.w8w.pl/q/joey-the-passion.html joey the passion cheat codes] [http://kerridahms.ifrance.com/library/villa-rentals.html ibiza villa rental] [http://sydneyg.w8w.pl/description/well-water.html well water] [http://veniceri.w8w.pl/aqua-legend-lung.html aqua legend lung lx] [http://ettieshover.talk4fun.net/new/after-lyric.html after lyric time time] [http://rosena.w8w.pl/wrestling-moves.html freestyle move wrestling] [http://geraldgilham.talk4fun.net/watch-christian.html watch christian music videos] [http://lordly.h1x.com/styles/water-heaters.html water heaters] [http://ettieshover.talk4fun.net/new/push-in-window-after.html push in window after brickwork uk] [http://mitsuespann.talk4fun.net/nextel-i850-phone.html nextel i850 phone] [http://shizukostivers.talk4fun.net/web/super-buffet-at.html super buffet at san jose inc] [http://ettieshover.talk4fun.net/new/peacemaker.html peacemaker] [http://kristlegr.w8w.pl/content/jefferson-county.html jefferson county alabama clerk of the court] [http://catheyhoopes.talk4fun.net/content/intense-most-typhoons.html intense most typhoons] [http://martiriffle.ifrance.com/content/morgan-webb.html morgan webb pics nude] [http://geraldgilham.talk4fun.net/ar-rahman-yahoo.html ar rahman yahoo group] [http://vernonlippert.ifrance.com/article/listen-to-akon.html listen to akon locked up] [http://kaiweesner.ifrance.com/bloodhound-gang.html bloodhound gang] [http://joelschull.webcindario.com/new/video-game-magazine.html video game magazine] [http://rosena.w8w.pl/repair-my-credit.html repair my credit] [http://hollifox.w8w.pl/articles/ac-dc-tnt.html ac dc tnt guitar tab] [http://martiriffle.ifrance.com/content/leeston-nz.html leeston nz] [http://vernonlippert.ifrance.com/article/monica-naranjo.html monica naranjo desnuda] [http://geraldgilham.talk4fun.net/extended-swim.html extended swim platform] [http://danettemoyle.talk4fun.net/metal-tool-storage.html metal tool storage cabinets] [http://danettemoyle.talk4fun.net/academy-art.html academy art fransico san] [http://luveniavine.webcindario.com/ontario-ice-skating.html ontario ice skating] [http://kerridahms.ifrance.com/library/drake-university.html drake university des moines iowa] [http://sungsellars.ifrance.com/huge-d-cups.html cup d huge] [http://placed.toythieves.com/comments/az-prison-inmate.html az prison inmate] [http://ettieshover.talk4fun.net/new/380-celeron-computer.html 380 celeron computer intel m notebook processor toshiba] [http://poise.lflinkup.net/hotwheel.html hotwheel] [http://rosena.w8w.pl/barbie-jeep.html barbie jeep] [http://kristlegr.w8w.pl/content/boys-in-tighty.html boy in tighty whities] [http://hollifox.w8w.pl/articles/physical-education.html physical education] [http://danettemoyle.talk4fun.net/silly-cartoon-characters.html silly cartoon characters] [http://markettacaouett.ifrance.com/nbcnews.html nbc news chicago] [http://shizukostivers.talk4fun.net/web/ultraiso-serial.html ultraiso serial number] [http://posset.sendsmtp.com/blog/window-width.html window width height] [http://liliros.w8w.pl/houston-public.html houston public library] [http://kristlegr.w8w.pl/content/spacers.html spacers] [http://kristlegr.w8w.pl/content/sand-key-resort.html sand key resort clearwater fl] [http://liliros.w8w.pl/nude-young-art.html art legal nude young] [http://mitsuespann.talk4fun.net/hosting-cheap-hosting.html hosting cheap hosting techcityhosting.net] [http://blakerivet.ifrance.com/us-bank-reliacard.html us bank reliacard] [http://placed.toythieves.com/comments/ramayana.html ramayana] [http://huepinder.talk4fun.net/blog/ge-profile-refrigerator.html ge profile refrigerator] [http://mildredg.w8w.pl/q/maura-tierney.html maura tierney] [http://jasmineki.w8w.pl/data/pse-archery.html pse archery] [http://jasmineki.w8w.pl/data/toefl-testing-locations.html toefl testing locations] [http://amybade.talk4fun.net/q/toronto-industrial.html toronto industrial plumbing supplies] [http://amelialinck.ifrance.com/new/who-makes-ranch.html who makes ranch king mowers] [http://ought.dynamicdns.me.uk/new/merits-of-laziness.html merits of laziness] [http://lordly.h1x.com/styles/girls-flashing.html girls flashing on spring break lake havasu] [http://amelialinck.ifrance.com/new/nessus-windows.html nessus windows] [http://rosena.w8w.pl/postgres.html collaboration database postgres] [http://amelialinck.ifrance.com/new/akwid.html akwid] [http://tawdry.ourhobby.com/styles/floor-wax.html floor wax] [http://pearls.organiccrap.com/comments/zoophile.html zoophile] [http://cassidyderoche.ifrance.com/new/ticket-ukraine.html air ticket ukraine] [http://markettacaouett.ifrance.com/hot-cold-seal-packaging.html hot cold seal packaging] [http://vernonlippert.ifrance.com/article/boston-properties.html boston properties] [http://singe.dsmtp.com/comments/mysql-number.html mysql number of rows in a table] [http://placed.toythieves.com/comments/ouick-time-player.html ouick time player] [http://meganmulvaney.ifrance.com/insightbbcom.html insightbb.com message private] [http://ettieshover.talk4fun.net/new/best-supplements.html best supplements] [http://sungsellars.ifrance.com/fall.html 11 9 decline ever fall from greatest katrina sold story truth] [http://geraldgilham.talk4fun.net/wccp-squid.html wccp squid] [http://kristlegr.w8w.pl/content/e-folder-list.html e folder list mail message] [http://geraldgilham.talk4fun.net/hairiest-woman.html hairiest woman in the world] [http://ought.dynamicdns.me.uk/new/sade.html sade] [http://renatabraatz.talk4fun.net/regulatory-consultant.html regulatory consultant] [http://danettemoyle.talk4fun.net/louisiana-map.html louisiana map] [http://vernonlippert.ifrance.com/article/logo-designs.html logo designs] [http://cassidyderoche.ifrance.com/new/laffy-taffy.html laffy taffy] [http://veniceri.w8w.pl/beach-sunset.html beach sunset] [http://martiriffle.ifrance.com/content/for-better-or.html for better or for worse] [http://cassidyderoche.ifrance.com/new/day-tripper.html day tab tripper] [http://martiriffle.ifrance.com/content/red-neon-lights.html red neon lights] [http://meganmulvaney.ifrance.com/tri-steel-home.html tri steel home] [http://zaidahartig.webcindario.com/lib/discount-internet.html discount internet pharmacies] [http://kaiweesner.ifrance.com/amish-country.html amish cooking country from from hearty kitchen mennonite quilt recipe] [http://pearls.organiccrap.com/comments/large-inner-labia.html large inner labia lips] [http://ettieshover.talk4fun.net/new/dirt-cheap-airfare.html dirt cheap airfare] [http://meganmulvaney.ifrance.com/custom-choppers.html chopper custom motorcycle]  
[[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 ==
* [[Viewmodels in XSI|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