Unity3D ShaderLab 创建自定义高光类型

来源:互联网 发布:软件售前 编辑:程序博客网 时间:2024/06/05 19:29
 在上一篇,我们认识了Unity基础的高光实现,本次主要是研究如何对Phong高光类型进行顶点操作,以及在表面着色器中使用Input结构体的新参数进行像素操作。更多精彩请关注【狗刨学习网】

    所以还是新建Shader,再建材质球然后打开编辑器进行shader的编辑。

1.修改Properties

  1. <font face="新宋体" size="2">Properties {

  2. _MainTex ("Base (RGB)", 2D) = "white" {}

  3. _MainTint("Diffuse Tint",Color)=(1,1,1,1)

  4. _SpecularColor("Specular Color",Color)=(1,1,1,1)

  5. _SpecularPower("Specular Power",Range(0,30))=1

  6. }</font>
复制代码


3.修改CGPROGRAM

  1. <font face="新宋体" size="2">CGPROGRAM

  2. #pragma surface surf Phong

  3. sampler2D _MainTex;

  4. float4 _SpecularColor;

  5. float4 _MainTint;

  6. float _SpecularPower;</font>
复制代码

4.编写自定义光照模型LightingPhong

  1. <font face="新宋体" size="2">inline fixed4 LightingPhong(SurfaceOutput s,fixed3 lightDir,half3 viewDir,fixed atten){

  2. float diff=dot(s.Normal,lightDir);

  3. float3 reflection=normalize(2.0*s.Normal*diff-lightDir);

  4. float spec = pow(max(0,dot(reflection,viewDir)),_SpecularPower);

  5. float3 finalSpec = _SpecularColor.rgb*spec;

  6. fixed4 c;

  7. c.rgb=(s.Albedo*_LightColor0.rgb*diff)+(_LightColor0.rgb*finalSpec)*(atten*2);

  8. c.a=1.0;

  9. return c;

  10. }</font>
复制代码

5.修改surf函数

  1. <font face="新宋体" size="2">half4 c = tex2D (_MainTex, IN.uv_MainTex)*_MainTint;</font>
复制代码

    通过以上步骤,我们完成的自定义的高光渲染光照模型。其中的操作步骤与以前并无差别。但是我们这次用到的LightingPhong函数是4个参数的,这其中的奥妙要介绍一下:

    之前说过:光照模型是几个名称由光照 (Lighting) 开头的正规函数。它们可声明于您的着色器文件或某个被包含的文件的任何位置。函数为:

    half4 Lighting+Name (SurfaceOutput s, half3 lightDir, half atten); 其在正向渲染路径中用于非与视线方向相关的光照模型(例如,漫反射)。
    half4 Lighting+Name (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten); 其在正向渲染路径中用于与视线方向相关的光照模型。
    half4 Lighting+Name_PrePass (SurfaceOutput s, half4 light); 其用于延时光照路径中。

    请注意,您不必声明所有函数。一种光照模型要么使用视线方向,要么不使用。类似地,如果光照模型在延时光照中无效,您就不必声明 _PrePass 函数,然后所有使用它的着色器都将仅编译成正向渲染。

    在上面我们创建的是一个高光着色器,所以我们需要选的是视点/视角相关型的光照函数结构。因此我们的函数就是:

    inline fixed4 LightingPhong(SurfaceOutput s,fixed3 lightDir,half3 viewDir,fixed atten){}

    这样就告诉着色器我们建立的是一个视点相关的光照模型。

    接下来,光照函数会首先计算顶点法线和光的入射方向,我们得到一个额返回值,该值为1物体正对光源方向,-1表面物体背对光源方向。

    然后我们计算反射向量,先对顶点法线的向量进行缩放,该值乘以2.0后再乘以diff,得到的值减去光照的方向向量值,这样做实现了法线朝向光源弯曲的效果。

    所以作为一个远离光源的法线向量,它将被强制朝向光源方向.

    再往下

    我们创建spec值和颜色值,为了得到这2个值,我们对反射向量和视角方向进行点乘运算,然后对结果求specpower次方,最后将SpecularColor.rgb乘以spec得到我们的高光值。

    最终效果如下,右侧是我们自定义的高光模型:

    
    

code start---------------------------------------------------------------------------------------------
  1. <font face="新宋体" size="2">Shader "91YGame/Phong2" {
  2.     Properties {
  3.         _MainTex ("Base (RGB)", 2D) = "white" {}
  4.         _MainTint("Diffuse Tint",Color)=(1,1,1,1)
  5.         _SpecularColor("Specular Color",Color)=(1,1,1,1)
  6.         _SpecularPower("Specular Power",Range(0,30))=1
  7.     }
  8.     SubShader {
  9.         Tags { "RenderType"="Opaque" }
  10.         LOD 200
  11.         
  12.         CGPROGRAM
  13.         #pragma surface surf Phong

  14.         sampler2D _MainTex;
  15.         float4 _SpecularColor;
  16.         float4 _MainTint;
  17.         float _SpecularPower;

  18.         struct Input {
  19.             float2 uv_MainTex;
  20.         };

  21.         inline fixed4 LightingPhong(SurfaceOutput s,fixed3 lightDir,half3 viewDir,fixed atten){
  22.             float diff=dot(s.Normal,lightDir);
  23.             float3 reflection=normalize(2.0*s.Normal*diff-lightDir);
  24.             float spec = pow(max(0,dot(reflection,viewDir)),_SpecularPower);
  25.             float3 finalSpec = _SpecularColor.rgb*spec;
  26.             fixed4 c;
  27.             c.rgb=(s.Albedo*_LightColor0.rgb*diff)+(_LightColor0.rgb*finalSpec)*(atten*2);
  28.             c.a=1.0;
  29.             return c;
  30.         }

  31.         void surf (Input IN, inout SurfaceOutput o) {
  32.             half4 c = tex2D (_MainTex, IN.uv_MainTex)*_MainTint;
  33.             o.Albedo = c.rgb;
  34.             o.Alpha = c.a;
  35.         }

  36.         ENDCG
  37.     }
  38.     FallBack "Diffuse"
  39. }</font>
0 0
原创粉丝点击