1
+ Shader "Normal Map Diffuse"
2
+ {
3
+ Properties
4
+ {
5
+ _MainTex ( "Diffuse Map" , 2D ) = "white" {}
6
+ _NormalMap ( "Normal Map" , 2D ) = "bump" {}
7
+ }
8
+ Subshader
9
+ {
10
+ Tags { "RenderType" ="Opaque" }
11
+ Pass
12
+ {
13
+ Tags { "LightMode" = "ForwardBase" }
14
+ CGPROGRAM
15
+ #pragma vertex vertex_shader
16
+ #pragma fragment pixel_shader
17
+ #pragma target 3.0
18
+
19
+ struct APPDATA
20
+ {
21
+ float4 vertex : POSITION ;
22
+ float3 normal : NORMAL ;
23
+ float4 tangent : TANGENT ;
24
+ float2 uv : TEXCOORD0 ;
25
+ };
26
+
27
+ struct SHADERDATA
28
+ {
29
+ float4 position : SV_POSITION ;
30
+ float2 uv : TEXCOORD0 ;
31
+ float4 vertex : TEXCOORD1 ;
32
+ float3 normal : TEXCOORD2 ;
33
+ float3 tangent : TEXCOORD3 ;
34
+ float3 binormal : TEXCOORD4 ;
35
+ };
36
+
37
+ float4 _LightColor0 , _MainTex_ST, _NormalMap_ST ;
38
+ sampler2D _MainTex, _NormalMap;
39
+
40
+ SHADERDATA vertex_shader (APPDATA input)
41
+ {
42
+ SHADERDATA vs;
43
+ vs.position = UnityObjectToClipPos (input.vertex);
44
+ vs.uv = input.uv;
45
+ vs.vertex = mul ( unity_ObjectToWorld , input.vertex );
46
+ vs.normal = normalize (mul ((float3x3 )unity_ObjectToWorld ,input.normal));
47
+ vs.tangent = normalize (mul ((float3x3 )unity_ObjectToWorld ,input.tangent.xyz));
48
+ vs.binormal = normalize (cross (vs.normal,vs.tangent)*input.tangent.w);
49
+ return vs;
50
+ }
51
+
52
+ float4 pixel_shader (SHADERDATA ps) : COLOR
53
+ {
54
+ float4 color = tex2D ( _MainTex, ps.uv * _MainTex_ST.xy + _MainTex_ST.zw );
55
+ float4 packednormal = tex2D ( _NormalMap, ps.uv * _NormalMap_ST.xy + _NormalMap_ST.zw );
56
+ float3 normal = float3 (2.0 *packednormal.ag-1.0 ,0.0 ); //convert from 0..1 range to -1..1 range
57
+ normal.z = sqrt (1.0 - dot (normal, normal)); //reconstruct depth
58
+ float3x3 tbn = float3x3 (ps.tangent,ps.binormal,ps.normal); //generate matrix
59
+ float3 AmbientLight = UNITY_LIGHTMODEL_AMBIENT ;
60
+ float3 LightDirection = normalize (_WorldSpaceLightPos0 .xyz);
61
+ float3 LightColor = _LightColor0 .rgb;
62
+ float3 NormalDirection = normalize (mul (normal,tbn)); //transform from Tangent Space to World Space
63
+ return float4 ((max (dot (LightDirection, NormalDirection),0.0 )*LightColor+AmbientLight)*color.rgb,color.a);
64
+ }
65
+ ENDCG
66
+ }
67
+ }
68
+ }
0 commit comments