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 clusterCount;
|
---|
9 | uniform float4x4 lightViewProj;
|
---|
10 | uniform float lightFarPlane;
|
---|
11 |
|
---|
12 | struct vsInputComputeWeights
|
---|
13 | {
|
---|
14 | float4 pos : POSITION;
|
---|
15 | float2 tex : TEXCOORD0;
|
---|
16 | };
|
---|
17 |
|
---|
18 | struct vsOutputComputeWeights
|
---|
19 | {
|
---|
20 | float4 pos : POSITION;
|
---|
21 | float2 tex : TEXCOORD0;
|
---|
22 | };
|
---|
23 |
|
---|
24 | vsOutputComputeWeights
|
---|
25 | vsComputeWeights(vsInputComputeWeights input)
|
---|
26 | {
|
---|
27 | vsOutputComputeWeights output = (vsOutputComputeWeights)0;
|
---|
28 | output.pos = input.pos;
|
---|
29 | output.tex = (input.pos + 1.0) / 2.0;
|
---|
30 | //output.tex = input.tex;
|
---|
31 | output.tex.y = 1.0 - output.tex.y;
|
---|
32 |
|
---|
33 | return output;
|
---|
34 | }
|
---|
35 |
|
---|
36 | float4 psComputeWeights(vsOutputComputeWeights input,
|
---|
37 | uniform sampler2D radionSampler : register(s0),
|
---|
38 | uniform sampler2D shadowMap : register(s1)) : COLOR0
|
---|
39 | {
|
---|
40 | float dataColumnWidth = 1.0 / (float)nRadionColumns;
|
---|
41 | int bushIndex = input.tex.x * 4096 + input.tex.y * nRadionColumns * 4096;
|
---|
42 | int werx = bushIndex % nRadionColumns;
|
---|
43 | int wery = bushIndex / nRadionColumns;
|
---|
44 | float3 pos = tex2D(radionSampler, float2((werx + 0.25) * dataColumnWidth, (wery + 0.5) / 4096.0) ).xyz;
|
---|
45 | float3 dir = tex2D(radionSampler, float2((werx + 0.75) * dataColumnWidth, (wery + 0.5) / 4096.0) ).xyz;
|
---|
46 |
|
---|
47 | float3 diff = lightPos - pos;
|
---|
48 | float dist2 = dot(diff, diff);
|
---|
49 | if(dist2 < SAMPLECUTDIST2)
|
---|
50 | dist2 = SAMPLECUTDIST2;
|
---|
51 | diff = normalize(diff);
|
---|
52 | float cosa = max(dot(lightDir, -diff), 0);
|
---|
53 | float cosb = max(dot(dir, diff), 0);
|
---|
54 |
|
---|
55 |
|
---|
56 | float4 lightVPos = mul(lightViewProj, float4(pos,1));
|
---|
57 | float dist = sqrt(dist2) / lightFarPlane;
|
---|
58 | lightVPos /= lightVPos.w;
|
---|
59 | lightVPos.xy = (lightVPos.xy + 1.0) / 2.0;
|
---|
60 | lightVPos.y = 1.0 - lightVPos.y;
|
---|
61 | float storedDist = tex2D(shadowMap, lightVPos.xy).r;
|
---|
62 | dist -= DIST_BIAS;
|
---|
63 | float visibility = (dist <= storedDist);
|
---|
64 |
|
---|
65 | //float visibility = 1;
|
---|
66 | //visibility = visibility * 0.00001 + 1.0;
|
---|
67 |
|
---|
68 | float4 ret = visibility * pow(cosa, 9) * cosb / dist2;
|
---|
69 | return ret;
|
---|
70 | }
|
---|
71 |
|
---|
72 | float4 psSumWeights(vsOutputComputeWeights input,
|
---|
73 | uniform sampler2D radionSampler : register(s0),
|
---|
74 | uniform sampler2D entryPointCountSampler : register(s1),
|
---|
75 | uniform sampler2D allWeightsSampler : register(s2)) : COLOR0
|
---|
76 | {
|
---|
77 | float halfPixel = 0.5 / clusterCount;
|
---|
78 | float iCluster = input.tex.x + halfPixel;
|
---|
79 | int entryPointCount = tex2D(entryPointCountSampler, float2(iCluster, 0.25));
|
---|
80 | int currentEntryPoint = tex2D(entryPointCountSampler, float2(iCluster, 0.75));
|
---|
81 | //sum entrypoint weights
|
---|
82 | float weight = 0;
|
---|
83 | float clusterRad = 0;
|
---|
84 | for(int i = 0; i < entryPointCount; i++)
|
---|
85 | {
|
---|
86 |
|
---|
87 | float dataColumnWidth = 1.0 / (float)nRadionColumns;
|
---|
88 | int werx = currentEntryPoint % nRadionColumns;
|
---|
89 | int wery = currentEntryPoint / nRadionColumns;
|
---|
90 | float2 coord1 = float2((werx + 0.5) * dataColumnWidth, (wery + 0.5) / 4096.0);
|
---|
91 | float2 coord2 = coord1 + float2(0.25 * dataColumnWidth, 0);
|
---|
92 | float w = tex2Dlod(allWeightsSampler, float4(coord1, 0, 0)).r;
|
---|
93 | float radrad = tex2Dlod(radionSampler, float4(coord1, 0, 0)).a;
|
---|
94 | weight += w * radrad;
|
---|
95 | clusterRad += radrad;
|
---|
96 | currentEntryPoint++;
|
---|
97 | }
|
---|
98 |
|
---|
99 | if(clusterRad >= 0)
|
---|
100 | weight /= clusterRad;
|
---|
101 | else
|
---|
102 | weight = 0;
|
---|
103 |
|
---|
104 | return weight;
|
---|
105 | }
|
---|
106 |
|
---|
107 | void EntryPointDisplayVS(float4 position :POSITION0,
|
---|
108 | float4 color :COLOR0,
|
---|
109 | uniform float4x4 worldViewProj,
|
---|
110 | out float4 hPos:POSITION,
|
---|
111 | out float2 texCoord: TEXCOORD0)
|
---|
112 | {
|
---|
113 | hPos = mul(worldViewProj, position);
|
---|
114 | texCoord = color.xy;
|
---|
115 | }
|
---|
116 |
|
---|
117 | float4 EntryPointDisplayPS(float2 texCoord : TEXCOORD0,
|
---|
118 | uniform sampler2D weightTexture : register(s0)):COLOR0
|
---|
119 | {
|
---|
120 | //return texCoord.y;
|
---|
121 | return texCoord.r;
|
---|
122 | }
|
---|
123 | |
---|