• Unity3D ShaderLab 使用BlinnPhong高光类型


    Unity3D shaderLab 使用BlinnPhong高光类型

    上一篇我们实现了自定义高光类型,这一篇,我们说Blinn高光,它是另一种计算和估算高光更高效的方式,它是通过视线防线和光线方向,所形成的半角向量来完成。

    这种方式比我们自己形成反射向量来进行计算更加高效。在UnityCG.cginc文件中内置的BlinnPhong光照模型就是半角矢量完成的。

     

    首先还是创建一个shader,一个材质球,双击shader,打开编辑器。

    1Properties 

    Properties {
    
    _MainTex ("Base (RGB)", 2D) = "white" {}
    
    _MainTint("Diffuse Tint",Color)=(1,1,1,1)
    
    _SpecularColor("Specular Color",Color)=(1,1,1,1)
    
    _SpecularPower("Specular Power",Range(0.1,30))=1
    
    }

    2SubShader申明变量

    CGPROGRAM
    
    #pragma surface surf MyBlinnPhong
    
     
    
    sampler2D _MainTex;
    
    float4 _SpecularColor;
    
    float4 _MainTint;
    
    float _SpecularPower;

    3:LightingMyBlinnPhong光照模型实现

    inline fixed4 LightingMyBlinnPhong(SurfaceOutput s,fixed3 lightDir,half3 viewDir, fixed atten){
    
    float3 halfVector = normalize(lightDir+viewDir);
    
    float diff = max(0,dot(s.Normal, lightDir));
    
    float nh = max(0,dot(s.Normal, halfVector));
    
    float spec = pow(nh,_SpecularPower)*_SpecularColor;
    
     
    
    float4 c;
    
    c.rgb = (s.Albedo*_LightColor0.rgb*diff)+(_LightColor0.rgb*_SpecularColor.rgb*spec)*(atten*2);
    
    c.a = s.Alpha;
    
    return c;
    
    }

    4surf函数

    half4 c = tex2D (_MainTex, IN.uv_MainTex)*_MainTint;

    最终效果如下:

    最左侧是BlinnPhong,最右侧是自定义Phong,中间的是最基础的高光效果。

     

                                                                                       

    BlinnPhong高光模型和phong高光模型效果几乎一致,除了前者使用更少的代码更高效,他们的效果几乎一致。

    在上面的 LightingMyBlinnPhong光照模型中,我们为了获得半角向量,视线方向和入射方向叠加,通过normalize获得了半角向量。

    我们再简单的对顶点法线和新的半角向量进行点乘运算从而获得我们的高光值,再对它进行 SpecularPower次方求幂后乘以高光颜色值 SpecularColor。                                                                                                                                                        

    虽然过程简化,但是同样能给我们带来出色的高光效果。

     

    code start--------------------------------------------------------------------------------

     

    Shader "91YGame/BlinnPhong" {
        Properties {
            _MainTex ("Base (RGB)", 2D) = "white" {}
            _MainTint("Diffuse Tint",Color)=(1,1,1,1)
            _SpecularColor("Specular Color",Color)=(1,1,1,1)
            _SpecularPower("Specular Power",Range(0.1,30))=1
        }
        SubShader {
            Tags { "RenderType"="Opaque" }
            LOD 200
            
            CGPROGRAM
            #pragma surface surf MyBlinnPhong
    
            sampler2D _MainTex;
            float4 _SpecularColor;
            float4 _MainTint;
            float _SpecularPower;
    
            struct Input {
                float2 uv_MainTex;
            };
    
            inline fixed4 LightingMyBlinnPhong(SurfaceOutput s,fixed3 lightDir,half3 viewDir, fixed atten){
                float3 halfVector = normalize(lightDir+viewDir);
                float diff = max(0,dot(s.Normal, lightDir));
                float nh = max(0,dot(s.Normal, halfVector));
                float spec = pow(nh,_SpecularPower)*_SpecularColor;
    
                float4 c;
                c.rgb = (s.Albedo*_LightColor0.rgb*diff)+(_LightColor0.rgb*_SpecularColor.rgb*spec)*(atten*2);
                c.a = s.Alpha;
                return c;
            }
    
            void surf (Input IN, inout SurfaceOutput o) {
                half4 c = tex2D (_MainTex, IN.uv_MainTex)*_MainTint;
                o.Albedo = c.rgb;
                o.Alpha = c.a;
            }
            ENDCG
        }
        FallBack "Diffuse"
    }



     

    code end --------------------------------------------------------------------------------

  • 相关阅读:
    个人永久性免费-Excel催化剂功能第18波-在Excel上也能玩上词云图
    个人永久性免费-Excel催化剂功能第17波-批量文件改名、下载、文件夹创建等
    个人永久性免费-Excel催化剂功能第16波-N多使用场景的多维表转一维表
    Excel催化剂开源第6波-Clickonce部署之自动升级瘦身之术
    Excel催化剂开源第5波-任务窗格在OFFICE2013中新建文档不能同步显示问题解决
    js中获取 table节点各tr及td的内容方法
    sql语句 怎么从一张表中查询数据插入到另一张表中?
    JS 如何获取自定义属性
    Ext.tree.TreePanel 属性详解
    Canvas 画圆
  • 原文地址:https://www.cnblogs.com/2Yous/p/4232210.html
Copyright © 2020-2023  润新知