To use the surface shaders, you need to define a surface function (void surf(Input IN, inout SurfaceOutput o)
) that takes any UVs or data you need as input, and fills in the output structure SurfaceOutput
. The SurfaceOutput
structure basically describes properties of the surface (that is albedo color, normal, emission, specularity, and so on). Then, you write this code in Cg/HLSL.
Surface shader compiler then figures out the inputs that are needed, the outputs that are filled, and so on, and generates actual vertex and pixel shaders as well as rendering passes to handle forward and deferred rendering.
The surface shaders placed inside CGPROGRAM...ENDCG
block, must be placed inside the SubShader
block, and uses the #pragma surface ...
directive to indicate that it's a surface shader. You will see that the surface shaders placed inside CGPROGRAM
and ENDCG
block in the following example:
Shader "My Lambert" { Properties { _MainTex ("Texture", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 200 //Optional that allows the script to turned the shader on or off when the player's hardware didn't support your shader. CGPROGRAM #pragma surface surf Lambert sampler2D _MainTex; struct Input { float2 uv_MainTex; }; void surf (Input IN, inout SurfaceOutput o) { fixed4 c = tex2D (_MainTex, IN.uv_MainTex); o.Albedo = c.rgb; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" }
The #pragma surface
directive is:
#pragma surface surfaceFunction lightModel [optionalparams]
The following are the required parameters for the #pragma surface
directive:
surfaceFunction
—the Cg
function that has surface shader code. The function should have the form void surf (Input IN, inout SurfaceOutput o)
, where Input
is a structure you have defined. Input
should contain any texture coordinates and extra automatic variables needed by the surface function.lightModel
—lighting model to use. Built-in ones are Lambert
(diffuse) and BlinnPhong
(specular). You can also write your own by using the following custom lighting models:half4 LightingName (SurfaceOutput s, half3 lightDir, half atten);
: This is used in a forward rendering path for light models that are not view direction dependent (for example, diffuse).half4 LightingName (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten);
: This is used in a forward rendering path for light models that are view direction dependent.half4 LightingName_PrePass (SurfaceOutput s, half4 light);
: This is used in a deferred lighting path.Note that you don't need to declare all functions. A lighting model either uses view direction or it does not. Similarly, if the lighting model will not work in deferred lighting, you just do not declare the _PrePass
function. All the shaders that use it will compile to forward rendering only, such as the shader that we did in Chapter 3, The Hero/Heroine Part I—Models and Shaders. We don't need the _PrePass
function because our shader needs the view direction(viewDir
) and the light direction(lightDir
) for our custom lighting function to calculate the ramp
effect for the cartoon style shader (Toon Shader/Cel Shader), which is only available in forward rendering.
[optionalparams]
:Additionally, you can write #pragma debug
inside the CGPROGRAM
block, and then the surface compiler will spit out a lot of comments of the generated code. You can view that using Open Compiled Shader in shader inspector.
The input structure Input
generally has any texture coordinates needed by the shader. Texture coordinates must be named uv
followed by a texture name (or start it with uv2
to use the second texture coordinate set).
Properties { _MainTex ("Texture", 2D) = "white" {} } ...... sampler2D _MainTex; ...... struct Input { float2 uv_MainTex; };
We can also have the additional values that can be put into the input structure:
The standard output structure of surface shaders is as follows:
struct SurfaceOutput { fixed3 Albedo; fixed3 Normal; fixed3 Emission; half Specular; fixed Gloss; fixed Alpha; };
You can also find it in the Lighting.cginc
file inside Unity in {unity install path}/Data/CGIncludes/Lighting.cginc
on Windows, and in /Applications/Unity/Unity.app/Contents/CGIncludes/Lighting.cginc
on a Mac.
3.144.9.124