Tuesday, September 6, 2011

Basic FX Materials in Unreal

VFX Materials
The Material system in Unreal is an amazing tool, since it has so many options and ways to achieve the same result. VFX rely on slick looking materials to achieve their visual target. Today, I'm going to go over the basic materials I use for sprites in Cascade.These are the three work horse materials: Additive, Traslucent and Modulate materials. This post will not be going over lit (sprite) or model materials for cascade as I'll be doing that in a later post. You should have a basic understanding of Unreal's material system.

Material links to the UDN
FX Artist Home 
UDN Materials Tutorals
UDN Materials Compendium
Depth Bias Alpha

Definitions and uses:
Additive Materials: Fire, motes, energy, glowy bits: Additive materials add the sprite against the scene, similar to a Screen blend in Photoshop.
Translucent Materials: For smokes, and faked debris bits: Translucent materials use the source color of the sprite and adds opacity (alpha) to it, then blends it against the scene color.
Modulate Materials: Dark smoke, decals, and some smaller debris: Modulative materials multiply the sprite against the scene, similar to the multiply blend mode in Photoshop.

Before we start
Import textures into unreal that you can use for this test. Make sure all textures are targa and are power of 2.
Additive texture: Black is knocked out, so anything black will not display in an additive blend mode. Remember to make sure the texture uses the space well since the sprite will still need to calculate the unseen areas. This can have an alpha texture but is not necessary for this case. 
Translucent texture: This can be any kind of texture, with an alpha channel that will serve as the knock out. For example if you wanted to have a leaf texture. the RGB would support the color and the alpha would serve as the transparency.
Modulate texture: This is the polar opposite of the Additive. White is knocked out and colors are multiplied against the scene. This can have an alpha texture. 

Additive Material Base
The basic material will be set up in the emissive, and to get anything in game you'll need very little. The following material will have static switch set ups for UV distortion and near fade and a control for depth bias alpha. This will look like this when we're done.

 I'll be breaking down each commented section while we go.

First, In Material make sure the blend mode is set to Additive and the Lighting Model is unlit. In Usage make sure the Used with Particle Sprites and Used with Particle SubUV is checked, otherwise the sprite will not display when you get it to Cascade.

Next drop a Param2d (name it Emissive), a Vertex color and a multiply module near the Emissive hook up. String the modules up like this

Vertex color mult by the texture equals Dynamic color changes in Cascade
This set up will allow you to change the color of the RGB values of the texture in Cascade dynamically. I use this for 99.9% of all FX materials that I use unless I know for sure the color of this will never be changed. At this point you can start working with the sprite in Cascade. This is really all you need. I usually add a couple other properties to the material, since I tend to need them in most particle systems. (Note: Make sure you string this into the Emissive, if you hook it into the diffuse it will appear solid black).

Save your material and package.

Distorting the UVs
Sometimes you'll want to distort the UVs of the texture for fire or to give the surface some extra motion. Set up the following like this and string it into the UV hook of the Param2d Emissive module.

  • The Panner moves your texture in the U and V space as well as controls time. 
  • This is hooked into the UV in of the Param2d UVDisort Texture. The green channel is multiplied against a parameter to control the intensity of the distort and is added to a texture coordinate module (I used the green channel since it had the most contrast). 
  • This creates a surface that can be distorted against the Emissive texture. 

The Switch parameter is set up in case we want to use the distortion. The A channel is the UV Distort option while the B channel is no distortion and is the default in this case. The switch parameter is an if/then statement and will show up in INST materials.

One of the functions of Depth Bias Alpha is to remove hard lines of sprites crashing into geometry. The set up is really simple. All you'd need to do is set up the DepthBiasAlpha module and string it into opacity. The lower the number in the depth bias the less smooth the blend and also the larger the blend. Experiment with this until you get a result you like (defaults to 250). The UDN has more info on this magic module here.

Near Screen fade. 
This next set fades sprites based on its camera position. This keeps sprites from crashing into the camera. Set up the following and hook it into the DepthBiasAlpha Alpha entry:

  • This takes the screen position and masks the Z position, since most sprites are camera facing on the Z axis, then multiplies a distance from the position and clamps it 0-1 to avoid some visual artifacts. 
  • This is then hooked into a switch parameter in case we want to use this functionality. 
From here create an instance of this that you can use for material variations.

That's the additive base in a nutshell. All the other materials are similar to this so I'll be working on the exceptions from here on out.

Translucent Material Base
This is the easiest to change since the base of the additive is most of the work. There are a couple things you'll need to add and change.

First make sure you make a copy, rename the material and change the blend mode to Translucent

Next Add in 2 multiply modules, a Vertex color and a constant clamp and set it up like the following:

  • Hook the alpha (white connector) if the Param2d Emissive to one of the multiply nodes A and connect the DepthBiasAlpha to B. 
  • To make the alpha dynamic in Cascade hook the Vertex color module and the other multiply. Then set up the constant clamp from 0-1 (default) and hook it up to the opacity node. 
  • This will keep the alpha multiplies within a 0-1 range.
You can now use this in Cascade as a translucent material. You can control the color and alpha of the sprite using the color modules. The rest of the material should look like this:

Modulate Material Base
This one will be a little different. The base is still the Additive material, but we're going to need to treat this as everything is blending to white instead of black, since this multiplies its values against the scene. (Note: as of this post I was getting an error with the modulate blend mode against the DepthBiasAlpha node, so it will be absent from the material. I'm looking onto it and will hopefully have an update to it soon)

First thing you'll need to do is change the blend mode to modulate

Next you'll need to re-string a couple entries to make the modulate blend mode work. Change the texture to the modulate texture and add in a Lerp(linearinterp), a constant, an add a constant clamp module. Set it up like this:

This is what this is set up to do. Again we're blending to white, since its a modulate texture
  • The Lerp is using the vertex color (alpha) to blend the texture from its surface to 1 (white)
  • The vertex color is also set up to control the color of the texture (black node to mult B)
  • The alpha of the modulate texture is masking the edging of the surface so if you change the color in Cascade it only covers the circle (in this case)
  • The Add module combines the mask and the lerp controls
  • the Constant clamp keeps it within a 0-1 range
The near fade:
The way the near fade is set up now is to blend to black, so we're going to need to change that. Drop a oneminus near the fade and set it up like this:

  • Place the one minus module between the multiply and the constant clamp to switch the black to white.
  • Then change the constant from 1 to 0 out of the Switch parameter, otherwise the base material will appear as black
  • String switch parameter into the add and then hook the whole thing into the Emissive. 
That should give you a modulate texture, that you can start to use in Cascade. I hope this helped and wasn't too confusing. I'm going to try to get at least one more post out before I am out til the new year. Thanks and until next time.



  1. Sweet post! I have a couple of questions though.
    First off, when you have a network turned off by a switch node, does that get rid of the overhead of having the network at all? What I mean is, is it better to have one shader with all the functionality you could want set up with switch nodes, or have a bunch of different permutations with only the nodes needed for that specific effect?

    Secondly, how do you calculate the cost between different permutations and when to use them? For example the near screen fade, if you are making a smoke grenade I guess it would be brilliant to have since you will always walk through it and need to be careful with your overdraw. But if you for example have a flare lying around. You can walk through it, but most of the time you won't. Having those extra nodes will make the flare more expensive all the time, but it would make it cheaper once you decide o walk through it. How do you decide on which option to go with?

    Last one! :P
    Do you have any suggestions for controlling your uv distortion (or any distortion) from the particle system. So far my method has been to plug distortion strength into the red colour channel. (I was asked to do a stream of urine, so I had the refraction start out strong and fade along the stream). This means I lost the possibility to alter the colour from the particlesystem so I had to do it in either the material or the texture. Are there better ways of doing it?

    Keep posting awesome stuff!

  2. Been having problems posting to my own comments. this is a test

  3. All right. It looks like blogger is having issues with me posting comments to my own site... WTH! When I use my google account it puts me in an endless loop... anyway I hope it gets fixed soon... this is kind of a hack...

    Ok Andreas to answer your questions. I will answer all of them with a large caveat. This is what I've heard...

    First question: First off, when you have a network turned off by a switch node, does that get rid of the overhead of having the network at all? Yes, on compile. The second half of your question with switch nodes depends. I've seen some pretty insane switch happy materials and there is a cost of using them, but its (as I've heard) minor unless they're overused. I use them only for functions I know I will want and switch between or use all the time. If you have a specialty material then keep it separate... if that makes any sense.

    Second Question: Secondly, how do you calculate the cost between different permutations and when to use them? Not to sound like a broken record... (wow that comment dates me) but it depends, Cost calculation you can see on the Instance of the material in the upper left corner of the display. So if you toggle between the 2 different kinds the material cost changes by 2 instructions per kind. These are pretty light weight, so unless your scene is insane or you have a load of overdraw to manage then you should be ok to use them.

    For the near camera fade, this is only a visual fix, the sprites are still being calculated even though they're not seen, so they're not being culled. For the flare example, it would depend on if you could ever walk up to it and if the visual clipping will look odd. So if the visual clipping on the camera is freaking horrible then use it, but if the sprites move through the camera fast enough to not be noticable then don't use it. If that makes any sense. Yes having the extra nodes would make the flare expensive all the time, and walking through it would not make it cheaper, if anything your threads would be hit by the overdraw since it has to draw through the sprite (possibly the whole camera would be filled with smoke) and into the scene. So yes the material would be cheaper by 2 instructions per sprite if you didn't use the camera fade. If you're using a lot of sprites to make the fx work then it would be exceptionally expensive. Oh no, I'm rambling! A lot of it depends on the kind of game you're making and what you're trying to achieve.

    Final one: That is a good question... about urine. Im still laughing, I had a similar issue with vomit... anyway. Again it depends. I would ask how often people are peeing, and how you're getting the stream to work. With Sprites, If you're using the red channel to control the refraction then you're stuck with the mat/texture limitations. I would also ask how close the player is getting to the stream. Can you get away with having less refraction to gain the color? If you wanted to get some color range back, I've seen a method of taking a strip of color and using it against the lifetime of the sprite. IT would be another texture call though, and there was some trickery to make it work. (There may have been some in house tech at work) If you could use a mesh, you could set the refraction to change along the UVS of the stream, as well as the alpha and call it in an event. (in the animation of the man/woman/creature urinating) and put a vertex deformer on the the materialto give it some drift. The fall off would be an issue though... Other than that, the only other thing I can think of is having it animated... but that's costly and not really worth it.

    mmm yup, I hope that answered your questions.


  4. Ace! This is brilliant. There is nowhere near enough people willing to discuss these things :D
    I'm trying to get into UDK for personal projects and so on. Even though the general techniques are the same there is so much stuff I need to get my head around.

    It sounds like it would be a good idea to make a big sprite material with all the goodies added behind switch nodes. Then I make my specific (Pee...) materials when I need them. Gawd I'm loving this feature in UDK. In the in house tools I'm used to all the sprites get a super optimized shader with no special effects on it. If I want something like uv distortion I have to emit quads. If I do that, there will be furious tech artists screaming at me for eating all their batch count. :S

    I misunderstood what the nearfade did it seems. I wasn't thinking much of the visual clipping through the camera. I am used to having so many visual glitches that it didn't even cross my mind. 95% of the time I'm not even allowed to use soft particles since most of what I've done recently has had the extra limitation that it has to run on the Wii as well.
    I expected it to cull the particles, but of course, that would be done with lodstates.

    The stream was made with flight aligned sprites. It is only ever seen in first person mode. I was hoping there was some extra attribute you could use, like in matinee there is a material variable track or something. I guess, as long as they don't ask for different shades I'm fine :D

    They never seem to mention these kind of effects in the interviews do they? :D I had to do some vomiting for a cutscene once, only to have the camera changed a week before gold so it didn't show up in the end anyway.

    Thanks for replying!

  5. Yes more people need to discuss this stuff. The best results come from groups hashing this stuff out.

    Wii dev. Oh my. I talked with a company that was
    workign on a WII project and they had their trickery together! I was super impressed on how they used old school ideas with new(ish) tech. Loads of smoke and mirrors there.

    Heh if they ask for different shades, then you could change the emitter in world with a color parm... although you'd then have to hard attach it to the (erm) member and change it out when and if they needed a color swap...

    No they really don't talk about this in interviews heh! Unless its a feature hah!

    Have a good one!


  6. Awesome post Fred! We really should consider putting together an FX specific blog that the two of us update from time to time...

    Static Switches and Material Instance Constants (MICs):

    You can easily spin off a MIC and enable/disable sections of your material to add/remove features you want for a particular effect. So in the case of your flare smoke, you could have two emitters with 2 materials using the same textures, i.e. particles on the ground your camera will never clip through, but you may look @ against the ground, and particles in the air at camera height. With two separate MICs, one with camera Fade, one without camera Fade you have the same texture cost, but save on instruction count. MICs are cheap and an easy way to have the same look, but control different material attributes.

    For the distortion, you could also try hooking up a dynamic parameter with a Linear Interpolate (plug the dynamic param into the alpha) and then control the amount of distortion over time with a constant curve (DynParam Module) in Cascade. Dynamic params can be tricky, start out trying to Lerp between a red/blue channel (0-1) to get the hang of setting one up.


    If you hook up a dynamic parameter and change the names in the material node, rClick the module in Cascade and update the module to see the names change. You could use a Lerp and a dynamic parameter to completely alter which shading network you are using @ any given point in a particle's lifetime.

    Most of all, your team gets 33.3 ms, fight for your fair share when it is necessary. Alpha artifacts can be nasty and wreck your hard work. While depth bias is not the cheapest, it is fairly standard on current gen console hardware (excluding the Wii.) Make sure you know all you can about profiling your target and what the budgets are. Do you really need to eliminate 10 instructions from your material to save ms if another asset is heavy? The more you know about optimization and scene management, the more you can fight for your share of GPU time.

  7. Hey Tims!
    I like this idea of setting up a group blog and posting fx to it. I think we should call it hoozits and whatsits! I'll shoot you a mail and we'll do it!