zoukankan      html  css  js  c++  java
  • 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 --------------------------------------------------------------------------------

  • 相关阅读:
    spring boot的actuator
    mongo的用户角色配置
    spring boot使用AOP统一处理web请求
    (原)luarocks install 提示 failed fetching manifest
    (原)ubuntu中安装kate
    (原+转)Ubuntu16.04软件中心闪退及wifi消失
    (原)torch使用caffe时,提示CUDNN_STATUS_EXECUTION_FAILED
    (原)torch中微调某层参数
    (原)torch的apply函数
    (原)torch的训练过程
  • 原文地址:https://www.cnblogs.com/2Yous/p/4232210.html
Copyright © 2011-2022 走看看