《Unity着色器圣经》3.0.3 | ShaderLab的属性

目录索引

译文

属性与一系列可以在 Unity 检查里修改的变量相对应,一共有八种有用的类型。我们可以在动态地、甚至在运行时通过着色器的属性调整效果。声明属性的语法如下所示:

PropertyName ("display name", type) = defaultValue. 

属性名(”PropertyName”)指属性的名称(例如_MainTex),展示名称(”display name”)对应的是属性们在 Unity 检查器中显示的名称(例如Texture),类型(”type”)代表属性的类型(例如Color、Vector、2D等),最后,默认值(”defaultValue”)指的是该变量的默认值(例如我们可以给一个颜色属性设置白色(1,1,1,1)为默认值)。

图片[1]-《Unity着色器圣经》3.0.3 | ShaderLab的属性-软件开发学习笔记
Fig. 3.0.3a

回到我们先前创建的“USB_simple_color”着色器,我们会发现它的属性语义块里已经声明了一个纹理类型的变量:

Properties 
{ 
    _MainTex ("Texture", 2D) = "white" {} 
}

需要注意的是,当我们声明一个属性时,我们需要避免在代码行的末尾加上分号(;),否则 GPU 将无法读取程序。


原文对照

The properties correspond to a list of parameters that can be manipulated from the Unity inspector. There are eight different properties both in value and usefulness. We use these properties concerning the shader that we want to create or modify, either dynamically or at runtime. The syntax for declaring a property is as follows:

PropertyName ("display name", type) = defaultValue. 

“PropertyName” refers to the name of the property (e.g. _MainTex), “display name” corresponds to the name of the property that will be displayed in the Unity inspector (e.g. Texture), “type” indicates the property type (e.g. Color, Vector, 2D, etc.) and finally, as its name implies, “defaultValue” is the default value assigned to the property (e.g. if the property is a “Color” we can set it as white as follows (1, 1, 1, 1)).

图片[1]-《Unity着色器圣经》3.0.3 | ShaderLab的属性-软件开发学习笔记
Fig. 3.0.3a

If we look at the properties of our USB_simple_color shader we will notice that there is a texture property that has been declared inside of the field, we can corroborate this in the following line of code.

Properties 
{ 
    _MainTex ("Texture", 2D) = "white" {} 
}

One factor to consider is that when we declare a property, it remains “open” within the field of properties, therefore we must avoid the semicolon (;) at the end of the code line, otherwise the GPU will not be able to read the program.

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容