Material proxies: Difference between revisions
m (→Proxy List) |
m (Directed link. Major formatting.) |
||
Line 1: | Line 1: | ||
=The Basics= | =The Basics= | ||
Line 46: | Line 45: | ||
} | } | ||
</pre> | </pre> | ||
=Variables= | =Variables= | ||
Like a standard programming language, the material proxies may declare temporary local variables to be used to store intermediate values for further use later in the proxy. Variables are declared outside of the | Like a standard programming language, the material proxies may declare temporary local variables to be used to store intermediate values for further use later in the proxy. Variables are declared outside of the ''Proxies'' block and have default values specified on their right-hand side. | ||
<pre> | <pre> | ||
Line 65: | Line 66: | ||
</pre> | </pre> | ||
There is no practical limit to the number of local variables declared. Local variables may be used to store results from proxies, or to pass data into proxies as parameters. They are often employed to chain mathematic function proxies (i.e. | There is no practical limit to the number of local variables declared. Local variables may be used to store results from proxies, or to pass data into proxies as parameters. They are often employed to chain mathematic function proxies (i.e. ''Add'', ''Subtract'', etc) together into longer equations for rendering values. | ||
=Common Result Variables= | =Common Result Variables= | ||
Line 88: | Line 91: | ||
{{Note|Certain variables like <code>$color</code> and <code>$baseTextureOffset</code> can be accessed like an [[array]]. <code>"$color[0]"</code> would access the red component of the variable.}} | {{Note|Certain variables like <code>$color</code> and <code>$baseTextureOffset</code> can be accessed like an [[array]]. <code>"$color[0]"</code> would access the red component of the variable.}} | ||
=Writing Material Proxy Implementations= | =Writing Material Proxy Implementations= | ||
Line 102: | Line 107: | ||
</pre> | </pre> | ||
The <code>Init()</code> function is called when a material is first created in a session. This function allows you to setup internal state for the material proxy, usually consisting of obtaining references to | The <code>Init()</code> function is called when a material is first created in a session. This function allows you to setup internal state for the material proxy, usually consisting of obtaining references to ''material variables'' for later use. | ||
<pre> | <pre> | ||
Line 119: | Line 124: | ||
{{Note|See <code>DummyProxy.cpp</code> for an example of a simple material proxy.}} | {{Note|See <code>DummyProxy.cpp</code> for an example of a simple material proxy.}} | ||
=Proxy List= | =Proxy List= | ||
Line 127: | Line 134: | ||
|- | |- | ||
| <code>Add</code> || Adds two variables together. || | | <code>Add</code> || Adds two variables together. || | ||
''srcVar1'' | |||
* Name of the first source variable. | * Name of the first source variable. | ||
''srcVar2'' | |||
* Name of the second source variable. | * Name of the second source variable. | ||
''resultVar'' | |||
* ( srcVar1 + | * ( srcVar1 + ''srcVar2'') | ||
|- | |- | ||
| <code>Multiply</code> || Multiplies two variables together. || | | <code>Multiply</code> || Multiplies two variables together. || | ||
''srcVar1'' | |||
* Name of the first source variable. | * Name of the first source variable. | ||
''srcVar2'' | |||
* Name of the second source variable. | * Name of the second source variable. | ||
''resultVar'' | |||
* ( | * ( ''srcVar1'' * ''srcVar2'') | ||
|- | |- | ||
| <code>Subtract</code> || Subtracts the second variable from the first. || | | <code>Subtract</code> || Subtracts the second variable from the first. || | ||
''srcVar1'' | |||
* Name of the first source variable. | * Name of the first source variable. | ||
''srcVar2'' | |||
* Name of the second source variable. | * Name of the second source variable. | ||
''resultVar'' | |||
* ( | * ( ''srcVar1'' - ''srcVar2'') | ||
|- | |- | ||
| <code>Divide</code> || Divides the first variable by the second. || | | <code>Divide</code> || Divides the first variable by the second. || | ||
''srcVar1'' | |||
* Name of the first source variable. | * Name of the first source variable. | ||
''srcVar2'' | |||
* Name of the second source variable. | * Name of the second source variable. | ||
''resultVar'' | |||
* ( | * ( ''srcVar1'' / ''srcVar2'') | ||
|- | |- | ||
| <code>Equals</code> || Copies a variable to the result variable. || | | <code>Equals</code> || Copies a variable to the result variable. || | ||
''srcVar1'' | |||
* Name of the source variable. | * Name of the source variable. | ||
''resultVar'' | |||
* Set equal to | * Set equal to ''srcVar1'' | ||
|- | |- | ||
| <code>Abs</code> || Computes the absolute value of a variable. || | | <code>Abs</code> || Computes the absolute value of a variable. || | ||
''srcVar1'' | |||
* Name of the source variable. | * Name of the source variable. | ||
''resultVar'' | |||
* abs( | * abs( ''srcVar1'') | ||
|- | |- | ||
| <code>Frac</code> || Returns the fractional component of a number.<br>{{Note|If a vector type variable is provided, this function will return the fractional component of each member of the [[array]].}} || | | <code>Frac</code> || Returns the fractional component of a number.<br>{{Note|If a vector type variable is provided, this function will return the fractional component of each member of the [[array]].}} || | ||
''srcVar1'' | |||
* Number to find the fractional component of | * Number to find the fractional component of | ||
|- | |- | ||
| <code>Exponential</code> || Computes ( A * e ( | | <code>Exponential</code> || Computes ( A * e (''srcVar1''+B) ). || | ||
''srcVar1'' | |||
* Name of the source variable. | * Name of the source variable. | ||
''scale'' | |||
* Specifies the value of A | * Specifies the value of A | ||
''offset'' | |||
* Specifies the value of B | * Specifies the value of B | ||
''minVal'' | |||
* Minimum clamping value | * Minimum clamping value | ||
''maxVal'' | |||
* Maximum clamping value | * Maximum clamping value | ||
''resultVar'' | |||
* ( A * e ( | * ( A * e (''srcVar1''+B) ) | ||
|- | |- | ||
| <code>Clamp</code> || Clamps a variable to a specified range of values. || | | <code>Clamp</code> || Clamps a variable to a specified range of values. || | ||
''srcVar1'' | |||
* Name of the source variable. | * Name of the source variable. | ||
''min'' | |||
* Minimum clamping value | * Minimum clamping value | ||
''max'' | |||
* Maximum clamping value | * Maximum clamping value | ||
''resultVar'' | |||
* clamp( | * clamp( ''srcVar1'', ''Min'', ''Max'' ) | ||
|- | |- | ||
| <code>LessOrEqual</code> || Returns different values based relation of supplied variables. || | | <code>LessOrEqual</code> || Returns different values based relation of supplied variables. || | ||
''srcVar1'' | |||
* Name of the first source variable. | * Name of the first source variable. | ||
''srcVar1'' | |||
* Name of the second source variable. | * Name of the second source variable. | ||
''lessEqualVar'' | |||
* Name of material variable to copy to result if ( | * Name of material variable to copy to result if (''srcVar1'' <= ''srcVar2'') | ||
''greaterVar'' | |||
* Name of material var to copy to result if ( | * Name of material var to copy to result if (''srcVar1'' > ''srcVar2'') | ||
''resultVar'' | |||
* Resulting value | * Resulting value | ||
|- | |- | ||
| <code>Sine</code> || Creates a sine wave. || | | <code>Sine</code> || Creates a sine wave. || | ||
''sineperiod'' | |||
* Period of the sine wave in seconds. | * Period of the sine wave in seconds. | ||
''sinemin'' | |||
* Minimum value of the sine wave. | * Minimum value of the sine wave. | ||
''sinemax'' | |||
* Maximum value of the sine wave. | * Maximum value of the sine wave. | ||
''timeoffset'' | |||
* Used to start the sine wave at a different point in time | * Used to start the sine wave at a different point in time | ||
''resultVar'' | |||
* Resulting value. | * Resulting value. | ||
|- | |- | ||
Line 225: | Line 232: | ||
rate | rate | ||
* Rate of increase of the value. | * Rate of increase of the value. | ||
''initialValue'' | |||
* Initial value to increase. | * Initial value to increase. | ||
''resultVar'' | |||
* Resulting value. | * Resulting value. | ||
|- | |- | ||
| <code>UniformNoise</code> || Produces a noisy signal where each value is equally likely to occur || | | <code>UniformNoise</code> || Produces a noisy signal where each value is equally likely to occur || | ||
''minVal'' | |||
* Minimum value range. | * Minimum value range. | ||
''maxVal'' | |||
* Maximum value range. | * Maximum value range. | ||
''resultVar'' | |||
* Resulting value. | * Resulting value. | ||
|- | |- | ||
Line 241: | Line 248: | ||
mean | mean | ||
* The average random value. | * The average random value. | ||
''halfWidth'' | |||
* The distance from the average at which it’s only 30% likely to occur. | * The distance from the average at which it’s only 30% likely to occur. | ||
''minVal'' | |||
* Minimum value range (clamped). | * Minimum value range (clamped). | ||
''maxVal'' | |||
* Maximum value range (clamped). | * Maximum value range (clamped). | ||
''resultVar'' | |||
* Resulting value. | * Resulting value. | ||
|- | |- | ||
| <code>MatrixRotate</code> || Creates a rotation matrix from the provided axis and angle. || | | <code>MatrixRotate</code> || Creates a rotation matrix from the provided axis and angle. || | ||
''axisVar'' | |||
* Axis of rotation (x,y,z) = (0,1,2). | * Axis of rotation (x,y,z) = (0,1,2). | ||
''angle'' | |||
* Degrees of rotation around axis. | * Degrees of rotation around axis. | ||
''resultVar'' | |||
* Rotation matrix. | * Rotation matrix. | ||
|- | |- | ||
| <code>PlayerProximity</code> || Stores the distance from the entity the material is on to the player into a variable. <br> {{Note|Does not work on world surfaces.}} || | | <code>PlayerProximity</code> || Stores the distance from the entity the material is on to the player into a variable. <br> {{Note|Does not work on world surfaces.}} || | ||
''scale'' | |||
* An amount to scale the distance by. | * An amount to scale the distance by. | ||
''resultVar'' | |||
* Resulting value. | * Resulting value. | ||
|- | |- | ||
| <code>PlayerView</code> || Stores the dot product of the view direction + the direction from the camera to the entity the material is on.<br>{{Note|Does not work on world surfaces.}} || | | <code>PlayerView</code> || Stores the dot product of the view direction + the direction from the camera to the entity the material is on.<br>{{Note|Does not work on world surfaces.}} || | ||
''scale'' | |||
* Amount to scale the dot product by. | * Amount to scale the dot product by. | ||
''resultVar'' | |||
* Resulting value. | * Resulting value. | ||
|- | |- | ||
| <code>PlayerSpeed</code> || Stores the speed of the player into a variable. || | | <code>PlayerSpeed</code> || Stores the speed of the player into a variable. || | ||
''scale'' | |||
* Amount to scale the speed by. | * Amount to scale the speed by. | ||
''resultVar'' | |||
* Resulting value. | * Resulting value. | ||
|- | |- | ||
| <code>PlayerPosition</code> || Stores the player’s position into a variable.<br>{{Note|Only works for vector variables like <code>$baseTextureOffset</code> or <code>$color</code>.}} || | | <code>PlayerPosition</code> || Stores the player’s position into a variable.<br>{{Note|Only works for vector variables like <code>$baseTextureOffset</code> or <code>$color</code>.}} || | ||
''scale'' | |||
* An amount to scale the position by. | * An amount to scale the position by. | ||
''resultVar'' | |||
* Resulting value. | * Resulting value. | ||
|- | |- | ||
| <code>EntitySpeed</code> || Stores the entity’s speed into a variable.<br>{{Note|Does not work on world surfaces.}} || | | <code>EntitySpeed</code> || Stores the entity’s speed into a variable.<br>{{Note|Does not work on world surfaces.}} || | ||
''scale'' | |||
* An amount to scale the speed by. | * An amount to scale the speed by. | ||
''resultVar'' | |||
* Resulting value. | * Resulting value. | ||
|- | |- | ||
| <code>TextureTransform</code> || Generates a texture transform matrix. || | | <code>TextureTransform</code> || Generates a texture transform matrix. || | ||
''centerVar'' | |||
* Name of a variable holding the center of rotation and scaling. [Optional] | * Name of a variable holding the center of rotation and scaling. [Optional] | ||
''scaleVar'' | |||
* Name of scale variable (2D vector). [Optional] | * Name of scale variable (2D vector). [Optional] | ||
''rotateVar'' | |||
* Name of rotation angle variable (float). [Optional] | * Name of rotation angle variable (float). [Optional] | ||
''translateVar'' | |||
* Name of the translation variable (2D vector). | * Name of the translation variable (2D vector). | ||
''resultVar'' | |||
* Resulting value. | * Resulting value. | ||
|- | |- | ||
Line 303: | Line 310: | ||
|- | |- | ||
| <code>TextureScroll</code> || Returns a transform matrix or vector that will translate a texture at a given angle at a given rate. || | | <code>TextureScroll</code> || Returns a transform matrix or vector that will translate a texture at a given angle at a given rate. || | ||
''textureScrollVar'' | |||
* Destination for the resulting transformation. | * Destination for the resulting transformation. | ||
''textureScrollRate'' | |||
* Rate of scroll in units per second. | * Rate of scroll in units per second. | ||
''textureScrollAngle'' | |||
* Angle of rotation to move along. (90 = right, 180 = left, etc) | * Angle of rotation to move along. (90 = right, 180 = left, etc) | ||
|- | |- | ||
| <code>ToggleTexture</code> || Toggles a texture based on the frame number set by the attached entity.<br> {{Note|Must be attached to an entity.}} || | | <code>ToggleTexture</code> || Toggles a texture based on the frame number set by the attached entity.<br> {{Note|Must be attached to an entity.}} || | ||
''toggleTextureVar'' | |||
* Texture to modify based on frames. | * Texture to modify based on frames. | ||
''toggleTextureFrameNumVar'' | |||
* Variable used for frame number. | * Variable used for frame number. | ||
''toggleShouldWrap'' | |||
* Whether the animation should wrap over not. | * Whether the animation should wrap over not. | ||
|- | |- | ||
| <code>RandomEntity</code> || A proxy that returns a random number associated with the entity the material is applied to. Can be helpful for making animated textures not all be in sync. || | | <code>RandomEntity</code> || A proxy that returns a random number associated with the entity the material is applied to. Can be helpful for making animated textures not all be in sync. || | ||
''scale'' | |||
* Amount to scale the random number by. | * Amount to scale the random number by. | ||
''resultVar'' | |||
* Resulting value. | * Resulting value. | ||
|- | |- | ||
| <code>CurrentTime</code> || Returns the current time as a float. || | | <code>CurrentTime</code> || Returns the current time as a float. || | ||
''resultVar'' | |||
* The current time. | * The current time. | ||
|- | |- | ||
| <code>PlayerHealth</code> || Stores the player health (0-1) in a variable. || | | <code>PlayerHealth</code> || Stores the player health (0-1) in a variable. || | ||
''scale'' | |||
* Amount to scale the health value by. | * Amount to scale the health value by. | ||
''resultVar'' | |||
* Resulting value. | * Resulting value. | ||
|- | |- | ||
| <code>PlayerDamageTime</code> || Stores the time since the player was last damaged, in a variable. || | | <code>PlayerDamageTime</code> || Stores the time since the player was last damaged, in a variable. || | ||
''scale'' | |||
* Amount to scale the time by. | * Amount to scale the time by. | ||
''resultVar'' | |||
* Resulting value. | * Resulting value. | ||
|- | |- | ||
Line 356: | Line 363: | ||
|- | |- | ||
| <code>ConveyorScroll</code> || Returns the scroll parameters for a texture used as a conveyor. <br>{{Note|Must be attached to <code>func_conveyor</code> entity.}} || | | <code>ConveyorScroll</code> || Returns the scroll parameters for a texture used as a conveyor. <br>{{Note|Must be attached to <code>func_conveyor</code> entity.}} || | ||
''textureScrollVar'' | |||
* Name of variable to place result in. | * Name of variable to place result in. | ||
* {{Note|must be a matrix or vector type variable (i.e. <code>$baseTextureOffset</code>).}} | * {{Note|must be a matrix or vector type variable (i.e. <code>$baseTextureOffset</code>).}} | ||
Line 367: | Line 374: | ||
|- | |- | ||
| <code>AnimatedTexture</code> || Increments frame variable || | | <code>AnimatedTexture</code> || Increments frame variable || | ||
''animatedtexturevar'' | |||
* Texture to increment frame for (i.e. <code>$basetexture</code>, <code>$bumpmap</code>) | * Texture to increment frame for (i.e. <code>$basetexture</code>, <code>$bumpmap</code>) | ||
''animatedtextureframenumvar'' | |||
* Frame variable to increment (i.e. <code>$frame</code>) | * Frame variable to increment (i.e. <code>$frame</code>) | ||
''animatedtextureframerate'' | |||
* Framerate in frames per second | * Framerate in frames per second | ||
|} | |} | ||
==See also== | ==See also== | ||
[[ | [[Material Creation]] | ||
[[Category:Material System]] |
Revision as of 17:57, 29 August 2007
The Basics
Material proxies allow materials to change their rendering properties (color, alpha values, etc) using game state and scripted equations. Proxies are defined as a block of text, using key/value pairing, inside of the VMT description of a material. Proxies are available for all materials. A proxy is defined as in the following example:
"LightmappedGeneric" { "$basetexture" "shadertest/LightmappedTexture" // Inside these braces are where all the proxies go "Proxies" { // This is a sine proxy "Sine" { // This is the data for the sine proxy // Notice there is no '$' on the proxy variables "resultVar" "$alpha" "sineperiod" 8 "sinemin" 0 "sinemax" 1 } } }
The above block of text instructs the material to use the Sine
material proxy to modify the alpha
value of the material when rendered. The other entries define parameters for the proxy to use in its calculations. Here the sine wave has a sineperiod
of 8 seconds and oscillates between a value of "0" and "1".
It is possible to define multiple proxies in one material within the Proxies section of that material's definition. If multiple proxies are defined, they are executed in a top to bottom order.
"Proxies" { // This is executed first "A" { . . . } // This is executed second "B" { . . . } }
Variables
Like a standard programming language, the material proxies may declare temporary local variables to be used to store intermediate values for further use later in the proxy. Variables are declared outside of the Proxies block and have default values specified on their right-hand side.
"LightmappedGeneric" { "$basetexture" "shadertest/LightmappedTexture" // A local variable for this material, defaulting to "0.5" "$myScale" 0.5 "Proxies" { . . . } }
There is no practical limit to the number of local variables declared. Local variables may be used to store results from proxies, or to pass data into proxies as parameters. They are often employed to chain mathematic function proxies (i.e. Add, Subtract, etc) together into longer equations for rendering values.
Common Result Variables
The following values are commonly used as $resultVar variables.
$alpha |
Fade value (0 = transparent, 1 = opaque) |
$color |
Modulation color (R,G,B) (1,1,1) = white, (0,0,0) = black |
$envmapmaskscale |
An amount to scale the environment map mask |
$frame |
Frame number of an animated texture |
$envmaptint |
Modulation color for the environment map |
$selfillumtint |
Modulation color for the self-illumination |

$alpha
and $color
values are clamped between 0 and 1.
$color
and $baseTextureOffset
can be accessed like an array. "$color[0]"
would access the red component of the variable.
Writing Material Proxy Implementations
Although many generic proxies are included in the client already, it will sometimes be necessary to create a custom material proxy for your MOD. To do this, code will be required on the client-side. Material proxies are descended from the IMaterialProxy
interface class. The proxy has three main functions to do its work in. The first is the Init() function, defined as:
bool Init( IMaterial *pMaterial, KeyValues *pKeyValues ); pMaterial Material we’re acting on pKeyValues List of key-value pairs for this material
The Init()
function is called when a material is first created in a session. This function allows you to setup internal state for the material proxy, usually consisting of obtaining references to material variables for later use.
void OnBind( void *pC_BaseEntity ); pC_BaseEntity Entity this material is being applied to (if any)
The OnBind()
function is called every time Bind()
is called on a material. This is where most of the material proxy’s work is done.
Finally, the proxy must expose its interface so that the named reference to the proxy can be linked up to the class implementation. This is done via the EXPOSE_INTERFACE
macro.
EXPOSE_INTERFACE( className, interfaceName, proxyName, IMATERIAL_PROXY_INTERFACE_VERSION );

DummyProxy.cpp
for an example of a simple material proxy.
Proxy List
The following proxies are defined in the client DLL for use in any Source game.
Proxy Name | Description | Variables |
---|---|---|
Add |
Adds two variables together. |
srcVar1
srcVar2
resultVar
|
Multiply |
Multiplies two variables together. |
srcVar1
srcVar2
resultVar
|
Subtract |
Subtracts the second variable from the first. |
srcVar1
srcVar2
resultVar
|
Divide |
Divides the first variable by the second. |
srcVar1
srcVar2
resultVar
|
Equals |
Copies a variable to the result variable. |
srcVar1
resultVar
|
Abs |
Computes the absolute value of a variable. |
srcVar1
resultVar
|
Frac |
Returns the fractional component of a number.![]() |
srcVar1
|
Exponential |
Computes ( A * e (srcVar1+B) ). |
srcVar1
scale
offset
minVal
maxVal
resultVar
|
Clamp |
Clamps a variable to a specified range of values. |
srcVar1
min
max
resultVar
|
LessOrEqual |
Returns different values based relation of supplied variables. |
srcVar1
srcVar1
lessEqualVar
greaterVar
resultVar
|
Sine |
Creates a sine wave. |
sineperiod
sinemin
sinemax
timeoffset
resultVar
|
LinearRamp |
Produces an ever increasing value. |
rate
initialValue
resultVar
|
UniformNoise |
Produces a noisy signal where each value is equally likely to occur |
minVal
maxVal
resultVar
|
GaussianNoise |
Produces a noisy signal where values are near the average. |
mean
halfWidth
minVal
maxVal
resultVar
|
MatrixRotate |
Creates a rotation matrix from the provided axis and angle. |
axisVar
angle
resultVar
|
PlayerProximity |
Stores the distance from the entity the material is on to the player into a variable. ![]() |
scale
resultVar
|
PlayerView |
Stores the dot product of the view direction + the direction from the camera to the entity the material is on.![]() |
scale
resultVar
|
PlayerSpeed |
Stores the speed of the player into a variable. |
scale
resultVar
|
PlayerPosition |
Stores the player’s position into a variable.![]() $baseTextureOffset or $color . |
scale
resultVar
|
EntitySpeed |
Stores the entity’s speed into a variable.![]() |
scale
resultVar
|
TextureTransform |
Generates a texture transform matrix. |
centerVar
scaleVar
rotateVar
translateVar
resultVar
|
Empty |
Used to comment out proxies. Surround a bunch of proxies with the empty proxy to cause those proxies to not operate. | |
TextureScroll |
Returns a transform matrix or vector that will translate a texture at a given angle at a given rate. |
textureScrollVar
textureScrollRate
textureScrollAngle
|
ToggleTexture |
Toggles a texture based on the frame number set by the attached entity.![]() |
toggleTextureVar
toggleTextureFrameNumVar
toggleShouldWrap
|
RandomEntity |
A proxy that returns a random number associated with the entity the material is applied to. Can be helpful for making animated textures not all be in sync. |
scale
resultVar
|
CurrentTime |
Returns the current time as a float. |
resultVar
|
PlayerHealth |
Stores the player health (0-1) in a variable. |
scale
resultVar
|
PlayerDamageTime |
Stores the time since the player was last damaged, in a variable. |
scale
resultVar
|
MaterialModify |
Sets the base texture to a value held by the entity (used for entity controlled texture animations).![]() |
No return value. |
WaterLOD |
Coordinates water LOD values between the map's env_waterlod entity and the materials internal values. |
No return value. |
BreakableSurface |
Sets the base texture to a material name held by the entity (used for switching surface material on shatter)![]() func_breakablesurface entity. |
No return value. |
ConveyorScroll |
Returns the scroll parameters for a texture used as a conveyor. ![]() func_conveyor entity. |
textureScrollVar
|
LampBeam |
Modulates the material’s alpha value based on angle between the beam’s direction and the viewer’s eye point. This is used to make the beams of volumetric light on lights fade as you look at them dead on.![]() |
No return value. |
LampHalo |
Modulates the material’s alpha value based on angle between the beam’s direction and the viewer’s eye point. Like the LampBeam proxy, but used for the halo at the beam’s base. ![]() |
No return value. |
AnimatedTexture |
Increments frame variable |
animatedtexturevar
animatedtextureframenumvar
animatedtextureframerate
|