Revision as of 17:14, 20 April 2019

Contents

Material proxies allow a game's compiled C++ code to manipulate the properties of a material. Many proxies perform specific tasks, but there are other more general ones that together provide rudimentary scripting support within VMT files.

Any number of proxies can be added to a material; they will be executed in the order in which they appear.

Bug:Some users have reported that the tools mode will not run some functional proxies in game.

To do: Extent of bug untested.

Usage

This material has a Sine proxy which makes it fade in and out of view over a period of eight seconds:

LightmappedGeneric{$basetextureshadertest/LightmappedTextureProxies// proxies are listed inside this block{Sine// a proxy which produces a sine wave{resultVar$alpha// The shader parameter to be manipulatedsineperiod8sinemin0sinemax1}}}

Variables

Materials can declare their own variables for internal use. Such variables must be declared outside the Proxies block, in the body of the material, and must have default values specified on the right-hand side.

These custom variables might be used to pass results between proxies or to submit hard-coded data to them. They are often employed to chain mathematic function proxies (i.e. Add, Subtract, etc) together into longer equations. For 2D/3D vectors ("[0 0 0]"), the variable name can have [0] suffixed to read/write a specific index. Writes to indexed variables should be encased in quotes.

This example extends the one above by staggering the starting position of the sine wave:

$pos"[0 0 0]"$posX.0//must be float or Clamp will not save the value properly$posY.0//must be float or Clamp will not save the value properly$posZ.0//must be float or Clamp will not save the value properly$zero0//Proxy that outputs a 3d vectorPlayerPosition{scale1resultVar"$pos"}//Split the 3d vector for further useClamp{srcVar1$zeromin"$pos[0]"max"$pos[0]"resultVar$posX}Clamp{srcVar1$zeromin"$pos[1]"max"$pos[1]"resultVar$posY}Clamp{srcVar1$zeromin"$pos[2]"max"$pos[2]"resultVar$posZ}

Warning: Quotes are needed when addressing a vector's component, or when defining a vector.

Writing new proxies

New proxies are easy to create. They exist on the client only and should inherit from IMaterialProxy or one of its descendants.

Called when the material is first precached. Use this function to initialise variables and grab references to the material vars you will be using. Return true on success and false on failure (in which case the proxy will not be run).

pKeyValues contains the proxy parameters from the VMT file.

void OnBind( void* pC_BaseEntity )

Called when the material is about to be rendered on an entity. This is where the work is done.

When coding this function it is important to remember that all entities using a material share the same material object, and that if you change it on one entity it changes everywhere else too. Since OnBind() is called every time an entity comes up for rendering this is not a problem so long as you reassign the value you want every time. Don't return early just because there has been no change, and don't store any input data in the proxy.

Note:pC_BaseEntity doesn't lead to a C_BaseEntity as its name suggests, but rather to the associated IClientRenderable. The easiest way to access the entity directly is to base your class on CEntityMaterialProxy (in proxyentity.h) and use the OnBind(C_BaseEntity*) overload it provides.