Accessing shader properties in Cg/HLSL 访问着色器属性 - Unity Shader Reference 系列9

这篇博客介绍了如何在Cg/HLSL中访问Unity着色器属性,包括颜色、向量、纹理等类型的属性映射。着色器属性在Material中设置,可以通过MaterialPropertyBlock、材质或全局属性提供。博客还讨论了特殊纹理属性如平铺和偏移,以及颜色空间转换。此外,讲解了如何为顶点程序提供顶点数据,如顶点位置、法线、纹理坐标等。
摘要由CSDN通过智能技术生成

Accessing shader properties in Cg/HLSL 访问着色器属性

本文档主要是对Unity官方手册的个人理解与总结(其实以翻译记录为主:>)
仅作为个人学习使用,不得作为商业用途,欢迎转载,并请注明出处。
文章中涉及到的操作都是基于Unity2018.2版本
参考链接:https://docs.unity3d.com/Manual/SL-PropertiesInPrograms.html
https://docs.unity3d.com/Manual/SL-VertexProgramInputs.html

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.
着色器在属性块中声明了材质属性。如果你想要访问着色程序中的一些属性,你需要声明一个Cg/HLSL变量,具有相同的名称和匹配的类型。在Shader教程中提供了一个例子:顶点和片段程序。
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关键字,但不需要:

uniform float4 _MyColor;

Property types in ShaderLab map to Cg/HLSL variable types this way:
属性类型在ShaderLab映射到Cg/HLSL变量类型如下:

  • Color and Vector properties map to float4, half4 or fixed4 variables. 颜色和向量属性映射到float4、half4或fixed4变量。
  • Range and Float properties map to float, half or fixed variables. Range和Float属性映射到float、half或fixed变量。
  • Texture properties map to sampler2D variables for regular (2D) textures; Cubemaps map to samplerCUBE; and 3D textures map to sampler3D. Texture属性映射到常规(2D)纹理的sampler2D变量;立方体贴图到samplerCUBE;3D纹理映射到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). 在MaterialPropertyBlock中设置的每个渲染器值。这通常是“每个实例”的数据(例如,为许多共享相同材质的对象定制的颜色,一个shader可对应多个材质)。
  • 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渲染代码本身来设置(参见内置着色器变量),也可以通过自己的脚本来设置(例如Shader.SetGlobalTexture)。

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.
优先级顺序与上面类似:每个实例的数据覆盖所有内容;然后使用材质数据;最后,如果着色器属性不存在于这两个地方,则使用全局属性值。最后,如果在任何地方没有定义着色器属性值,那么将提供“默认”值(浮点值为0,颜色为黑色,纹理为纯白色)。

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

Materials c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值