ShaderLab syntax for Shader learning: Properties

This article starts by introducing the syntax of ShaderLab, and the reference materials mainly come from the official manual.

As mentioned before, all Shaders in Unity are written in a declarative language (Declarative programming) called "ShaderLab". The real "shader code" is written in the CGPROGRAMsnippets of the same shader file. CGPROGRAM snippets are written in the general HLSL/Cg language.

Properties

Properties { Property [Property ...] }

  • Format_Name
    ("Display Name",type) = defaultValue{options}
    Attribute name("Display Name",type) = default value{optional parameters}

  • Numbers and Sliders
    Float , Int , Range (min, max)
    all define number properties with default values. The Range format will display a slider from min to max.

    name ("display name", Range (min, max)) = number
    name ("display name", Float) = number

  • Colors and Vectors
    define an RGBA color, or a 4D vector attribute.

    name ("display name", Color) = (number,number,number,number)
    name ("display name", Vector) = (number,number,number,number)

  • Textures
    define a 2D texturecubemap  or  3D (volume)  attribute.

    name ("display name", 2D) = "defaulttexture" {}
    name ("display name", Cube) = "defaulttexture" {}
    name ("display name", 3D) = "defaulttexture" {}

    • 2D: A texture of order size 2. This map will be converted into a color corresponding to each pixel based on the model UV after sampling, and finally displayed.
    • Cube: Cube map texture. Simply put, it is a combination of six related 2D textures. It is mainly used for reflection effects (such as sky boxes and dynamic reflections) and will also be converted into samples of corresponding points.
    • 3D: Unity supports 3D textures, used and created from shaders and scripts. Currently, 3D textures can only be created via scripts.
  • 细节
    每个属性都是根据name引用的(在Unity中一般以下划线_开始),而在编辑器界面显示的是display name,等号后面指定了默认值:

    • 对于Range和Float其只是一个浮点数,color范围0~1
    • Color和Vector是括号内的四个数
    • 贴图(2D、Cube)默认值是一个空字符串,或者是内建的默认贴图:“white”, “black”, “gray” or “bump”.

    之后在shader的fixed function部分,属性值可以通过方括号加属性名获取:[name]。
    Properties块中的属性会被序列化成material数据,这对于在脚本控制值非常有用 (使用 Material.SetFloat 或类似函数).

  • Property attributes and drawers
    对之前的属性,都可以使用方括号指定可选的修饰(attributes),下面是一些Unity可以认出的修饰,或是可以自己指定(MaterialPropertyDrawer classes) 以控制它们在material inspector中如何被渲染。

    • [HideInInspector] 不在inspector显示
  • [NoScaleOffset] 对于texture属性,不会显示贴图的 tiling/offset字段。
  • [Normal] 指示一个贴图是normal-map。
  • [HDR] 指示一个贴图是高动态范围(high-dynamic range (HDR))贴图。
  • [PerRendererData] 指示一个贴图是来自MaterialPropertyBlock格式的 per-renderer data 。

参考:

Unity Manual - ShaderLab: Properties

Guess you like

Origin blog.csdn.net/qq_25189313/article/details/78032213