Cg Tutorial to Unity

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Shader C3E3 texture sampling)
m (Text replace - "<shaderlab>" to "<syntaxhighlight lang="shaderlab">")
Line 15: Line 15:
  
 
Example 2-1
 
Example 2-1
<shaderlab>
+
<syntaxhighlight lang="shaderlab">
 
struct C2E1v_Output  
 
struct C2E1v_Output  
 
{
 
{
Line 31: Line 31:
 
</shaderlab>
 
</shaderlab>
 
Example 2-3
 
Example 2-3
<shaderlab>
+
<syntaxhighlight lang="shaderlab">
 
struct C2E2f_Output  
 
struct C2E2f_Output  
 
{
 
{
Line 45: Line 45:
 
</shaderlab>
 
</shaderlab>
 
The above two pieces combined to obtain a unity shader C2E1:
 
The above two pieces combined to obtain a unity shader C2E1:
<shaderlab>
+
<syntaxhighlight lang="shaderlab">
 
//Shaders start with the Shader keyword followed by shader's name in ""
 
//Shaders start with the Shader keyword followed by shader's name in ""
 
//Custom/C2E1 means that the shader name is C2E1 and is placed in a shadr group called Custom
 
//Custom/C2E1 means that the shader name is C2E1 and is placed in a shadr group called Custom
Line 111: Line 111:
 
Vertex shader takes data from Unity, processes them and passes them down to the graphics pipeline. ( Simplified view )<br>
 
Vertex shader takes data from Unity, processes them and passes them down to the graphics pipeline. ( Simplified view )<br>
 
What data get in ? Vertex data. Each vertex in the mesh has attributes. Those attributes are :<br>
 
What data get in ? Vertex data. Each vertex in the mesh has attributes. Those attributes are :<br>
<shaderlab>
+
<syntaxhighlight lang="shaderlab">
 
//From UnityCG.cginc
 
//From UnityCG.cginc
 
struct appdata_full {
 
struct appdata_full {
Line 127: Line 127:
 
It runs once for each vertex and it fills a struct with data to be passed down to the graphics pipeline.
 
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 :
 
In this example, the "main function" is :
<shaderlab>
+
<syntaxhighlight lang="shaderlab">
 
// function name : C2E1v_green,  
 
// function name : C2E1v_green,  
 
//  takes a float2 named position with a POSITION semantic
 
//  takes a float2 named position with a POSITION semantic
Line 147: Line 147:
 
</shaderlab>
 
</shaderlab>
 
To send this data down the graphics pipeline you create a struct that describes what gets out.
 
To send this data down the graphics pipeline you create a struct that describes what gets out.
<shaderlab>
+
<syntaxhighlight lang="shaderlab">
 
//Declare a struct called C2E1v_Output
 
//Declare a struct called C2E1v_Output
 
struct C2E1v_Output  
 
struct C2E1v_Output  
Line 164: Line 164:
 
The fragment shader gets data from the vertex shader, processes them and sends them to the frame buffer.<br>
 
The fragment shader gets data from the vertex shader, processes them and sends them to the frame buffer.<br>
 
This fragment shader " main function " is described below :
 
This fragment shader " main function " is described below :
<shaderlab>
+
<syntaxhighlight lang="shaderlab">
 
// function name : C2E2f_passthrough,  
 
// function name : C2E2f_passthrough,  
 
//  takes a float4 named color with a COLOR semantic
 
//  takes a float4 named color with a COLOR semantic
Line 180: Line 180:
 
</shaderlab>
 
</shaderlab>
 
The data is packed in a struct
 
The data is packed in a struct
<shaderlab>
+
<syntaxhighlight lang="shaderlab">
 
//Declare a struct called C2E2f_Output
 
//Declare a struct called C2E2f_Output
 
struct C2E2f_Output
 
struct C2E2f_Output
Line 194: Line 194:
 
along with some minor tweaks. This approach is nearer to the style that many people in the forums<br>
 
along with some minor tweaks. This approach is nearer to the style that many people in the forums<br>
 
choose to code.  
 
choose to code.  
<shaderlab>
+
<syntaxhighlight lang="shaderlab">
 
//Shaders start with the Shader keyword followed by shader's name in ""
 
//Shaders start with the Shader keyword followed by shader's name in ""
 
//Custom/C2E1tweaked means that the shader name is C2E1tweaked and is placed in a shadr group called Custom
 
//Custom/C2E1tweaked means that the shader name is C2E1tweaked and is placed in a shadr group called Custom
Line 290: Line 290:
  
 
==Shader C3E1 uniform parameter==
 
==Shader C3E1 uniform parameter==
<shaderlab>
+
<syntaxhighlight lang="shaderlab">
 
//Shaders start with the Shader keyword followed by shader's name in ""
 
//Shaders start with the Shader keyword followed by shader's name in ""
 
//Custom/C2E1 means that the shader name is C2E1 and is placed in a shadr group called Custom
 
//Custom/C2E1 means that the shader name is C2E1 and is placed in a shadr group called Custom
Line 355: Line 355:
 
==Shader C3E3 texture sampling==
 
==Shader C3E3 texture sampling==
 
<br>
 
<br>
<shaderlab>
+
<syntaxhighlight lang="shaderlab">
 
//Shaders start with the Shader keyword followed by shader's name in ""
 
//Shaders start with the Shader keyword followed by shader's name in ""
 
//Custom/C2E1 means that the shader name is C2E1 and is placed in a shadr group called Custom
 
//Custom/C2E1 means that the shader name is C2E1 and is placed in a shadr group called Custom
Line 423: Line 423:
 
</shaderlab>
 
</shaderlab>
 
<br>
 
<br>
<shaderlab>
+
<syntaxhighlight lang="shaderlab">
 
//Shaders start with the Shader keyword followed by shader's name in ""
 
//Shaders start with the Shader keyword followed by shader's name in ""
 
//Custom/C2E1 means that the shader name is C2E1 and is placed in a shadr group called Custom
 
//Custom/C2E1 means that the shader name is C2E1 and is placed in a shadr group called Custom
Line 487: Line 487:
 
</shaderlab>
 
</shaderlab>
 
<br>
 
<br>
<shaderlab>
+
<syntaxhighlight lang="shaderlab">
 
//Shaders start with the Shader keyword followed by shader's name in ""
 
//Shaders start with the Shader keyword followed by shader's name in ""
 
//Custom/C2E1 means that the shader name is C2E1 and is placed in a shadr group called Custom
 
//Custom/C2E1 means that the shader name is C2E1 and is placed in a shadr group called Custom

Revision as of 21:55, 17 January 2012

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.
If you find this code useful, please consider a donation to my site : http://ippomed.com
Thanks !

Chapter 2

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

Example 2-1

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


struct C2E1v_Output 
{
  float4 position : POSITION;
  float4 color    : COLOR;
};

C2E1v_Output C2E1v_green(float2 position : POSITION)
{
  C2E1v_Output OUT;
  OUT.position = float4(position, 0, 1);
  OUT.color    = float4(0, 1, 0, 1);  // RGBA green
  return OUT;
}
</shaderlab>
Example 2-3
<syntaxhighlight lang="shaderlab">
struct C2E2f_Output 
{
  float4 color : COLOR;
};

C2E2f_Output C2E2f_passthrough(float4 color : COLOR)
{
  C2E2f_Output OUT;
  OUT.color = color;
  return OUT;
}
</shaderlab>
The above two pieces combined to obtain a unity shader C2E1:
<syntaxhighlight lang="shaderlab">
//Shaders start with the Shader keyword followed by shader's name in ""
//Custom/C2E1 means that the shader name is C2E1 and is placed in a shadr group called Custom
//Refer here : http://unity3d.com/support/documentation/Components/SL-Shader.html
Shader "Custom/C2E1" 
{
	//Refer here : http://unity3d.com/support/documentation/Components/SL-SubShader.html
	SubShader 
	{
		//Refer here : http://unity3d.com/support/documentation/Components/SL-Pass.html
		Pass 
		{
		//Refer here : http://unity3d.com/support/documentation/Components/SL-ShaderPrograms.html
		CGPROGRAM
		// The vertex shader name should match the vertex shader function name
		#pragma vertex C2E1v_green
		// The fragment shader name should match the fragment shader function name
		#pragma fragment C2E2f_passthrough
		
				
		struct C2E1v_Output 
		{
			float4 position : POSITION;
			float4 color : COLOR;
		};
				
		C2E1v_Output C2E1v_green(float2 position : POSITION)
		{
		  C2E1v_Output OUT;
		  OUT.position = float4(position,0,1);
		  OUT.color    = float4(0, 1, 0, 1);  // RGBA green
		  return OUT;
		}		
		
		struct C2E2f_Output
		{
			float4 color : COLOR;
		};
		C2E2f_Output C2E2f_passthrough(float4 color : COLOR)
		{
			C2E2f_Output OUT;
			OUT.color = color;
			return OUT;
		}

		ENDCG
		}
	}
}
</shaderlab>
To see the above shader in action do the following :
*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.
The expected result is to see a green rectangle.<br><br>
[[File:C2E1.png|caption]]

==Code analysis==
The code provided in chapter two comprises two shaders:
*a vertex shader called C2E1v_green 
*a fragment shader called C2E2f_passthrough
To construct from them a shader in unity, we will first analyze them.<br>
Vertex shader takes data from Unity, processes them and passes them down to the graphics pipeline. ( Simplified view )<br>
What data get in ? Vertex data. Each vertex in the mesh has attributes. Those attributes are :<br>
<syntaxhighlight lang="shaderlab">
//From UnityCG.cginc
struct appdata_full {
    float4 vertex : POSITION;// The position of the vertex in object space
    float4 tangent : TANGENT;// The tangent of the vertex
    float3 normal : NORMAL;// The normal of the vertex
    float4 texcoord : TEXCOORD0;// base texture uv coordinates of the vertex
    float4 texcoord1 : TEXCOORD1;// second texture uv2 coordinates of the vertex
    fixed4 color : COLOR; // vertex color
    //As long as you call one of the semantics that unity supports, POSITION, TANGENT, NORMAL, TEXCOORD0, TEXCOORD1, COLOR it's ok.
};
</shaderlab>
We can feed with some of this data the vertex shader.<br>
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 :
<syntaxhighlight lang="shaderlab">
// function name : C2E1v_green, 
//   takes a float2 named position with a POSITION semantic
//   and outputs a C2E1v_Output ( see below ).
C2E1v_Output C2E1v_green(float2 position : POSITION)
{
  // Create a new struct of type C2E1v_Output named OUT
  C2E1v_Output OUT;
  // Fill the position member of the OUT struct 
  //   with a float4, first two floats are from Unity app ( the "float2 position : POSITION" )
  //   other two are 0, 1  
  OUT.position = float4(position,0,1);
  // Fill the color member of the struct 
  //   with a float4, whose value corresponds to green
  OUT.color    = float4(0, 1, 0, 1);  // RGBA green
  // // return the OUT with new data.
  return OUT;
        } 
</shaderlab>
To send this data down the graphics pipeline you create a struct that describes what gets out.
<syntaxhighlight lang="shaderlab">
//Declare a struct called C2E1v_Output
struct C2E1v_Output 
{
  //This struct has two members.
  //First member is a float4 ( type ), called position, that has the POSITION semantic
  //Refer to http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter02.html for an explanation on 
  //  what is the meaning of identifier, semantic, type is.
  float4 position : POSITION;
  float4 color    : COLOR;
  //As long as you call one of the semantics that unity supports, POSITION, TANGENT, NORMAL, TEXCOORD0, TEXCOORD1, COLOR it's ok.
  //Remember to place the colon ";"
};
</shaderlab>
So, for each vertex, we retrieve its position and assign a color.<br>
The fragment shader gets data from the vertex shader, processes them and sends them to the frame buffer.<br>
This fragment shader " main function " is described below :
<syntaxhighlight lang="shaderlab">
// function name : C2E2f_passthrough, 
//   takes a float4 named color with a COLOR semantic
//   and outputs a C2E2f_Output ( see below ).
C2E2f_Output C2E2f_passthrough(float4 color : COLOR)
{
  // Create a new struct of type C2E2f_Output named OUT
  C2E2f_Output OUT;
  // Fill the color member of the struct 
  //   with the float4 color from function input
  OUT.color = color;
  // return the OUT with new data.
  return OUT;
}
</shaderlab>
The data is packed in a struct
<syntaxhighlight lang="shaderlab">
//Declare a struct called C2E2f_Output
struct C2E2f_Output
{
  //First member is a float4 ( type ), called color, that has the COLOR semantic
  float4 color : COLOR;
};
</shaderlab>
And it is send down to the frame buffer.
==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,<br> 
along with some minor tweaks. This approach is nearer to the style that many people in the forums<br>
choose to code. 
<syntaxhighlight lang="shaderlab">
//Shaders start with the Shader keyword followed by shader's name in ""
//Custom/C2E1tweaked means that the shader name is C2E1tweaked and is placed in a shadr group called Custom
//Refer here : http://unity3d.com/support/documentation/Components/SL-Shader.html
Shader "Custom/C2E1tweaked" 
{
	//Refer here : http://unity3d.com/support/documentation/Components/SL-SubShader.html
	SubShader 
	{
		//Refer here : http://unity3d.com/support/documentation/Components/SL-Pass.html
		Pass 
		{
		//Refer here : http://unity3d.com/support/documentation/Components/SL-ShaderPrograms.html
		CGPROGRAM
		// The vertex shader name should match the vertex shader function name
		//    Makes sense to name it vert
		#pragma vertex vert
		// The fragment shader name should match the fragment shader function name
		//    Makes sense to name it frag
		#pragma fragment frag
		
		// Declare a struct that gets data from application (Unity) to the vertex shader
		//    called a2v
		//    Refer : http://unity3d.com/support/documentation/Components/SL-VertexProgramInputs.html 
		struct a2v
		{
			//a little catch : the identifier for the POSITION semantic must be vertex only
			//  for NORMAL normal only etc. 
			//  Refer : http://unity3d.com/support/documentation/Components/SL-VertexProgramInputs.html 
			//  ( second paragraph, after the first example )   
			float4 vertex:POSITION;
		};
		// We call the struct that takes data from vertex to fragment v2f		
		struct v2f 
		{
			float4 position : POSITION;
			float4 color : COLOR;
		};
		// The vert function takes a a2v struct called In and returns a v2f struct called Out		 
		v2f  vert(a2v In)
		{
		  v2f Out;
		  Out.position = In.vertex;
		  Out.color    = float4(0, 1, 0, 1);  // RGBA green
		  return Out;
		}		
		// It is not necessary to declare a struct for the fragment output
		// We specify instead that the frag function takes a v2f struct that we name In
		//   ( contains the data from the vertex shader v2f Out )
		//   and returns a float4 with a COLOR semantic 
		float4 frag( v2f In ): COLOR
		{
			return In.color;
		}

		ENDCG
		}
	}
}
</shaderlab>
=Chapter 3=
In chapter 3, there are 7 new fragment and vertex shaders.<br>
Remember, the f ( e.g. C2E2f_passthru ) means they are fragment shaders,<br>
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 :
{|style="color:black; background-color:#ffffff;" cellpadding="20" cellspacing="10"
|
*C3E1 : uniform parameter
**C3E1v_anycolor
**C2E2f_passthru
||
*C3E2 : varying parameter
**C3E2v_varying
**C2E2f_passthru
||
*C3E3 : texture sampling
**C3E2v_varying
**C3E3f_texture
|-
||
*C3E4 : vertex twisting
**C3E4v_twist
**C2E2f_passthru
||
*C3E5 : two texture accesses
**C3E5v_twoTextures
**C3E6f_twoTextures
||
*C3E6 : two texture double vision
**C3E5v_twoTextures
**C3E7f_twoTextures
|}
<br>

==Shader C3E1 uniform parameter==
<syntaxhighlight lang="shaderlab">
//Shaders start with the Shader keyword followed by shader's name in ""
//Custom/C2E1 means that the shader name is C2E1 and is placed in a shadr group called Custom
//Reference : http://unity3d.com/support/documentation/Components/SL-Shader.html
Shader "Custom/C3E1" 
{
	Properties //Reference : http://unity3d.com/support/documentation/Components/SL-Properties.html
	{
		//There is some conflict when naming this to constantColor
		// so we choose the _constantColor name instead
		_constantColor ("Refraction color", Color)  = (.34, .85, .92, 1) // color
    }
	//Reference  : http://unity3d.com/support/documentation/Components/SL-SubShader.html
	SubShader 
	{
		//Reference  : http://unity3d.com/support/documentation/Components/SL-Pass.html
		Pass 
		{
		//Reference  : http://unity3d.com/support/documentation/Components/SL-ShaderPrograms.html
		CGPROGRAM
		// The vertex shader name should match the vertex shader function name
		#pragma vertex C3E1v_anyColor
		// The fragment shader name should match the fragment shader function name
		#pragma fragment C2E2f_passthrough
		
				
		struct C3E1v_Output 
		{
			float4 position : POSITION;
			float4 color    : COLOR;
		};
				
		C3E1v_Output C3E1v_anyColor(float2 position : POSITION,		
									uniform float4 _constantColor)
		{
			C3E1v_Output OUT;
			OUT.position = float4(position, 0, 1);
			OUT.color = _constantColor;  // Some RGBA color
			return OUT;
		}		
		
		struct C2E2f_Output
		{
			float4 color : COLOR;
		};
		
		C2E2f_Output C2E2f_passthrough(float4 color : COLOR)
		{
			C2E2f_Output OUT;
			OUT.color = color;
			return OUT;
		}

		ENDCG
		}
	}
}
</shaderlab>
<br>
==Shader C3E2 varying parameter==

<br>

==Shader C3E3 texture sampling==
<br>
<syntaxhighlight lang="shaderlab">
//Shaders start with the Shader keyword followed by shader's name in ""
//Custom/C2E1 means that the shader name is C2E1 and is placed in a shadr group called Custom
//Reference : http://unity3d.com/support/documentation/Components/SL-Shader.html
Shader "Custom/C3E3" 
{
	Properties //
	{
		decal ("Base (RGB)", 2D) = "white" {}
    }
	//Reference  : http://unity3d.com/support/documentation/Components/SL-SubShader.html
	SubShader 
	{
		//Reference  : http://unity3d.com/support/documentation/Components/SL-Pass.html
		Pass 
		{
		//Reference  : http://unity3d.com/support/documentation/Components/SL-ShaderPrograms.html
		CGPROGRAM
		// The vertex shader name should match the vertex shader function name
		#pragma vertex C3E2v_varying
		// The fragment shader name should match the fragment shader function name
		#pragma fragment C3E3f_texture
		
				

		struct v2f 
		{
			float4 position : SV_POSITION;
			float4 color    : COLOR;
			float4 texCoord  : TEXCOORD0;
		};
		
		struct a2v
		{
			float4 vertex   : POSITION;
			float4 color    : COLOR;
			float4 texcoord : TEXCOORD0;
		};		

		v2f  C3E2v_varying(a2v In)
		{
			v2f  OUT;
			OUT.position = float4(In.vertex.xy, 0, 1);
			OUT.color    = In.color;
			//o.uv_MainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
			OUT.texCoord  = In.texcoord ;
			return OUT;
		}
		
		struct C3E3f_Output 
		{
			float4 color : COLOR;
		};
				
		C3E3f_Output C3E3f_texture(float4 texcoord : TEXCOORD0,
									sampler2D decal)
		{
			C3E3f_Output OUT;
			OUT.color = tex2D(decal, texcoord.xy);
			return OUT;
		}

		ENDCG
		}
	}
}
</shaderlab>
<br>
<syntaxhighlight lang="shaderlab">
//Shaders start with the Shader keyword followed by shader's name in ""
//Custom/C2E1 means that the shader name is C2E1 and is placed in a shadr group called Custom
//Reference : http://unity3d.com/support/documentation/Components/SL-Shader.html
Shader "Custom/C3E3tweaked" 
{
	Properties //
	{
		decal ("Base (RGB)", 2D) = "white" {}
    }
	//Reference  : http://unity3d.com/support/documentation/Components/SL-SubShader.html
	SubShader 
	{
		//Reference  : http://unity3d.com/support/documentation/Components/SL-Pass.html
		Pass 
		{
		//Reference  : http://unity3d.com/support/documentation/Components/SL-ShaderPrograms.html
		CGPROGRAM
		// The vertex shader name should match the vertex shader function name
		#pragma vertex vert
		// The fragment shader name should match the fragment shader function name
		#pragma fragment frag
		
				

		struct v2f 
		{
			float4 position : SV_POSITION;
			float4 texcoord  : TEXCOORD0;
		};
		
		struct a2v
		{
			float4 vertex   : POSITION;
			float4 texcoord : TEXCOORD0;
		};		

		v2f  vert(a2v In)
		{
			v2f  OUT;
			OUT.position = float4(In.vertex.xy, 0, 1);
			OUT.texcoord  = In.texcoord ;
			return OUT;
		}
		
		struct C3E3f_Output 
		{
			float4 color : COLOR;
		};
				
		
		sampler2D decal;
		float4 frag( v2f In ): COLOR
		{
			float4 color = tex2D(decal, In.texcoord.xy);
			return color;
		}

		ENDCG
		}
	}
}
</shaderlab>
<br>
<syntaxhighlight lang="shaderlab">
//Shaders start with the Shader keyword followed by shader's name in ""
//Custom/C2E1 means that the shader name is C2E1 and is placed in a shadr group called Custom
//Reference : http://unity3d.com/support/documentation/Components/SL-Shader.html
Shader "Custom/C3E3tweaked2" 
{
	Properties //
	{
		_MainTex ("Base (RGB)", 2D) = "white" {}
    }
	//Reference  : http://unity3d.com/support/documentation/Components/SL-SubShader.html
	SubShader 
	{
		//Reference  : http://unity3d.com/support/documentation/Components/SL-Pass.html
		Pass 
		{
		//Reference  : http://unity3d.com/support/documentation/Components/SL-ShaderPrograms.html
		CGPROGRAM
		// The vertex shader name should match the vertex shader function name
		#pragma vertex vert
		// The fragment shader name should match the fragment shader function name
		#pragma fragment frag
		#include "UnityCG.cginc"
				

		struct v2f 
		{
			float4 position : SV_POSITION;
			float2 texcoord  : TEXCOORD0;
		};
		
		struct a2v
		{
			float4 vertex   : POSITION;
			float4 texcoord : TEXCOORD0;
		};		
		//Necessary for line 48 to work
		float4 _MainTex_ST;
		v2f  vert(a2v In)
		{
			v2f  OUT;
			OUT.position = float4(In.vertex.xy, 0, 1);
			
			//From unityCG.cginc : Transforms 2D UV by scale/bias property
			//  #define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)
			//  when user enters TRANSFORM_TEX(texUvCoords, texName) process it as (texUvCoords.xy * texName_ST.xy + texName_ST.zw)
			//  This way, we can use the offset - tiling properties of a material
			//  Reference : http://unity3d.com/support/documentation/Components/class-Material.html
			OUT.texcoord = TRANSFORM_TEX(In.texcoord, _MainTex);
			return OUT;
		}
		
		struct C3E3f_Output 
		{
			float4 color : COLOR;
		};
				
		
		sampler2D _MainTex;
		float4 frag( v2f In ): COLOR
		{
			float4 color = tex2D(_MainTex, In.texcoord.xy);
			return color;
		}

		ENDCG
		}
	}
}

</shaderlab>
<br>

==Shader C3E4 : vertex twisting==
<br>
==Shader C3E5 : two textures accesses==
<br>
==Shader C3E6 : two textures double vision==
<br>
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox