1 | float4x4 world_IT; |
---|
2 | |
---|
3 | float3x3 TransfModelToTangent( in half3 Tangent, in half3 Binormal, in half3 Normal ) { |
---|
4 | half T2 = dot(Tangent, Tangent);
|
---|
5 | half B2 = dot(Binormal, Binormal);
|
---|
6 | half N2 = dot(Normal, Normal);
|
---|
7 | half BT = dot(Binormal, Tangent);
|
---|
8 | half det = B2 * T2 - BT * BT;
|
---|
9 |
|
---|
10 | return float3x3( (B2 * Tangent - BT * Binormal)/det,
|
---|
11 | (T2 * Binormal - BT * Tangent)/det,
|
---|
12 | Normal/N2 );
|
---|
13 | /*
|
---|
14 | // simplified solution |
---|
15 | return float3x3(Tangent/T2, Binormal/B2, Normal/N2); |
---|
16 | */ |
---|
17 | }
|
---|
18 |
|
---|
19 | struct VertOut
|
---|
20 | {
|
---|
21 | half4 hPosition :POSITION;
|
---|
22 | half2 texCoord :TEXCOORD;
|
---|
23 | half3 wPosition :TEXCOORD1;
|
---|
24 | half3 Normal :TEXCOORD2;
|
---|
25 | half3 Tangent : TEXCOORD3; // model space tangent vector |
---|
26 | half3 Binormal : TEXCOORD4; // model space binormal vector
|
---|
27 | };
|
---|
28 |
|
---|
29 |
|
---|
30 | VertOut deaultVS(half4 position : POSITION,
|
---|
31 | half3 normal:NORMAL,
|
---|
32 | half3 Tangent : TEXCOORD1,
|
---|
33 | half2 texCoord : TEXCOORD0,
|
---|
34 | uniform float4x4 worldViewProj,
|
---|
35 | uniform float4x4 world)
|
---|
36 | {
|
---|
37 | VertOut OUT;
|
---|
38 | OUT.texCoord = texCoord;
|
---|
39 | OUT.hPosition = mul(worldViewProj, position);
|
---|
40 | OUT.wPosition = mul(world, position).xyz;
|
---|
41 | OUT.Normal = normal;
|
---|
42 | OUT.Tangent = Tangent;
|
---|
43 | OUT.Binormal = cross(Tangent, normal);
|
---|
44 | return OUT;
|
---|
45 | }
|
---|
46 |
|
---|
47 | half4 deaultPS(VertOut IN,
|
---|
48 | uniform sampler2D Texture : register(s0),
|
---|
49 | uniform sampler2D NormalMap : register(s1),
|
---|
50 | uniform half3 lightPosition,
|
---|
51 | uniform half3 cameraPos
|
---|
52 | ):COLOR
|
---|
53 | {
|
---|
54 | half4 Color = half4(1,1,1,1);
|
---|
55 | ///get normal
|
---|
56 | float3x3 ModelToTangent = float3x3(IN.Tangent, IN.Binormal, IN.Normal);
|
---|
57 | //float3x3 ModelToTangent = TransfModelToTangent(IN.Tangent, IN.Binormal, IN.Normal ); |
---|
58 | half3 tNormal = tex2D(NormalMap, IN.texCoord).rgb; |
---|
59 |
|
---|
60 |
|
---|
61 | half3 wNormal =mul(tNormal, ModelToTangent );
|
---|
62 | //wNormal = IN.Normal;
|
---|
63 | wNormal = normalize(mul( wNormal, world_IT ));
|
---|
64 | ///shading
|
---|
65 | half3 V = normalize(IN.wPosition - cameraPos);
|
---|
66 | half3 light = (lightPosition - IN.wPosition) / 200.0;
|
---|
67 | half3 L = normalize(light);
|
---|
68 | half3 H = normalize(L+V);
|
---|
69 | half4 lighting = lit(dot(wNormal, L),dot(wNormal, H), 80);
|
---|
70 |
|
---|
71 | half4 ambient = float4(0.1, 0.1, 0.1, 1.0);
|
---|
72 |
|
---|
73 | Color = tex2D(Texture, IN.texCoord) * (lighting.y /*+ ambient*/) + lighting.z * half4(0.3,0.3,0.3,1);
|
---|
74 |
|
---|
75 | return Color;
|
---|
76 | } |
---|