凹凸纹理
效果过程代码
效果
过程
使法线纹理上存储的信息,将相关变量转到切线空间进行统一计算。 漫反射由法线方向和光源方向求得, 高光由半方向和法线方向求得, 半方向由光源方向和法线求得, 环境光可以由系统内置变量拿到,
代码
Shader
"Custom/NorMalTangentSpaceMat" {
Properties
{
_Color
( "Color", Color
) = (1,1,1,1)
_MainTex
("Main Tex", 2D
) = "white" {}
_BumpMap( "Normal Map", 2D
) = "bump" {}
_BumpScale("Bump Scale", Float
) = 1
_Specular
("Specular", Color
) = (1,1,1,1)
_Gloss
("Gloss", Range(2,256)) = 20
}
SubShader
{
Pass
{
Tags
{ "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
fixed4 _Color
;
sampler2D _MainTex
;
sampler2D _BumpMap
;
float _BumpScale
;
fixed4 _Specular
;
float _Gloss
;
float4 _MainTex_ST
;
float4 _BumpMap_ST
;
struct a2v
{
float4 vertex
: POSITION
;
float3 normal
:NORMAL
;
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
);
o
.uv
.xy
= v
.texcoord
.xy
* _MainTex_ST
.xy
+ _MainTex_ST
.zw
;
o
.uv
.zw
= v
.texcoord
.xy
* _BumpMap_ST
.xy
+ _BumpMap_ST
.zw
;
TANGENT_SPACE_ROTATION
;
o
.lightDir
= mul( rotation
, ObjSpaceLightDir(v
.vertex
)).xyz
;
o
.viewDir
= mul
( rotation
, ObjSpaceViewDir(v
.vertex
)).xyz
;
return o
;
};
fixed4 frag
(v2f f
):SV_Target
{
fixed3 tangentLightDir
= normalize
( f
.lightDir
);
fixed3 tangentViewDir
= normalize
(f
.viewDir
);
fixed4 packedNormal
= tex2D
( _BumpMap
, f
.uv
.zw
);
fixed3 tangentNormal
;
tangentNormal
= UnpackNormal
(packedNormal
);
tangentNormal
.xy
*= _BumpScale
;
tangentNormal
.z
= sqrt
( 1.0- saturate
(dot
(tangentNormal
.xy
, tangentNormal
.xy
)) );
fixed3 albedo
= tex2D
( _MainTex
, f
.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( specular
+ diffuse
+ ambient
, 1);
};
ENDCG
}
}
FallBack
"Diffuse"
}
转载请注明原文地址:https://tech.qufami.com/read-5877.html