1 | #define SAMPLECUTDIST2 0.01
|
---|
2 | #define WEIGHTCUTOFF 0.01
|
---|
3 | #define DIST_BIAS 0.005
|
---|
4 |
|
---|
5 | uniform int nRadionColumns;
|
---|
6 | uniform float3 lightPos;
|
---|
7 | uniform float3 lightDir;
|
---|
8 | uniform float lightPower;
|
---|
9 | uniform float4 lightColor;
|
---|
10 | uniform float clusterCount;
|
---|
11 | uniform float4x4 lightViewProj;
|
---|
12 | uniform float lightFarPlane;
|
---|
13 | uniform float4 lightAttenuation;
|
---|
14 | uniform float spotLightFalloff;
|
---|
15 | uniform float lightAngleCos;
|
---|
16 |
|
---|
17 | struct vsInputComputeWeights
|
---|
18 | {
|
---|
19 | float4 pos : POSITION;
|
---|
20 | float2 tex : TEXCOORD0;
|
---|
21 | };
|
---|
22 |
|
---|
23 | struct vsOutputComputeWeights
|
---|
24 | {
|
---|
25 | float4 pos : POSITION;
|
---|
26 | float2 tex : TEXCOORD0;
|
---|
27 | };
|
---|
28 |
|
---|
29 | vsOutputComputeWeights
|
---|
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 |
|
---|
42 | float4 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 |
|
---|
92 | float4 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 | float3 pos = tex2D(radionSampler, float2(input.tex.x + 0.25 * dataColumnWidth, input.tex.y + 0.5 / 4096.0) ).xyz;
|
---|
103 | float3 dir = tex2D(radionSampler, float2(input.tex.x + 0.75 * dataColumnWidth, input.tex.y + 0.5 / 4096.0) ).xyz;
|
---|
104 | // float3 pos = tex2D(radionSampler, float2(0.25, input.tex.y + 0.5 / 4096.0) ).xyz;
|
---|
105 | // float3 dir = tex2D(radionSampler, float2(0.75, input.tex.y + 0.5 / 4096.0) ).xyz;
|
---|
106 |
|
---|
107 | dir = normalize(dir);
|
---|
108 |
|
---|
109 | float3 diff = lightPos - pos;
|
---|
110 | float dist2 = dot(diff, diff);
|
---|
111 | float dist = sqrt(dist2);
|
---|
112 | diff = normalize(diff);
|
---|
113 | float cosb = max(dot(dir, diff), 0);
|
---|
114 |
|
---|
115 | float visibility = 1;
|
---|
116 | float4 lightVPos = mul(lightViewProj, float4(pos,1));
|
---|
117 |
|
---|
118 | float distNorm = dist / lightFarPlane;
|
---|
119 | float storedDist = texCUBE(shadowMap, float3(-diff.xy, diff.z)).r;
|
---|
120 | dist -= DIST_BIAS;
|
---|
121 | visibility = (distNorm <= storedDist);
|
---|
122 | //visibility = visibility * 0.0000001 + 1.0;
|
---|
123 | visibility *= 1.0 / (lightAttenuation.y + dist * lightAttenuation.z + dist * dist * lightAttenuation.w);
|
---|
124 |
|
---|
125 | float4 ret = visibility * cosb * lightPower * lightColor;
|
---|
126 |
|
---|
127 | //ret = 0.00000001 * ret * nRadionColumns + (dist<25.0);//float4(pos,1);//input.tex.y * 4096.0;
|
---|
128 | return ret;
|
---|
129 | }
|
---|
130 |
|
---|
131 | float4 psSumWeights(vsOutputComputeWeights input,
|
---|
132 | uniform sampler2D radionSampler : register(s0),
|
---|
133 | uniform sampler2D entryPointCountSampler : register(s1),
|
---|
134 | uniform sampler2D allWeightsSampler : register(s2)) : COLOR0
|
---|
135 | {
|
---|
136 | float halfPixel = 0.5 / clusterCount;
|
---|
137 | float iCluster = input.tex.x + halfPixel;
|
---|
138 | int entryPointCount = tex2D(entryPointCountSampler, float2(iCluster, 0.25));
|
---|
139 | int currentEntryPoint = tex2D(entryPointCountSampler, float2(iCluster, 0.75));
|
---|
140 | //sum entrypoint weights
|
---|
141 | float3 weight = 0;
|
---|
142 | float clusterRad = 0;
|
---|
143 | for(int i = 0; i < entryPointCount; i++)
|
---|
144 | {
|
---|
145 | float dataColumnWidth = 1.0 / (float)nRadionColumns;
|
---|
146 | int werx = currentEntryPoint % nRadionColumns;
|
---|
147 | int wery = currentEntryPoint / nRadionColumns;
|
---|
148 | float2 coord1 = float2((werx + 0.5) * dataColumnWidth, (wery + 0.5) / 4096.0);
|
---|
149 | float2 coord2 = coord1 + float2(0.25 * dataColumnWidth, 0);
|
---|
150 |
|
---|
151 | //coord1 = coord1 * 0.00000001 + float2(0.5, (currentEntryPoint + 0.5)/4096);
|
---|
152 | //coord2 = coord1 + float2(0.25, 0);
|
---|
153 |
|
---|
154 | float3 w = tex2Dlod(allWeightsSampler, float4(coord1, 0, 0)).rgb;
|
---|
155 | float radrad = tex2Dlod(radionSampler, float4(coord2, 0, 0)).a;
|
---|
156 | weight += w * radrad;
|
---|
157 | clusterRad += radrad;
|
---|
158 | currentEntryPoint++;
|
---|
159 | }
|
---|
160 |
|
---|
161 | if(clusterRad >= 0)
|
---|
162 | weight /= clusterRad;
|
---|
163 | else
|
---|
164 | weight = 0;
|
---|
165 |
|
---|
166 | float4 ret = 0;
|
---|
167 | //if(abs(input.tex.x - 2*halfPixel*2)<halfPixel/2.0)
|
---|
168 | ret = float4(weight, 1);
|
---|
169 | return ret;
|
---|
170 | }
|
---|
171 |
|
---|
172 | void EntryPointDisplayVS(float4 position :POSITION0,
|
---|
173 | float4 color :COLOR0,
|
---|
174 | uniform float4x4 worldViewProj,
|
---|
175 | out float4 hPos:POSITION,
|
---|
176 | out float3 texCoord: TEXCOORD0)
|
---|
177 | {
|
---|
178 | hPos = mul(worldViewProj, float4(position.x,0,position.z,1));
|
---|
179 | texCoord = color.xyz;
|
---|
180 | texCoord = position.y;
|
---|
181 | }
|
---|
182 |
|
---|
183 | float4 EntryPointDisplayPS(float3 texCoord : TEXCOORD0,
|
---|
184 | uniform sampler2D weightTexture : register(s0),
|
---|
185 | uniform sampler2D clusterWeightTexture : register(s1)):COLOR0
|
---|
186 | {
|
---|
187 | float entryPointID = (256.0f * texCoord.r * 256.0f + 256.0f * texCoord.g + 0.5) / 4096.0f;
|
---|
188 | entryPointID = texCoord.x;
|
---|
189 | //return float4(texCoord, 1);
|
---|
190 | //return tex2D(clusterWeightTexture, float2(0.5, 1.0 - ((entryPointID + 0.5 )/ 4096.0)));
|
---|
191 | return tex2D(weightTexture, float2(0.5, ((entryPointID + 0.5 )/ 4096.0)));
|
---|
192 | //return abs(tex2D(weightTexture, float2(0.5, ((entryPointID + 0.5 )/ 4096.0))) - entryPointID);
|
---|
193 | return entryPointID;
|
---|
194 | }
|
---|
195 | |
---|