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