Shader学习过程9——Cg入门

渲染管线 可以定制顶点和片段程序,目的是对颜色进行计算

顶点和片段程序的输入输出

常用语义: POSITION COLOR TEXCOORD

向量:

    swizzle操作 

 xyzw  rgba   float4(xy,0,1)  可以随便颠倒顺序


    宏定义:

# define MACROFL  float4(0,1,0,1);

    宏使用:

float4 a = MACROFL;


        矩阵

float2x2 m2x2 = {1,0,1,1};

float2x4 m2x4 = {1,0,1,1 ,0,1,0,1}; = {1,0,1,1} , {0,1,0,1}; 

float4 col = m2x4[0]

        数组

 float arr[4] = (1, 0.5, 0.5, 1);

floot4 col=float4 ( arr[0],arr[1],arr[2],arr[3] );

结构体申明

struct v2f 

{

    float4 pos;

    float2 uv;

}

    结构体使用

v2f o;

o.pos =float4 (1,1,1,1);

o.uv = float2(0,1);

Shader "Unlit/NewUnlitShader"
{
	
	SubShader
	{
		
		Pass
		{
			CGPROGRAM
// Upgrade NOTE: excluded shader from DX11; has structs without semantics (struct v2f members pos,uv)
#pragma exclude_renderers d3d11
// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it uses non-square matrices
#pragma exclude_renderers gles
			#pragma vertex vert
			#pragma fragment frag
			//类型别名
			typedef float4 FL4;
			//宏定义
			#define MACROFL float4(fl4.rgba);
			
			//结构体
			struct v2f{
				float4 pos;
				float2 uv;

			}




			void vert (in float2 objPos:POSITION,out float4 pos:POSITION,out float4 col:COLOR)
			{
				pos = float4(objPos,0,1);
				col = pos;
			}
			
			void frag (inout float4 col:COLOR)
			{
				//col = float4(1,0,0,1);
				fixed r = 1;
				fixed g = 0;
				fixed b = 0;
				fixed a = 1;
				col = fixed4(r,g,b,a);


				
				fixed2 x = fixed2(1,0);
				//float ==> float2 ==> float3 ==> float4  32位精度浮点
				//half ==> half2 ==> half3 ==> half4      16位精度
				//fixed ==> fixed2 ==>fixed3 ==> fixed4    8位 颜色的输出用fixed已经足够了
				//bool bl = true;
				//sampler
				//int 在c里面为32位整数 但是在cg中基本是跟float一样的浮点数

				bool bl = true;
				//col = bl? col:fixed4(0,1,0,1);// 如果bl为真,取col的值  如果为假,取fixe4...

				float2 fl2 = float2(1,0);
				float3 fl3 = float3(1,0,1);
				float4 fl4 =float4 (1,1,0,1);
				//swizzle
				float4 fl = float4 (fl2.xy,0,1);
				 fl = float4 (fl2.yx,0,1);
				 fl = float4(fl4);
				 fl = float4(fl3.xyxx);
				 //类型别名的使用
				 fl = FL4 (fl4.rgba);
				 //fl= float4 (fl4.raxy);错误 要么rgba 要么xyzw
				 //宏使用 ,定义的时候有了“;”,所以这里就不要分号了
				 fl = MACROFL
				 col =fl;
				 //矩阵
				 float2x2 M2x2 = {1,0,1,1};
				 float2x4 M2x4 = {1,0,1,1,0,1,0,1}; //也可以用 {1,0,1,1},{0,1,0,1}    也可以用 {fL4,{0,1,0,1}}

				 col = M2x4[0];

				 //数组
				 float arr[4] = {1,0.5,0.5,1};
				 col = float4(arr[0],arr[1],arr[2],arr[3]);

				 //结构体使用
				 v2f o;
				 o.pos = fl4;
				 o.uv = fl2;..................
				 .............

			
			}
			ENDCG
		}
	}
}




猜你喜欢

转载自blog.csdn.net/baicaishisan/article/details/80567351