ShaderLab属性类型和CG变量类型的匹配关系
ShaderLab 属性类型和 CG 变量类型的匹配关系
一、ShaderLab 的属性类型对应 CG 中变量类型的
| Color,Vector |
float4,half4,fixed4 |
| Range,Float,Int |
float,half,fixed |
| 2D |
sampler2D |
| Cube |
samplerCUBE |
| 3D |
sampler2D |
| 2DArray |
sampler2DArray |
二、如何在 CG 语句块中使用 ShaderLab 中声明的属性
直接在 CG 语句块中 声明和属性中对应类型的同名变量即可
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
Shader "Lesson20/NewUnlitShader" { Properties { _MyInt("MyInt",Int) =1 _Myfloat("Myfloat",Float) =1 _MyRang("MyRang",Range(0,5)) =1
_MyColor("MyColor",Color)=(0,0,0,0) _MyVector("MyVector",Vector)=(1,2,0,0)
_My2D("My2d",2D)=""{} _MyCube("MyCube",Cube)=""{}
_My2DArray("My2DArray",2DArray)=""{} _My3D("My3D",3D)=""{}
} SubShader {
Pass { CGPROGRAM #pragma vertex vert; #pragma fragment frag; float4 _MyInt; float _Myfloat; fixed _MyRang;
fixed4 _MyColor; float4 _MyVector; sampler2D _My2D; samplerCUBE _MyCube; struct a2v { float4 vertex:POSITION; float3 nomal:NORMAL; float2 uv:TEXCOORD0; }; struct v2f { float4 position:SV_POSITION; float3 nomal:NORMAL; float2 uv:TEXCOORD0; }; v2f vert(a2v data) { v2f v2fData; v2fData.position = UnityObjectToClipPos(data.vertex); v2fData.nomal = data.nomal; v2fData.uv = data.uv;
return v2fData;
} fixed4 frag(v2f data):SV_TARGET { return _MyColor; }
ENDCG } } }
|