Changeset 1590 for GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs
- Timestamp:
- 10/09/06 21:27:06 (18 years ago)
- Location:
- GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/GameTools_Diffuse.hlsl
r1565 r1590 11 11 { 12 12 float dist = texCUBE(dcm, float3(coord.xy, - coord.z)).r; 13 if(dist == 0) dist = 100000 ; ///sky13 if(dist == 0) dist = 1000000; ///sky 14 14 return dist; 15 15 } … … 48 48 /////////////////// 49 49 50 float4 GetContibution(float3 L1, float3 L2, float3 L3, float3 L4, float3 pos, float3 N, samplerCUBE SmallEnvMapSampler, float d) 51 { 52 /* 53 L1 = readDistanceCubeMap(DistanceEnvMapSampler, L1) * normalize(L1); 54 L2 = readDistanceCubeMap(DistanceEnvMapSampler, L2) * normalize(L2); 55 L3 = readDistanceCubeMap(DistanceEnvMapSampler, L3) * normalize(L3); 56 L4 = readDistanceCubeMap(DistanceEnvMapSampler, L4) * normalize(L4); 57 */ 58 50 float4 GetContibution(float3 L, float3 L1, float3 L2, float3 L3, float3 L4, float3 pos, float3 N, samplerCUBE cubemap) 51 { 52 float d; 53 //d = texCUBE(cubemap, L).a; 54 d = readDistanceCubeMap(cubemap, L1).r; 59 55 L1 = d * normalize(L1); 56 d = readDistanceCubeMap(cubemap, L2).r; 60 57 L2 = d * normalize(L2); 61 L3 = d * normalize(L3); 58 d = readDistanceCubeMap(cubemap, L3).r; 59 L3 = d * normalize(L3); 60 d = readDistanceCubeMap(cubemap, L4).r; 62 61 L4 = d * normalize(L4); 63 62 64 63 65 64 float3 r1 = normalize(L1 - pos); … … 67 66 float3 r3 = normalize(L3 - pos); 68 67 float3 r4 = normalize(L4 - pos); 69 70 71 float kd = 0.3; // 0.3 72 68 /* 73 69 float tri1 = acos(dot(r1, r2)) * dot(cross(r1, r2), N); 74 70 float tri2 = acos(dot(r2, r3)) * dot(cross(r2, r3), N); 75 71 float tri3 = acos(dot(r3, r4)) * dot(cross(r3, r4), N); 76 72 float tri4 = acos(dot(r4, r1)) * dot(cross(r4, r1), N); 77 73 */ 74 float3 crossP = cross(r1, r2); 75 float r = length(crossP); 76 float dd = dot(r1,r2); 77 float tri1 = acos(dd) * dot(crossP/r, N); 78 79 crossP = cross(r2, r3); 80 r = length(crossP); 81 dd = dot(r1,r2); 82 float tri2 = acos(dd) * dot(crossP/r, N); 83 84 crossP = cross(r3, r4); 85 r = length(crossP); 86 dd = dot(r1,r2); 87 float tri3 = acos(dd) * dot(crossP/r, N); 88 89 crossP = cross(r4, r1); 90 r = length(crossP); 91 dd = dot(r1,r2); 92 float tri4= acos(dd) * dot(crossP/r, N); 93 94 78 95 return max(tri1 + tri2 + tri3 + tri4, 0); 79 } 96 //return tri1 + tri2 + tri3 + tri4; 97 } 98 80 99 81 100 struct vertOUT … … 97 116 OUT.hPos = mul(worldViewProj, position); 98 117 OUT.wPos = mul(world, position).xyz; 99 //mNormal = mul(normal, world_IT);100 OUT.mNormal = normal;118 OUT.mNormal = mul(world, normal); 119 //OUT.mNormal = normal; 101 120 OUT.texCoord = texCoord; 102 121 return OUT; 103 122 } 104 123 105 /* 124 106 125 float4 DiffusePS( vertOUT IN, 107 126 uniform float3 cameraPos, 108 127 uniform float3 lastCenter, //LI// 109 128 uniform samplerCUBE SmallEnvMapSampler : register(s0), 110 uniform samplerCUBE DistanceEnvMapSampler : register(s1), 111 uniform float4x4 world_IT 129 uniform samplerCUBE DistanceEnvMapSampler : register(s1) 112 130 ) : COLOR0 113 131 { 114 REDUCED_CUBEMAP_SIZE = 4; 115 float width = 1.0 /REDUCED_CUBEMAP_SIZE; 116 117 normalize( V ); 118 float3 V = normalize(IN.wPos - cameraPos); // 119 float3 N = mul( float4(IN.mNormal,1), world_IT).xyz; 120 //float3 N = IN.mNormal; 132 float M = 4; 133 134 float3 N = IN.mNormal; 121 135 N = normalize( N ); 122 float3 R = reflect(V, N);123 136 float3 pos = IN.wPos - lastCenter; 124 125 float4 intens = 0; 126 //intens = readCubeMap(SmallEnvMapSampler, pos); 127 128 float3 L1, L2, L3, L4, L; 129 float4 Le; 130 float d; 131 132 for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++) // az envmap minden texelére 133 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++) 134 { 135 float2 p, tpos; 136 tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE; // 0..1 137 tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE; // 0..1 138 139 p.x = tpos.x; 140 p.y = 1-tpos.y; 141 p.xy = 2 * p.xy - 1; // -1..1 // az adott texel középpont pozíciója 142 137 138 // return float4(N,1)+ lastCenter.x*0.000001; 139 // return readCubeMap(SmallEnvMapSampler, pos) + lastCenter.x*0.000001; 140 141 float4 I = 0; 142 float3 L1, L2, L3, L4, L; 143 float4 Le; 144 float width = 1.0 / M; 145 float width2 = width * 2; 146 float d; 147 148 for (float x = 0.5; x < M; x++) 149 for (float y = 0.5; y < M; y++) 150 { 151 float2 p, tpos; 152 tpos.x = x * width; 153 tpos.y = y * width; 154 155 p = tpos.xy; 156 p = 2.0 * p - 1.0; //-1..1 157 143 158 L1 = float3(p.x - width, p.y - width, 1); 144 159 L2 = float3(p.x + width, p.y - width, 1); … … 146 161 L4 = float3(p.x - width, p.y + width, 1); 147 162 L = float3(p.x, p.y, 1); 148 Le = readCubeMap(SmallEnvMapSampler, L);149 d = readDistanceCubeMap(DistanceEnvMapSampler, L);150 intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d);151 }152 153 for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++) // az envmap minden texelére154 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++)155 {156 float2 p, tpos;157 tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE; // 0..1158 tpos. y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE; // 0..1159 160 p.x = tpos.x;161 p .y = 1-tpos.y;162 p .xy = 2 * p.xy - 1; // -1..1 // az adott texel középpont pozíciója163 163 Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1); 164 165 I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler); 166 167 } 168 169 for (float x = 0.5; x < M; x++) 170 for (float y = 0.5; y < M; y++) 171 { 172 float2 p, tpos; 173 tpos.x = x * width; // 0..1 174 tpos.y = y * width; // 0..1 175 176 p = tpos.xy; 177 p = 2.0 * p - 1.0; //-1..1 178 164 179 L4 = float3(p.x - width, p.y - width, -1); 165 180 L3 = float3(p.x + width, p.y - width, -1); … … 167 182 L1 = float3(p.x - width, p.y + width, -1); 168 183 L = float3(p.x, p.y, -1); 169 Le = readCubeMap(SmallEnvMapSampler, L); 170 d = readDistanceCubeMap(DistanceEnvMapSampler, L); 171 intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d); 172 } 173 174 for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++) // az envmap minden texelére 175 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++) 176 { 177 float2 p, tpos; 178 tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE; // 0..1 179 tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE; // 0..1 180 181 p.x = tpos.x; 182 p.y = 1-tpos.y; 183 p.xy = 2 * p.xy - 1; // -1..1 // az adott texel középpont pozíciója 184 184 Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1); 185 186 I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler); 187 } 188 189 for (float x = 0.5; x < M; x++) 190 for (float y = 0.5; y < M; y++) 191 { 192 float2 p, tpos; 193 tpos.x = x * width; // 0..1 194 tpos.y = y * width; // 0..1 195 196 p = tpos.xy; 197 p = 2.0 * p - 1.0; //-1..1 198 185 199 L4 = float3(p.x - width, 1, p.y - width); 186 200 L3 = float3(p.x + width, 1, p.y - width); … … 188 202 L1 = float3(p.x - width, 1, p.y + width); 189 203 L = float3(p.x, 1, p.y); 190 Le = readCubeMap(SmallEnvMapSampler, L); 191 d = readDistanceCubeMap(DistanceEnvMapSampler, L); 192 intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d); 193 } 194 195 for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++) // az envmap minden texelére 196 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++) 197 { 198 float2 p, tpos; 199 tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE; // 0..1 200 tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE; // 0..1 201 202 p.x = tpos.x; 203 p.y = 1-tpos.y; 204 p.xy = 2 * p.xy - 1; // -1..1 // az adott texel középpont pozíciója 205 204 Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1); 205 206 I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler); 207 } 208 209 for (float x = 0.5; x < M; x++) 210 for (float y = 0.5; y < M; y++) 211 { 212 float2 p, tpos; 213 tpos.x = x * width; // 0..1 214 tpos.y = y * width; // 0..1 215 216 p = tpos.xy; 217 p = 2.0 * p - 1.0; //-1..1 218 206 219 L1 = float3(p.x - width, -1, p.y - width); 207 220 L2 = float3(p.x + width, -1, p.y - width); … … 209 222 L4 = float3(p.x - width, -1, p.y + width); 210 223 L = float3(p.x, -1, p.y); 211 Le = readCubeMap(SmallEnvMapSampler, L); 212 d = readDistanceCubeMap(DistanceEnvMapSampler, L); 213 intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d); 214 } 215 216 for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++) // az envmap minden texelére 217 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++) 218 { 219 float2 p, tpos; 220 tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE; // 0..1 221 tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE; // 0..1 222 223 p.x = tpos.x; 224 p.y = 1-tpos.y; 225 p.xy = 2 * p.xy - 1; // -1..1 // az adott texel középpont pozíciója 226 224 Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1); 225 226 I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler); 227 } 228 229 for (float x = 0.5; x < M; x++) 230 for (float y = 0.5; y < M; y++) 231 { 232 float2 p, tpos; 233 tpos.x = x * width; // 0..1 234 tpos.y = y * width; // 0..1 235 236 p = tpos.xy; 237 p = 2.0 * p - 1.0; //-1..1 238 227 239 L1 = float3(1, p.x - width, p.y - width); 228 240 L2 = float3(1, p.x + width, p.y - width); … … 230 242 L4 = float3(1, p.x - width, p.y + width); 231 243 L = float3(1, p.x, p.y); 232 Le = readCubeMap(SmallEnvMapSampler, L);233 d = readDistanceCubeMap(DistanceEnvMapSampler, L);234 intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d);244 Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1); 245 246 I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler); 235 247 } 236 237 for (int x = 0; x < REDUCED_CUBEMAP_SIZE - 1; x++) // az envmap minden texelére 238 for (int y = 0; y < REDUCED_CUBEMAP_SIZE - 1; y++) 239 { 240 float2 p, tpos; 241 tpos.x = (x + 0.5) /(float)REDUCED_CUBEMAP_SIZE; // 0..1 242 tpos.y = (y + 0.5) /(float)REDUCED_CUBEMAP_SIZE; // 0..1 243 244 p.x = tpos.x; 245 p.y = 1-tpos.y; 246 p.xy = 2 * p.xy - 1; // -1..1 // az adott texel középpont pozíciója 247 248 249 for (float x = 0.5; x < M; x++) 250 for (float y = 0.5; y < M; y++) 251 { 252 float2 p, tpos; 253 tpos.x = x * width; // 0..1 254 tpos.y = y * width; // 0..1 255 256 p = tpos.xy; 257 p = 2.0 * p - 1.0; //-1..1 258 248 259 L4 = float3(-1, p.x - width, p.y - width); 249 260 L3 = float3(-1, p.x + width, p.y - width); … … 251 262 L1 = float3(-1, p.x - width, p.y + width); 252 263 L = float3(-1, p.x, p.y); 253 Le = readCubeMap(SmallEnvMapSampler, L); 254 d = readDistanceCubeMap(DistanceEnvMapSampler, L); 255 intens += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, SmallEnvMapSampler, d); 256 } 257 258 259 //return float4(pos, 1); 260 return intens; 261 } 262 263 */ 264 265 266 float4 DiffusePS( vertOUT IN, 267 uniform float3 cameraPos, 268 uniform float3 lastCenter, //LI// 269 uniform samplerCUBE SmallEnvMapSampler : register(s0), 270 uniform samplerCUBE DistanceEnvMapSampler : register(s1), 271 uniform float4x4 world_IT 272 ) : COLOR0 273 { 274 float3 corner[] = {float3(1,-1,1),// float3(0,0,1), 275 float3(-1,-1,-1), // 0,0,-1 276 float3(-1,1,-1), // 0,1,0 277 float3(-1,-1, 1), // 0,-1,0 278 float3(1,-1,-1), // 1,0,0 279 float3(-1,-1, 1)}; // -1,0,0 280 281 float3 right[] = {float3(-1,0,0), 282 float3(1,0,0), 283 float3(1,0,0), 284 float3(-1,0,0), 285 float3(0,0,1), 286 float3(0,0,-1)}; 287 288 float3 up[] = {float3(0,1,0), 289 float3(0,1,0), 290 float3(0,0,1), 291 float3(0,0,1), 292 float3(0,1,0), 293 float3(0,1,0)}; 294 295 REDUCED_CUBEMAP_SIZE = 4; 296 float width = 1.0 /REDUCED_CUBEMAP_SIZE; 297 298 //V = /*-*/normalize( V ); 299 float3 V = normalize(IN.wPos - cameraPos); // 300 float3 N = mul( float4(IN.mNormal,1), world_IT).xyz; 301 //float3 N = IN.mNormal; 264 Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1); 265 266 I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler); 267 } 268 float kd = 0.3; 269 return kd * I; 270 } 271 272 float4 P2PContr(float3 N, float3 Nl, float3 pos, float3 L, samplerCUBE cubemap, samplerCUBE distmap) 273 { 274 Nl = normalize(Nl); 275 L = normalize(L); 276 float4 Le = readCubeMap(cubemap, L); 277 float d = readDistanceCubeMap(distmap, L); 278 float3 Lpos = L * d; 279 float3 Ldir = Lpos - pos; 280 float dist = dot(Ldir, Ldir); 281 Ldir = normalize(Ldir); 282 dist /= 10000.0; 283 return Le * max(dot(N, Ldir),0) * dot(Nl, -1 * Ldir) / dist; 284 } 285 286 float4 EnvMapDiffuseP2PPS( vertOUT IN, 287 uniform float3 lastCenter, //LI// 288 uniform samplerCUBE SmallEnvMapSampler : register(s0), 289 uniform samplerCUBE DistanceEnvMapSampler : register(s1)) : COLOR 290 { 291 float M = 4.0; 292 293 //float3 N = mul( world_IT, float4(IN.mNormal,1)).xyz; 294 float3 N = IN.mNormal; 302 295 N = normalize( N ); 303 float3 R = reflect(V, N); 304 float3 pos = IN.wPos - lastCenter; 305 306 float4 intens = 0; 307 //intens = readCubeMap(SmallEnvMapSampler, pos); 308 309 float3 L1, L2, L3, L4, L; 310 float4 Le; 311 float d; 312 /* 313 for (int x = 0; x < REDUCED_CUBEMAP_SIZE; x++) 314 for (int y = 0; y < REDUCED_CUBEMAP_SIZE; y++) 315 for (int f = 0; f < 6; f++) 316 { 317 318 float2 p, tpos; 319 tpos.x = (x + 0.5) * 2 * width; // 0..2 320 tpos.y = (y + 0.5) * 2 * width; // 0..2 321 322 p = tpos; 323 324 L = corner[0] + p.x * right[0] + p.y * up[0]; 325 L1 = normalize(L - width * right[f] + width * up[f]); 326 L2 = normalize(L + width * right[f] + width * up[f]); 327 L3 = normalize(L + width * right[f] - width * up[f]); 328 L4 = normalize(L - width * right[f] - width * up[f]); 329 Le = texCUBE(SmallEnvMapSampler, L); 330 d = texCUBE(DistanceEnvMapSampler, L).r; 331 332 L1 = d * L1; 333 L2 = d * L2; 334 L3 = d * L3; 335 L4 = d * L4; 336 337 float3 r1 = normalize(L1 - pos); 338 float3 r2 = normalize(L2 - pos); 339 float3 r3 = normalize(L3 - pos); 340 float3 r4 = normalize(L4 - pos); 341 342 float tri1 = acos(dot(r1, r2)) * dot(cross(r1, r2), N); 343 float tri2 = acos(dot(r2, r3)) * dot(cross(r2, r3), N); 344 float tri3 = acos(dot(r3, r4)) * dot(cross(r3, r4), N); 345 float tri4 = acos(dot(r4, r1)) * dot(cross(r4, r1), N); 346 347 // intens += Le / 16.0 + pos.x*0.000000000001; 348 intens += 0.5 * Le * max(tri1 + tri2 + tri3 + tri4, 0); 349 }*/ 350 351 return intens; 352 } 296 float3 pos = IN.wPos - lastCenter; 297 298 float4 I = 0; 299 float3 L; 300 float4 Le; 301 float width = 1.0 / M; 302 float d; 303 304 //float vdir = length(pos); 305 //return (readDistanceCubeMap(DistanceEnvMapSampler, pos) - vdir); 306 //return readCubeMap(SmallEnvMapSampler, pos); 307 //return readDistanceCubeMap(DistanceEnvMapSampler, pos); 308 309 for (float x = 0.5; x < M; x++) 310 for (float y = 0.5; y < M; y++) 311 { 312 float2 p, tpos; 313 tpos.x = x * width; 314 tpos.y = y * width; 315 316 p = tpos.xy; 317 p = 2.0 * p - 1.0; //-1..1 318 319 I += P2PContr(N, float3(0,0,-1), pos, float3(p.x, p.y, 1), SmallEnvMapSampler, DistanceEnvMapSampler); 320 I += P2PContr(N, float3(0,0,1), pos, float3(-p.x, p.y, -1), SmallEnvMapSampler, DistanceEnvMapSampler); 321 I += P2PContr(N, float3(-1,0,0), pos, float3(1, p.y, -p.x), SmallEnvMapSampler, DistanceEnvMapSampler); 322 I += P2PContr(N, float3(1,0,0), pos, float3(-1, p.y, p.x), SmallEnvMapSampler, DistanceEnvMapSampler); 323 I += P2PContr(N, float3(0,-1,0), pos, float3(p.x, 1, -p.y), SmallEnvMapSampler, DistanceEnvMapSampler); 324 I += P2PContr(N, float3(0,1,0), pos, float3(p.x, -1, p.y), SmallEnvMapSampler, DistanceEnvMapSampler); 325 } 326 327 float kd = 0.8; 328 return kd * I; 329 }
Note: See TracChangeset
for help on using the changeset viewer.