Surface Shader的研究

      Surface Shaders需要受到灯光和阴影的影响。Surface Shaders是一种比较容易编写光照的shader - 这是与Unity的抽象封装。

表面着色器的标准输出结构是这样的:

struct SurfaceOutput
{
    fixed3 Albedo;  // diffuse color
    fixed3 Normal;  // tangent space normal, if written
    fixed3 Emission;
    half Specular;  // specular power in 0..1 range
    fixed Gloss;    // specular intensity
    fixed Alpha;    // alpha for transparencies
};
在Unity 5中,表面着色器还可以使用基于物理的照明模型。内置标准和标准的光照模型(见下文)分别使用这些输出结构:

struct SurfaceOutputStandard
{
    fixed3 Albedo;      // base (diffuse or specular) color
    fixed3 Normal;      // tangent space normal, if written
    half3 Emission;
    half Metallic;      // 0=non-metal, 1=metal
    half Smoothness;    // 0=rough, 1=smooth
    half Occlusion;     // occlusion (default 1)
    fixed Alpha;        // alpha for transparencies
};
struct SurfaceOutputStandardSpecular
{
    fixed3 Albedo;      // diffuse color
    fixed3 Specular;    // specular color
    fixed3 Normal;      // tangent space normal, if written
    half3 Emission;
    half Smoothness;    // 0=rough, 1=smooth
    half Occlusion;     // occlusion (default 1)
    fixed Alpha;        // alpha for transparencies
};

Surface Shader输入结构

float3 viewDir- 包含视图方向,用于计算视差效果,边缘照明等。
float4与COLOR语义-包含插值每个顶点的颜色。
float4 screenPos - 包含反射或屏幕空间效果的屏幕空间位置。请注意,这不适合GrabPass ; 你需要使用ComputeGrabScreenPos函数自己计算自定义UV 。
float3 worldPos - 包含世界空间位置。
float3 worldRefl-世界反射向量。
float3 worldNormal- 世界法线向量。

 

Surface Shader编译指令

1.它必须放在SubShader块,而不是Pass。表面着色器本身将编译为多个Pass。
2.它使用#pragma surface ...指令来指示它是表面着色器

Surface Shader必需的参数

surfaceFunction - 哪个Cg函数具有表面着色器代码。
1.该函数应具有以下形式:
    void surf (Input IN, inout SurfaceOutput o)
Input是您定义的结构。输入应包含表面函数所需的任何纹理坐标和额外自动变量。

2.lightModel - 使用的照明模型

内置的有基于物理的Standard和StandardSpecular,以及简单的非基于物理的Lambert(漫射)和BlinnPhong(镜面)。请参阅自定义照明模型页面以了解如何编写自己的。
Standard照明模型使用SurfaceOutputStandard输出结构,并匹配Unity中的标准着色器。
StandardSpecular照明模型使用SurfaceOutputStandardSpecular输出结构,并匹配Unity中的标准着色器。
Lambert和BlinnPhong照明模型不是基于物理的(Unity 4.x),但使用它们的着色器可以更快地在低端硬件上渲染。

Surface Shader简单示例

 示例:
Shader "Example/Diffuse Simple" {
    SubShader {
      Tags { "RenderType" = "Opaque" }
      CGPROGRAM
#pragma surface surf Lambert
      struct Input
    {
        float4 color : COLOR;
      };
    void surf(Input IN, inout SurfaceOutput o)
    {
        o.Albedo = 1;
    }
    ENDCG
}
Fallback "Diffuse"
  }

 

posted @ 2018-10-30 14:32  carsonche  阅读(655)  评论(0编辑  收藏  举报