source: GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/GameTools_DepthShadow.hlsl @ 1131

Revision 1131, 1.7 KB checked in by szirmay, 18 years ago (diff)
RevLine 
[777]1///////depth map
2struct VS_OUT
3{
4        float4 hPosition        : POSITION;
[1118]5        float4 Position         : TEXCOORD0;   
[777]6};
7
[1118]8VS_OUT DepthVS(float4 position : POSITION,     
[1131]9                                uniform float4x4 worldViewProj)
[777]10{
11 
12  VS_OUT OUT;
[1118]13 
[777]14  OUT.hPosition = mul(worldViewProj, position);
[1131]15  OUT.Position = mul(worldViewProj, position);
[1118]16 
[777]17  return OUT;
18}
19
[1118]20//for directional light
[777]21float4 DepthPS(VS_OUT IN ):COLOR
22{
[1118]23        float4 pos = (IN.Position / IN.Position.w);
24        pos = (pos +1.0 ) / 2.0;
[1131]25        return float4(pos.z, pos.z * pos.z, 1, 1);     
[777]26}
27
28///////////////Shadow
29
30struct VS_OUT_SHADOW
31{
32        float4 hPosition        : POSITION;
33        float4 Position         : TEXCOORD0;
[1118]34        float4 lPosition        : TEXCOORD1;
[777]35};
36
37VS_OUT_SHADOW depthShadowVS(float4 position : POSITION,
[1118]38                                                        uniform float4x4 worldViewProj,
[1131]39                                                        uniform float4x4 world,
[1118]40                                                        uniform float4x4 lightViewProj)
[777]41{
42  VS_OUT_SHADOW OUT;
43  OUT.hPosition = mul(worldViewProj, position);
[1131]44  float4 wPos = mul(world, position);
45  OUT.lPosition = mul(lightViewProj, wPos); 
[1118]46  OUT.Position = position; 
[777]47  return OUT;
48}
49
50
51float4 depthShadowPS(VS_OUT_SHADOW IN,
52                                        uniform float4x4 lightViewProj,
[1118]53                                        uniform sampler2D depthShadowMap : register(s0)
54                                        ):COLOR
55{       
[1131]56        float bias = 0.1;
[1118]57        float4 light = float4(1,1,1,1);
58        float4 shadow = float4(0.3,0.3,0.3,1);
[777]59       
[1118]60        float4 pos = (IN.lPosition / IN.lPosition.w);
[1131]61        pos.xy = (pos.xy +1.0 ) / 2.0;
[1118]62        pos.y = 1.0 - pos.y;
63        float4 storedDepth = tex2D(depthShadowMap, pos.xy);
[1131]64       
65        if(storedDepth.r + bias > pos.z)
66        {
67                float M1 = storedDepth.r;
68                float M2 = storedDepth.g;
69                float v2 = M2 - M1 * M1;
70                float pmax = v2 / (v2 + pow(M1 - pos.z, 2));
71                light = shadow + (1 - shadow) * pmax;
72               
73                //light = shadow;
74        }
[1118]75        //return abs( pos.z - storedDepth.z ); 
76        return light;   
[777]77}
78
Note: See TracBrowser for help on using the repository browser.