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)
Line 
1///////depth map
2struct VS_OUT
3{
4        float4 hPosition        : POSITION;
5        float4 Position         : TEXCOORD0;   
6};
7
8VS_OUT DepthVS(float4 position : POSITION,     
9                                uniform float4x4 worldViewProj)
10{
11 
12  VS_OUT OUT;
13 
14  OUT.hPosition = mul(worldViewProj, position);
15  OUT.Position = mul(worldViewProj, position);
16 
17  return OUT;
18}
19
20//for directional light
21float4 DepthPS(VS_OUT IN ):COLOR
22{
23        float4 pos = (IN.Position / IN.Position.w);
24        pos = (pos +1.0 ) / 2.0;
25        return float4(pos.z, pos.z * pos.z, 1, 1);     
26}
27
28///////////////Shadow
29
30struct VS_OUT_SHADOW
31{
32        float4 hPosition        : POSITION;
33        float4 Position         : TEXCOORD0;
34        float4 lPosition        : TEXCOORD1;
35};
36
37VS_OUT_SHADOW depthShadowVS(float4 position : POSITION,
38                                                        uniform float4x4 worldViewProj,
39                                                        uniform float4x4 world,
40                                                        uniform float4x4 lightViewProj)
41{
42  VS_OUT_SHADOW OUT;
43  OUT.hPosition = mul(worldViewProj, position);
44  float4 wPos = mul(world, position);
45  OUT.lPosition = mul(lightViewProj, wPos); 
46  OUT.Position = position; 
47  return OUT;
48}
49
50
51float4 depthShadowPS(VS_OUT_SHADOW IN,
52                                        uniform float4x4 lightViewProj,
53                                        uniform sampler2D depthShadowMap : register(s0)
54                                        ):COLOR
55{       
56        float bias = 0.1;
57        float4 light = float4(1,1,1,1);
58        float4 shadow = float4(0.3,0.3,0.3,1);
59       
60        float4 pos = (IN.lPosition / IN.lPosition.w);
61        pos.xy = (pos.xy +1.0 ) / 2.0;
62        pos.y = 1.0 - pos.y;
63        float4 storedDepth = tex2D(depthShadowMap, pos.xy);
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        }
75        //return abs( pos.z - storedDepth.z ); 
76        return light;   
77}
78
Note: See TracBrowser for help on using the repository browser.