source: GTP/trunk/App/Demos/Illum/Ogre/Media/MORIA/bump.hlsl @ 2345

Revision 2345, 5.1 KB checked in by szirmay, 18 years ago (diff)
Line 
1uniform sampler2D ColorMapSampler : register(s0);
2uniform sampler2D DetailMapSampler   : register(s1);
3uniform sampler2D BumpMapSampler  : register(s2);
4uniform float4x4 WorldViewProj;
5uniform float4x4 World;
6uniform float4x4 WorldI;
7uniform float4 wLightPos;
8uniform float3 wLightDir;
9uniform float3 wCamPos;
10uniform float4 lightRange;
11uniform float lightPower;
12#define SpotLightFalloff 1.0
13
14
15float3x3 TransfModelToTangent( in  float3 Tangent, in float3 Binormal, in float3 Normal ) {
16        float T2 = dot(Tangent, Tangent);
17        float B2 = dot(Binormal, Binormal);
18        float N2 = dot(Normal, Normal);
19        float BT = dot(Binormal, Tangent);
20        float det = B2 * T2 - BT * BT;
21
22        return float3x3( (B2 * Tangent - BT * Binormal)/det,
23                         (T2 * Binormal - BT * Tangent)/det,
24                         Normal/N2 );
25/*                                                     
26        // simplified solution
27        return float3x3(Tangent/T2, Binormal/B2, Normal/N2);
28*/
29}
30
31float4 Illumination(float3 Light, float3 Normal, float3 View, float2 tileTexCoord, float2 detailTexCoord2)
32{
33        // Blinn lighting
34        float d = length(Light);
35        Light = normalize(Light);
36        float3 Half = normalize(Light + View);
37        float4 Lighting = lit(dot(Normal, Light), dot(Normal, Half), 120);
38        float4 Color= tex2D(ColorMapSampler, tileTexCoord) * tex2D(DetailMapSampler, detailTexCoord2);
39
40        return (Lighting.y * Color + Lighting.z * 0.5) * (d < lightRange.x) / (lightRange.y + d * lightRange.z + d * d * lightRange.w) * lightPower;
41}
42
43struct VS_INPUT {
44    float4 Position  : POSITION;     // point in modeling space
45    float2 TexCoord  : TEXCOORD0;    // texture coordinates
46    float2 TexCoord2 : TEXCOORD1;    // texture coordinates
47    float2 TexCoord3 : TEXCOORD2;    // texture coordinates
48    float3 Tangent   : TEXCOORD3;        // model space tangent vector
49    float3 Normal    : NORMAL;           // model space triangle normal vector
50};
51
52struct VS_OUTPUT {
53    float4 hPosition : POSITION;     // point in normalized device space before homogeneous division
54    float2 TexCoord  : TEXCOORD0;    // texture coordinates
55    float2 TexCoord2  : TEXCOORD6;    // texture coordinates
56    float2 TexNormalCoord  : TEXCOORD7;    // texture coordinates
57    float3 mView     : TEXCOORD1;    // model space view vector
58    float3 mLight    : TEXCOORD2;    // model space light vector
59    float3 wTangent   : TEXCOORD3;       // model space tangent vector
60    float3 wBinormal  : TEXCOORD4;       // model space binormal vector
61    float3 wNormal    : TEXCOORD5;       // model space triangle normal vector
62};
63
64
65
66
67//------------------------------------------------------------------------------------
68//
69// Base vertex shader: vertex shader for all methods: calculates tangent-space
70//              tLight, tView, hPosition vectors
71//
72//------------------------------------------------------------------------------------
73VS_OUTPUT BaseVS(VS_INPUT IN, uniform float normalCoord)
74{
75        VS_OUTPUT OUT;
76
77        float3 wPos = mul(World, IN.Position).xyz;
78                // world-space view vector       
79                OUT.mView = wCamPos - wPos;
80               
81                // world-space light vector
82                OUT.mLight = wLightPos.xyz - wPos * wLightPos.w;
83               
84                OUT.wTangent = normalize(mul(float4(IN.Tangent, 1),WorldI)).rgb;
85                OUT.wNormal = normalize(mul(float4(IN.Normal, 1),WorldI)).rgb;         
86                OUT.wBinormal = cross(OUT.wTangent, OUT.wNormal).rgb;           
87               
88        // vertex position before homogenious division
89        OUT.hPosition = mul(WorldViewProj, IN.Position);
90                // tex coordinates passed to pixel shader
91        OUT.TexCoord = IN.TexCoord;
92        OUT.TexCoord2 = IN.TexCoord2;
93               
94        if(normalCoord == 0)
95                OUT.TexNormalCoord = IN.TexCoord;
96        else
97                OUT.TexNormalCoord = IN.TexCoord2;
98               
99                return OUT;
100}
101
102float4 BumpPS(VS_OUTPUT IN) : COLOR
103{
104float SpotLightAngle = 120.0 / 180.0 * 3.14;
105
106
107        //return tex2D(BumpMapSampler,IN.TexCoord).a;
108        IN.wTangent = normalize(IN.wTangent);
109        IN.wBinormal = normalize(IN.wBinormal);
110        IN.wNormal = normalize(IN.wNormal);
111        // needs normalization because of linear interpolation   
112    float3 mLight = IN.mLight;
113    float3 mView = normalize( IN.mView );
114   
115    // get model space normal vector
116        float3x3 ModelToTangent = TransfModelToTangent(IN.wTangent, IN.wBinormal, IN.wNormal );
117    // get model space normal vector
118    float3 tNormal = tex2D(BumpMapSampler, IN.TexNormalCoord).rgb;
119    if(length(tNormal == 0))
120     tNormal = float3(0,0,1);
121    tNormal.xy = (tNormal.xy *2.0) - 1.0;
122        // Normal vector should be transformed with the inverse transpose of TangentToModel
123        // which is the transpose of ModelToTangent
124    float3 mNormal = normalize( mul( tNormal, ModelToTangent ) );
125        //spot angle
126        float spotFalloff = 1;
127    if(wLightPos.w && dot(wLightDir, wLightDir) != 0)
128    {
129        //spotFalloff = pow(dot(normalize(-mLight), normalize(wLightDir)), 5);
130        spotFalloff = (dot(normalize(-mLight), normalize(wLightDir)) - cos(SpotLightAngle / 2.0)) / (1.0 - cos(SpotLightAngle / 2.0));
131        spotFalloff = pow(saturate(spotFalloff),1);
132     }
133        // illumination calculation
134        float4 illum = Illumination(mLight, mNormal/*IN.wNormal*/, mView, IN.TexCoord2, IN.TexCoord) * spotFalloff;
135        return float4(illum.rgb, 0.7); 
136   
137}
Note: See TracBrowser for help on using the repository browser.