12.unity内置函数

UnityCG.cginc中一些常用的函数

//摄像机方向(视角方向)
float3 WorldSpaceViewDir(float4 v)          根据模型空间中的顶点坐标 得到 (世界空间)从这个点到摄像机的观察方向
float3 UnityWorldSpaceViewDir(float4 v) 世界空间中的顶点坐标==》世界空间从这个点到摄像机的观察方向
float3 ObjSpaceViewDir(float4 v)                 模型空间中的顶点坐标==》模型空间从这个点到摄像机的观察方向
//光源方向
float3 WorldSpaceLightDir(float4 v)         模型空间中的顶点坐标==》世界空间中从这个点到光源的方向
float3 UnityWorldSpaceLightDir(float4 v)         世界空间中的顶点坐标==》世界空间中从这个点到光源的方向
float3 ObjSpaceLightDir(float4 v)         模型空间中的顶点坐标==》模型空间中从这个点到光源的方向
//方向转换
float3 UnityObjectToWorldNormal(float3 norm) 把法线方向 模型空间==》世界空间
float3 UnityObjectToWorldDir(float3 dir) 把方向 模型空间=》世界空间
float3 UnityWorldToObjectDir(float3 dir) 把方向 世界空间=》模型空间

用这里面的方法改掉之前的Blinn-Phong里的写法

Shader "Custom/MySpecularShader-Blinn-Phong-New"
{
    Properties
    {
        _Diffuse("Diffuse color",Color) = (1,1,1,1)
        _Gloss("Gloss",Range(8,200)) = 10
        _Specular("Specular",Color) = (1,1,1,1)
    }
    SubShader
    {
        pass
        {
            Tags { "LightMode"="ForwardBase" }
            CGPROGRAM
            #include "Lighting.cginc"
            #pragma vertex vert
            #pragma fragment frag
            fixed4 _Diffuse;
            half _Gloss;
            fixed3 _Specular;
            struct a2v
            {
                float4 vertex:POSITION;
                float3 normal:NORMAL;
            };
            struct v2f
            {
                float4 pos:SV_POSITION;
                fixed3 worldNormalDir:color;
                float4 worldVert:TEXCOORD1;
            };
            v2f vert(a2v i)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(i.vertex);//UnityObjectToClipPos(i.vertex);
                //o.worldNormalDir = mul(i.normal,(float3x3)unity_WorldToObject);
                o.worldNormalDir = UnityObjectToWorldNormal(i.normal);
                o.worldVert = mul(unity_ObjectToWorld, i.vertex);
                // o.color = tempColor;
                return o;
            }
            float4 frag(v2f i):SV_TARGET
            {
                //fixed3 worldNormalDir = mul(i.normal,(float3x3)unity_WorldToObject);
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb;//获取环境光
                fixed3 normalDir = normalize(i.worldNormalDir);  //这个矩阵把一个方向从世界空间转换到模型空间  这样放在后面就是模型到世界了
                //fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz);//对于每个顶点来说光的位置就是光的方向,因为光是平行的。
                fixed3 lightDir = normalize(UnityWorldSpaceLightDir(i.pos));
                fixed3 halfLambert = dot(normalDir,lightDir) * 0.5 + 0.5;  //半兰伯特光照公式 max(dot(normalDir,lightDir),0);
                fixed3 diffuse = _LightColor0.rgb * halfLambert * _Diffuse;//取得漫反射的颜色
                
                //fixed3 reflectDir = normalize(reflect(-lightDir,normalDir));//反射光方向 
                //fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.pos.xyz);//视野方向
                fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldVert.xyz));
                fixed3 halfDir = normalize(viewDir + lightDir);
                fixed3 specular = _LightColor0.rgb * _Specular * pow(max(dot(normalDir, halfDir) , 0) , _Gloss);
                fixed3 tempColor = diffuse + ambient + specular;
                return fixed4(tempColor,1);
            }  
            ENDCG
        }
    }
    FallBack "Diffuse"
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值