Cg Tutorial to Unity

=Description= Ippokratis 11:07, 30 September 2011 (PDT) This is a port of the source code from Cg tutorial ( http://developer.nvidia.com/node/76 ) to Shaderlab. Users are expected to read the original material and find here some help and explanations on how this code could be ported to Unity. For each pair of vertex and fragment shaders, a unity shaderlab is provided. There are some caveats in porting, hopefully here we will overcome some.

=Chapter 2= In chapter 2 the code is in the examples 2-1 and 2-3.

Example 2-1 Example 2-3 The above two pieces combined to obtain a unity shader C2E1: To see the above shader in action do the following : The expected result is to see a green rectangle.
 * In the Project View
 * Create a new shader, name it C2E1 and copy / paste the above code
 * Create a new material, name it C2E1 and assign to it the above shader
 * In the Hierarchy View
 * Create a cube and drag the C2E1 material on it.

Code analysis
The code provided in chapter two comprises two shaders: To construct from them a shader in unity, we will first analyze them. Vertex shader takes data from Unity, processes them and passes them down to the graphics pipeline. ( Simplified view ) What data get in ? Vertex data. Each vertex in the mesh has attributes. Those attributes are : We can feed with some of this data the vertex shader. The vertex shader has a "main function" that processes this data. It runs once for each vertex and it fills a struct with data to be passed down to the graphics pipeline. In this example, the "main function" is : To send this data down the graphics pipeline you create a struct that describes what gets out. So, for each vertex, we retrieve its position and assign a color. The fragment shader gets data from the vertex shader, processes them and sends them to the frame buffer. This fragment shader " main function " is described below : The data is packed in a struct And it is send down to the frame buffer.
 * a vertex shader called C2E1v_green
 * a fragment shader called C2E2f_passthrough

Code porting and tweaking
We have already integrated the above code in a Unity shader. Now we will see another example that does the same thing but uses a different naming approach, along with some minor tweaks. This approach is nearer to the style that many people in the forums choose to code.

=Chapter 3= In chapter 3, there are 7 new fragment and vertex shaders. Remember, the f ( e.g. C2E2f_passthru ) means they are fragment shaders, while the v ( e.g. C3E1v_anycolor ) means they are vertex shaders. After having a look at the examples in the Cg toolkit ( not the manual, the file ), they are combined in 6 unity shaders in the following way :

Shader C3E4 : vertex twisting
For this example to work properly, we'll either need a cube or a plane with a higher tessellation. To achieve this we'll need the CreatePlane script from Michael Garforth. Create your plane with 64x64 subdivisions and add a material to it. Link the material to the C3E4_Twisting shader: