source: GTP/trunk/App/Demos/Illum/Ogre/Media/Demo/parallax.hlsl @ 2152

Revision 2152, 3.6 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*/
23}
24
25float4 Illumination(float3 Light, float3 Normal, float3 View, float2 TexCoord, float2 TexCoord2)
26{
27        // Blinn lighting
28        float3 Half = normalize(Light + View);
29        float Diffuse = dot(Normal, Light);
30        float Specular = dot(Normal, Half);
31        float4 Lighting = lit(Diffuse, Specular, 16);
32        float4 Color= tex2D(ColorMapSampler, TexCoord) * tex2D(DetailMapSampler, TexCoord2);
33
34        return (Lighting.y + 0.1) * Color;
35}
36
37struct VS_OUTPUT {
38    float4 hPosition : POSITION;     // point in normalized device space before homogeneous division
39    float2 TexCoord  : TEXCOORD0;    // texture coordinates
40    float2 TexCoord2  : TEXCOORD6;    // texture coordinates
41    float3 mView     : TEXCOORD1;    // model space view vector
42    float3 mLight    : TEXCOORD2;    // model space light vector
43    float3 wTangent   : TEXCOORD3;       // model space tangent vector
44    float3 wBinormal  : TEXCOORD4;       // model space binormal vector
45    float3 wNormal    : TEXCOORD5;       // model space triangle normal vector
46};
47
48void DISCARD_BY_TEX(float2 Tex)
49{
50        if (Tex.x > textureRepeat || Tex.x < 0 || Tex.y > textureRepeat || Tex.y < 0)
51                discard;
52}
53
54float2 PARALLAX_MAPPING_ITER(sampler2D heightMap, float3 TexCoord, float3 View)
55{
56    for(int i = 0; i < PARALLAX_ITERATION; i++) {
57                float4 Normal = tex2D(heightMap, TexCoord);
58                float h = Normal.a * HEIGHT_SCALE + HEIGHT_BIAS;
59                TexCoord += (h - TexCoord.z) * Normal.z * View;
60        }
61        return TexCoord.xy;
62}
63
64//------------------------------------------------------------------------------------
65//
66// ParallaxSlopeIterationPS: Iterative Parallax Mapping with Slope pixel shader
67//
68//------------------------------------------------------------------------------------
69float4 ParallaxSlopeIterationPS(VS_OUTPUT IN) : COLOR
70{
71        //return tex2D(BumpMapSampler,IN.TexCoord).a;
72        IN.wTangent = normalize(IN.wTangent);
73        IN.wBinormal = normalize(IN.wBinormal);
74        IN.wNormal = normalize(IN.wNormal);
75        // get model space normal vector
76        float3x3 ModelToTangent = TransfModelToTangent(IN.wTangent, IN.wBinormal, IN.wNormal );
77        // needs normalization because of linear interpolation   
78    float3 mLight = normalize( IN.mLight );
79    float3 mView = normalize( IN.mView );
80        float3 tView = normalize( mul(ModelToTangent, IN.mView ) );
81     
82    // performing parallax mapping
83    float2 ParallaxTex = PARALLAX_MAPPING_ITER(BumpMapSampler, float3(IN.TexCoord, 0), tView);
84        DISCARD_BY_TEX(ParallaxTex);
85       
86    // get model space normal vector
87    float3 tNormal = tex2D(BumpMapSampler, ParallaxTex).rgb;
88        // Normal vector should be transformed with the inverse transpose of TangentToModel
89        // which is the transpose of ModelToTangent
90    float3 mNormal = normalize( mul( tNormal, ModelToTangent ) );
91        //mNormal = normalize(IN.wNormal);
92    // illumination calculation
93    return Illumination(mLight, mNormal, mView, ParallaxTex, IN.TexCoord2);
94}
Note: See TracBrowser for help on using the repository browser.