赞
踩
- Shader "Unity Shaders Book/Chapter 7/Normal Map In Tangent Space"
- {
- Properties
- {
- _Color ("Color Tint", Color) = (1,1,1,1)
- _MainTex("Main Tex", 2D) = "White" {}
- //法线纹理,bump是内置法线纹理
- _BumpMap ("Normal Map", 2D) = "bump" {}
- //控制凹凸程度,为0时则不会对光照产生影响
- _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;
- //存储切线信息,tangent.w分量决定副切线的方向性,所以是float4类型
- 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存储法线贴图的纹理坐标
- 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(normalze(v.normal), normalize(v.tangent.xyz)) * v.tangent.w;
- //float3 rotation = float3x3(v.tangent.xyz, binormal, v.normal);
- //或者可直接使用Unity内置的宏
- 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);
-
- //利用tex2D对法线纹理_BumpMap进行采样
- fixed4 packedNormal = tex2D(_BumpMap, i.uv.zw);
-
- fixed3 tangentNormal;
- //如果法线纹理的类型没有被设置成“Normal map”,则需要在代码中手动进行操作
- //将法线纹理中存储的像素值反映射回来
- //tangentNormal.xy = (packedNormal.xy * 2 -1) * _BumpScale;
- //tangentNormal.z = sqrt(1.0 - saturate(dot(tangentNormal.xy,tangentNormal.xy)));
-
- //如果法线纹理的类型已经被设置成“Normal map”,使用上述方法就会出错,此时使用内置函数UnpackNormal
- tangentNormal = UnpackNormal(packedNormal);
- 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));
- //Blinn-Phong光照
- 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);
-
- }
- ENDCG
- }
- }
- Fallback"Specular"
- }
- Shader "Unity Shaders Book/Chapter 7/Normal Map In World Space"
- {
- Properties
- {
- _Color ("Color Tint", Color) = (1,1,1,1)
- _MainTex("Main Tex", 2D) = "White" {}
- //法线纹理,bump是内置法线纹理
- _BumpMap ("Normal Map", 2D) = "bump" {}
- //控制凹凸程度,为0时则不会对光照产生影响
- _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;
- //存储切线信息,tangent.w分量决定副切线的方向性,所以是float4类型
- float4 tangent : TANGENT;
- float4 texcoord: TEXCOORD0;
- };
-
-
- //v2f需要包含从切线空间到世界空间的变换矩阵
- struct v2f
- {
- float4 pos : SV_POSITION;
- float4 uv : TEXCOORD0;
- //一个插值寄存器最多只能存储float4大小的变量,可以将矩阵按行拆成多个变量进行存储
- //TtoW0、TtoW1、TtoW2依次存储了从切线空间到世界空间的变换矩阵的每一行
- //其中float4中的w分量存储的是世界空间下的顶点位置
- float4 TtoW0 : TEXCOORD1;
- float4 TtoW1 : TEXCOORD2;
- float4 TtoW2 : TEXCOORD3;
- };
-
- //计算从切线空间到世界空间的变换矩阵
- v2f vert (a2v v)
- {
- v2f o;
- o.pos = UnityObjectToClipPos(v.vertex);
- //uv的xy存储主贴图的纹理坐标,zw存储法线贴图的纹理坐标
- 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(_Object2World, v.vertex).xyz;
- fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
- fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
- fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
-
-
- //把上述矩阵按行存储在TtoW0、TtoW1、TtoW2中,顶点位置存储在w分量中
- 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
- {
- //从TtoW0、TtoW1、TtoW2的w分量中构建世界空间下的坐标
- float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);
-
- //得到世界空间下的光照和视角方向
- fixed3 lightDir =normalize( UnityWorldSpaceLightDir(worldPos));
- fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));
-
- //对法线纹理进行采样、解码、缩放
- fixed3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw));
- bump.xy *= _BumpScale;
- bump.z = sqrt(1.0 - saturate(dot(bump.xy, bump.xy)));
- //使用变换矩阵将法线变换到世界空间下
- 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));
- //Blinn-Phong光照
- 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);
-
- }
- ENDCG
- }
- }
- Fallback"Specular"
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。