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