Accessing shader properties in Cg/HLSL //访问cg/hlsl中的着色属性

Accessing shader properties in Cg/HLSL 访问cg/hlsl中的着色属性

Shader declares Material properties in a Properties block. If you want to access some of those properties in a shader program, you need to declare a Cg/HLSL variable with the same name and a matching type. An example is provided in Shader Tutorial: Vertex and Fragment Programs.

Shader在一个属性块中声明了物质属性。如果您想在一个着色程序中访问这些属性,您需要声明一个具有相同名称和匹配类型的cg/hlsl变量。在着色器教程中提供了一个例子:顶点和片段程序。

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/HLSL code as:

将在cg/hlsl代码中被声明为:

fixed4 _MyColor; // low precision type is usually enough for colors
float4 _MyVector;
float _MyFloat; 
sampler2D _MyTexture;
samplerCUBE _MyCubemap;

Cg/HLSL can also accept uniform keyword, but it is not necessary:

cg/hlsl也可以接受统一的关键字,但没有必要:

uniform float4 _MyColor;

Property types in ShaderLab map to Cg/HLSL variable types this way:

ShaderLab的属性类型映射到cg/hlsl变量类型:

  • Color and Vector properties map to float4half4 or fixed4 variables.
  • Range and Float properties map to floathalf or fixed variables.
  • Texture properties map to sampler2D variables for regular (2D) textures; Cubemaps map to samplerCUBE; and 3D textures map to sampler3D.

How property values are provided to shaders 如何将属性值提供给着色器

Shader property values are found and provided to shaders from these places:

着色的属性值被发现并提供给这些地方的着色者:

  • Per-Renderer values set in MaterialPropertyBlock. This is typically “per-instance” data (e.g. customized tint color for a lot of objects that all share the same material).
  • 在材料属性块中设置的每个呈现器值。这是典型的“per-instance”数据(例如,为许多共享相同内容的对象定制的颜色)。
  • Values set in the Material that’s used on the rendered object.
  • 在呈现对象上使用的材料中设置的值。
  • Global shader properties, set either by Unity rendering code itself (see built-in shader variables), or from your own scripts (e.g. Shader.SetGlobalTexture).
  • 全局着色属性,由Unity渲染代码本身(参见内建的着色器变量),或者来自您自己的脚本(例如:着色。setglobal纹理)。

The order of precedence is like above: per-instance data overrides everything; then Material data is used; and finally if shader property does not exist in these two places then global property value is used. Finally, if there’s no shader property value defined anywhere, then “default” (zero for floats, black for colors, empty white texture for textures) value will be provided.

优先顺序就像上面一样:每个实例的数据覆盖所有的东西,然后使用材质数据;最后,如果在这两个地方不存在着色的属性,那么使用全局属性值。最后,如果在任何地方都没有定义着色属性值,那么将提供“缺省值”(用于浮点数的零值,颜色的黑色,纹理的空白纹理)。

Serialized and Runtime Material properties 序列化和运行时的材料属性

Materials can contain both serialized and runtime-set property values.

材料可以包含序列化和运行时设置的属性值。

Serialized data is all the properties defined in shader’s Properties block. Typically these are values that need to be stored in the material, and are tweakable by the user in Material Inspector.

序列化数据是在着色器的属性块中定义的所有属性。通常,这些值需要存储在材料中,并且可以由用户在材料检查器中进行操作。

A material can also have some properties that are used by the shader, but not declared in shader’s Properties block. Typically this is for properties that are set from script code at runtime, e.g. via Material.SetColor. Note that matrices and arrays can only exist as non-serialized runtime properties (since there’s no way to define them in Properties block).

一个材质也可以有一些被着色器使用的属性,但是没有在着色器的属性块中声明。通常,这是在运行时从脚本代码中设置的属性,例如通过物料。setcolor。注意,矩阵和数组只能作为非序列化的运行时属性存在(因为在属性块中没有定义它们的方法)。

Special Texture properties 特殊材质属性

For each texture that is setup as a shader/material property, Unity also sets up some extra information in additional vector properties.

对于每一个被设置为着色/材质属性的纹理,Unity也会在额外的矢量属性中设置一些额外信息。

Texture tiling & offset

Materials often have Tiling and Offset fields for their texture properties. This information is passed into shaders in a float4 {TextureName}_ST property:

材料通常有平铺和偏移的纹理属性。这个信息被传递到一个浮动4的TextureName ST属性中:

  • x contains X tiling value
  • y contains Y tiling value
  • z contains X offset value
  • w contains Y offset value

For example, if a shader contains texture named _MainTex, the tiling information will be in a _MainTex_ST vector.

例如,如果一个着色器包含了名为MainTex的纹理,那么tiling信息将会在一个MainTex ST向量中。

Texture size

{TextureName}_TexelSize - a float4 property contains texture size information:

TextureName TexelSize-浮动4属性包含纹理大小信息:

  • x contains 1.0/width
  • y contains 1.0/height
  • z contains width
  • w contains height
Texture HDR parameters 纹理HDR参数

{TextureName}_HDR - a float4 property with information on how to decode a potentially HDR (e.g. RGBM-encoded) texture depending on the color space used. SeeDecodeHDR function in UnityCG.cginc shader include file.

TextureName HDR-一个浮动属性,它有关于如何解码一个潜在HDR(如rgbm编码)纹理的信息,取决于所使用的颜色空间。在UnityCG SeeDecodeHDR函数。cginc着色器包含文件。

Color spaces and color/vector shader data 颜色空间和颜色/矢量着色数据

When using Linear color space, all material color properties are supplied as sRGB colors, but are converted into linear values when passed into shaders.

当使用线性颜色空间时,所有的材料颜色属性都是作为sRGB的颜色提供的,但是当被传送到着色器时,会被转换成线性值。

For example, if your Properties shader block contains a Color property called “MyColor“, then the corresponding ”MyColor” HLSL variable will get the linear color value.

例如,如果您的属性着色块包含一个名为“MyColor”的颜色属性,那么相应的“MyColor”HLSL变量将得到线性颜色值。

For properties that are marked as Float or Vector type, no color space conversions are done by default; it is assumed that they contain non-color data. It is possible to add [Gamma] attribute for float/vector properties to indicate that they are specified in sRGB space, just like colors (see Properties).

对于被标记为浮动或向量类型的属性,缺省情况下不会进行颜色空间转换;假定它们包含非彩色数据。可以为浮动/向量属性添加Gamma属性,以表明它们是在sRGB空间中指定的,就像颜色一样(参见属性)。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值