表面着色器示例        

这里列举了一些表面着色的示例。下文中的示例侧重于使用内置光照模型。有关如何实现自定义光照模型的示例,请参阅表面着色器光照示例。

简单

我们将从非常简单的着色器开始,然后在此基础上演示其他示例。这是一个仅将表面颜色设置为“白色”的着色器。其使用内置 Lambert(漫反射)光照模式。

  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"  }

这是它在设置有两种灯光的模型上的样子:

纹理

一个纯白色的对象显得非常单调,让我们添加一个纹理。我们将为着色器添加一个属性块,这样在“材质”(Material) 中就会有一个纹理选择器。其他更改在下文中以粗体显示。

  Shader "Example/Diffuse Texture" {    Properties {      _MainTex ("Texture", 2D) = "white" {}    }    SubShader {      Tags { "RenderType" = "Opaque" }      CGPROGRAM      #pragma surface surf Lambert      struct Input {          float2 uv_MainTex;      };      sampler2D _MainTex;      void surf (Input IN, inout SurfaceOutput o) {          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;      }      ENDCG    }     Fallback "Diffuse"  }

法线贴图 (Normal Mapping)

让我们添加一些法线贴图 (normal mapping):

  Shader "Example/Diffuse Bump" {    Properties {      _MainTex ("Texture", 2D) = "white" {}      _BumpMap ("Bumpmap", 2D) = "bump" {}    }    SubShader {      Tags { "RenderType" = "Opaque" }      CGPROGRAM      #pragma surface surf Lambert      struct Input {        float2 uv_MainTex;        float2 uv_BumpMap;      };      sampler2D _MainTex;      sampler2D _BumpMap;      void surf (Input IN, inout SurfaceOutput o) {        o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;        o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));      }      ENDCG    }     Fallback "Diffuse"  }

边缘光照 (Rim Lighting)

现在,试着添加一些边缘光照 (Rim Lighting) 以高亮显示对象的边缘。我们将基于表面法线与视线方向之间的角度添加一些发射光。为此,我们将使用 viewDir 内置表面着色器变量。

  Shader "Example/Rim" {    Properties {      _MainTex ("Texture", 2D) = "white" {}      _BumpMap ("Bumpmap", 2D) = "bump" {}      _RimColor ("Rim Color", Color) = (0.26,0.19,0.16,0.0)      _RimPower ("Rim Power", Range(0.5,8.0)) = 3.0    }    SubShader {      Tags { "RenderType" = "Opaque" }      CGPROGRAM      #pragma surface surf Lambert      struct Input {          float2 uv_MainTex;          float2 uv_BumpMap;          float3 viewDir;      };      sampler2D _MainTex;      sampler2D _BumpMap;      float4 _RimColor;      float _RimPower;      void surf (Input IN, inout SurfaceOutput o) {          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;          o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));          half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));          o.Emission = _RimColor.rgb * pow (rim, _RimPower);      }      ENDCG    }     Fallback "Diffuse"  }

细节纹理 (Detail Texture)

为实现一种不同的效果,让我们添加一个与基础纹理 (base texture) 结合的细节纹理 (detail texture)。细节纹理 (detail texture) 使用“材质”(Material) 中相同的 UV,但通常使用不同的“铺设”(Tiling)。因此我们必须使用不同的输入 UV 坐标。

  Shader "Example/Detail" {    Properties {      _MainTex ("Texture", 2D) = "white" {}      _BumpMap ("Bumpmap", 2D) = "bump" {}      _Detail ("Detail", 2D) = "gray" {}    }    SubShader {      Tags { "RenderType" = "Opaque" }      CGPROGRAM      #pragma surface surf Lambert      struct Input {          float2 uv_MainTex;          float2 uv_BumpMap;          float2 uv_Detail;      };      sampler2D _MainTex;      sampler2D _BumpMap;      sampler2D _Detail;      void surf (Input IN, inout SurfaceOutput o) {          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;          o.Albedo *= tex2D (_Detail, IN.uv_Detail).rgb * 2;          o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));      }      ENDCG    }     Fallback "Diffuse"  }

虽然使用查看器纹理 (checker texture) 没有什么实际意义,但会说明发生了什么:

屏幕空间中的细节纹理 (Detail Texture)

那屏幕空间中的细节纹理 (detail texture) 有何效果?虽然它对士兵头部模型的意义不大,但会说明可以如何使用内置 screenPos 输入:

  Shader "Example/ScreenPos" {    Properties {      _MainTex ("Texture", 2D) = "white" {}      _Detail ("Detail", 2D) = "gray" {}    }    SubShader {      Tags { "RenderType" = "Opaque" }      CGPROGRAM      #pragma surface surf Lambert      struct Input {          float2 uv_MainTex;          float4 screenPos;      };      sampler2D _MainTex;      sampler2D _Detail;      void surf (Input IN, inout SurfaceOutput o) {          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;          float2 screenUV = IN.screenPos.xy / IN.screenPos.w;          screenUV *= float2(8,6);          o.Albedo *= tex2D (_Detail, screenUV).rgb * 2;      }      ENDCG    }     Fallback "Diffuse"  }

我从上面的着色器中移除了法线贴图 (normal mapping),只是为了使其变得更短:

立方体贴图反射

这是一个使用内置 worldRefl 输入进行立方体贴图反射的着色器。它其实与内置反射 (Reflective)/漫反射 (Diffuse) 着色器非常类似:

  Shader "Example/WorldRefl" {    Properties {      _MainTex ("Texture", 2D) = "white" {}      _Cube ("Cubemap", CUBE) = "" {}    }    SubShader {      Tags { "RenderType" = "Opaque" }      CGPROGRAM      #pragma surface surf Lambert      struct Input {          float2 uv_MainTex;          float3 worldRefl;      };      sampler2D _MainTex;      samplerCUBE _Cube;      void surf (Input IN, inout SurfaceOutput o) {          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * 0.5;          o.Emission = texCUBE (_Cube, IN.worldRefl).rgb;      }      ENDCG    }     Fallback "Diffuse"  }

由于它将反射色指定为发光颜色,所以士兵非常闪亮:

如果您想实现受法线贴图 (normal map) 影响的反射,还需执行稍微多一些的操作:您需要将 INTERNAL_DATA 添加到输入 (Input) 结构中,并在编写完法线 (Normal) 输出后使用 WorldReflectionVector 函数计算逐像素反射向量。

  Shader "Example/WorldRefl Normalmap" {    Properties {      _MainTex ("Texture", 2D) = "white" {}      _BumpMap ("Bumpmap", 2D) = "bump" {}      _Cube ("Cubemap", CUBE) = "" {}    }    SubShader {      Tags { "RenderType" = "Opaque" }      CGPROGRAM      #pragma surface surf Lambert      struct Input {          float2 uv_MainTex;          float2 uv_BumpMap;          float3 worldRefl;          INTERNAL_DATA      };      sampler2D _MainTex;      sampler2D _BumpMap;      samplerCUBE _Cube;      void surf (Input IN, inout SurfaceOutput o) {          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * 0.5;          o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));          o.Emission = texCUBE (_Cube, WorldReflectionVector (IN, o.Normal)).rgb;      }      ENDCG    }     Fallback "Diffuse"  }

这便是一名经法线贴图的闪亮士兵:

经世界坐标空间位置的切片

这是在近于水平的环中通过丢弃像素来“切分”对象的着色器。其根据像素的世界坐标位置使用 clip() Cg/HLSL 函数来实现。我们将使用 worldPos 内置表面着色器变量。

  Shader "Example/Slices" {    Properties {      _MainTex ("Texture", 2D) = "white" {}      _BumpMap ("Bumpmap", 2D) = "bump" {}    }    SubShader {      Tags { "RenderType" = "Opaque" }      Cull Off      CGPROGRAM      #pragma surface surf Lambert      struct Input {          float2 uv_MainTex;          float2 uv_BumpMap;          float3 worldPos;      };      sampler2D _MainTex;      sampler2D _BumpMap;      void surf (Input IN, inout SurfaceOutput o) {          clip (frac((IN.worldPos.y+IN.worldPos.z*0.1) * 5) - 0.5);          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;          o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));      }      ENDCG    }     Fallback "Diffuse"  }

使用顶点修改器进行法线挤压

可使用“顶点修改器”(vertex modifier) 函数,该函数将修改顶点着色器中的输入顶点数据。这可用于程序性动画、沿法线的挤压等。使用表面着色器编译指令 vertex:functionName 和一个使用 inout appdata_full 参数的函数。

Here's a shader that moves vertices along their normals by the amount specified in the material:

  Shader "Example/Normal Extrusion" {    Properties {      _MainTex ("Texture", 2D) = "white" {}      _Amount ("Extrusion Amount", Range(-1,1)) = 0.5    }    SubShader {      Tags { "RenderType" = "Opaque" }      CGPROGRAM      #pragma surface surf Lambert vertex:vert      struct Input {          float2 uv_MainTex;      };      float _Amount;      void vert (inout appdata_full v) {          v.vertex.xyz += v.normal * _Amount;      }      sampler2D _MainTex;      void surf (Input IN, inout SurfaceOutput o) {          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;      }      ENDCG    }     Fallback "Diffuse"  }

沿法线移动顶点使士兵变得臃肿:

逐顶点计算的自定义数据

使用顶点修改器 (vertex modifier) 函数还可以在顶点着色器中计算自定义数据,然后所计算的自定义数据将被逐像素传递到表面着色器函数。使用相同的编译指令 vertex:functionName,但函数应使用两个参数:inout appdata_fullout Input。您可以填入任何不是内置值的输入 (Input) 成员。

下面的示例定义了一个在顶点函数中计算出的自定义 float3 customColor 成员:

  Shader "Example/Custom Vertex Data" {    Properties {      _MainTex ("Texture", 2D) = "white" {}    }    SubShader {      Tags { "RenderType" = "Opaque" }      CGPROGRAM      #pragma surface surf Lambert vertex:vert      struct Input {          float2 uv_MainTex;          float3 customColor;      };      void vert (inout appdata_full v, out Input o) {          UNITY_INITIALIZE_OUTPUT(Input,o);          o.customColor = abs(v.normal);      }      sampler2D _MainTex;      void surf (Input IN, inout SurfaceOutput o) {          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;          o.Albedo *= IN.customColor;      }      ENDCG    }     Fallback "Diffuse"  }

在此示例中,customColor 设置为法线的绝对值:

更多的实际应用可为:计算任何非内置输入 (Input) 变量提供 的逐顶点数据,或优化着色器计算。例如,可以用它在对象的顶点计算边缘光照 (Rim Lighting),而不是在表面着色器中逐像素进行计算。

最终颜色修改器 (Final Color Modifier)

可使用“最终颜色修改器”(Final Color Modifier) 函数,该函数将修改由着色器计算的最终颜色。使用着色器编译指令 finalcolor:functionName 和一个使用 Input IN、SurfaceOutput o、inout fixed4 color 参数的函数。

这是一个将色调应用于最终颜色的简单着色器。这与仅将色调应用于表面反射率 (Albedo) 颜色不同:此色调也会影响来自光照贴图、光探头和类似额外来源的任何颜色。

  Shader "Example/Tint Final Color" {    Properties {      _MainTex ("Texture", 2D) = "white" {}      _ColorTint ("Tint", Color) = (1.0, 0.6, 0.6, 1.0)    }    SubShader {      Tags { "RenderType" = "Opaque" }      CGPROGRAM      #pragma surface surf Lambert finalcolor:mycolor      struct Input {          float2 uv_MainTex;      };      fixed4 _ColorTint;      void mycolor (Input IN, SurfaceOutput o, inout fixed4 color)      {          color *= _ColorTint;      }      sampler2D _MainTex;      void surf (Input IN, inout SurfaceOutput o) {           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;      }      ENDCG    }     Fallback "Diffuse"  }

使用最终颜色修改器自定义雾

最终颜色修改器 (Final Color Modifier)(见上文)的常见应用为实现完全自定义的雾 (Fog)。雾 (Fog) 需要影响最终计算出的像素着色器颜色,这恰恰是最终颜色修改器的用处所在。

这是一个基于到屏幕中心的距离来应用雾色调的着色器。这将顶点修改器与自定义顶点数据(雾)和最终颜色修改器结合在一起。在正向渲染附加通道中使用时,雾 (Fog) 需要淡化为黑色。该示例进行处理时还检查了 UNITY_PASS_FORWARDADD

  Shader "Example/Fog via Final Color" {    Properties {      _MainTex ("Texture", 2D) = "white" {}      _FogColor ("Fog Color", Color) = (0.3, 0.4, 0.7, 1.0)    }    SubShader {      Tags { "RenderType" = "Opaque" }      CGPROGRAM      #pragma surface surf Lambert finalcolor:mycolor vertex:myvert      struct Input {          float2 uv_MainTex;          half fog;      };      void myvert (inout appdata_full v, out Input data)      {          UNITY_INITIALIZE_OUTPUT(Input,data);          float4 hpos = mul (UNITY_MATRIX_MVP, v.vertex);          data.fog = min (1, dot (hpos.xy, hpos.xy) * 0.1);      }      fixed4 _FogColor;      void mycolor (Input IN, SurfaceOutput o, inout fixed4 color)      {          fixed3 fogColor = _FogColor.rgb;          #ifdef UNITY_PASS_FORWARDADD          fogColor = 0;          #endif          color.rgb = lerp (color.rgb, fogColor, IN.fog);      }      sampler2D _MainTex;      void surf (Input IN, inout SurfaceOutput o) {           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;      }      ENDCG    }     Fallback "Diffuse"  }

线性雾 (Linear Fog)

Shader "Example/Linear Fog" {

  Properties {    _MainTex ("Base (RGB)", 2D) = "white" {}  }  SubShader {    Tags { "RenderType"="Opaque" }    LOD 200    CGPROGRAM    #pragma surface surf Lambert finalcolor:mycolor vertex:myvert    sampler2D _MainTex;    uniform half4 unity_FogColor;    uniform half4 unity_FogStart;    uniform half4 unity_FogEnd;    struct Input {      float2 uv_MainTex;      half fog;    };    void myvert (inout appdata_full v, out Input data) {      UNITY_INITIALIZE_OUTPUT(Input,data);      float pos = length(mul (UNITY_MATRIX_MV, v.vertex).xyz);      float diff = unity_FogEnd.x - unity_FogStart.x;      float invDiff = 1.0f / diff;      data.fog = clamp ((unity_FogEnd.x - pos) * invDiff, 0.0, 1.0);    }    void mycolor (Input IN, SurfaceOutput o, inout fixed4 color) {      fixed3 fogColor = unity_FogColor.rgb;      #ifdef UNITY_PASS_FORWARDADD      fogColor = 0;      #endif      color.rgb = lerp (fogColor, color.rgb, IN.fog);    }    void surf (Input IN, inout SurfaceOutput o) {      half4 c = tex2D (_MainTex, IN.uv_MainTex);      o.Albedo = c.rgb;      o.Alpha = c.a;    }    ENDCG  }   FallBack "Diffuse"

}

,