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

Revision 2409, 5.9 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    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 dist = 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 = dist < storedDist + DIST_BIAS;
77                }
78    }
79    //visibility = 1.0 - visibility;
80    //visibility = 1.0 + 0.00001 * visibility;
81        //float spotFalloff = (dot(-diff, lightDir) - lightAngleCos) / (1.0 - lightAngleCos);
82        float spotFalloff = max( dot(-diff, lightDir), 0) + 0.0000000001 * lightAngleCos;
83        //spotFalloff = 1.0 + spotFalloff * 0.000000001;
84        spotFalloff = pow(saturate(spotFalloff), spotLightFalloff);
85               
86        visibility *= spotFalloff / (lightAttenuation.y + dist * lightAttenuation.z + dist2 * lightAttenuation.w);
87       
88        float4 ret = visibility * cosb  *  lightPower * lightColor;
89        return ret;
90}
91
92float4 psComputeWeightsPoint(vsOutputComputeWeights input,
93                                                uniform sampler2D radionSampler : register(s0),
94                                                uniform samplerCUBE shadowMap : register(s1)) : COLOR0
95{
96        float dataColumnWidth = 1.0 / (float)nRadionColumns;
97        int bushIndex = input.tex.x * 4096 + input.tex.y * nRadionColumns * 4096;
98        int werx = bushIndex % nRadionColumns;
99        int wery = bushIndex / nRadionColumns;
100        float3 pos = tex2D(radionSampler, float2((werx + 0.25) * dataColumnWidth, (wery  + 0.5) / 4096.0) ).xyz;
101        float3 dir = tex2D(radionSampler, float2((werx + 0.75) * dataColumnWidth, (wery  + 0.5) / 4096.0) ).xyz;
102        dir = normalize(dir);
103       
104        float3 diff = lightPos - pos;
105        float dist2 = dot(diff, diff);
106        float dist = sqrt(dist2);
107        diff = normalize(diff);
108        float cosb = max(dot(dir, diff), 0);   
109       
110        float visibility = 1;
111        float4 lightVPos = mul(lightViewProj, float4(pos,1));
112       
113        float distNorm = dist / lightFarPlane;
114        float storedDist = texCUBE(shadowMap, float3(-diff.xy, diff.z)).r;
115        dist -= DIST_BIAS;
116        visibility = (distNorm <= storedDist);
117         
118        visibility = 1.0 + 0.00001 * visibility;               
119        visibility *=  1.0 / (lightAttenuation.y + dist * lightAttenuation.z + dist * dist * lightAttenuation.w);
120       
121        float4 ret = visibility * cosb * lightPower * lightColor;
122        return ret;
123}
124
125float4 psSumWeights(vsOutputComputeWeights input,
126                                        uniform sampler2D radionSampler       : register(s0),
127                                        uniform sampler2D entryPointCountSampler : register(s1),
128                                        uniform sampler2D allWeightsSampler   : register(s2)) : COLOR0
129{
130        float halfPixel = 0.5 / clusterCount;
131        float iCluster = input.tex.x + halfPixel;
132        int entryPointCount = tex2D(entryPointCountSampler, float2(iCluster, 0.25));
133        int currentEntryPoint = tex2D(entryPointCountSampler, float2(iCluster, 0.75));
134        //sum entrypoint weights
135        float3 weight = 0;
136        float clusterRad = 0;
137        for(int i = 0; i < entryPointCount; i++)
138        {
139               
140                float dataColumnWidth = 1.0 / (float)nRadionColumns;
141                int werx = currentEntryPoint % nRadionColumns;
142                int wery = currentEntryPoint / nRadionColumns;
143                float2 coord1 = float2((werx + 0.5) * dataColumnWidth, (wery  + 0.5) / 4096.0);
144                float2 coord2 = coord1 + float2(0.25 * dataColumnWidth, 0);
145                float3 w = tex2Dlod(allWeightsSampler, float4(coord1, 0, 0)).rgb;
146                float radrad = tex2Dlod(radionSampler, float4(coord2, 0, 0)).a;         
147                weight += w * radrad;           
148                clusterRad += radrad;
149                currentEntryPoint++;
150        }       
151       
152        if(clusterRad >= 0)
153                weight /= clusterRad;
154        else
155                weight = 0;
156   
157        return float4(weight, 1);
158}
159
160void EntryPointDisplayVS(float4 position :POSITION0,
161                                                 float4 color :COLOR0,
162                                                 uniform float4x4 worldViewProj,
163                                                 out float4 hPos:POSITION,
164                                                 out float2 texCoord: TEXCOORD0)
165{
166        hPos = mul(worldViewProj, position);
167        texCoord = color.xy;
168}
169
170float4 EntryPointDisplayPS(float2 texCoord : TEXCOORD0,
171                                                        uniform sampler2D weightTexture : register(s0)):COLOR0
172{
173        //return texCoord.y;
174        return texCoord.r;
175}
176                                                         
Note: See TracBrowser for help on using the repository browser.