So, I've come across three different ways to write shaders in Unity.
First, there's the fixed function pipeline, where you use only ShaderLab code like
next, there is the custom surface and lighting function using Cg, looking like this
and finally, you can write your very own vertex and fragment programs with
If I understand the documentation right, you can combine them in different ways but also use them (more or less) exclusively to achieve a certain effect.
My question now is: If I'm only interested in simple shaders that can possibly be achieved by either of the above methods (simple light models or unlit, various texture combines, and so on), which one should I choose?
Most of the time, I've found myself using the surface and lighting function method, because I've had previous experience with Cg. But I was wondering if there is maybe a performance gain with the fixed function pipeline or such.
asked Jan 28 '11 at 05:16 PM
Sorry to bring back an old question. I just noticed it looking for something else. Benefits of each:
FixedFunction: This is the fastest method. It provides vertex-lighting and simple texture combiners at the cheapest cost. If you are worried about performance, then you should use fixed-function shaders. They are also the easiest to write since Unity handles the transformations and math for you.
Surface Shaders: Surface shaders are really just vertex and fragment programs at a different abstraction level. AFAIK, Unity compiles that into standard CG code. It lets you determine the look of the surface without having to mess with the low-level stuff. A simple example, think how often you would put this into a CG program
Surface shaders handle that stuff including transforming coordinates so that the programmer can focus on the look, not the repetitive chunks of code. It should be compiled down into identical (if not more optimized) code than you would have written by hand.
CG programs: CG programs provide lower level control than surface shaders. With the creation of surface shaders though, they are really only needed for special effects. It is usually a hassle to create lighting and shadowing when Unity could do that for you with the above technique. You only want to use vertex and fragment programs if you are creating a special effect, possibly some sort of GUI.
answered Mar 20 '11 at 02:05 PM