Ramp texture and mask texture

  • Ramp texture 

Texture may define not only a color of an object, can also be used to store any surface properties. For example with a gradient to control the texture diffuse lighting results. By using different ramp texture diffuse lighting controlled object.

Shader "Custom/RampTexture"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        //渐变纹理
        _RampTex ("Ramp Tex", 2D) = "white" {}
        _Specular("Specular",Color)=(1,1,1,1)
        _Gloss ("Gloss", Range(8,256)) = 20
        
    }
    SubShader
    {
       pass
       {
           Tags{"LightMode"="ForwardBase"}
           CGPROGRAM

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

            fixed4 _Color ;
            sampler2D _RampTex;
            float4 _RampTex_ST;
            fixed4 _Specular;
            float _Gloss;

            struct a2v
            {
                float4 vertex :POSITION;
                float3 normal:NORMAL;
                float4 texcoord:TEXCOORD0;

            };

            struct v2f
            {
                float4 pos:SV_POSITION;
                float3 worldNormal:TEXCOORD0;
                float3 worldPos:TEXCOORD1;
                float2 uv:TEXCOORD2;
            };

            v2f vert(a2v v)
            {
                v2f o;
                o.pos=UnityObjectToClipPos(v.vertex);
                o.worldNormal=UnityObjectToWorldNormal(v.normal);
                o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
                //计算经过平铺 和偏移后的纹理坐标
                o.uv=TRANSFORM_TEX(v.texcoord,_RampTex);
                return o;
            }   

            fixed4 frag(v2f i):SV_Target
            {
                fixed3 worldNormal=normalize(i.worldNormal);
                fixed3 worldLightDir=normalize(UnityWorldSpaceLightDir(i.worldPos));
                fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz;
                //half Lambert
                fixed  halfLambert=0.5 * dot(worldNormal,worldLightDir)+0.5;
                fixed3 diffuseColor=tex2D(_RampTex,fixed2(halfLambert,halfLambert)).rgb * _Color.rgb;

                fixed3 diffuse=_LightColor0.rgb*diffuseColor;
                fixed3 viewDir=normalize(UnityWorldSpaceViewDir(i.worldPos));
                fixed3 halfDir=normalize(worldLightDir+viewDir);
                fixed3 specular=_LightColor0.rgb*_Specular.rgb*pow(max(0,dot(worldNormal,halfDir)),_Gloss);

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

By using different ramp texture to change the style of the model

FIG using the first as a pale yellow to purple gradient texture. Second intermediate ramp texture boundary is slightly red, illustration of the shade with this tone.

Third cartoon commonly used, there is no smooth transition mutation tone, color shading simulation of cartoon.

  • Mask texture

It allows us to protect certain areas from being modified. For example, we want to model certain regions a number of reflective intensity strong, a little weak in some areas, then we can use a mask to control light texture. There is a terrain material in the production of multiple images to be mixed, such as grass texture, texture performance stones, bare land, texture, texture using a mask to control how these mixed textures.

Texture mask process is: texel value obtained by sampling the texture mask, and then using one of several channels or a value (such texel.r) by multiplying with a certain surface properties, such that when the passage a value of 0, it can not protect the surface properties.

Shader "Custom/MaskTexture"
{ 
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Main Tex", 2D) = "white" {}
        _BumpMap("Normal Map",2D)="bump" {}
        _BumpScale("Bump Scale",Float)=1
        _SpecularMask("Specular Mask",2D)="white"{}
        _SpecularScale("Specular Scale",Float)=1
        _Specular("Specular",Color)=(1,1,1,1)
        _Gloss ("Gloss", Range(8,256)) = 20
        
    }
    SubShader
    {
       pass
       {
           Tags{"LightMode"="ForwardBase"}
           CGPROGRAM

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

            fixed4 _Color ;
            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _BumpMap;
            float _BumpScale;
            sampler2D _SpecularMask;
            float _SpecularScale;
            fixed4 _Specular;
            float _Gloss;

            struct a2v
            {
                float4 vertex :POSITION;
                float3 normal:NORMAL;
                float4 tangent:TANGENT;
                float4 texcoord:TEXCOORD0;

            };

            struct v2f
            {
                float4 pos:SV_POSITION;
                float2 uv:TEXCOORD0;
                float3 lightDir:TEXCOORD1;
                float3 viewDir:TEXCOORD2;
            };

            v2f vert(a2v v)
            {
                v2f o;
                o.pos=UnityObjectToClipPos(v.vertex);
                o.uv.xy=v.texcoord.xy*_MainTex_ST.xy+_MainTex_ST.zw;
                TANGENT_SPACE_ROTATION;
                o.lightDir=mul(rotation,ObjSpaceLightDir(v.vertex)).xyz;
                o.viewDir=mul(rotation,ObjSpaceViewDir(v.vertex)).xyz;

                return o;
            }   

            fixed4 frag(v2f i):SV_Target
            {
                fixed3 tangentLightDir=normalize(i.lightDir);
                fixed3 tangentViewDir=normalize(i.viewDir);
                fixed3 tangentNormal=UnpackNormal(tex2D(_BumpMap,i.uv));
                tangentNormal.xy*=_BumpScale;
                tangentNormal.z=sqrt(1-saturate(dot(tangentNormal.xy,tangentNormal.xy)));
                fixed3 albedo=tex2D(_MainTex,i.uv).rgb*_Color.rgb;
                fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
                fixed3 diffuse=_LightColor0.rgb*albedo*max(0,dot(tangentNormal,tangentLightDir));
                fixed3 halfDir=normalize(tangentLightDir+tangentViewDir);
                //获取遮罩值 ,采用r 分量来计算掩码值,来控制高光反射强度
                fixed3 specularMask=tex2D(_SpecularMask,i.uv).r *_SpecularScale;
                //计算高光部分,和高光遮罩
                fixed3 specular=_LightColor0.rgb*_Specular.rgb*pow(max(0,dot(tangentNormal,halfDir)),_Gloss)*specularMask;
                //fixed3 specular=_LightColor0.rgb*_Specular.rgb*pow(max(0,dot(tangentNormal,halfDir)),_Gloss);

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

By using high-order light texture mask following effects.

It allows us to be more fine-grained control lighting details, to get better results.

 

 

Published 76 original articles · won praise 43 · views 40000 +

Guess you like

Origin blog.csdn.net/hnzmdlhc/article/details/104432518