source: GTP/trunk/App/Demos/Illum/Ogre/Media/materials/GTPPathMap/PathMapWeightCompute.hlsl @ 2403

Revision 2403, 5.6 KB checked in by szirmay, 17 years ago (diff)
Line 
1#define SAMPLECUTDIST2 0.01
2#define WEIGHTCUTOFF   0.01
3#define DIST_BIAS      0.005
4
5uniform int nRadionColumns;
6uniform float3 lightPos;
7uniform float3 lightDir;
8uniform float lightPower;
9uniform float4 lightColor;
10uniform float clusterCount;
11uniform float4x4 lightViewProj;
12uniform float lightFarPlane;
13uniform float4 lightAttenuation;
14uniform float spotLightFalloff;
15uniform float lightAngleCos;
16
17struct vsInputComputeWeights
18{
19    float4      pos                     : POSITION;
20    float2      tex                     : TEXCOORD0;
21};
22
23struct vsOutputComputeWeights
24{
25    float4      pos                     : POSITION;
26    float2      tex                     : TEXCOORD0;
27};
28
29vsOutputComputeWeights
30        vsComputeWeights(vsInputComputeWeights input)
31{
32    vsOutputComputeWeights output = (vsOutputComputeWeights)0;
33    output.pos = input.pos;
34    output.tex = (input.pos + 1.0) / 2.0;
35    //output.tex = input.tex;
36    output.tex.y = 1.0 - output.tex.y;
37   
38    return output;
39}
40
41float4 psComputeWeights(vsOutputComputeWeights input,
42                                                uniform sampler2D radionSampler : register(s0),
43                                                uniform sampler2D shadowMap : register(s1)) : COLOR0
44{
45        float dataColumnWidth = 1.0 / (float)nRadionColumns;
46        int bushIndex = input.tex.x * 4096 + input.tex.y * nRadionColumns * 4096;
47        int werx = bushIndex % nRadionColumns;
48        int wery = bushIndex / nRadionColumns;
49        float3 pos = tex2D(radionSampler, float2((werx + 0.25) * dataColumnWidth, (wery  + 0.5) / 4096.0) ).xyz;
50        float3 dir = tex2D(radionSampler, float2((werx + 0.75) * dataColumnWidth, (wery  + 0.5) / 4096.0) ).xyz;
51        dir = normalize(dir);
52       
53        float3 diff = lightPos - pos;
54        float dist2 = dot(diff, diff);
55        float dist = sqrt(dist2);
56        diff = normalize(diff);
57        float cosb = max(dot(dir, diff), 0);   
58       
59        float visibility = 0;
60        float4 lightVPos = mul(lightViewProj, float4(pos,1));
61               
62        //if( lightVPos.z  > 0.0)
63        //{     
64        lightVPos /= lightVPos.w;
65                //float d = length(lightVPos.xy);
66               
67                //if(d <= 1.0)
68        {
69                        float dist = dist / lightFarPlane;
70                        lightVPos.xy = (lightVPos.xy + 1.0) / 2.0;
71                        lightVPos.y = 1.0 - lightVPos.y;
72                        float storedDist = tex2D(shadowMap, lightVPos.xy).r;
73                        visibility = dist < storedDist + DIST_BIAS;
74                }
75   // }
76    //visibility = 1.0 - visibility;
77    visibility = 1.0 + 0.00001 * visibility;
78        float spotFalloff = (dot(-diff, normalize(lightDir)) - lightAngleCos) / (1.0 - lightAngleCos);
79        //spotFalloff = saturate(dot(normalize(pos - lightPos), normalize(lightDir))) + 0.0000000001 * lightAngleCos;
80        spotFalloff = 1.0 + spotFalloff * 0.000000001;
81        spotFalloff = pow(saturate(spotFalloff), spotLightFalloff);
82               
83        visibility *= spotFalloff / (lightAttenuation.y + dist * lightAttenuation.z + dist2 * lightAttenuation.w);
84       
85        float4 ret = visibility * cosb * lightPower * lightColor;
86        return ret;
87}
88
89float4 psComputeWeightsPoint(vsOutputComputeWeights input,
90                                                uniform sampler2D radionSampler : register(s0),
91                                                uniform samplerCUBE shadowMap : register(s1)) : COLOR0
92{
93        float dataColumnWidth = 1.0 / (float)nRadionColumns;
94        int bushIndex = input.tex.x * 4096 + input.tex.y * nRadionColumns * 4096;
95        int werx = bushIndex % nRadionColumns;
96        int wery = bushIndex / nRadionColumns;
97        float3 pos = tex2D(radionSampler, float2((werx + 0.25) * dataColumnWidth, (wery  + 0.5) / 4096.0) ).xyz;
98        float3 dir = tex2D(radionSampler, float2((werx + 0.75) * dataColumnWidth, (wery  + 0.5) / 4096.0) ).xyz;
99        dir = normalize(dir);
100       
101        float3 diff = lightPos - pos;
102        float dist2 = dot(diff, diff);
103        float dist = sqrt(dist2);
104        diff = normalize(diff);
105        float cosb = max(dot(dir, diff), 0);   
106       
107        float visibility = 1;
108        float4 lightVPos = mul(lightViewProj, float4(pos,1));
109       
110        float distNorm = dist / lightFarPlane;
111        float storedDist = texCUBE(shadowMap, float3(-diff.xy, diff.z)).r;
112        dist -= DIST_BIAS;
113        visibility = (distNorm <= storedDist);
114         
115        //visibility = 1.0 + 0.00001 * visibility;             
116        visibility *=  1.0 / (lightAttenuation.y + dist * lightAttenuation.z + dist * dist * lightAttenuation.w);
117       
118        float4 ret = visibility * cosb * lightPower * lightColor;
119        return ret;
120}
121
122float4 psSumWeights(vsOutputComputeWeights input,
123                                        uniform sampler2D radionSampler       : register(s0),
124                                        uniform sampler2D entryPointCountSampler : register(s1),
125                                        uniform sampler2D allWeightsSampler   : register(s2)) : COLOR0
126{
127        float halfPixel = 0.5 / clusterCount;
128        float iCluster = input.tex.x + halfPixel;
129        int entryPointCount = tex2D(entryPointCountSampler, float2(iCluster, 0.25));
130        int currentEntryPoint = tex2D(entryPointCountSampler, float2(iCluster, 0.75));
131        //sum entrypoint weights
132        float3 weight = 0;
133        float clusterRad = 0;
134        for(int i = 0; i < entryPointCount; i++)
135        {
136               
137                float dataColumnWidth = 1.0 / (float)nRadionColumns;
138                int werx = currentEntryPoint % nRadionColumns;
139                int wery = currentEntryPoint / nRadionColumns;
140                float2 coord1 = float2((werx + 0.5) * dataColumnWidth, (wery  + 0.5) / 4096.0);
141                float2 coord2 = coord1 + float2(0.25 * dataColumnWidth, 0);
142                float3 w = tex2Dlod(allWeightsSampler, float4(coord1, 0, 0)).rgb;
143                float radrad = tex2Dlod(radionSampler, float4(coord2, 0, 0)).a;         
144                weight += w * radrad;           
145                clusterRad += radrad;
146                currentEntryPoint++;
147        }       
148       
149        if(clusterRad >= 0)
150                weight /= clusterRad;
151        else
152                weight = 0;
153   
154        return float4(weight, 1);
155}
156
157void EntryPointDisplayVS(float4 position :POSITION0,
158                                                 float4 color :COLOR0,
159                                                 uniform float4x4 worldViewProj,
160                                                 out float4 hPos:POSITION,
161                                                 out float2 texCoord: TEXCOORD0)
162{
163        hPos = mul(worldViewProj, position);
164        texCoord = color.xy;
165}
166
167float4 EntryPointDisplayPS(float2 texCoord : TEXCOORD0,
168                                                        uniform sampler2D weightTexture : register(s0)):COLOR0
169{
170        //return texCoord.y;
171        return texCoord.r;
172}
173                                                         
Note: See TracBrowser for help on using the repository browser.