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);
            }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值