#define SPOT_ANGLE 2.093 #define SPOT_FALLOFF 1 #define SHADOW_COLOR float4(0.2,0.2,0.2,1.0) #ifdef HIGH_QUALITY #define SHADOW_BIAS_POINT 0.0001 #define SHADOW_EPSILON_POINT 0.005 #else #define SHADOW_BIAS_POINT 0.002 #endif float4 Illumination(float3 N, float3 L, float3 V, float4 lightColor, float4 lightRange, float3 lightDir, float4 diffuseColor, float specularity, float4 specularColor) { float d = length(L); L = normalize(L); #ifdef HIGH_QUALITY //Phong-Blinn float3 H = normalize(L + V); float4 Lighting = lit(dot(N, L), dot(N, H), specularity); Lighting = saturate(Lighting); float lightAngleCos = cos(70.0 / 180.0 * 3.14); float falloff = (dot(-L, lightDir) - lightAngleCos) / (1.0 - lightAngleCos); falloff = pow(saturate(falloff), 2); return lightColor * (Lighting.y * diffuseColor + Lighting.z * specularColor) //color / (lightRange.y + d * lightRange.z + d * d * lightRange.w) //attenuation * falloff //spot falloff ; #else return lightColor * diffuseColor * dot(L,N) * pow(max(dot(-L, lightDir),0),9) / (3*d*d); #endif } float3 NormalMap(float3 tangent, float3 binormal, float3 normal, float2 texCoord, sampler2D normalMap) { normal = normalize(normal); //return normal; float3 mNormal; float3 tNormal = tex2D(normalMap, texCoord).rgb; //tNormal = float3(0.5,0.5,1.0); tangent = normalize(tangent); binormal = normalize(binormal); float3x3 TangentToModel = float3x3(tangent, binormal, normal ); tNormal.xy = (tNormal.xy *2.0) - 1.0; mNormal = normalize( mul( tNormal, TangentToModel ) ); return mNormal; } /* float shadowPoint(samplerCUBE shadowMap, float3 lightCPos, float lightFarPlane) { float light = 1; float dist = length(lightCPos) / lightFarPlane; float4 storedDist = texCUBE(shadowMap, float3(lightCPos.xy, -lightCPos.z)); dist -= SHADOW_BIAS_POINT; float lit_factor = (dist <= storedDist.r); float M1 = storedDist.r; float M2 = storedDist.g; float v2 = min(max(M2 - M1 * M1, 0.0) + SHADOW_EPSILON_POINT, 1.0); float m_d = M1 - dist; float pmax = v2 / (v2 + m_d * m_d); light = max(lit_factor, pmax); return (SHADOW_COLOR + (1 - SHADOW_COLOR) * light); } */ float shadowMap(sampler2D shadowMap, float4 lightVPos, float3 lightCPos, float lightFarPlane) { #ifdef HIGH_QUALITY float light = 1; if(lightVPos.z > 0) { lightVPos.xyz /= lightVPos.w; float d = length(lightVPos.xy); if(d <= 1.0) { lightVPos.xy = (lightVPos.xy + 1.0) / 2.0; lightVPos.y = 1.0 - lightVPos.y; float dist = length(lightCPos) / lightFarPlane; float4 storedDist = tex2D(shadowMap, lightVPos.xy); dist -= SHADOW_BIAS_POINT; float lit_factor = (dist <= storedDist.r); float M1 = storedDist.r; float M2 = storedDist.g; float v2 = min(max(M2 - M1 * M1, 0.0) + SHADOW_EPSILON_POINT, 1.0); float m_d = M1 - dist; float pmax = v2 / (v2 + m_d * m_d); light = max(lit_factor, pmax); } } // return (SHADOW_COLOR + (1 - SHADOW_COLOR) * light); return light; #else lightVPos.xyz /= lightVPos.w; lightVPos.xy = (lightVPos.xy + 1.0) / 2.0; lightVPos.y = 1.0 - lightVPos.y; //return max(tex2D(shadowMap, lightVPos.xy).r > lightVPos.z - 0.01, SHADOW_COLOR); return max(tex2Dlod(shadowMap, float4(lightVPos.xy,0,0)).r > lightVPos.z - SHADOW_BIAS_POINT, SHADOW_COLOR); //distance //return tex2D(shadowMap, lightVPos.xy).r > length(lightCPos) / lightFarPlane - 0.01; //return tex2Dlod(shadowMap, float4(lightVPos.xy,0,1)).r > length(lightCPos) / lightFarPlane - 0.01; #endif } uniform float4 prmAtlasTilesHalfPixel; uniform float allClusterCount; uniform float clusterCount; float4 PathMapIndirect(sampler2D PMTex, sampler2D weightIndexTex, sampler2D weightTex, float2 texAtlas) { int2 prmAtlasTiles = prmAtlasTilesHalfPixel.xy; float2 atlasHalfPixel = prmAtlasTilesHalfPixel.zw; float3 col = 0; for(int iCluster = 0; iCluster < 8; iCluster++) { float2 prmTexPos = float2( (texAtlas.x + (iCluster % prmAtlasTiles.x)) / prmAtlasTiles.x, 1.0 - (texAtlas.y + (iCluster / prmAtlasTiles.x)) / prmAtlasTiles.y) + atlasHalfPixel; // float weightIndex = tex2Dlod(weightIndexTex, float4(((float)iCluster + 0.5) / clusterCount, 0.5, 0, 0)).r; // float3 weight = tex2Dlod(weightTex, float4((weightIndex + 0.5)/allClusterCount, 0.5, 0, 0)).rgb; // float3 val = tex2Dlod(PMTex, float4(prmTexPos, 0, 0)).xyz; float weightIndex = tex2D(weightIndexTex, float2(((float)iCluster + 0.5) / clusterCount, 0.5)).r; float3 weight = tex2D(weightTex, float2((weightIndex + 0.5)/allClusterCount, 0.5)).rgb; float3 val = tex2D(PMTex, prmTexPos).xyz; col += val.xyz * weight; } return float4(col,1); } float Caustics(samplerCUBE cauMap, samplerCUBE distMap, float3 dir, float attenuation) { float d = length(dir); float4 caustics = texCUBE(cauMap, float3(dir.xy, -dir.z)); //caustics.r *= d < caustics.a; caustics *= 1.0 - saturate(d / attenuation); return caustics.r; }