Animated Particles

From Valve Developer Community
Revision as of 14:43, 12 November 2015 by Shawnolson (talk | contribs) (Added info about MKSHEET integration in 3ds Max)
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.

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 are different between the RGB and Alpha frames.

Example output - RGB Example output - Alpha

Compiling the Sheet

Once the materials are created, move all of your .tga and .mks files to the "Steamapps/common/SourceSDK/bin/orangebox/bin" folder. Inside this directory, create a .bat file, and inside it, write:

mksheet <sheetname>.mks <sheetname>.sht <sheetname>.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. Finally, the third optional parameter is the .tga file to create, consisting of all the tga files previously specified. The second and third parameters must bear the name of the final material you wish to create. For example, the build call for the smoke1.vmt material would be:

mksheet smoke1.mks smoke1.sht smoke1.tga

From this, you will get a/some compiled tga files, a .sht file and a blank <sheetname>.file file. feel free to delete the .file file.

Compiling the Texture

At this point you should have a .tga file. You can now compile the output .tga file using the vtex.exe tool. Drag both the final .tga file/s, and the .sht file to your game's "materialsrc" file. For Half Life 2, this folder is located at "common/Half-Life 2/hl2/materialsrc". From there, open a new window, and navigate to the location of vtex.exe. Then, simply drag the .sht file onto the vtex.exe. Your finished material will be located in the base materials folder. For Half Life 2, this folder is located at ""common/Half-Life 2/hl2/materials".

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.

Step by Step Tutorial

Warning.png Warning: At the time of writing(2014.12) this tutorial worked for mksheet.exe from Left 4 Dead 2 and didn't work for Alien Swarm's mksheet.exe because of error.

step 0. Starting with two 128x128 32bit .TGA files: 'test000.tga' and 'test001.tga'

step 1. Place these two .TGAs into the bin folder of your project. It must be the same folder where the MKSHEET application is located.

step 2. Now place your .mks file into this same folder.. in this case it is named 'test.mks' and its contents look like this:

sequence-rgba 0
frame test000.tga 1
frame test001.tga 1
// I'm just trying to create a two image sheet!

step 3. Using your Command Prompt, navigate to the same folder where MKSHEET and your .TGAs and .MKS files are.. (maybe looks like this: ...common\left 4 dead\bin)

step 4. Now type in a command like this: mksheet test.mks test.sht

step 5. Press enter and watch mksheet make both a new .tga and .sht file!

if it worked correctly, you should be able to open the new .tga file in photoshop and see that it is an array of all your sequence images.

Now you should be able to add your new .TGA and .SHT along with an empty .TXT file (named test.txt in this example) into a materialsrc folder and drop the .txt file onto VTEX. VTEX should then wrap it all up into a new .VTF (with sht info included).

This new .VTF (along with a basic spritecard .vmt of course) will contain all the animation info that the particle system editor needs to get your sprites animating

Automation in 3ds Max

You can now export rendered sequences directly into Source with Wall Worm. The system allows you to export IFL (Image File List) bitmaps made of TGA bitmaps. Based on the bitmap parameters, the MKS is generated automatically, then sent to mkshheet with the .SHT file and TGA files. See complete documentation at