UnityShader 学习笔记 3 凹凸映射

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

原图:
在这里插入图片描述
在这里插入图片描述
效果图:
在这里插入图片描述
在这里插入图片描述

  • 切线空间下的凹凸映射:


Shader "_MyShader/4_Texture/2_BumpMap/TangentSpace"

{

    Properties

    {

        _Color ("Color",COLOR) = (1,1,1,1)

            _MainTex ("MainTex",2D) = "white" {}

            _BumpMap ("NormalMap",2D) = "bump" {}

            _BumpScale ("BumpScale",float) = 1.0

            _SpecularRange ("SpecularRange",Range(10,250)) = 20

    }

    SubShader

    {

        Pass

        {

            Tags{"LightMode" = "ForwardBase"}

            CGPROGRAM

            #pragma vertex vert

            #pragma fragment frag



            #include "Lighting.cginc"

            #include "UnityCG.cginc"



            fixed4 _Color;

            sampler2D _MainTex;

            float4 _MainTex_ST;

            sampler2D _BumpMap;

            float4 _BumpMap_ST;

            float _BumpScale;

            float _SpecularRange;





            struct a2v {

                float4 vertex:POSITION;

                float4 tangent:TANGENT;

                float4 texcoord:TEXCOORD0;

                float3 normal:NORMAL;

            };



            struct v2f{

                float4 pos:SV_POSITION;

                float4 uv:TEXCOORD0;

                float3 tangentLightDir:TEXCOORD1;

                float3 tangentViewDir:TEXCOORD2;

                float3 blinn_Phong_Dir:TEXCOORD3;

            };



            v2f vert(a2v v){

                v2f o;

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



                //o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;

                //o.uv.zw = v.texcoord.xy * _BumpMap_ST.xy + _BumpMap_ST.zw;

                o.uv.xy = TRANSFORM_TEX(v.texcoord,_MainTex);

                o.uv.zw = TRANSFORM_TEX(v.texcoord,_BumpMap);



                TANGENT_SPACE_ROTATION;

                o.tangentLightDir = normalize(mul(rotation,ObjSpaceLightDir(v.vertex)));

                o.tangentViewDir = normalize(mul(rotation,ObjSpaceViewDir(v.vertex)));



                //Blinn-Phong

                o.blinn_Phong_Dir = normalize(o.tangentLightDir + o.tangentViewDir);



                return o;

            }



            fixed4 frag(v2f i):SV_Target{

                //UnpackNormal,UnpackNormalDXT5nm 得到正确的法线方向

                fixed3 tangentNormal = UnpackNormalDXT5nm(tex2D(_BumpMap,i.uv.zw));

                tangentNormal *= _BumpScale;



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

                fixed3 ambientColor = UNITY_LIGHTMODEL_AMBIENT.xyz * uvTex;



                fixed3 diffuseColor =    _LightColor0 * uvTex * saturate(dot(tangentNormal,i.tangentLightDir));



                fixed3 SpecularColor =    _LightColor0 * pow(saturate(dot(tangentNormal,i.blinn_Phong_Dir)),_SpecularRange);



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



                return col;

            }





            ENDCG

        }

    }

    FallBack "Specular"

}




  • 世界空间下的凹凸映射


Shader "_MyShader/4_Texture/2_BumpMap/WorldSpace"

{

    Properties

    {

        _Color ("Color",COLOR) = (1,1,1,1)

            _MainTex ("MainTex",2D) = "white" {}

            _BumpMap ("NormalMap",2D) = "bump" {}

            _BumpScale ("BumpScale",float) = 1.0

            _SpecularRange ("SpecularRange",Range(10,250)) = 20

    }

    SubShader

    {

        Pass

        {

            Tags{"LightMode" = "ForwardBase"}

            CGPROGRAM

            #pragma vertex vert

            #pragma fragment frag



            #include "Lighting.cginc"

            #include "UnityCG.cginc"



            fixed4 _Color;

            sampler2D _MainTex;

            float4 _MainTex_ST;

            sampler2D _BumpMap;

            float4 _BumpMap_ST;

            float _BumpScale;

            float _SpecularRange;





            struct a2v {

                float4 vertex:POSITION;

                float4 tangent:TANGENT;

                float4 texcoord:TEXCOORD0;

                float3 normal:NORMAL;

            };



            struct v2f{

                float4 pos:SV_POSITION;

                float4 uv:TEXCOORD0;

                float3 worldLightDir:TEXCOORD1;

                float3 blinn_Phong_Dir:TEXCOORD2;

                float3 TtoW0:TEXCOORD3;

                float3 TtoW1:TEXCOORD4;

                float3 TtoW2:TEXCOORD5;

            };



            v2f vert(a2v v){

                v2f o;

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



                //o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;

                //o.uv.zw = v.texcoord.xy * _BumpMap_ST.xy + _BumpMap_ST.zw;

                o.uv.xy = TRANSFORM_TEX(v.texcoord,_MainTex);

                o.uv.zw = TRANSFORM_TEX(v.texcoord,_BumpMap);



                o.worldLightDir = normalize(WorldSpaceLightDir(v.vertex));

                float3 worldViewDir = normalize(WorldSpaceViewDir(v.vertex));

                float3 worldNormal = UnityObjectToWorldNormal(v.normal);



                float3 worldTangent = UnityObjectToWorldDir(v.tangent);



                float3 binormal = cross( normalize(v.normal), normalize(v.tangent.xyz) ) * v.tangent.w;



                //Blinn-Phong

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



                o.TtoW0 = float3(worldTangent.x,binormal.x,worldNormal.x);

                o.TtoW1 = float3(worldTangent.y,binormal.y,worldNormal.y);

                o.TtoW2 = float3(worldTangent.z,binormal.z,worldNormal.z);



                return o;

            }



            fixed4 frag(v2f i):SV_Target{

                //UnpackNormal,UnpackNormalDXT5nm 得到正确的法线方向



                //fixed3 worldNormal = UnpackNormalDXT5nm(tex2D(_BumpMap,i.uv.zw));

                //worldNormal *= _BumpScale;

                fixed3 bump = UnpackNormal(tex2D(_BumpMap,i.uv.zw));

                bump.xy *= _BumpScale;

                bump.z = sqrt(1 - saturate(dot(bump.xy, bump.xy)));



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

                fixed3 ambientColor = UNITY_LIGHTMODEL_AMBIENT.xyz * uvTex;



                fixed3 diffuseColor =    _LightColor0 * uvTex * saturate(dot(bump,i.worldLightDir));



                fixed3 SpecularColor =    _LightColor0 * pow(saturate(dot(bump,i.blinn_Phong_Dir)),_SpecularRange);



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



                return col;

            }





            ENDCG

        }

    }

    FallBack "Specular"

}




猜你喜欢

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