Sintaxis ShaderLab: Shader
ShaderLab: SubShader

ShaderLab syntax: Propiedades

Los sombreadores pueden definir una lista de parámetros para ser establecidos por artistas en el material inspector de Unity. El bloque de las propiedades en el archivo del sombreador las define. The Properties block in the shader file defines them.

Sintaxis

Propiedades

Properties { Property [Property ...] }

Define el bloque de propiedades. Dentro de llaves múltiples propiedades se definen como sigue.

Numbers and Sliders

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

These all defines a number (scalar) property with a default value. The Range form makes it be displayed as a slider between min and max ranges.

Colors and Vectors

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

Defines a color property with default value of given RGBA components, or a 4D vector property with a default value. Color properties have a color picker shown for them, and are adjusted as needed depending on the color space (see Properties in Shader Programs). Vector properties are displayed as four number fields.

Textures

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

Defines a 2D texture, cubemap or 3D (volume) property respectively.

Detalles

Cada propiedad dentro el sombreador es referenciado por name (en Unity, es comúnmente comenzar los nombres de propiedades del sombreador con rayita abajo “_”). La propiedad va a mostrarse en el inspector del material como display name. Para cada propiedad un valor predeterminado es dado después del signo igual:

  • Para las propiedades Range y Float es simplemente un número.
  • Para las propiedades de Color y Vector son cuatro números en parentesis.
  • Para texture (2D, Rect, Cube) el valor predeterminado es ya sea un string vacío, o una de las texturas predeterminadas integradas: “white”, “black”, “gray” o “bump”.

Después en el sombreador, los valores de la propiedad son accedidos utilizando el nombre de la propiedad en los braquets cuadrados: [name]. square brackets: [name]. For example, you could make blending mode be driven by a material property by declaring two integer properties (say “SrcBlend“ and ”DstBlend”), and later on make Blend Command use them: Blend [_SrcBlend] [_DstBlend].

Shader parameters that are in the Properties block are serialized as Material data. Shader programs can actually have more parameters (like matrices, vectors and floats) that are set on the material from code at runtime, but if they are not part of the Properties block then their values will not be saved. This is mostly useful for values that are completely script code-driven (using Material.SetFloat and similar functions).

Property attributes and drawers

In front of any property, optional attributes in square brackets can be specified. These are either attributes recognized by Unity, or they can indicate your own MaterialPropertyDrawer classes to control how they should be rendered in the material inspector. Attributes recognized by Unity:

  • [HideInInspector] - does not show the property value in the material inspector.
  • [NoScaleOffset] - material inspector will not show texture tiling/offset fields for texture properties with this attribute.
  • [Normal] - indicates that a texture property expects a normal-map.
  • [HDR] - indicates that a texture property expects a high-dynamic range (HDR) texture.
  • [Gamma] - indicates that a float/vector property is specified as sRGB value in the UI (just like colors are), and possibly needs conversion according to color space used. See Properties in Shader Programs.
  • [PerRendererData] - indicates that a texture property will be coming from per-renderer data in the form of a MaterialPropertyBlock. Material inspector changes the texture slot UI for these properties.

Ejemplo

// properties for a water shader
Properties
{
    _WaveScale ("Wave scale", Range (0.02,0.15)) = 0.07 // sliders
    _ReflDistort ("Reflection distort", Range (0,1.5)) = 0.5
    _RefrDistort ("Refraction distort", Range (0,1.5)) = 0.4
    _RefrColor ("Refraction color", Color) = (.34, .85, .92, 1) // color
    _ReflectionTex ("Environment Reflection", 2D) = "" {} // textures
    _RefractionTex ("Environment Refraction", 2D) = "" {}
    _Fresnel ("Fresnel (A) ", 2D) = "" {}
    _BumpMap ("Bumpmap (RGB) ", 2D) = "" {}
}

Opciones de la propiedad de Textura

Before Unity 5, texture properties could have options inside the curly brace block, e.g. TexGen CubeReflect. These were controlling fixed function texture coordinate generation. This functionality was removed in 5.0; if you need texgen you should write a vertex shader instead. See Implementing Fixed Function TexGen page page for examples.

Véase también

Sintaxis ShaderLab: Shader
ShaderLab: SubShader