This appendix presents a brief overview of the structure of surface shaders and Cg/HLSL programming.
Shaders in Unity can be written in one of the following three different ways:
.FX
files or NVIDIA's CgFX.Regardless of which type you choose, the actual meat of the shader code will always be wrapped in ShaderLab, which is used to organize the shader structure. It looks similar to the following code:
Shader "MyShader" { Properties { // All properties go here _MyTexture ("My Texture", 2D) = "white" { } } SubShader { // Choose your written style // - surface shader or // - vertex and fragment shader or // - fixed function shader } SubShader { // Optional - A simpler version of the SubShader above that can run on older graphics cards } }
However, we will only talk about the surface shaders, which we used in Project 3, The Hero/Heroine Part I—Models and Shaders.
From the preceding example code, in the Properties
block, we can define the type of properties, as shown in the following table:
Each property inside the shader is referenced by name
(in Unity, it's common to start shader property names with underscore). The property will show up in material inspector as Display name. For each property a default value is given after the equals sign:
Range
and Float
properties: It's just a single numberColor
and Vector
properties: It's four numbers in parentheses2D
, Rect
, Cube
): The default value is either an empty string, or one of the built-in default textures—white
, black
, gray
, or bump
.18.189.171.125