1 | uniform vec4 lightDiffuse; |
---|
2 | uniform vec4 scaleBias; |
---|
3 | uniform vec4 spotParams; |
---|
4 | uniform vec4 lightDiffuse1; |
---|
5 | uniform vec4 spotParams1; |
---|
6 | |
---|
7 | uniform sampler2D normalHeightMap; |
---|
8 | uniform sampler2D diffuseMap; |
---|
9 | uniform sampler2D shadowMap1; |
---|
10 | uniform sampler2D shadowMap2; |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | varying vec3 tangentEyeDir; |
---|
15 | varying vec3 tangentLightDir[2]; |
---|
16 | varying vec3 tangentSpotDir[2]; |
---|
17 | varying vec4 shadowUV[2]; |
---|
18 | |
---|
19 | |
---|
20 | // Expand a range-compressed vector |
---|
21 | vec3 expand(vec3 v) |
---|
22 | { |
---|
23 | return (v - 0.5) * 2.0; |
---|
24 | } |
---|
25 | |
---|
26 | |
---|
27 | void main() |
---|
28 | { |
---|
29 | // get the height using the tex coords |
---|
30 | float height = texture2D(normalHeightMap, gl_TexCoord[0].xy).a; |
---|
31 | // scale and bias factors |
---|
32 | float scale = scaleBias.x; |
---|
33 | float bias = scaleBias.y; |
---|
34 | |
---|
35 | // calculate displacement |
---|
36 | float displacement = (height * scale) + bias; |
---|
37 | //float displacement = (height * 0.04) - 0.02; |
---|
38 | |
---|
39 | vec3 scaledEyeDir = tangentEyeDir * displacement; |
---|
40 | |
---|
41 | // calculate the new tex coord to use for normal and diffuse |
---|
42 | vec2 newTexCoord = (scaledEyeDir + gl_TexCoord[0].xyz).xy; |
---|
43 | |
---|
44 | // get the new normal and diffuse values |
---|
45 | vec3 normal = expand(texture2D(normalHeightMap, newTexCoord).xyz); |
---|
46 | vec4 diffuse = texture2D(diffuseMap, newTexCoord); |
---|
47 | |
---|
48 | vec4 col1 = diffuse * clamp(dot(normal, tangentLightDir[0]),0.0,1.0) * lightDiffuse; |
---|
49 | // factor in spotlight angle |
---|
50 | float rho = clamp(dot(tangentSpotDir[0], tangentLightDir[0]),0.0,1.0); |
---|
51 | // factor = (rho - cos(outer/2) / cos(inner/2) - cos(outer/2)) ^ falloff |
---|
52 | float spotFactor = pow( |
---|
53 | clamp(rho - spotParams.y,0.0,1.0) / (spotParams.x - spotParams.y), spotParams.z); |
---|
54 | col1 = col1 * spotFactor; |
---|
55 | vec4 col2 = diffuse * clamp(dot(normal, tangentLightDir[1]),0.0,1.0) * lightDiffuse1; |
---|
56 | // factor in spotlight angle |
---|
57 | rho = clamp(dot(tangentSpotDir[1], tangentLightDir[1]),0.0,1.0); |
---|
58 | // factor = (rho - cos(outer/2) / cos(inner/2) - cos(outer/2)) ^ falloff |
---|
59 | spotFactor = pow( |
---|
60 | clamp(rho - spotParams1.y,0.0,1.0) / (spotParams1.x - spotParams1.y), spotParams1.z); |
---|
61 | col2 = col2 * spotFactor; |
---|
62 | |
---|
63 | // shadow textures |
---|
64 | col1 = col1 * texture2DProj(shadowMap1, shadowUV[0]); |
---|
65 | col2 = col2 * texture2DProj(shadowMap2, shadowUV[1]); |
---|
66 | |
---|
67 | gl_FragColor = col1 + col2; |
---|
68 | |
---|
69 | } |
---|