shader篇-紋理-凹凸映射

shader篇-紋理-凹凸映射


簡介

紋理的一種常見運動是凹凸映射,使用一種紋理改變模型表面的法線,這種方法並不會真的改變模型的頂點位置,只是讓模型看起來凹凸不平。

有2種主要的方法可以用來進行凹凸映射:
一種方法是使用一張高度紋理來模擬表面位移,然後得到一個修改後的法線值,這種方法也被稱爲高度映射
另一種方法是使用一張法線紋理來直接存儲表面法線,這種方法被稱爲法線映射。

高度紋理

高度紋理使用一張高度圖,高度圖存儲的是強度值,表面模型的海拔高度,顏色越淺表面位置越向外凸起,顏色越深越向裏凹,這種方法的好處是非常直觀,可以完全從高度圖裏瞭解到模型表面的凹凸情況。缺點是計算複雜,實時計算中需要間接從像素灰度值計算表面法線,消耗更多性能。
高度圖通常和法線映射一起使用,用以給出表面凹凸的額外信息,也就是說我們會通常用法線映射修改光照。

法線紋理

法線紋理存儲的是表面的法線方向。由於大學方向的分量在[-1,1],而像素的分量爲[0,1],因此我們需要做一個映射:

pixel=normal+12

這就要求我們在shader中對紋理採樣後進行一次反映射得到法線方向。

有一種直接的想法是將修改後的模型空間的表面法線存儲在一張紋理中,這種紋理被稱爲是模型空間的法線紋理。
但實際製作過程中我們我們會失憶模型空間的切線空間來存儲法線。這種紋理被稱爲切線空間的法線紋理。實際上模型空間法線紋理更直觀,但因爲美術人員更喜歡切線空間的法線紋理。。、

優缺點比較

模型空間存儲法線優點:
1.程序人員實現它簡單,更加直觀
2.紋理座標的縫合處和尖銳邊角處,突變少,更平滑。
切線空間存儲法線優點:
1.自由度高,可運用到不同的模型
2.可進行Uv動畫
3.可重用法線紋理
4.可壓縮

切線空間下進行計算

基本思路是在片元着色器中通過紋理採樣得到切線空間下的法線,然後再與切線空間下的視角方向、光照方向等進行計算


Properties {
        _Color ("Color Tint", Color) = (1, 1, 1, 1)
        _MainTex ("Main Tex", 2D) = "white" {}
        _BumpMap ("Normal Map", 2D) = "bump" {}
        _BumpScale ("Bump Scale", Float) = 1.0
        _Specular ("Specular", Color) = (1, 1, 1, 1)
        _Gloss ("Gloss", Range(8.0, 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;
            float4 _BumpMap_ST;
            float _BumpScale;
            fixed4 _Specular;
            float _Gloss;

            struct a2v {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                //target爲切線方向
                float4 tangent : TANGENT;
                float4 texcoord : TEXCOORD0;
            };

            struct v2f {
                float4 pos : SV_POSITION;
                float4 uv : TEXCOORD0;
                float3 lightDir: TEXCOORD1;
                float3 viewDir : TEXCOORD2;
            };
v2f vert(a2v v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                //uv變量的xy分量和zw分量分別存儲2張貼圖的紋理座標
                o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
                o.uv.zw = v.texcoord.xy * _BumpMap_ST.xy + _BumpMap_ST.zw;
                //計算副切線
                float3 binormal = cross( normalize(v.normal), normalize(v.tangent.xyz) ) * v.tangent.w;
                //獲取模型空間到切線空間的變換矩陣
                float3x3 rotation = float3x3(v.tangent.xyz, binormal, v.normal);
                //TANGENT_SPACE_ROTATION;
                //也可以使用TANGENT_SPACE_ROTATION;來幫助我們計算rotation矩陣,她來自UnityCG.cginc

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

                o.viewDir = mul(rotation, normalize(ObjSpaceViewDir(v.vertex))).xyz;
                return o;
            }
fixed4 frag(v2f i) : SV_Target {                
                fixed3 tangentLightDir = normalize(i.lightDir);
                fixed3 tangentViewDir = normalize(i.viewDir);


                fixed4 packedNormal = tex2D(_BumpMap, i.uv.zw);
                fixed3 tangentNormal;
                // If the texture is not marked as "Normal map"
//              tangentNormal.xy = (packedNormal.xy * 2 - 1) * _BumpScale;
//              tangentNormal.z = sqrt(1.0 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));

                // Or mark the texture as "Normal map", and use the built-in funciton
                tangentNormal = UnpackNormal(packedNormal);
                //_BumpScale控制凹凸程度
                tangentNormal.xy *= _BumpScale;
                tangentNormal.z = sqrt(1.0 - 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);
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(tangentNormal, halfDir)), _Gloss);

                return fixed4(ambient + diffuse + specular, 1.0);
            }

在世界空間下進行計算

基本思路是在頂點着色器上計算切線空間到世界空間的變換矩陣。

//Ttow0,1,2存儲切線空間到世界空間的變換矩陣
//多餘的w分量用以存儲世界空間下的頂點位置
struct v2f {
                float4 pos : SV_POSITION;
                float4 uv : TEXCOORD0;
                float4 TtoW0 : TEXCOORD1;  
                float4 TtoW1 : TEXCOORD2;  
                float4 TtoW2 : TEXCOORD3; 
            };
v2f vert(a2v v) {
                v2f o;
                o.pos = UnityObjectToClipPos(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;

                float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;  
                fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);  
                fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);  
                fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w; 

                // Compute the matrix that transform directions from tangent space to world space
                // Put the world position in w component for optimization
                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 {
                // Get the position in world space      
                float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);
                // Compute the light and view dir in world space
                fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));
                fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));

                // Get the normal in tangent space
                fixed3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw));
                bump.xy *= _BumpScale;
                bump.z = sqrt(1.0 - saturate(dot(bump.xy, bump.xy)));
                // Transform the narmal from tangent space to world space
                bump = normalize(half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump)));

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

                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;

                fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(bump, lightDir));

                fixed3 halfDir = normalize(lightDir + viewDir);
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(bump, halfDir)), _Gloss);

                return fixed4(ambient + diffuse + specular, 1.0);
            }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章