Unity Shader 法线贴图(NormalMap)

normalmap

  • 存储物体表面法线的纹理

作用

  • 计算表面光照信息
  • 提高渲染效率
    • 可以在平面上模拟出非平面的效果,不需要很多mesh数据

分类

  • 世界空间法线
    • 彩色,因为法线会指向各个方向
  • 切线空间法线
    • 淡蓝色,切线空间,法线向外

无法线贴图

Unity Shader 法线贴图(NormalMap)_第1张图片

有法线贴图

Unity Shader 法线贴图(NormalMap)_第2张图片

Material面板

Unity Shader 法线贴图(NormalMap)_第3张图片

shader

Shader "DC/Shader/ShaderDemo/NormalMap"
{
    Properties
    {
        [Toggle] _UseNormalMap("UseNormalMap",float) = 0
        _MainTex ("Texture", 2D) = "white" {}
        _NormalMap ("NormalMap", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma shader_feature _USENORMALMAP_ON

            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float4 uv : TEXCOORD0;
                float4 tangent : TEXCOORD1;
                float4 normal : TEXCOORD2;
            };

            struct v2f
            {
                float4 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                float3 lightDir : TEXCOORD1;
                float4 TtoW0 : TEXCOORD2;
                float4 TtoW1 : TEXCOORD3;
                float4 TtoW2 : TEXCOORD4;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            sampler2D _NormalMap;
            float4 _NormalMap_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
                o.uv.zw = TRANSFORM_TEX(v.uv, _NormalMap);
                o.lightDir = WorldSpaceLightDir(v.vertex);
                float3 worldNormal = UnityObjectToWorldNormal(v.normal);
                float3 worldTangent = UnityObjectToWorldDir(v.tangent);
                float3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
                float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;

                //tbnp
                o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
                o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
                o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv.xy);
                #if _USENORMALMAP_ON
                    fixed3 normal = UnpackNormal(tex2D(_NormalMap, i.uv.zw));
                    normal = normalize(half3(dot(i.TtoW0, normal), dot(i.TtoW1, normal), dot(i.TtoW2, normal)));
                    normal.z = sqrt(1.0 - saturate(dot(normal.xy, normal.xy)));
                    // return fixed4(normal,1);
                    fixed3 finalCol = dot(normal.xyz, normalize(i.lightDir.xyz)) * col.rgb * _LightColor0.rgb;
                    return fixed4(finalCol, col.a);
                #else
                    // return col;
                    return fixed4(col.rgb * _LightColor0.rgb, col.a);
                #endif
            }
            ENDCG
        }
    }
}

你可能感兴趣的:(unity,shader)