1 |
|
---|
2 | #define CUBEMAP_SIZE 128
|
---|
3 | #define REDUCED_CUBEMAP_SIZE 4
|
---|
4 | #define RATE 32
|
---|
5 |
|
---|
6 | float4 readCubeMap(samplerCUBE cm, float3 coord)
|
---|
7 | {
|
---|
8 | float4 color = texCUBElod( cm, float4(coord.xy, -coord.z, 0) );
|
---|
9 | color.a = 1;
|
---|
10 | return color;
|
---|
11 | }
|
---|
12 |
|
---|
13 | float readDistanceCubeMap(samplerCUBE dcm, float3 coord)
|
---|
14 | {
|
---|
15 | float dist = texCUBElod(dcm, float4(coord.xy, - coord.z, 0)).r;
|
---|
16 | if(dist == 0) dist = 1000000; ///sky
|
---|
17 | return dist;
|
---|
18 | }
|
---|
19 |
|
---|
20 | ////////////////////////////
|
---|
21 | /// Reduce cube map shader
|
---|
22 | ///////////////////////////
|
---|
23 | struct MPos_OUT
|
---|
24 | {
|
---|
25 | float4 VPos : POSITION;
|
---|
26 | float4 MPos : TEXCOORD0;
|
---|
27 | };
|
---|
28 | /*
|
---|
29 | float4 ReduceCubeMap_PS(MPos_OUT IN,
|
---|
30 | uniform int nFace,
|
---|
31 | uniform samplerCUBE EnvironmentMapSampler : register(s0) ) : COLOR
|
---|
32 | {
|
---|
33 | float4 color = 0;
|
---|
34 | float3 dir;
|
---|
35 |
|
---|
36 | for (int i = 0; i < RATE; i++)
|
---|
37 | for (int j = 0; j < RATE; j++)
|
---|
38 | {
|
---|
39 | float2 pos;
|
---|
40 | pos.x = IN.MPos.x + (2*i + 1)/(float)CUBEMAP_SIZE;
|
---|
41 | pos.y = IN.MPos.y - (2*j + 1)/(float)CUBEMAP_SIZE; // y=-u
|
---|
42 |
|
---|
43 | // "scrambling"
|
---|
44 | if (nFace == 0) dir = float3(1, pos.y, -pos.x);
|
---|
45 | if (nFace == 1) dir = float3(-1, pos.y, pos.x);
|
---|
46 | if (nFace == 2) dir = float3(pos.x, 1, -pos.y);
|
---|
47 | if (nFace == 3) dir = float3(pos.x, -1, pos.y);
|
---|
48 | if (nFace == 4) dir = float3(pos.x, pos.y, 1);
|
---|
49 | if (nFace == 5) dir = float3(-pos.x, pos.y,-1);
|
---|
50 |
|
---|
51 | color += texCUBE( EnvironmentMapSampler, dir);
|
---|
52 | }
|
---|
53 |
|
---|
54 | return color / (RATE * RATE);
|
---|
55 | }
|
---|
56 | */
|
---|
57 |
|
---|
58 | float4 ReduceCubeMap_PS(MPos_OUT IN,
|
---|
59 | uniform int nFace,
|
---|
60 | uniform samplerCUBE EnvironmentMapSampler : register(s0) ) : COLOR
|
---|
61 | {
|
---|
62 | float4 color = 0;
|
---|
63 | float3 dir;
|
---|
64 |
|
---|
65 | for (int i = 0; i < RATE/2; i+=1)
|
---|
66 | for (int j = 0; j < RATE/2; j+=1)
|
---|
67 | {
|
---|
68 | float2 pos;
|
---|
69 | pos.x = IN.MPos.x + (4*i + 2)/(float)CUBEMAP_SIZE;
|
---|
70 | pos.y = IN.MPos.y - (4*j + 2)/(float)CUBEMAP_SIZE; // y=-u
|
---|
71 |
|
---|
72 | // "scrambling"
|
---|
73 | if (nFace == 0) dir = float3(1, pos.y, -pos.x);
|
---|
74 | if (nFace == 1) dir = float3(-1, pos.y, pos.x);
|
---|
75 | if (nFace == 2) dir = float3(pos.x, 1, -pos.y);
|
---|
76 | if (nFace == 3) dir = float3(pos.x, -1, pos.y);
|
---|
77 | if (nFace == 4) dir = float3(pos.x, pos.y, 1);
|
---|
78 | if (nFace == 5) dir = float3(-pos.x, pos.y,-1);
|
---|
79 |
|
---|
80 | color += texCUBE( EnvironmentMapSampler, dir);
|
---|
81 | }
|
---|
82 |
|
---|
83 | return color / (RATE * RATE / 4.0);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /// Disc to point form factor
|
---|
87 |
|
---|
88 | float4 Disc2Point_Contr(float3 L, float3 pos, float3 N, float3 V, samplerCUBE SmallEnvMapSampler, samplerCUBE DistanceEnvMapSampler) // Phong-Blinn
|
---|
89 | // L: a hossza lényeges (az egységkocka faláig ér)
|
---|
90 | {
|
---|
91 | float mindist = 1.0;
|
---|
92 |
|
---|
93 | float kd = 0.3; // 0.3
|
---|
94 | float ks = 0; // 0.5
|
---|
95 | float shininess = 10;
|
---|
96 |
|
---|
97 | float l = length(L);
|
---|
98 | L = normalize(L);
|
---|
99 |
|
---|
100 | //dw
|
---|
101 | float dw = 4 / (REDUCED_CUBEMAP_SIZE*REDUCED_CUBEMAP_SIZE*l*l*l + 4/3.1416f);
|
---|
102 | //Lin
|
---|
103 | float4 Lin = readCubeMap(SmallEnvMapSampler, L);
|
---|
104 | //r
|
---|
105 | float doy = readDistanceCubeMap(DistanceEnvMapSampler, L);
|
---|
106 | float dxy = length(L * doy - pos);
|
---|
107 |
|
---|
108 | dxy = max(mindist, dxy);
|
---|
109 |
|
---|
110 |
|
---|
111 | //dws
|
---|
112 | float dws = (doy*doy * dw) / (dxy*dxy*(1 - dw/3.1416f) + doy*doy*dw/3.1416f); // localization:
|
---|
113 | //float dws = dw;
|
---|
114 |
|
---|
115 | //L = L * doy - pos; // L: x->y, az objektumtól induljon, ne a középpontból
|
---|
116 | L = normalize(L);
|
---|
117 | float3 H = normalize(L + V); // felezõvektor
|
---|
118 |
|
---|
119 | float a = kd * max(dot(N,L),0) +
|
---|
120 | ks * pow(max(dot(N,H),0), shininess); // diffuse + specular
|
---|
121 |
|
---|
122 | // 1.: eddigi
|
---|
123 | //return Lin * a * dws;
|
---|
124 |
|
---|
125 | float ctheta_in = dot(N,L);
|
---|
126 | float ctheta_out = dot(N,V);
|
---|
127 |
|
---|
128 | return Lin * a * dws;
|
---|
129 | }
|
---|
130 |
|
---|
131 | float4 Diffuse_Disc2Point_PS(Shaded_OUT IN,
|
---|
132 | uniform float3 lastCenter,
|
---|
133 | uniform float3 cameraPos,
|
---|
134 | uniform samplerCUBE SmallEnvMapSampler : register(s0),
|
---|
135 | uniform samplerCUBE DistanceEnvMapSampler : register(s1),
|
---|
136 | uniform sampler2D ColorMap : register(s2),
|
---|
137 | ) : COLOR0
|
---|
138 | {
|
---|
139 | float M = REDUCED_CUBEMAP_SIZE;
|
---|
140 |
|
---|
141 | float3 N = IN.wNormal.xyz;
|
---|
142 | N = normalize( N );
|
---|
143 | float3 V = normalize(IN.wPos.xyz - cameraPos);
|
---|
144 | float3 pos = IN.wPos.xyz - lastCenter;
|
---|
145 |
|
---|
146 | float4 I = 0;
|
---|
147 | float3 L;
|
---|
148 | float4 Le;
|
---|
149 | float width = 1.0 / M;
|
---|
150 | float d;
|
---|
151 |
|
---|
152 | for (float x = 0.5; x < M; x++)
|
---|
153 | for (float y = 0.5; y < M; y++)
|
---|
154 | {
|
---|
155 | float2 p, tpos;
|
---|
156 | tpos.x = x * width;
|
---|
157 | tpos.y = y * width;
|
---|
158 |
|
---|
159 | p = tpos.xy;
|
---|
160 | p = 2.0 * p - 1.0; //-1..1
|
---|
161 |
|
---|
162 | float3 L;
|
---|
163 |
|
---|
164 | L = float3(p.x, p.y, 1);
|
---|
165 | I += Disc2Point_Contr( L * 0.75, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
166 |
|
---|
167 | L = float3(p.x, p.y, -1);
|
---|
168 | I += Disc2Point_Contr( L * 0.75, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
169 |
|
---|
170 | L = float3(p.x, 1, p.y);
|
---|
171 | I += Disc2Point_Contr( L * 0.75, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
172 |
|
---|
173 | L = float3(p.x, -1, p.y);
|
---|
174 | I += Disc2Point_Contr( L * 0.75, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
175 |
|
---|
176 | L = float3(1, p.x, p.y);
|
---|
177 | I += Disc2Point_Contr( L * 0.75, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
178 |
|
---|
179 | L = float3(-1, p.x, p.y);
|
---|
180 | I += Disc2Point_Contr( L * 0.75, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
181 | }
|
---|
182 | float kd = 1.0;
|
---|
183 | float indirect = kd * I;
|
---|
184 |
|
---|
185 |
|
---|
186 | }
|
---|
187 |
|
---|
188 | float4 Glossy_Disc2Point_PS(Shaded_OUT IN,
|
---|
189 | uniform float3 lastCenter,
|
---|
190 | uniform float3 cameraPos,
|
---|
191 | uniform samplerCUBE SmallEnvMapSampler : register(s0),
|
---|
192 | uniform samplerCUBE DistanceEnvMapSampler : register(s1)
|
---|
193 | ) : COLOR0
|
---|
194 | {
|
---|
195 | float M = REDUCED_CUBEMAP_SIZE;
|
---|
196 |
|
---|
197 | float3 N = IN.wNormal.xyz;
|
---|
198 | N = normalize( N );
|
---|
199 | float3 V = normalize(IN.wPos.xyz - cameraPos);
|
---|
200 | float3 pos = IN.wPos.xyz - lastCenter;
|
---|
201 | float3 R = reflect( V, N );
|
---|
202 |
|
---|
203 | float rr = max( max(abs(R.x), abs(R.y)), abs(R.z) ); // select the largest component
|
---|
204 | R /= rr; // scale the largest component to value +/-1
|
---|
205 |
|
---|
206 | float3 offset1 = float3(1,0,0);
|
---|
207 | float3 offset2 = float3(0,1,0);
|
---|
208 | if (abs(R.x) > abs(R.y) && abs(R.x) > abs(R.z))
|
---|
209 | offset1 = float3(0,0,1);
|
---|
210 | if (abs(R.y) > abs(R.x) && abs(R.y) > abs(R.z))
|
---|
211 | offset2 = float3(0,0,1);
|
---|
212 |
|
---|
213 |
|
---|
214 | float4 I = 0;
|
---|
215 | float3 L;
|
---|
216 | float width = 2.0 / M;
|
---|
217 |
|
---|
218 | L = R;
|
---|
219 | I += Disc2Point_Contr( L * 0.75, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
220 |
|
---|
221 | L = R + offset1 * width;
|
---|
222 | I += Disc2Point_Contr( L * 0.75, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
223 |
|
---|
224 | L = R - offset1 * width;
|
---|
225 | I += Disc2Point_Contr( L * 0.75, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
226 |
|
---|
227 | L = R + offset2 * width;
|
---|
228 | I += Disc2Point_Contr( L * 0.75, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
229 |
|
---|
230 | L = R - offset2 * width;
|
---|
231 | I += Disc2Point_Contr( L * 0.75, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
232 |
|
---|
233 |
|
---|
234 | float kd = 1.0;
|
---|
235 | //return readCubeMap(SmallEnvMapSampler, pos) + lastCenter.x*0.0000000001;
|
---|
236 | return kd * I * 2 * M;
|
---|
237 | } |
---|