1 | uniform float textureRepeat = 6;
|
---|
2 | uniform sampler2D ColorMapSampler : register(s0);
|
---|
3 | uniform sampler2D DetailMapSampler : register(s1);
|
---|
4 | uniform sampler2D BumpMapSampler : register(s2);
|
---|
5 | uniform float4x4 WorldViewProj;
|
---|
6 | uniform float4x4 World;
|
---|
7 | uniform float4x4 WorldI;
|
---|
8 | uniform float4 wLightPos;
|
---|
9 | uniform float3 wCamPos;
|
---|
10 |
|
---|
11 |
|
---|
12 | float3x3 TransfModelToTangent( in float3 Tangent, in float3 Binormal, in float3 Normal ) {
|
---|
13 | float T2 = dot(Tangent, Tangent);
|
---|
14 | float B2 = dot(Binormal, Binormal);
|
---|
15 | float N2 = dot(Normal, Normal);
|
---|
16 | float BT = dot(Binormal, Tangent);
|
---|
17 | float det = B2 * T2 - BT * BT;
|
---|
18 |
|
---|
19 | return float3x3( (B2 * Tangent - BT * Binormal)/det,
|
---|
20 | (T2 * Binormal - BT * Tangent)/det,
|
---|
21 | Normal/N2 );
|
---|
22 | /*
|
---|
23 | // simplified solution
|
---|
24 | return float3x3(Tangent/T2, Binormal/B2, Normal/N2);
|
---|
25 | */
|
---|
26 | }
|
---|
27 |
|
---|
28 | float4 Illumination(float3 Light, float3 Normal, float3 View, float2 TexCoord, float2 TexCoord2)
|
---|
29 | {
|
---|
30 | // Blinn lighting
|
---|
31 | float3 Half = normalize(Light + View);
|
---|
32 | float Diffuse = dot(Normal, Light);
|
---|
33 | float Specular = dot(Normal, Half);
|
---|
34 | float4 Lighting = lit(Diffuse, Specular, 16);
|
---|
35 | float4 Color= tex2D(ColorMapSampler, TexCoord) * tex2D(DetailMapSampler, TexCoord2);
|
---|
36 |
|
---|
37 | return (Lighting.y + 0.1) * Color;
|
---|
38 | }
|
---|
39 |
|
---|
40 | struct VS_INPUT {
|
---|
41 | float4 Position : POSITION; // point in modeling space
|
---|
42 | float2 TexCoord : TEXCOORD0; // texture coordinates
|
---|
43 | float2 TexCoord2 : TEXCOORD1; // texture coordinates
|
---|
44 | float3 Tangent : TEXCOORD2; // model space tangent vector
|
---|
45 | float3 Normal : NORMAL; // model space triangle normal vector
|
---|
46 | };
|
---|
47 |
|
---|
48 | struct VS_OUTPUT {
|
---|
49 | float4 hPosition : POSITION; // point in normalized device space before homogeneous division
|
---|
50 | float2 TexCoord : TEXCOORD0; // texture coordinates
|
---|
51 | float2 TexCoord2 : TEXCOORD6; // texture coordinates
|
---|
52 | float3 mView : TEXCOORD1; // model space view vector
|
---|
53 | float3 mLight : TEXCOORD2; // model space light vector
|
---|
54 | float3 wTangent : TEXCOORD3; // model space tangent vector
|
---|
55 | float3 wBinormal : TEXCOORD4; // model space binormal vector
|
---|
56 | float3 wNormal : TEXCOORD5; // model space triangle normal vector
|
---|
57 | };
|
---|
58 |
|
---|
59 |
|
---|
60 |
|
---|
61 |
|
---|
62 | //------------------------------------------------------------------------------------
|
---|
63 | //
|
---|
64 | // Base vertex shader: vertex shader for all methods: calculates tangent-space
|
---|
65 | // tLight, tView, hPosition vectors
|
---|
66 | //
|
---|
67 | //------------------------------------------------------------------------------------
|
---|
68 | VS_OUTPUT BaseVS(VS_INPUT IN)
|
---|
69 | {
|
---|
70 | VS_OUTPUT OUT;
|
---|
71 |
|
---|
72 | float3 wPos = mul(World, IN.Position);
|
---|
73 | // world-space view vector
|
---|
74 | OUT.mView = wCamPos - wPos;
|
---|
75 | // world-space light vector
|
---|
76 | OUT.mLight = wLightPos.xyz - wPos * wLightPos.w;
|
---|
77 |
|
---|
78 | OUT.wTangent = normalize(mul(float4(IN.Tangent, 1),WorldI));
|
---|
79 | OUT.wNormal = normalize(mul(float4(IN.Normal, 1),WorldI));
|
---|
80 | OUT.wBinormal = cross(OUT.wTangent, OUT.wNormal);
|
---|
81 |
|
---|
82 | // vertex position before homogenious division
|
---|
83 | OUT.hPosition = mul(WorldViewProj, IN.Position);
|
---|
84 | // tex coordinates passed to pixel shader
|
---|
85 | OUT.TexCoord = textureRepeat * IN.TexCoord;
|
---|
86 | OUT.TexCoord2 = IN.TexCoord2;
|
---|
87 |
|
---|
88 | return OUT;
|
---|
89 | }
|
---|
90 |
|
---|
91 | float4 BumpPS(VS_OUTPUT IN) : COLOR
|
---|
92 | {
|
---|
93 | //return tex2D(BumpMapSampler,IN.TexCoord).a;
|
---|
94 | IN.wTangent = normalize(IN.wTangent);
|
---|
95 | IN.wBinormal = normalize(IN.wBinormal);
|
---|
96 | IN.wNormal = normalize(IN.wNormal);
|
---|
97 | // needs normalization because of linear interpolation
|
---|
98 | float3 mLight = normalize( IN.mLight );
|
---|
99 | float3 mView = normalize( IN.mView );
|
---|
100 |
|
---|
101 | // get model space normal vector
|
---|
102 | float3x3 ModelToTangent = TransfModelToTangent(IN.wTangent, IN.wBinormal, IN.wNormal );
|
---|
103 | // get model space normal vector
|
---|
104 | float3 tNormal = tex2D(BumpMapSampler, IN.TexCoord).rgb;
|
---|
105 | // Normal vector should be transformed with the inverse transpose of TangentToModel
|
---|
106 | // which is the transpose of ModelToTangent
|
---|
107 | float3 mNormal = normalize( mul( tNormal, ModelToTangent ) );
|
---|
108 | // illumination calculation
|
---|
109 | return Illumination(mLight, mNormal, mView, IN.TexCoord, IN.TexCoord2);
|
---|
110 |
|
---|
111 |
|
---|
112 | } |
---|