source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/demos/OgreGames/SpaceStation/Media/Station/stationIllum.hlsl @ 3255

Revision 3255, 5.1 KB checked in by szirmay, 15 years ago (diff)
Line 
1#define SPOT_ANGLE 2.093
2#define SPOT_FALLOFF 1
3#define SHADOW_COLOR float4(0.2,0.2,0.2,1.0)
4
5#ifdef HIGH_QUALITY
6        #define SHADOW_BIAS_POINT 0.0001
7        #define SHADOW_EPSILON_POINT 0.005
8#else
9        #define SHADOW_BIAS_POINT 0.002
10#endif
11
12float4 Illumination(float3 N, float3 L, float3 V, float4 lightColor, float4 lightRange, float3 lightDir, float4 diffuseColor, float specularity, float4 specularColor)
13{
14        float d = length(L);
15        L = normalize(L);
16               
17//#ifdef HIGH_QUALITY
18
19        //Phong-Blinn
20        float3 H = normalize(L + V);
21        float4 Lighting = lit(dot(N, L), dot(N, H), specularity);
22        Lighting = saturate(Lighting);
23       
24        float lightAngleCos = cos(70.0 / 180.0 * 3.14);
25        float falloff = (max(dot(-L, lightDir), 0) - lightAngleCos) / (1.0 - lightAngleCos);
26        falloff = pow(saturate(falloff), 2);
27       
28        return   lightColor * (Lighting.y * diffuseColor + Lighting.z * specularColor) //color
29                         / (lightRange.y + d * lightRange.z + d * d * lightRange.w) //attenuation
30                         * falloff //spot falloff
31                         ;
32                         
33//#else
34
35//      return  lightColor * diffuseColor * dot(L,N) * pow(max(dot(-L, lightDir),0),9) / (3*d*d);
36       
37//#endif       
38}
39
40float3 NormalMap(float3 tangent, float3 binormal, float3 normal, float2 texCoord, sampler2D normalMap)
41{       
42        normal = normalize(normal);     
43        //return normal;
44        float3 mNormal;
45        float3 tNormal = tex2D(normalMap, texCoord).rgb;
46        //tNormal = float3(0.5,0.5,1.0);
47       
48    tangent = normalize(tangent);
49        binormal = normalize(binormal);
50        float3x3 TangentToModel = float3x3(tangent, binormal, normal );
51               
52        tNormal.xy = (tNormal.xy *2.0) - 1.0;
53        mNormal = normalize( mul( tNormal, TangentToModel ) );
54       
55    return mNormal;
56}
57
58/*
59float shadowPoint(samplerCUBE shadowMap, float3 lightCPos, float lightFarPlane)
60{
61          float light = 1;
62         
63          float dist = length(lightCPos) / lightFarPlane;
64          float4 storedDist = texCUBE(shadowMap, float3(lightCPos.xy, -lightCPos.z));
65          dist -= SHADOW_BIAS_POINT;
66          float lit_factor = (dist <= storedDist.r);   
67                 
68          float M1 = storedDist.r;
69          float M2 = storedDist.g;
70          float v2 = min(max(M2 - M1 * M1, 0.0) +  SHADOW_EPSILON_POINT, 1.0);
71          float m_d = M1 - dist;
72          float pmax = v2 / (v2 + m_d * m_d);
73                                                       
74          light = max(lit_factor, pmax);       
75                 
76          return (SHADOW_COLOR + (1 - SHADOW_COLOR) * light);
77}
78*/
79float shadowMap(sampler2D shadowMap, float4 lightVPos, float3 lightCPos, float lightFarPlane)
80{
81#ifdef HIGH_QUALITY
82       
83          float light = 1;
84          if(lightVPos.z > 0)
85          {
86                lightVPos.xyz /= lightVPos.w;
87                float d = length(lightVPos.xy);
88            if(d <= 1.0)
89        {
90
91                  lightVPos.xy = (lightVPos.xy + 1.0) / 2.0;
92                  lightVPos.y = 1.0 - lightVPos.y;
93
94                  float dist = length(lightCPos) / lightFarPlane;
95                  float4 storedDist = tex2D(shadowMap, lightVPos.xy);
96                  dist -= SHADOW_BIAS_POINT;
97                  float lit_factor = (dist <= storedDist.r);   
98                         
99                  float M1 = storedDist.r;
100                  float M2 = storedDist.g;
101                  float v2 = min(max(M2 - M1 * M1, 0.0) +  SHADOW_EPSILON_POINT, 1.0);
102                  float m_d = M1 - dist;
103                  float pmax = v2 / (v2 + m_d * m_d);
104                                                               
105                  light = max(lit_factor, pmax);       
106           }
107          }
108                 
109         // return (SHADOW_COLOR + (1 - SHADOW_COLOR) * light);
110         return light;
111         
112#else
113         
114          lightVPos.xyz /= lightVPos.w;
115          lightVPos.xy = (lightVPos.xy + 1.0) / 2.0;
116          lightVPos.y = 1.0 - lightVPos.y;
117         
118          //return max(tex2D(shadowMap, lightVPos.xy).r > lightVPos.z - 0.01, SHADOW_COLOR);
119          return max(tex2Dlod(shadowMap, float4(lightVPos.xy,0,0)).r > lightVPos.z - SHADOW_BIAS_POINT, SHADOW_COLOR);
120         
121          //distance
122          //return tex2D(shadowMap, lightVPos.xy).r > length(lightCPos) / lightFarPlane - 0.01;
123          //return tex2Dlod(shadowMap, float4(lightVPos.xy,0,1)).r > length(lightCPos) / lightFarPlane - 0.01;
124         
125#endif
126}
127
128uniform float4 prmAtlasTilesHalfPixel;
129uniform float allClusterCount;
130uniform float clusterCount;
131
132float4 PathMapIndirect(sampler2D PMTex, sampler2D weightIndexTex, sampler2D weightTex, float2 texAtlas)
133{
134        int2 prmAtlasTiles = prmAtlasTilesHalfPixel.xy;
135        float2 atlasHalfPixel = prmAtlasTilesHalfPixel.zw;
136       
137        float3 col = 0;
138        for(int iCluster = 0; iCluster < 8; iCluster++)
139        {
140                float2 prmTexPos = float2(
141                        (texAtlas.x + (iCluster % prmAtlasTiles.x)) / prmAtlasTiles.x,
142                        1.0 - (texAtlas.y + (iCluster / prmAtlasTiles.x)) / prmAtlasTiles.y) + atlasHalfPixel;
143
144        //      float weightIndex = tex2Dlod(weightIndexTex, float4(((float)iCluster + 0.5) / clusterCount, 0.5, 0, 0)).r;
145        //      float3 weight = tex2Dlod(weightTex, float4((weightIndex + 0.5)/allClusterCount, 0.5, 0, 0)).rgb;
146        //      float3 val = tex2Dlod(PMTex, float4(prmTexPos, 0, 0)).xyz;
147               
148                float weightIndex = tex2D(weightIndexTex, float2(((float)iCluster + 0.5) / clusterCount, 0.5)).r;
149                float3 weight = tex2D(weightTex, float2((weightIndex + 0.5)/allClusterCount, 0.5)).rgb;
150                float3 val = tex2D(PMTex, prmTexPos).xyz;
151               
152                col += val.xyz * weight;               
153        }
154       
155        return float4(col,1);
156}
157
158float3 Caustics(samplerCUBE cauMap, samplerCUBE distMap, float3 dir, float attenuation)
159{
160        float d = length(dir);
161        float4 caustics = texCUBE(cauMap, float3(dir.xy, -dir.z));
162        //caustics.r *= d < caustics.a;
163        caustics *= 1.0 - saturate(d / attenuation);
164       
165        return caustics.rgb;
166}
Note: See TracBrowser for help on using the repository browser.