1 | ///////////////////////////////////////////////////////////////////////////////
|
---|
2 | //
|
---|
3 | // ## ######
|
---|
4 | // ###### ###
|
---|
5 | // ## ############### Shark 3D Engine (www.shark3d.com)
|
---|
6 | // ########## # # #
|
---|
7 | // ######## Copyright (c) 1996-2006 Spinor GmbH.
|
---|
8 | // ######### # # # All rights reserved.
|
---|
9 | // ## ##########
|
---|
10 | // ##
|
---|
11 | //
|
---|
12 | ///////////////////////////////////////////////////////////////////////////////
|
---|
13 |
|
---|
14 |
|
---|
15 | //#define Poly2Point
|
---|
16 |
|
---|
17 | struct PS_INPUT
|
---|
18 | {
|
---|
19 | float3 normalWorld : TEXCOORD1;
|
---|
20 | float3 posWorld : TEXCOORD0;
|
---|
21 | };
|
---|
22 |
|
---|
23 | sampler colorSamp: register(s0);
|
---|
24 | sampler distanceSamp: register(s1);
|
---|
25 |
|
---|
26 | ///////////////////////////////////////////////////////////////////////////////
|
---|
27 | // Pixelshader
|
---|
28 | // Profile: 3x0
|
---|
29 |
|
---|
30 | #ifdef Poly2Point
|
---|
31 |
|
---|
32 | float4 GetContibution(float3 L, float3 L1, float3 L2, float3 L3, float3 L4, float3 pos, float3 N, samplerCUBE cubemap)
|
---|
33 | {
|
---|
34 | float d;
|
---|
35 | d = texCUBE(cubemap, L1).r;
|
---|
36 | L1 = d * normalize(L1);
|
---|
37 | d = texCUBE(cubemap, L2).r;
|
---|
38 | L2 = d * normalize(L2);
|
---|
39 | d = texCUBE(cubemap, L3).r;
|
---|
40 | L3 = d * normalize(L3);
|
---|
41 | d = texCUBE(cubemap, L4).r;
|
---|
42 | L4 = d * normalize(L4);
|
---|
43 |
|
---|
44 |
|
---|
45 | float3 r1 = normalize(L1 - pos);
|
---|
46 | float3 r2 = normalize(L2 - pos);
|
---|
47 | float3 r3 = normalize(L3 - pos);
|
---|
48 | float3 r4 = normalize(L4 - pos);
|
---|
49 |
|
---|
50 | float3 crossP = cross(r1, r2);
|
---|
51 | float r = length(crossP);
|
---|
52 | float dd = dot(r1,r2);
|
---|
53 | float tri1 = acos(dd) * dot(crossP/r, N);
|
---|
54 |
|
---|
55 | crossP = cross(r2, r3);
|
---|
56 | r = length(crossP);
|
---|
57 | dd = dot(r1,r2);
|
---|
58 | float tri2 = acos(dd) * dot(crossP/r, N);
|
---|
59 |
|
---|
60 | crossP = cross(r3, r4);
|
---|
61 | r = length(crossP);
|
---|
62 | dd = dot(r1,r2);
|
---|
63 | float tri3 = acos(dd) * dot(crossP/r, N);
|
---|
64 |
|
---|
65 | crossP = cross(r4, r1);
|
---|
66 | r = length(crossP);
|
---|
67 | dd = dot(r1,r2);
|
---|
68 | float tri4= acos(dd) * dot(crossP/r, N);
|
---|
69 |
|
---|
70 |
|
---|
71 | return max(tri1 + tri2 + tri3 + tri4, 0);
|
---|
72 | }
|
---|
73 |
|
---|
74 | float4 main(PS_INPUT input): COLOR0
|
---|
75 | {
|
---|
76 | float M = 4;
|
---|
77 |
|
---|
78 | float3 N = normalize( input.normalWorld );
|
---|
79 | float3 pos = input.posWorld;
|
---|
80 |
|
---|
81 | //return texCUBE(colorSamp, N);
|
---|
82 |
|
---|
83 | float4 I = 0;
|
---|
84 | float3 L1, L2, L3, L4, L;
|
---|
85 | float4 Le;
|
---|
86 | float width = 1.0 / M;
|
---|
87 | float width2 = width * 2;
|
---|
88 | float d;
|
---|
89 |
|
---|
90 | for (float x = 1; x < M; x++)
|
---|
91 | for (float y = 1; y < M; y++)
|
---|
92 | {
|
---|
93 | float2 p, tpos;
|
---|
94 | tpos.x = x * width;
|
---|
95 | tpos.y = y * width;
|
---|
96 |
|
---|
97 | p = tpos.xy;
|
---|
98 | p = 2.0 * p - 1.0; //-1..1
|
---|
99 |
|
---|
100 | L1 = float3(p.x - width, p.y - width, 1);
|
---|
101 | L2 = float3(p.x + width, p.y - width, 1);
|
---|
102 | L3 = float3(p.x + width, p.y + width, 1);
|
---|
103 | L4 = float3(p.x - width, p.y + width, 1);
|
---|
104 | L = float3(p.x, p.y, 1);
|
---|
105 | Le = float4(texCUBE(colorSamp, L).rgb, 1);
|
---|
106 |
|
---|
107 | I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, distanceSamp);
|
---|
108 |
|
---|
109 | }
|
---|
110 |
|
---|
111 | for (float x = 1; x < M; x++)
|
---|
112 | for (float y = 1; y < M; y++)
|
---|
113 | {
|
---|
114 | float2 p, tpos;
|
---|
115 | tpos.x = x * width;// 0..1
|
---|
116 | tpos.y = y * width;// 0..1
|
---|
117 |
|
---|
118 | p = tpos.xy;
|
---|
119 | p = 2.0 * p - 1.0; //-1..1
|
---|
120 |
|
---|
121 | L4 = float3(p.x - width, p.y - width, -1);
|
---|
122 | L3 = float3(p.x + width, p.y - width, -1);
|
---|
123 | L2 = float3(p.x + width, p.y + width, -1);
|
---|
124 | L1 = float3(p.x - width, p.y + width, -1);
|
---|
125 | L = float3(p.x, p.y, -1);
|
---|
126 | Le = float4(texCUBE(colorSamp, L).rgb, 1);
|
---|
127 |
|
---|
128 | I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, distanceSamp);
|
---|
129 | }
|
---|
130 |
|
---|
131 | for (float x = 1; x < M; x++)
|
---|
132 | for (float y = 1; y < M; y++)
|
---|
133 | {
|
---|
134 | float2 p, tpos;
|
---|
135 | tpos.x = x * width;// 0..1
|
---|
136 | tpos.y = y * width;// 0..1
|
---|
137 |
|
---|
138 | p = tpos.xy;
|
---|
139 | p = 2.0 * p - 1.0; //-1..1
|
---|
140 |
|
---|
141 | L4 = float3(p.x - width, 1, p.y - width);
|
---|
142 | L3 = float3(p.x + width, 1, p.y - width);
|
---|
143 | L2 = float3(p.x + width, 1, p.y + width);
|
---|
144 | L1 = float3(p.x - width, 1, p.y + width);
|
---|
145 | L = float3(p.x, 1, p.y);
|
---|
146 | Le = float4(texCUBE(colorSamp, L).rgb, 1);
|
---|
147 |
|
---|
148 | I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, distanceSamp);
|
---|
149 | }
|
---|
150 |
|
---|
151 | for (float x = 1; x < M; x++)
|
---|
152 | for (float y = 1; y < M; y++)
|
---|
153 | {
|
---|
154 | float2 p, tpos;
|
---|
155 | tpos.x = x * width;// 0..1
|
---|
156 | tpos.y = y * width;// 0..1
|
---|
157 |
|
---|
158 | p = tpos.xy;
|
---|
159 | p = 2.0 * p - 1.0; //-1..1
|
---|
160 |
|
---|
161 | L1 = float3(p.x - width, -1, p.y - width);
|
---|
162 | L2 = float3(p.x + width, -1, p.y - width);
|
---|
163 | L3 = float3(p.x + width, -1, p.y + width);
|
---|
164 | L4 = float3(p.x - width, -1, p.y + width);
|
---|
165 | L = float3(p.x, -1, p.y);
|
---|
166 | Le = float4(texCUBE(colorSamp, L).rgb, 1);
|
---|
167 |
|
---|
168 | I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, distanceSamp);
|
---|
169 | }
|
---|
170 |
|
---|
171 | for (float x = 1; x < M; x++)
|
---|
172 | for (float y = 1; y < M; y++)
|
---|
173 | {
|
---|
174 | float2 p, tpos;
|
---|
175 | tpos.x = x * width;// 0..1
|
---|
176 | tpos.y = y * width;// 0..1
|
---|
177 |
|
---|
178 | p = tpos.xy;
|
---|
179 | p = 2.0 * p - 1.0; //-1..1
|
---|
180 |
|
---|
181 | L1 = float3(1, p.x - width, p.y - width);
|
---|
182 | L2 = float3(1, p.x + width, p.y - width);
|
---|
183 | L3 = float3(1, p.x + width, p.y + width);
|
---|
184 | L4 = float3(1, p.x - width, p.y + width);
|
---|
185 | L = float3(1, p.x, p.y);
|
---|
186 | Le = float4(texCUBE(colorSamp, L).rgb, 1);
|
---|
187 |
|
---|
188 | I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, distanceSamp);
|
---|
189 | }
|
---|
190 |
|
---|
191 | for (float x = 1; x < M; x++)
|
---|
192 | for (float y = 1; y < M; y++)
|
---|
193 | {
|
---|
194 | float2 p, tpos;
|
---|
195 | tpos.x = x * width;// 0..1
|
---|
196 | tpos.y = y * width;// 0..1
|
---|
197 |
|
---|
198 | p = tpos.xy;
|
---|
199 | p = 2.0 * p - 1.0; //-1..1
|
---|
200 |
|
---|
201 | L4 = float3(-1, p.x - width, p.y - width);
|
---|
202 | L3 = float3(-1, p.x + width, p.y - width);
|
---|
203 | L2 = float3(-1, p.x + width, p.y + width);
|
---|
204 | L1 = float3(-1, p.x - width, p.y + width);
|
---|
205 | L = float3(-1, p.x, p.y);
|
---|
206 | Le = float4(texCUBE(colorSamp, L).rgb, 1);
|
---|
207 |
|
---|
208 | I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, distanceSamp);
|
---|
209 | }
|
---|
210 | float kd = 0.5;
|
---|
211 | return 1;
|
---|
212 | return kd * I;
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 |
|
---|
217 | #else
|
---|
218 |
|
---|
219 | float4 P2PContr(float3 N, float3 Nl, float3 pos, float3 L, samplerCUBE cubemap, samplerCUBE distmap)
|
---|
220 | {
|
---|
221 | Nl = normalize(Nl);
|
---|
222 | L = normalize(L);
|
---|
223 | float4 Le = texCUBE(cubemap, L);
|
---|
224 | float d = texCUBE(distmap, L).r;
|
---|
225 | float3 Lpos = L * d;
|
---|
226 | float3 Ldir = Lpos - pos;
|
---|
227 | float dist2 = dot(Ldir, Ldir);
|
---|
228 | if (dist2<0.5) dist2 = 0.5;
|
---|
229 | float dist = 4 * dist2;
|
---|
230 | Ldir = normalize(Ldir);
|
---|
231 |
|
---|
232 | return Le * (max(dot(N, Ldir),0) * dot(Nl, -1 * Ldir)) / dist;
|
---|
233 | }
|
---|
234 |
|
---|
235 | float4 main(PS_INPUT input): COLOR0
|
---|
236 | {
|
---|
237 | float M = 4.0;
|
---|
238 |
|
---|
239 | float3 N = normalize( input.normalWorld );
|
---|
240 | float3 pos = input.posWorld;
|
---|
241 |
|
---|
242 | //return texCUBE(distanceSamp, N).r;
|
---|
243 |
|
---|
244 | //return float4(N,1);
|
---|
245 |
|
---|
246 | float4 I = 0;
|
---|
247 | float3 L;
|
---|
248 | float4 Le;
|
---|
249 | float width = 1.0 / M;
|
---|
250 | float d;
|
---|
251 |
|
---|
252 | float kd = 1.0;
|
---|
253 |
|
---|
254 | for (float x = 0.5; x < M; x++)
|
---|
255 | for (float y = 0.5; y < M; y++)
|
---|
256 | {
|
---|
257 | float2 p, tpos;
|
---|
258 | tpos.x = x * width;
|
---|
259 | tpos.y = y * width;
|
---|
260 |
|
---|
261 | p = tpos.xy;
|
---|
262 | p = 2.0 * p - 1.0; //-1..1
|
---|
263 |
|
---|
264 | I += P2PContr(N, float3(0,0,-1), pos, float3(p.x, p.y, 1), colorSamp, distanceSamp);
|
---|
265 | I += P2PContr(N, float3(0,0,1), pos, float3(-p.x, p.y, -1), colorSamp, distanceSamp);
|
---|
266 | I += P2PContr(N, float3(-1,0,0), pos, float3(1, p.y, -p.x), colorSamp, distanceSamp);
|
---|
267 | I += P2PContr(N, float3(1,0,0), pos, float3(-1, p.y, p.x), colorSamp, distanceSamp);
|
---|
268 | I += P2PContr(N, float3(0,-1,0), pos, float3(p.x, 1, -p.y), colorSamp, distanceSamp);
|
---|
269 | I += P2PContr(N, float3(0,1,0), pos, float3(p.x, -1, p.y), colorSamp, distanceSamp);
|
---|
270 | }
|
---|
271 |
|
---|
272 | //return 1;
|
---|
273 | return kd * I;
|
---|
274 |
|
---|
275 | }
|
---|
276 | #endif
|
---|
277 | ///////////////////////////////////////////////////////////////////////////////
|
---|