实例化Shader为Material时,可以通过为属性赋值达到创建具体对象的目的,可以在Properties块中定义自己所需要的属性
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}//图片形式的属性
_MyColor("Color of Object",Color) = (1,1,1,1)//颜色属性
_MyCube("Enironment map",Cube) = "white"{}//3D贴图,需要6张图片
_MyVector("Vector",vector) = (1,1,1,1)//4个元素的向量
_MyFloat("Float value",float)=1.0//浮点小数
_MyRange("Another type of float",range(-13,14))=1.0//限定范围的浮点数
}
通过图形界面可以操作这些属性
也可以通过代码来设置和读取Shader中的属性,下面是设置的代码
public Material mat;
public Texture myPic;
public Color purple;
public Cubemap cube;
public Vector4 vec;
public float val_1;
public float val_2;
// Update is called once per frame
void Update () {
mat.SetTexture("_MyTexture", myPic);
mat.SetColor("_MyColor", purple);
mat.SetTexture("_MyCube", cube);
mat.SetVector("_MyVector", vec);
mat.SetFloat("_MyFloat", val_1);
mat.SetFloat("_MyRange", val_2);
}
另外,矩阵不能再Properties中定义,我们必须首先在Shader中声明,然后通过脚本来进行读取和写入。
uniformfloat4x4 myMatrix;
mat.SetMatrix("myMatrix",matrix);
matrix =mat.GetMatrix("myMatrix");
Cg代码中使用属性,除了Unity的固定管线因为有特定语法之外,在Properties块中定义的属性必须在代码中再声明一次才能使用。
sampler2D _MainTex;
float4 _MyColor;
samplerCUBE _MyCube;
float4 _MyVector;
float _MyFloat;
float _MyRange;