标签:des style blog http color strong io for
【Writing Surface Shaders】
Writing shaders that interact with lighting is complex. There are different light types, different shadow options, different rendering paths (forward and deferred rendering), and the shader should somehow handle all that complexity.
Surface Shaders in Unity is a code generation approach that makes it much easier to write lit shaders than using low level vertex/pixel shader programs. Note that there is no custom languages, magic or ninjas involved in Surface Shaders; it just generates all the repetitive code that would have to be written by hand. You still write shader code in Cg / HLSL.
You define a “surface function” that takes any UVs or data you need as input, and fills in output structure SurfaceOutput
. SurfaceOutput basically describes properties of the surface (it’s albedo color, normal, emission, specularity etc.). You write this code in Cg / HLSL.
Surface Shader compiler then figures out what inputs are needed, what outputs are filled and so on, and generates actual vertex&pixel shaders, as well as rendering passes to handle forward and deferred rendering.
【Surface Shader compile directives】
Surface shader is placed inside CGPROGRAM..ENDCG
block, just like any other shader. The differences are:
#pragma surface ...
directive to indicate it’s a surface shader. The #pragma surface
directive is:
Required parameters:
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 surface function.Lambert
(diffuse) and BlinnPhong
(specular). See Custom Lighting Models page for how to write your own.【Optional parameters】
The input structure Input
generally has any texture coordinates needed by the shader. Texture coordinates must be named “uv
” followed by texture name (or start it with “uv2
” to use second texture coordinate set).
Writing Surface Shaders,布布扣,bubuko.com
标签:des style blog http color strong io for
原文地址:http://www.cnblogs.com/tekkaman/p/3885870.html