Unity Shader学习-高光反射

Unity Shader学习-高光反射

高光反射计算公式

高光反射 = 光源的色彩和强度 * 材质的高光反射系数 * pow(max(0,视角方向 · 反射方向),_Gloss)
视角方向 = reflect(入射方向,法线)
max函数

max(0,a) 如果a大于0 则返回a,如果a小于0 返回0

pow函数

pow(x, y) 返回x的y次方

Gloss

控制高光区域的大小

光源的色彩和强度

需要引用UnityShader自带库 “Lighting.cginc”,通过使用自带属性 _LightColor0 可以得到场景中光源的rgb值。

材质的高光反射系数

在Properties中定义的属性

Properties{
	_Specular("Specular",Color) = (1,1,1,1)
}
视角方向

摄像机的方向向量减去物体自身的方向向量得到从物体到摄像机的视角方向

fixed3 viewDir = (_WorldSpaceCameraPos.xyz - worldPos.xyz);
反射方向

通过函数reflect得到,reflect需要两个参数 入射方向和法线
reflect(入射方向,法线)
入射方向通过对世界坐标下的光源方向取反来得到。
重要:
_WorldSpaceLightPos0 取得光源的方向
UnityWorldSpaceLightDir 函数输入一个顶点,返回从该顶点到光源的方向向量
取得的都是光源的方向而不是光照的方向

fixed3 worldLightDir = _WorldSpaceLightPos0.xyz;
reflect(-worldLightDir,worldNormal);

完整代码

Shader "Custom/SpecularFragment"
{
    Properties
    {
        _Diffuse ("Diffuse", Color) = (1,1,1,1)
        _Specular ("Specular", Color) = (1,1,1,1)
        _Gloss ("Gloss", Range(8,256)) = 20
    }
    SubShader
    {
        Pass{
            Tags { "LightMode"="ForwardBase" }
            LOD 200

            CGPROGRAM
            #include "Lighting.cginc"
            #pragma vertex vert
            #pragma fragment frag


            fixed4 _Diffuse;
            fixed4 _Specular;
            float _Gloss;

            struct a2v{
                float4 vertex : POSITION;
                fixed3 normal : NORMAL;
            };

            struct v2f{
                float4 pos : SV_POSITION;
                fixed3 worldNormal : TEXCOORD0;
                float3 worldPos : TEXCOORD1;
            };

            v2f vert(a2v v):POSITION{
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
                return o;
            }

            fixed4 frag(v2f i) : SV_Target{
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

                //世界坐标下的法线
                fixed3 worldNormal = normalize(i.worldNormal);
                
                //取得光源的方向
                fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
                //计算漫反射
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLightDir));

                //反射方向
                fixed3 reflectDir = normalize(reflect(-worldLightDir,worldNormal));

                //视角方向
                fixed3 viewDir =normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
                
                //计算高光反射
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0,dot(reflectDir,viewDir)), _Gloss);

                return fixed4(ambient + diffuse + specular,1);
            }
            ENDCG
        }
    }
    FallBack "Specular"
}

Blinn-Phong光照模型

第一种的高光实现为标准高光模型,还有一种为Blinn-Phone光照模型,使用这种模型会使高光区域更大更亮,表现效果会更好
绝大多数情况都会使用这种渲染方式
与标准光照模型不同的地方在于,Phong光照模型没有使用反射方向而是通过对视角方向和光照方向相加并将结果标准化来得到。

	fixed3 viewDir =normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
	fixed3 halfDir =normalize(worldLightDir + viewDir);
	fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0,dot(worldNormal,halfDir)), _Gloss);
	return fixed4(ambient + diffuse + specular,1);
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值