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