标题: 编写顶点和片段着色器:在Cg中访问着色器属性 [打印本页] 作者: . 时间: 2013-2-24 09:56 标题: 编写顶点和片段着色器:在Cg中访问着色器属性 Shader declares it's properties in a Properties block. If you want to access some of those properties in a Cg shader program, you need to declare a Cg variable with the same name and a matching type. An example is provided in Shader Tutorial: Vertex and Fragment Programs.
着色器是在属性块声明它的属性。如果你想在Cg 着色器程序中访问这些属性的话,你需要在Cg中声明一个与之相同名字对应类型的变量。在着色器教程:顶点和片段程序中提供了一个例子。
For example these shader properties:
下面是一些着色器属性:
_MyColor ("Some Color", Color) = (1,1,1,1)
_MyVector ("Some Vector", Vector) = (0,0,0,0)
_MyFloat ("My float", Float) = 0.5
_MyTexture ("Texture", 2D) = "white" {}
_MyCubemap ("Cubemap", CUBE) = "" {}
would be declared for access in Cg code as:
在Cg代码中的声明:
float4 _MyColor;
float4 _MyVector;
float _MyFloat;
sampler2D _MyTexture;
samplerCUBE _MyCubemap;
Cg can also accept uniform keyword, but it is not necessary:
Cg也可以接受uniform关键字,但这是不必要的:
uniform float4 _MyColor;
Property types in ShaderLab map to Cg variable types this way:
在着色器语言中的属性类型与Cg变量类型的对应:
Color and Vector properties map to float4 variables.
Color和Vector属性对应 float4 类型。
Range and Float properties map to float variables.
Range和Float属性对应 float 类型。
Texture properties map to sampler2D variables for regular (2D) textures.
Texture属性对应 普通2D纹理的sampler2D 类型。
CUBE and RECT textures map to samplerCUBE and samplerRECT variables respectively.