[3255] | 1 | #define SPOT_ANGLE 2.093
|
---|
| 2 | #define SPOT_FALLOFF 1
|
---|
| 3 | #define SHADOW_COLOR float4(0.0,0.0,0.0,1.0)
|
---|
| 4 | #define SHADOW_BIAS_POINT 0.0025
|
---|
| 5 | #define SHADOW_EPSILON_POINT 0.000001
|
---|
| 6 |
|
---|
| 7 | float4 Illumination(float3 N, float3 L, float3 V, float4 lightColor, float4 lightRange, float4 diffuseColor, float specularity, float4 specularColor)
|
---|
| 8 | {
|
---|
| 9 | // Blinn lighting
|
---|
| 10 | float d = length(L);
|
---|
| 11 | L = normalize(L);
|
---|
| 12 | float3 H = normalize(L + V);
|
---|
| 13 | float4 Lighting = lit(dot(N, L), dot(N, H), specularity);
|
---|
| 14 | Lighting = saturate(Lighting);
|
---|
| 15 |
|
---|
| 16 | return lightColor * (Lighting.y * diffuseColor + Lighting.z * specularColor) //color
|
---|
| 17 | / (lightRange.y + d * lightRange.z + d * d * lightRange.w) //attenuation
|
---|
| 18 | ;
|
---|
| 19 |
|
---|
| 20 | //return lightColor * (Lighting.y * diffuseColor);
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | #define HEIGHT_SCALE 0.01
|
---|
| 24 | #define HEIGHT_BIAS 0
|
---|
| 25 | #define PARALLAX_ITERATION 4
|
---|
| 26 |
|
---|
| 27 | float2 PARALLAX_MAPPING_OFFSET_LIMIT(sampler2D heightMap, float2 TexCoord, float3 View)
|
---|
| 28 | {
|
---|
| 29 | float4 Normal = tex2D(heightMap, TexCoord);
|
---|
| 30 | Normal.xy = (Normal.xy *2.0) - 1.0;
|
---|
| 31 | float h = Normal.a * HEIGHT_SCALE + HEIGHT_BIAS;
|
---|
| 32 | return TexCoord + h * View.xy;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | float2 PARALLAX_MAPPING_ITER(sampler2D heightMap, float2 TexCoord, float3 View)
|
---|
| 36 | {
|
---|
| 37 | for(int i = 0; i < PARALLAX_ITERATION; i++) {
|
---|
| 38 | float4 Normal = tex2D(heightMap, TexCoord);
|
---|
| 39 | Normal.xy = (Normal.xy *2.0) - 1.0;
|
---|
| 40 | float h = Normal.a * HEIGHT_SCALE + HEIGHT_BIAS;
|
---|
| 41 | TexCoord += h * Normal.z * View;
|
---|
| 42 | }
|
---|
| 43 | return TexCoord.xy;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | float3 ParallaxMap(float3 tangent, float3 binormal, float3 normal, float3 View, inout float2 texCoord, sampler2D heightMap)
|
---|
| 47 | {
|
---|
| 48 | normal = normalize(normal);
|
---|
| 49 | tangent = normalize(tangent);
|
---|
| 50 | binormal = normalize(binormal);
|
---|
| 51 | float3x3 TangentToModel = float3x3(tangent, binormal, normal );
|
---|
| 52 |
|
---|
| 53 | View = mul(TangentToModel, View);
|
---|
| 54 |
|
---|
| 55 | texCoord = PARALLAX_MAPPING_OFFSET_LIMIT(heightMap, texCoord, View);
|
---|
| 56 | float3 mNormal;
|
---|
| 57 | float3 tNormal = tex2D(heightMap, texCoord).rgb;
|
---|
| 58 |
|
---|
| 59 | tNormal.xy = (tNormal.xy *2.0) - 1.0;
|
---|
| 60 | mNormal = normalize( mul( tNormal, TangentToModel ) );
|
---|
| 61 |
|
---|
| 62 | return mNormal;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | float3 NormalMap(float3 tangent, float3 binormal, float3 normal, float2 texCoord, sampler2D normalMap)
|
---|
| 66 | {
|
---|
| 67 | normal = normalize(normal);
|
---|
| 68 | tangent = normalize(tangent);
|
---|
| 69 | binormal = normalize(binormal);
|
---|
| 70 | float3x3 TangentToModel = float3x3(tangent, binormal, normal );
|
---|
| 71 |
|
---|
| 72 | float3 mNormal;
|
---|
| 73 | float3 tNormal = tex2D(normalMap, texCoord).rgb;
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | tNormal.xy = (tNormal.xy *2.0) - 1.0;
|
---|
| 77 | mNormal = normalize( mul( tNormal, TangentToModel ) );
|
---|
| 78 |
|
---|
| 79 | return mNormal;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | float shadowPoint(samplerCUBE shadowMap, float3 lightCPos, float lightFarPlane)
|
---|
| 84 | {
|
---|
| 85 | float light = 1;
|
---|
| 86 |
|
---|
| 87 | float dist = length(lightCPos) / lightFarPlane;
|
---|
| 88 | float4 storedDist = texCUBE(shadowMap, float3(lightCPos.xy, -lightCPos.z));
|
---|
| 89 | // dist -= SHADOW_BIAS_POINT;
|
---|
| 90 | float lit_factor = (dist <= storedDist.r);
|
---|
| 91 |
|
---|
| 92 | float M1 = storedDist.r;
|
---|
| 93 | float M2 = storedDist.g;
|
---|
| 94 | float variance = M2 - M1 * M1;
|
---|
| 95 |
|
---|
| 96 | float v2 = min(max(variance , 0.0) + SHADOW_EPSILON_POINT, 1.0);
|
---|
| 97 | float m_d = M1 - dist;
|
---|
| 98 | float pmax = v2 / (v2 + m_d * m_d);
|
---|
| 99 |
|
---|
| 100 | light = max(lit_factor, pmax);
|
---|
| 101 | /*
|
---|
| 102 | light = saturate(M1 / dist); //Markov
|
---|
| 103 |
|
---|
| 104 | light = variance / (variance + m_d * m_d); //könyvfejezet
|
---|
| 105 | light = min(max(lit_factor, light), 1); //könyvfejezet
|
---|
| 106 |
|
---|
| 107 | light = lit_factor; //classic
|
---|
| 108 |
|
---|
| 109 | return light;
|
---|
| 110 | */
|
---|
| 111 | return SHADOW_COLOR + (1 - SHADOW_COLOR) * light;
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | uniform float4 prmAtlasTilesHalfPixel;
|
---|
| 117 | uniform float allClusterCount;
|
---|
| 118 | uniform float clusterCount;
|
---|
| 119 |
|
---|
| 120 | float4 PathMapIndirect(sampler2D PMTex, sampler2D weightIndexTex, sampler2D weightTex, float2 texAtlas)
|
---|
| 121 | {
|
---|
| 122 | int2 prmAtlasTiles = prmAtlasTilesHalfPixel.xy;
|
---|
| 123 | float2 atlasHalfPixel = prmAtlasTilesHalfPixel.zw;
|
---|
| 124 |
|
---|
| 125 | float3 col = 0;
|
---|
| 126 | for(int iCluster = 0; iCluster < 16; iCluster++)
|
---|
| 127 | {
|
---|
| 128 | float2 prmTexPos = float2(
|
---|
| 129 | (texAtlas.x + (iCluster % prmAtlasTiles.x)) / prmAtlasTiles.x,
|
---|
| 130 | 1.0 - (texAtlas.y + (iCluster / prmAtlasTiles.x)) / prmAtlasTiles.y);// + atlasHalfPixel;
|
---|
| 131 |
|
---|
| 132 | float weightIndex = tex2D(weightIndexTex, float2(((float)iCluster + 0.5) / clusterCount, 0.5)).r;
|
---|
| 133 | float3 weight = tex2D(weightTex, float2((weightIndex + 0.5)/allClusterCount, 0.5)).rgb;
|
---|
| 134 | float3 val = tex2D(PMTex, prmTexPos).xyz;
|
---|
| 135 | val = min(1,val);
|
---|
| 136 | if(length(val - float3(1,1,1)) == 0)
|
---|
| 137 | val = 0;
|
---|
| 138 | col += val.xyz * weight;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | return float4(col,1);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | #define CUBEMAP_SIZE 128
|
---|
| 145 | #define REDUCED_CUBEMAP_SIZE 4
|
---|
| 146 | #define RATE 32
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 | float4 readCubeMap(samplerCUBE cm, float3 coord)
|
---|
| 150 | {
|
---|
| 151 | float4 color = texCUBElod( cm, float4(coord.xy, -coord.z,0) );
|
---|
| 152 | color.a = 1;
|
---|
| 153 | return color;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | float readDistanceCubeMap(samplerCUBE dcm, float3 coord)
|
---|
| 157 | {
|
---|
| 158 | float dist = texCUBElod(dcm, float4(coord.xy, - coord.z,0)).a;
|
---|
| 159 | if(dist == 0) dist = 1000000; ///sky
|
---|
| 160 | return dist;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | struct MPos_OUT
|
---|
| 164 | {
|
---|
| 165 | float4 VPos : POSITION;
|
---|
| 166 | float4 MPos : TEXCOORD0;
|
---|
| 167 | };
|
---|
| 168 |
|
---|
| 169 | float4 ReduceCubeMap_PS(MPos_OUT IN,
|
---|
| 170 | uniform int nFace,
|
---|
| 171 | uniform samplerCUBE EnvironmentMapSampler : register(s0) ) : COLOR
|
---|
| 172 | {
|
---|
| 173 | float4 color = 0;
|
---|
| 174 | float3 dir;
|
---|
| 175 |
|
---|
| 176 | for (int i = 0; i < RATE/2; i+=1)
|
---|
| 177 | for (int j = 0; j < RATE/2; j+=1)
|
---|
| 178 | {
|
---|
| 179 | float2 pos;
|
---|
| 180 | pos.x = IN.MPos.x + (4*i + 2)/(float)CUBEMAP_SIZE;
|
---|
| 181 | pos.y = IN.MPos.y - (4*j + 2)/(float)CUBEMAP_SIZE; // y=-u
|
---|
| 182 |
|
---|
| 183 | // "scrambling"
|
---|
| 184 | if (nFace == 0) dir = float3(1, pos.y, -pos.x);
|
---|
| 185 | if (nFace == 1) dir = float3(-1, pos.y, pos.x);
|
---|
| 186 | if (nFace == 2) dir = float3(pos.x, 1, -pos.y);
|
---|
| 187 | if (nFace == 3) dir = float3(pos.x, -1, pos.y);
|
---|
| 188 | if (nFace == 4) dir = float3(pos.x, pos.y, 1);
|
---|
| 189 | if (nFace == 5) dir = float3(-pos.x, pos.y,-1);
|
---|
| 190 |
|
---|
| 191 | color += texCUBE( EnvironmentMapSampler, dir);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | return color / (RATE * RATE / 4.0);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | float4 Disc2Point_Contr(float3 L, float3 pos, float3 N, float3 V, samplerCUBE SmallEnvMapSampler, samplerCUBE DistanceEnvMapSampler) // Phong-Blinn
|
---|
| 198 | // L: a hossza lényeges (az egységkocka faláig ér)
|
---|
| 199 | {
|
---|
| 200 | float mindist = 1.0;
|
---|
| 201 |
|
---|
| 202 | float kd = 0.3; // 0.3
|
---|
| 203 | float ks = 0; // 0.5
|
---|
| 204 | float shininess = 10;
|
---|
| 205 |
|
---|
| 206 | float l = length(L);
|
---|
| 207 | L = normalize(L);
|
---|
| 208 |
|
---|
| 209 | //dw
|
---|
| 210 | float dw = 4 / (REDUCED_CUBEMAP_SIZE*REDUCED_CUBEMAP_SIZE*l*l*l + 4/3.1416f);
|
---|
| 211 | //Lin
|
---|
| 212 | float4 Lin = readCubeMap(SmallEnvMapSampler, L);
|
---|
| 213 | //r
|
---|
| 214 | float doy = readDistanceCubeMap(DistanceEnvMapSampler, L);
|
---|
| 215 | float dxy = length(L * doy - pos);
|
---|
| 216 |
|
---|
| 217 | dxy = max(mindist, dxy);
|
---|
| 218 |
|
---|
| 219 |
|
---|
| 220 | //dws
|
---|
| 221 | float dws = (doy*doy * dw) / (dxy*dxy*(1 - dw/3.1416f) + doy*doy*dw/3.1416f); // localization:
|
---|
| 222 | //float dws = dw;
|
---|
| 223 |
|
---|
| 224 | //L = L * doy - pos; // L: x->y, az objektumtól induljon, ne a középpontból
|
---|
| 225 | L = normalize(L);
|
---|
| 226 | float3 H = normalize(L + V); // felezõvektor
|
---|
| 227 |
|
---|
| 228 | float a = kd * max(dot(N,L),0) +
|
---|
| 229 | ks * pow(max(dot(N,H),0), shininess); // diffuse + specular
|
---|
| 230 |
|
---|
| 231 | // 1.: eddigi
|
---|
| 232 | //return Lin * a * dws;
|
---|
| 233 |
|
---|
| 234 | float ctheta_in = dot(N,L);
|
---|
| 235 | float ctheta_out = dot(N,V);
|
---|
| 236 |
|
---|
| 237 | return Lin * a * dws;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | float4 diffuseIndirect(samplerCUBE colorEnvMap, samplerCUBE distEnvMap, float3 N, float3 pos, float3 V)
|
---|
| 241 | {
|
---|
| 242 | float M = REDUCED_CUBEMAP_SIZE;
|
---|
| 243 |
|
---|
| 244 | N = normalize( N );
|
---|
| 245 |
|
---|
| 246 | float4 I = 0;
|
---|
| 247 | float3 L;
|
---|
| 248 | float4 Le;
|
---|
| 249 | float width = 1.0 / M;
|
---|
| 250 | float d;
|
---|
| 251 |
|
---|
| 252 | for (float x = 0.5; x < M; x++)
|
---|
| 253 | for (float y = 0.5; y < M; y++)
|
---|
| 254 | {
|
---|
| 255 | float2 p, tpos;
|
---|
| 256 | tpos.x = x * width;
|
---|
| 257 | tpos.y = y * width;
|
---|
| 258 |
|
---|
| 259 | p = tpos.xy;
|
---|
| 260 | p = 2.0 * p - 1.0; //-1..1
|
---|
| 261 |
|
---|
| 262 | float3 L;
|
---|
| 263 |
|
---|
| 264 | L = float3(p.x, p.y, 1);
|
---|
| 265 | I += Disc2Point_Contr( L * 0.75, pos, N, V, colorEnvMap, distEnvMap);
|
---|
| 266 |
|
---|
| 267 | L = float3(p.x, p.y, -1);
|
---|
| 268 | I += Disc2Point_Contr( L * 0.75, pos, N, V, colorEnvMap, distEnvMap);
|
---|
| 269 |
|
---|
| 270 | L = float3(p.x, 1, p.y);
|
---|
| 271 | I += Disc2Point_Contr( L * 0.75, pos, N, V, colorEnvMap, distEnvMap);
|
---|
| 272 |
|
---|
| 273 | L = float3(p.x, -1, p.y);
|
---|
| 274 | I += Disc2Point_Contr( L * 0.75, pos, N, V, colorEnvMap, distEnvMap);
|
---|
| 275 |
|
---|
| 276 | L = float3(1, p.x, p.y);
|
---|
| 277 | I += Disc2Point_Contr( L * 0.75, pos, N, V, colorEnvMap, distEnvMap);
|
---|
| 278 |
|
---|
| 279 | L = float3(-1, p.x, p.y);
|
---|
| 280 | I += Disc2Point_Contr( L * 0.75, pos, N, V, colorEnvMap, distEnvMap);
|
---|
| 281 | }
|
---|
| 282 | float kd = 1.0;
|
---|
| 283 | float indirect = kd * I;
|
---|
| 284 |
|
---|
| 285 | return indirect;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | float4 glossyIndirect(samplerCUBE colorEnvMap, samplerCUBE distEnvMap, float3 N, float3 pos, float3 V)
|
---|
| 289 | {
|
---|
| 290 | float M = REDUCED_CUBEMAP_SIZE;
|
---|
| 291 |
|
---|
| 292 | float3 R = reflect( V, N );
|
---|
| 293 |
|
---|
| 294 | float rr = max( max(abs(R.x), abs(R.y)), abs(R.z) ); // select the largest component
|
---|
| 295 | R /= rr; // scale the largest component to value +/-1
|
---|
| 296 |
|
---|
| 297 | float3 offset1 = float3(1,0,0);
|
---|
| 298 | float3 offset2 = float3(0,1,0);
|
---|
| 299 | if (abs(R.x) > abs(R.y) && abs(R.x) > abs(R.z))
|
---|
| 300 | offset1 = float3(0,0,1);
|
---|
| 301 | if (abs(R.y) > abs(R.x) && abs(R.y) > abs(R.z))
|
---|
| 302 | offset2 = float3(0,0,1);
|
---|
| 303 |
|
---|
| 304 |
|
---|
| 305 | float4 I = 0;
|
---|
| 306 | float3 L;
|
---|
| 307 | float width = 2.0 / M;
|
---|
| 308 |
|
---|
| 309 | L = R;
|
---|
| 310 | I += Disc2Point_Contr( L * 0.75, pos, N, V, colorEnvMap, distEnvMap);
|
---|
| 311 |
|
---|
| 312 | L = R + offset1 * width;
|
---|
| 313 | I += Disc2Point_Contr( L * 0.75, pos, N, V, colorEnvMap, distEnvMap);
|
---|
| 314 |
|
---|
| 315 | L = R - offset1 * width;
|
---|
| 316 | I += Disc2Point_Contr( L * 0.75, pos, N, V, colorEnvMap, distEnvMap);
|
---|
| 317 |
|
---|
| 318 | L = R + offset2 * width;
|
---|
| 319 | I += Disc2Point_Contr( L * 0.75, pos, N, V, colorEnvMap, distEnvMap);
|
---|
| 320 |
|
---|
| 321 | L = R - offset2 * width;
|
---|
| 322 | I += Disc2Point_Contr( L * 0.75, pos, N, V, colorEnvMap, distEnvMap);
|
---|
| 323 |
|
---|
| 324 |
|
---|
| 325 | float kd = 1.0;
|
---|
| 326 | //return readCubeMap(SmallEnvMapSampler, pos) + lastCenter.x*0.0000000001;
|
---|
| 327 | return kd * I * 2 * M;
|
---|
| 328 |
|
---|
| 329 | } |
---|