Animated Particles

From Valve Developer Community
Revision as of 18:55, 19 January 2009 by VDCBot (talk | contribs) (Robot: Automated text replacement (-Category:Material system +Category:Material System))
Jump to: navigation, search

Particles are animated by using a material comprised of a collection of materials all built together into a "sheet". This is accomplished by using the mksheet.exe tool.

Warning.png Warning: mksheet.exe is currently non-functional.

Creating an MKS File

First, place your materials that will make up the sheet in a separate sub-directory, usually named for the material they will ultimately represent. For instance, the smoke1.vmt is placed under the materials/particles/smoke1 subdirectory.

Next, create a file with the same name as the material you'd like to make, and give it an .mks file extension. For the smoke1.vmt, you would call this smoke1.mks.

The .mks file defines how the sheet is interpreted when the particle is rendered. You can organize materials in to sequences for playback, define the number of frames and their playback rate, and whether a sequence should loop continuously.

In the .mks file, this looks like:

// First sequence
sequence 0
frame mymaterial1.tga 1
frame mymaterial2.tga 1

// Second sequence
sequence 1
frame mymaterial3.tga 1

// multiple image sequence (two images per frame, for multi-texturing)
sequence 2
frame fire_base0.tga fire_additive0.tga 1
frame fire_base1.tga fire_additive1.tga 1

// Sequence that combines the alpha channels of two frames at a time
//  into the alpha and green channels of a frame for a special shader
sequence 3
frame frame0.tga{g=a},frame1.tga{a=a} 1
frame frame2.tga{g=a},frame3.tga{a=a} 1
Tells mksheet that the following frames are to be grouped together into a sequence, which can be referred to by number. This allows you to pick different animations or frame groups when the particle is created.
Takes two parameters. The first is the material to use for this frame. The second is the playback rate. A value of 1 tells the renderer to playback this frame for the normal time duration. A value of 0.5 would play the frame for half as long as was specified in the particle definition, and a value of 2 would make the frame render for twice as long.
Tells the renderer to loop the frames continuously. Without this identifier the renderer would play all the frames in the sequence once and stop on the last frame.

Additionally, frames can be packed separately in RGB from Alpha. This takes the alphas from a set of input frames and stores them in the alpha of the output sheet. It takes the RGBs and stores them in the RGB. The interesting thing about this is that each get their own sequences. They also have their frame sizes entirely decoupled, so the RGB's can have 200x200 images while the alpha has 150x150, for example. See Below :

// Sequence that stores separate frame data in the RGB from the alpha
//  for dual sequencing combining one set of RGBs and another set of alphas

// Packmode sets mksheet to separate the RGB frames from the Alpha ones.
packmode rgb+a

// First Sequence - Looping Alpha Frames
sequence-a 0
frame reframedSmokeSprites170_0033.tga 1
frame reframedSmokeSprites170_0035.tga 1

// Second Sequence - Looping RGB Frames
sequence-rgb 1
frame smokeTex0001_341.tga 1
frame smokeTex0002_341.tga 1

The output from this .mks file can be seen below. The RGB and alpha channels are shown. Note that the individual frame sizes are all non-power-of-two and that they different between the RGB and Alpha frames.

Example output - RGB Example output - Alpha

Compiling the Sheet

Once the materials are in the proper directory, along with the .mks file, you can compile the images in to one sheet. To do this, we use the mksheet.exe tool.

mksheet sheet.mks [sheet.sht sheet.tga]

The tool takes one main parameter and two optional ones. The first is the .mks sheet which will define how the .sht and .tga files are created. The second, optional parameter is the .sht file to create (used by the engine). Finally, the third optional parameter is the .tga file to create which is the packed version of all the materials specified in the .mks file. The second and third parameters should almost always bear the name of the ultimate material you wish to create. For example, the build call for the smoke1.vmt material would be:

Note.png Note:  .sht files are no longer used by the engine - they are now incorporated directly into the .vtf.
mksheet smoke1.mks [smoke1.sht smoke1.tga]

Compiling the Texture

At this point you should have a .sht and .tga file. You can now compile the output .tga file using the vtex.exe tool like any other material. The vtex.exe tool will automatically place the .sht file in the correct place. Once the material is compiled, it is ready for use in the engine.

Note.png Note:  .sht files are no longer used by the engine and not required.
Warning.png Warning: VTFEdit probably doesn't support this type of compiling yet.


You can use the same image file in multiple sequences (or multiple times within the same sequence) without it being duplicated in the output sheet. Examples where you would want to do this are sequences with different timing, particle sequences, looped and non-looped versions of a sequence, etc.

To the extent practical, you should combine as many sprite textures into one sheet as possible, and use different sequences for the different particle systems (hmm this implies that we might want named sequences for sanity's sake). This will allow particle systems to be drawn with fewer state changes or even as one batch.