美文网首页Unity_Shader
遮罩纹理(凹凸纹理高光)

遮罩纹理(凹凸纹理高光)

作者: _Arturia | 来源:发表于2018-08-24 11:37 被阅读21次

Shader "_MyShader/4_Texture/4_MaskTexture/SpecularMask"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color",COLOR) = (1,1,1,1)
_DiffuseColor ("DiffuseColor",COLOR) = (1,1,1,1)
_NormalTexture ("NormalTexture", 2D) = "white" {}
_BumpScale ("BumpScale",Range(-2,10)) = 1
_SpecularColor ("SpecularColor",COLOR) = (1,1,1,1)
_SpecularScale ("SpecularScale",Range(-2,20)) = 5
_SpecularMaskTexture ("SpecularMaskTexture",2D) = "white" {}
_MaskScale ("MaskScale",Range(-2,10)) = 1
}
SubShader
{

    Pass
    {
        Tags {"LightMode" = "ForwardBase"}
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #include "UnityCG.cginc"
        #include "Lighting.cginc"


        sampler2D _MainTex;
        float4 _MainTex_ST;
        fixed4 _Color;
        fixed4 _DiffuseColor;

        sampler2D _NormalTexture;
        float _BumpScale;

        fixed4 _SpecularColor;
        float _SpecularScale;
        sampler2D _SpecularMaskTexture;
        float _MaskScale;



        struct a2v
        {
            float4 ver : POSITION;
            float3 normal : NORMAL;
            float4 tangent : TANGENT;
            float4 texc : TEXCOORD0;
        };

        struct v2f
        {
            float4 pos : SV_POSITION;
            float3 tanLightDir : POSITION1;
            float2 uv : TEXCOORD1;
            float3 blinn_Phong : POSITION2;
        };

        
        v2f vert (a2v v)
        {
            v2f o;
            o.pos = mul(UNITY_MATRIX_MVP, v.ver);

            TANGENT_SPACE_ROTATION;
            o.tanLightDir = normalize(mul(rotation,ObjSpaceLightDir(v.ver)));
            float3 tanViewDir = normalize(mul(rotation,ObjSpaceViewDir(v.ver)));
            o.blinn_Phong = normalize(o.tanLightDir + tanViewDir);

            o.uv = TRANSFORM_TEX(v.texc, _MainTex);

            return o;
        }
        
        fixed4 frag (v2f i) : SV_Target
        {
            float3 tanNormalDir = UnpackNormal(tex2D(_NormalTexture,i.uv));
            tanNormalDir.xy *= _BumpScale;
            tanNormalDir.z = sqrt(1 - saturate(dot(tanNormalDir.xy,tanNormalDir.xy)));

            fixed3 uvTex = tex2D(_MainTex,i.uv) * _Color.rgb;

            fixed3 diffuseC = saturate(dot(tanNormalDir,i.tanLightDir)) * _LightColor0.rgb * uvTex * _DiffuseColor.rgb;

            fixed specularMaskUV = tex2D(_SpecularMaskTexture,i.uv).r * _MaskScale;

            fixed3 specularC = pow(saturate(dot(i.blinn_Phong,tanNormalDir)),_SpecularScale) * _LightColor0.rgb * specularMaskUV * _SpecularColor.rgb;

            fixed3 ambC = UNITY_LIGHTMODEL_AMBIENT.xyz * uvTex;

            fixed4 col = fixed4(ambC + diffuseC + specularC,1);

            return col;
        }
        ENDCG
    }
}
FallBack "Specular"

}

相关文章

网友评论

    本文标题:遮罩纹理(凹凸纹理高光)

    本文链接:https://www.haomeiwen.com/subject/fzjbiftx.html