source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/precompiled/app/OgreIllumModule_Resources/materials/GTPPathMap/PathMapWeightCompute.hlsl @ 3255

Revision 3255, 7.1 KB checked in by szirmay, 15 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.xy + 1.0) / 2.0;
35    //output.tex = input.tex;
36    output.tex.y = 1.0 - output.tex.y;
37    //input.pos.y *= -1;
38   
39    return output;
40}
41
42float4 psComputeWeights(vsOutputComputeWeights input,
43                                                uniform sampler2D radionSampler : register(s0),
44                                                uniform sampler2D shadowMap : register(s1)) : COLOR0
45{
46        float dataColumnWidth = 1.0 / (float)nRadionColumns;
47        /*int bushIndex = input.tex.x * 4096 + input.tex.y * nRadionColumns * 4096;
48        int werx = bushIndex % nRadionColumns;
49        int wery = bushIndex / nRadionColumns;
50        float3 pos = tex2D(radionSampler, float2((werx + 0.25) * dataColumnWidth, (wery  + 0.5) / 4096.0) ).xyz;
51        float3 dir = tex2D(radionSampler, float2((werx + 0.75) * dataColumnWidth, (wery  + 0.5) / 4096.0) ).xyz;*/
52        float3 pos = tex2D(radionSampler, float2(input.tex.x + 0.25 * dataColumnWidth, input.tex.y  + 0.5 / 4096.0) ).xyz;
53        float3 dir = tex2D(radionSampler, float2(input.tex.x + 0.75 * dataColumnWidth, input.tex.y  + 0.5 / 4096.0) ).xyz;
54        dir = normalize(dir);
55       
56        float3 diff = lightPos - pos;
57        float dist2 = dot(diff, diff);
58        float dist = sqrt(dist2);
59        diff = normalize(diff);
60        float cosb = max(dot(dir, diff), 0);   
61       
62        float visibility = 0;
63        float4 lightVPos = mul(lightViewProj, float4(pos,1));
64               
65        if( lightVPos.z  > 0.0)
66        {       
67                lightVPos /= lightVPos.w;
68                float d = length(lightVPos.xy);
69               
70                if(d <= 1.0)
71                {
72                        float dd = dist / lightFarPlane;
73                        lightVPos.xy = (lightVPos.xy + 1.0) / 2.0;
74                        lightVPos.y = 1.0 - lightVPos.y;
75                        float storedDist = tex2D(shadowMap, lightVPos.xy).r;
76                        visibility = dd < storedDist + DIST_BIAS;
77                }
78        }
79        //visibility = 1;
80    //visibility = 1.0 - visibility;
81    //visibility = 1.0 + 0.00001 * visibility;
82        float spotFalloff = (dot(-diff, lightDir) - lightAngleCos) / (1.0 - lightAngleCos);
83        //float spotFalloff = max( dot(-diff, lightDir), 0) + 0.0000000001 * lightAngleCos;
84        //spotFalloff = 1.0 + spotFalloff * 0.000000001;
85        spotFalloff = pow(saturate(spotFalloff), spotLightFalloff);
86               
87        visibility *= spotFalloff / (lightAttenuation.y + dist * lightAttenuation.z + dist2 * lightAttenuation.w);
88       
89        float4 ret = visibility * cosb  *  lightPower * lightColor;
90        return ret;
91}
92
93float4 psComputeWeightsPoint(vsOutputComputeWeights input,
94                                                uniform sampler2D radionSampler : register(s0),
95                                                uniform samplerCUBE shadowMap : register(s1)) : COLOR0
96{
97        float dataColumnWidth = 1.0 / (float)nRadionColumns;
98/*      int bushIndex = input.tex.x * 4096 + input.tex.y * nRadionColumns * 4096;
99        int werx = bushIndex % nRadionColumns;
100        int wery = bushIndex / nRadionColumns;
101        float3 pos = tex2D(radionSampler, float2((werx + 0.25) * dataColumnWidth, (wery  + 0.5) / 4096.0) ).xyz;
102        float3 dir = tex2D(radionSampler, float2((werx + 0.75) * dataColumnWidth, (wery  + 0.5) / 4096.0) ).xyz;*/
103        float3 pos = tex2D(radionSampler, float2(input.tex.x + 0.25 * dataColumnWidth, input.tex.y  + 0.5 / 4096.0) ).xyz;
104        float3 dir = tex2D(radionSampler, float2(input.tex.x + 0.75 * dataColumnWidth, input.tex.y  + 0.5 / 4096.0) ).xyz;
105//      float3 pos = tex2D(radionSampler, float2(0.25, input.tex.y  + 0.5 / 4096.0) ).xyz;
106//      float3 dir = tex2D(radionSampler, float2(0.75, input.tex.y  + 0.5 / 4096.0) ).xyz;
107       
108        dir = normalize(dir);
109       
110        float3 diff = lightPos - pos;
111        float dist2 = dot(diff, diff);
112        float dist = sqrt(dist2);
113        diff = normalize(diff);
114        float cosb = max(dot(dir, diff), 0);   
115       
116        float visibility = 1;
117        float4 lightVPos = mul(lightViewProj, float4(pos,1));
118       
119        float distNorm = dist / lightFarPlane;
120        float storedDist = texCUBE(shadowMap, float3(-diff.xy, diff.z)).r;
121        dist -= DIST_BIAS;
122        visibility = (distNorm <= storedDist);
123        //visibility = visibility * 0.0000001 + 1.0; 
124        visibility *=  1.0 / (lightAttenuation.y + dist * lightAttenuation.z + dist * dist * lightAttenuation.w);
125       
126        float4 ret = visibility * cosb * lightPower * lightColor;
127       
128        //ret = 0.00000001 * ret * nRadionColumns + (dist<25.0);//float4(pos,1);//input.tex.y * 4096.0;
129        return ret;
130}
131
132float4 psSumWeights(vsOutputComputeWeights input,
133                                        uniform sampler2D radionSampler       : register(s0),
134                                        uniform sampler2D entryPointCountSampler : register(s1),
135                                        uniform sampler2D allWeightsSampler   : register(s2)) : COLOR0
136{
137        float halfPixel = 0.5 / clusterCount;
138        float iCluster = input.tex.x + halfPixel;
139        int entryPointCount = tex2D(entryPointCountSampler, float2(iCluster, 0.25));
140        int currentEntryPoint = tex2D(entryPointCountSampler, float2(iCluster, 0.75));
141        //sum entrypoint weights
142        float3 weight = 0;
143        float clusterRad = 0;
144        for(int i = 0; i < entryPointCount; i++)
145        {
146                float dataColumnWidth = 1.0 / (float)nRadionColumns;
147                int werx = currentEntryPoint % nRadionColumns;
148                int wery = currentEntryPoint / nRadionColumns;
149                float2 coord1 = float2((werx + 0.5) * dataColumnWidth, (wery  + 0.5) / 4096.0);         
150                float2 coord2 = coord1 + float2(0.25 * dataColumnWidth, 0);
151               
152                //coord1 = coord1 * 0.00000001 + float2(0.5, (currentEntryPoint + 0.5)/4096);
153                //coord2 = coord1 + float2(0.25, 0);
154               
155                float3 w = tex2Dlod(allWeightsSampler, float4(coord1, 0, 0)).rgb;
156                float radrad = tex2Dlod(radionSampler, float4(coord2, 0, 0)).a;         
157                weight += w * radrad;           
158                clusterRad += radrad;
159                currentEntryPoint++;
160        }       
161       
162        if(clusterRad >= 0)
163                weight /= clusterRad;
164        else
165                weight = 0;
166   
167        float4 ret = 0;
168        //if(abs(input.tex.x - 2*halfPixel*2)<halfPixel/2.0)
169                ret = float4(weight, 1);       
170        return ret;
171}
172
173void EntryPointDisplayVS(float4 position :POSITION0,
174                                                 float4 color :COLOR0,
175                                                 uniform float4x4 worldViewProj,
176                                                 out float4 hPos:POSITION,
177                                                 out float3 texCoord: TEXCOORD0)
178{
179        hPos = mul(worldViewProj, float4(position.x,0,position.z,1));
180        texCoord = color.xyz;
181        texCoord = position.y;
182}
183
184float4 EntryPointDisplayPS(float3 texCoord : TEXCOORD0,
185                                                        uniform sampler2D weightTexture : register(s0),
186                                                        uniform sampler2D clusterWeightTexture : register(s1)):COLOR0
187{
188        float entryPointID = (256.0f * texCoord.r * 256.0f + 256.0f * texCoord.g + 0.5) / 4096.0f;
189        entryPointID = texCoord.x;
190        //return float4(texCoord, 1);
191        //return tex2D(clusterWeightTexture, float2(0.5, 1.0 - ((entryPointID + 0.5 )/ 4096.0)));
192        return tex2D(weightTexture, float2(0.5, ((entryPointID + 0.5 )/ 4096.0)));
193        //return abs(tex2D(weightTexture, float2(0.5, ((entryPointID + 0.5 )/ 4096.0))) - entryPointID);
194        return entryPointID;
195}
196                                                         
Note: See TracBrowser for help on using the repository browser.