UnityShader 学习笔记 2 高光

版权声明:本文为博主原创文章,未经博主允许不得转载。如有问题,欢迎指正。 https://blog.csdn.net/qq_37352817/article/details/85113449
  • 逐像素


Shader "_MyShader/2_Specular/fragment"

{

    Properties

    {

        _DiffuseColor ("DiffuseColor",COLOR) =(1,1,1,1)

            _SpecularColor ("SpecularColor",COLOR) =(1,1,1,1)

            _SpecularRange ("SpecularRange",Range(5,100)) = 20

    }

    SubShader

    {

        Pass

        {

            Tags{"LightMode" = "ForwardBase"}

            CGPROGRAM

            #pragma vertex vert

            #pragma fragment frag



            #include "Lighting.cginc"



            fixed4 _DiffuseColor;

            fixed4 _SpecularColor;

            float _SpecularRange;



            struct a2v {

                float4 vertex:POSITION;

                float3 normal:NORMAL;

            };



            struct v2f{

                float4 pos:SV_POSITION;

                fixed3 worldNormalDir:POSITION1;

                fixed3 worldLightDir:POSITION2;

                half3 reflectDir:POSITION3;

                fixed3 worldViewDir:POSITION4;

                //Blinn-Phong

                fixed3 blinn_Phong_Dir:POSITION5;

            };



            v2f vert(a2v v){

                v2f o;

                o.pos=mul(UNITY_MATRIX_MVP,v.vertex);



                o.worldLightDir = normalize(_WorldSpaceLightPos0);

                o.worldNormalDir = normalize(mul(v.normal,(float3x3)_World2Object));



                //计算反射向量

                o.reflectDir = normalize(reflect(-o.worldLightDir,o.worldNormalDir));

                //计算视角向量

                o.worldViewDir = normalize(_WorldSpaceCameraPos - mul(_Object2World,v.vertex));



                //Blinn-Phong

                o.blinn_Phong_Dir = normalize(o.worldLightDir + o.worldViewDir);



                return o;

            }



            fixed4 frag(v2f i):SV_Target{

                //漫反射阶段

                fixed3 ambientColor=UNITY_LIGHTMODEL_AMBIENT;



                fixed3 diffuseColor = _LightColor0 * _DiffuseColor*saturate(dot(i.worldNormalDir,i.worldLightDir));



                //高光阶段

                fixed3 specularColor = _LightColor0 * _SpecularColor * pow(saturate(dot(i.reflectDir,i.worldViewDir)),_SpecularRange);

                //Blinn-Phong光照

                //fixed3 specularColor = _LightColor0 * _SpecularColor * pow(saturate(dot(i.worldNormalDir,i.blinn_Phong_Dir)),_SpecularRange);



                fixed4 col = fixed4(diffuseColor + ambientColor + specularColor,1);

                return col;

            }





            ENDCG

        }

    }

    FallBack "Specular"

}




  • 逐顶点


Shader "_MyShader/2_Specular/vertex"

{

    Properties

    {

        _DiffuseColor ("DiffuseColor",COLOR) =(1,1,1,1)

            _SpecularColor ("SpecularColor",COLOR) =(1,1,1,1)

            _SpecularRange ("SpecularRange",Range(5,100)) = 20

    }

    SubShader

    {

        Pass

        {

            Tags{"LightMode" = "ForwardBase"}

            CGPROGRAM

            #pragma vertex vert

            #pragma fragment frag



            #include "Lighting.cginc"



            fixed4 _DiffuseColor;

            fixed4 _SpecularColor;

            float _SpecularRange;



            struct a2v {

                float4 vertex:POSITION;

                float3 normal:NORMAL;

            };



            struct v2f{

                float4 pos:SV_POSITION;

                fixed3 color:COLOR;

            };



            v2f vert(a2v v){

                v2f o;

                //漫反射阶段

                o.pos=mul(UNITY_MATRIX_MVP,v.vertex);

                fixed3 lightColor=_LightColor0;

                fixed3 ambientColor=UNITY_LIGHTMODEL_AMBIENT;

                fixed3 worldNormalDir=normalize(mul(v.normal,(float3x3)_World2Object));

                fixed3 worldLightDir=normalize(_WorldSpaceLightPos0);

                fixed3 diffuseColor=lightColor*_DiffuseColor*saturate(dot(worldNormalDir,worldLightDir));



                //高光阶段

                //计算反射光

                //计算结果是一个half3类型的数据

                half3 reflectDir = normalize(reflect(-worldLightDir,worldNormalDir));



                //计算视角向量

                fixed3 viewDir = normalize(_WorldSpaceCameraPos - mul(_Object2World,v.vertex));



                //计算高光

                fixed3 specularColor = lightColor * _SpecularColor * pow(saturate(dot(reflectDir,viewDir)),_SpecularRange);



                o.color = diffuseColor + ambientColor + specularColor;

                return o;

            }



            fixed4 frag(v2f i):SV_Target{

                fixed4 col = fixed4 (i.color,1);

                return col;

            }





            ENDCG

        }

    }

    FallBack "Specular"

}




猜你喜欢

转载自blog.csdn.net/qq_37352817/article/details/85113449