Unity shader学习笔记 ——4.Blinn(高光反射)

 Spcular = 直射光 * pow(max(cosθ,0),高光的参数) 

θ是反射光方向和视野方向的夹角。pow是次方函数cosθ的高光次方

(顶点函数处理方式实现)

Shader "Custom/6 six"
{   
    //Properties可以没有
    Properties
    {
        _Diffuse("Diffuse Color",Color)=(1,1,1,1)
    }
    SubShader
    {
         pass
        {
            Tags{"LightMode"="ForwardBase"}///添加tag
            CGPROGRAM 

            #include "Lighting.cginc"///引用光照 取得第一个直射光的颜色 _LightColor0
            #pragma vertex vert
            #pragma fragment frag
            fixed4 _Diffuse;

            struct a2v{
                float4 vertex:POSITION;

                ///取法线(模型空间下)
                float3 normal:NORMAL;
            };

            struct v2f{
                float4 position:SV_POSITION;
                fixed3 color:COLOR;
            };

            
            v2f vert(a2v v)
            {
                v2f f;                
                f.position = UnityObjectToClipPos(v.vertex);

                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb;

                fixed3 normalDir =normalize(mul(v.normal,(float3x3) unity_WorldToObject));

                fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz);

                float halflambert = dot(normalDir,lightDir)*0.5 +0.5;

                fixed3 diffuse = _LightColor0.rgb * halflambert *_Diffuse.rgb;

                ///获取反射方向向量   光照方向的反向  光线法向量
                fixed3 reflectDir = normalize(reflect(-lightDir,normalDir));

                ///获取摄像机视野方向向量    摄像机位置-顶点位置
                fixed3 viewDir = normalize(_WorldSpaceCameraPos - mul(v.vertex,unity_WorldToObject).xyz);

                ///公式计算高光反射       max()取正值
                fixed3 specular = _LightColor0.rgb * pow(max(dot(reflectDir,viewDir),0),10);
                
                f.color = diffuse + ambient + specular;

                return f;
            }
            
            fixed4 frag(v2f f) : SV_Target
            {
                return fixed4(f.color,1);
            }

            ENDCG
        }
        
    }
    FallBack "Diffuse"
}

实现外部控制高光shader

1.高光光圈大小(高光程度)

 Properties
    {
        _Diffuse("Diffuse Color",Color)=(1,1,1,1)
        _Gloss("Gloss",Range(8,20)) = 10
    }
            #pragma vertex vert
            #pragma fragment frag
            fixed4 _Diffuse;
            half _Gloss;
///公式计算高光反射       max()取正值
  fixed3 specular = _LightColor0.rgb * pow(max(dot(reflectDir,viewDir),0),_Gloss);

2.高光颜色

Properties
    {
        _Diffuse("Diffuse Color",Color)=(1,1,1,1)
        _Gloss("Gloss",Range(8,20)) = 10

        _SpeColor("SpeColor",Color) = (1,1,1,1)
    }
  #pragma vertex vert
            #pragma fragment frag
            fixed4 _Diffuse;
            half _Gloss;
            fixed4 _SpeColor;

  ///公式计算高光反射       max()取正值
  fixed3 specular = _LightColor0.rgb * _SpeColor * pow(max(dot(reflectDir,viewDir),0),_Gloss);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值