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

Revision 2337, 5.4 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 = 1;
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
70                float dist = sqrt(dist2);
71                float distNorm = dist / lightFarPlane;
72                lightVPos.xy = (lightVPos.xy + 1.0) / 2.0;
73                lightVPos.y = 1.0 - lightVPos.y;
74                float storedDist = tex2D(shadowMap, lightVPos.xy).r;
75                dist -= DIST_BIAS;
76                visibility = (distNorm <= storedDist); 
77               
78                }
79        }
80       
81        float spotFalloff = (dot(normalize(-diff), normalize(lightDir)) - lightAngleCos) / (1.0 - lightAngleCos);
82        spotFalloff = pow(saturate(spotFalloff), spotLightFalloff);
83
84       
85        visibility *= spotFalloff / (lightAttenuation.y + dist * lightAttenuation.z + dist * dist * lightAttenuation.w);
86
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 / (lightAttenuation.y + dist * lightAttenuation.z + dist * dist * lightAttenuation.w);
119       
120        float4 ret = visibility * cosb * lightPower * lightColor;
121        return ret;
122}
123
124float4 psSumWeights(vsOutputComputeWeights input,
125                                        uniform sampler2D radionSampler       : register(s0),
126                                        uniform sampler2D entryPointCountSampler : register(s1),
127                                        uniform sampler2D allWeightsSampler   : register(s2)) : COLOR0
128{
129        float halfPixel = 0.5 / clusterCount;
130        float iCluster = input.tex.x + halfPixel;
131        int entryPointCount = tex2D(entryPointCountSampler, float2(iCluster, 0.25));
132        int currentEntryPoint = tex2D(entryPointCountSampler, float2(iCluster, 0.75));
133        //sum entrypoint weights
134        float3 weight = 0;
135        float clusterRad = 0;
136        for(int i = 0; i < entryPointCount; i++)
137        {
138               
139                float dataColumnWidth = 1.0 / (float)nRadionColumns;
140                int werx = currentEntryPoint % nRadionColumns;
141                int wery = currentEntryPoint / nRadionColumns;
142                float2 coord1 = float2((werx + 0.5) * dataColumnWidth, (wery  + 0.5) / 4096.0);
143                float2 coord2 = coord1 + float2(0.25 * dataColumnWidth, 0);
144                float3 w = tex2Dlod(allWeightsSampler, float4(coord1, 0, 0)).rgb;
145                float radrad = tex2Dlod(radionSampler, float4(coord1, 0, 0)).a;         
146                weight += w * radrad;           
147                clusterRad += radrad;
148                currentEntryPoint++;
149        }       
150       
151        if(clusterRad >= 0)
152                weight /= clusterRad;
153        else
154                weight = 0;
155   
156        return float4(weight, 1);
157}
158
159void EntryPointDisplayVS(float4 position :POSITION0,
160                                                 float4 color :COLOR0,
161                                                 uniform float4x4 worldViewProj,
162                                                 out float4 hPos:POSITION,
163                                                 out float2 texCoord: TEXCOORD0)
164{
165        hPos = mul(worldViewProj, position);
166        texCoord = color.xy;
167}
168
169float4 EntryPointDisplayPS(float2 texCoord : TEXCOORD0,
170                                                        uniform sampler2D weightTexture : register(s0)):COLOR0
171{
172        //return texCoord.y;
173        return texCoord.r;
174}
175                                                         
Note: See TracBrowser for help on using the repository browser.