source: GTP/trunk/App/Demos/Illum/Ogre/Media/Towers/parallax.hlsl @ 2441

Revision 2441, 3.9 KB checked in by szirmay, 17 years ago (diff)
Line 
1uniform float textureRepeat;
2uniform sampler2D ColorMapSampler : register(s0);
3uniform sampler2D DetailMapSampler   : register(s1);
4uniform sampler2D BumpMapSampler  : register(s2);
5#define PARALLAX_ITERATION 4
6uniform float HEIGHT_SCALE;
7uniform float HEIGHT_BIAS;
8
9float3x3 TransfModelToTangent( in  float3 Tangent, in float3 Binormal, in float3 Normal ) {
10        float T2 = dot(Tangent, Tangent);
11        float B2 = dot(Binormal, Binormal);
12        float N2 = dot(Normal, Normal);
13        float BT = dot(Binormal, Tangent);
14        float det = B2 * T2 - BT * BT;
15
16        return float3x3( (B2 * Tangent - BT * Binormal)/det,
17                         (T2 * Binormal - BT * Tangent)/det,
18                         Normal/N2 );
19                                                       
20        // simplified solution
21        //return float3x3(Tangent/T2, Binormal/B2, Normal/N2);
22        // more simplified solution
23        //return float3x3(Tangent, Binormal, Normal);
24}
25
26float4 Illumination(float3 Light, float3 Normal, float3 View, float2 TexCoord, float2 TexCoord2)
27{
28        // Blinn lighting
29        float3 Half = normalize(Light + View);
30        float Diffuse = dot(Normal, Light);
31        float Specular = dot(Normal, Half);
32        float4 Lighting = lit(Diffuse, Specular, 16);
33        float4 Color= tex2D(ColorMapSampler, TexCoord) * tex2D(DetailMapSampler, TexCoord2);
34
35        return (Lighting.y + 0.1) * Color;
36}
37
38struct VS_OUTPUT {
39    float4 hPosition : POSITION;     // point in normalized device space before homogeneous division
40    float2 TexCoord  : TEXCOORD0;    // texture coordinates
41    float2 TexCoord2  : TEXCOORD6;    // texture coordinates
42    float3 mView     : TEXCOORD1;    // model space view vector
43    float3 mLight    : TEXCOORD2;    // model space light vector
44    float3 wTangent   : TEXCOORD3;       // model space tangent vector
45    float3 wBinormal  : TEXCOORD4;       // model space binormal vector
46    float3 wNormal    : TEXCOORD5;       // model space triangle normal vector
47};
48
49void DISCARD_BY_TEX(float2 Tex)
50{
51        if (Tex.x > textureRepeat || Tex.x < 0 || Tex.y > textureRepeat || Tex.y < 0)
52                discard;
53}
54
55float2 PARALLAX_MAPPING_OFFSET_LIMIT(sampler2D heightMap, float2 TexCoord, float3 View)
56{
57    float4 Normal = tex2D(heightMap, TexCoord);
58    float h = Normal.a * HEIGHT_SCALE + HEIGHT_BIAS;
59    return TexCoord + h * View.xy;
60}
61
62float2 PARALLAX_MAPPING_ITER(sampler2D heightMap, float2 TexCoord, float3 View)
63{
64    for(int i = 0; i < PARALLAX_ITERATION; i++) {
65                float4 Normal = tex2D(heightMap, TexCoord);
66                float h = Normal.a * HEIGHT_SCALE + HEIGHT_BIAS;
67                TexCoord += h * Normal.z * View;
68        }
69        return TexCoord.xy;
70}
71
72//------------------------------------------------------------------------------------
73//
74// ParallaxSlopeIterationPS: Iterative Parallax Mapping with Slope pixel shader
75//
76//------------------------------------------------------------------------------------
77float4 ParallaxSlopeIterationPS(VS_OUTPUT IN) : COLOR
78{
79        //return tex2D(BumpMapSampler,IN.TexCoord).a;
80        IN.wTangent = normalize(IN.wTangent);
81        IN.wBinormal = normalize(IN.wBinormal);
82        IN.wNormal = normalize(IN.wNormal);
83        // get model space normal vector
84        float3x3 ModelToTangent = TransfModelToTangent(IN.wTangent, IN.wBinormal, IN.wNormal );
85        // needs normalization because of linear interpolation   
86    float3 mLight = normalize( IN.mLight );
87    float3 mView = normalize( IN.mView );
88        float3 tView = normalize( mul(ModelToTangent, IN.mView ) );
89     
90    // performing parallax mapping
91    float2 ParallaxTex = PARALLAX_MAPPING_ITER(BumpMapSampler, IN.TexCoord, tView);
92        DISCARD_BY_TEX(ParallaxTex);
93       
94    // get model space normal vector
95    float3 tNormal = tex2D(BumpMapSampler, ParallaxTex).rgb;
96        // Normal vector should be transformed with the inverse transpose of TangentToModel
97        // which is the transpose of ModelToTangent
98    float3 mNormal = normalize( mul( tNormal, ModelToTangent ) );
99        //mNormal = normalize(IN.wNormal);
100    // illumination calculation
101    return Illumination(mLight, mNormal, mView, ParallaxTex, IN.TexCoord2);
102}
Note: See TracBrowser for help on using the repository browser.