CG类型

来源:互联网 发布:决胜60秒源码 编辑:程序博客网 时间:2024/06/05 00:17

CG类型

CG支持7种数据类型:

float 32位浮点数half 16位浮点数int   32位整形数fixed 12位定点数bool  布尔数据sampler 纹理对象的句柄 共有:sampler、sampler1D、sampler2D、sampler3D、samplerCUBE、和samplerRECT六种。string 字符串,其实没有必要在CG中用到字符串

此外,CG还支持矩阵数据类型 如:

float2×4 matrix; //表示2×4阶矩阵,包含8个float类型数据

类型使用技巧:

 1.精度够用就好 2.颜色和单位向量,使用fixed 3.其他情况,尽量使用half(即范围在[-6万,+6万]、精确的小数点3.3位);否则才使用用float

final color modifier:

    编译指令为finalcolor:functionName,函数 接收三个参数 Input IN, SurfaceOutput o, inout fixed4 color     final color会影响渲染的最终颜色,它在所有的计算的最后进行影响,比如lightmap、lightprobe等产生的颜色也会受此函数影响。    final color可以用来实现fog,fog只影响渲染的rgb,而不影响alpha,fog的原理是让物体在最终的rgb和fog.color之间根据距离进行过度。

例子:

Shader "MollyWendy/test1" {  Properties {    _MainTex ("Base (RGB)", 2D) = "white" {}  }  SubShader {    Tags { "RenderType"="Opaque" }    LOD 200    CGPROGRAM    #pragma surface surf Lambert finalcolor:mycolor vertex:myvert    sampler2D _MainTex;    uniform half4 unity_FogColor;    uniform half4 unity_FogStart;    uniform half4 unity_FogEnd;    struct Input {      float2 uv_MainTex;      half fog;    };    // 顶点着色函数    void myvert (inout appdata_full v, out Input data) {      UNITY_INITIALIZE_OUTPUT(Input,data);      float pos = length(mul (UNITY_MATRIX_MV, v.vertex).xyz);      float diff = unity_FogEnd.x - unity_FogStart.x;      float invDiff = 1.0f / diff;      data.fog = clamp ((unity_FogEnd.x - pos) * invDiff, 0.0, 1.0);    }    // final color处理函数    void mycolor (Input IN, SurfaceOutput o, inout fixed4 color) {      fixed3 fogColor = unity_FogColor.rgb;      #ifdef UNITY_PASS_FORWARDADD      fogColor = 0;      #endif      color.rgb = lerp (fogColor, color.rgb, IN.fog);    }    void surf (Input IN, inout SurfaceOutput o) {      half4 c = tex2D (_MainTex, IN.uv_MainTex);      o.Albedo = c.rgb;      o.Alpha = c.a;    }    ENDCG  }   FallBack "Diffuse"}
0 0
原创粉丝点击