Improve runway reflection shader by Emilian H.
[fg:toms-fgdata.git] / Shaders / runway-gbuffer.frag
1 // -*- mode: C; -*-\r
2 // Licence: GPL v2\r
3 // © Emilian Huminiuc and Vivian Meazza 2011\r
4 \r
5 #version 120\r
6 \r
7 varying vec3  rawpos;\r
8 varying vec3  VNormal;\r
9 varying vec3  VTangent;\r
10 varying vec3  VBinormal;\r
11 varying vec3  vViewVec;\r
12 varying vec3  reflVec;\r
13 \r
14 varying vec4 Diffuse;\r
15 varying float alpha;\r
16 //varying float fogCoord;\r
17 \r
18 uniform samplerCube Environment;\r
19 uniform sampler2D Rainbow;\r
20 uniform sampler2D BaseTex;\r
21 uniform sampler2D Fresnel;\r
22 uniform sampler2D Map;\r
23 uniform sampler2D NormalTex;\r
24 uniform sampler3D Noise;\r
25 \r
26 uniform float spec_adjust;\r
27 uniform float rainbowiness;\r
28 uniform float fresneliness;\r
29 uniform float noisiness;\r
30 uniform float ambient_correction;\r
31 uniform float normalmap_dds;\r
32 \r
33 void encode_gbuffer(vec3 normal, vec3 color, int mId, float specular, float shininess, float emission, float depth);\r
34 \r
35 void main (void)\r
36 {\r
37 \r
38     vec4 texel = texture2D(BaseTex, gl_TexCoord[0].st);\r
39     vec4 nmap  = texture2D(NormalTex, gl_TexCoord[0].st * 8.0);\r
40     vec4 map   = texture2D(Map, gl_TexCoord[0].st * 8.0);\r
41     vec4 specNoise = texture3D(Noise, rawpos.xyz * 0.0045);\r
42     vec4 noisevec = texture3D(Noise, rawpos.xyz);\r
43     vec3 ambient = vec3(0.85,0.85,0.9);//placeholder for sun ambient\r
44     vec3 N;\r
45     float emission = dot( gl_FrontLightModelProduct.sceneColor.rgb + gl_FrontMaterial.emission.rgb,\r
46                           vec3( 0.3, 0.59, 0.11 ) );\r
47 \r
48     N = nmap.rgb * 2.0 - 1.0;\r
49     N = normalize(N.x * VTangent + N.y * VBinormal + N.z * VNormal);\r
50     if (normalmap_dds > 0)\r
51         N = -N;\r
52 \r
53     float nFactor = 1.0 - N.z;\r
54     float lightness = dot(texel.rgb, vec3( 0.3, 0.59, 0.11 ));\r
55 \r
56     // calculate the specular light\r
57     float refl_correction = spec_adjust * 2.5 - 1.0;\r
58     float shininess = max (0.35, refl_correction) * nmap.a * nFactor;\r
59 \r
60 \r
61     float specular = dot(vec3(1.0) * lightness , vec3( 0.3, 0.59, 0.11 )) * nFactor;\r
62 \r
63     vec4 color = vec4(1.0);\r
64 \r
65     color.a = texel.a * alpha;\r
66     color = clamp(color, 0.0, 1.0);\r
67 \r
68     vec3 viewVec = normalize(vViewVec);\r
69 \r
70     // Map a rainbowish color\r
71     float v = abs(dot(viewVec, normalize(VNormal)));\r
72     vec4 rainbow = texture2D(Rainbow, vec2(v, 0.0));\r
73 \r
74     // Map a fresnel effect\r
75     vec4 fresnel = texture2D(Fresnel, vec2(v, 0.0));\r
76 \r
77     // map the refection of the environment\r
78     vec4 reflection = textureCube(Environment, reflVec * dot(N,VNormal));\r
79 \r
80 \r
81     // set the user shininess offset\r
82     float transparency_offset = clamp(refl_correction, -1.0, 1.0);\r
83     float reflFactor = 0.0;\r
84 \r
85     float MixFactor = specNoise.r * specNoise.g * specNoise.b * 350.0;\r
86 \r
87     MixFactor = 0.75 * smoothstep(0.0, 1.0, MixFactor);\r
88 \r
89     reflFactor = max(map.a * (texel.r + texel.g), 1.0 - MixFactor)  * nFactor  + transparency_offset ;\r
90     reflFactor =0.75 * smoothstep(0.05, 1.0, reflFactor);\r
91 \r
92     // set ambient adjustment to remove bluiness with user input\r
93     float ambient_offset = clamp(ambient_correction, -1.0, 1.0);\r
94     vec3 ambient_Correction = vec3(ambient.rg, ambient.b * 0.6) * ambient_offset;\r
95     ambient_Correction = clamp(ambient_Correction, -1.0, 1.0);\r
96 \r
97     // add fringing fresnel and rainbow effects and modulate by reflection\r
98     vec4 reflcolor = mix(reflection, rainbow, rainbowiness * v);\r
99 \r
100     vec4 reflfrescolor = mix(reflcolor, fresnel, fresneliness * v);\r
101     vec4 noisecolor = mix(reflfrescolor, noisevec, noisiness);\r
102     vec4 raincolor = vec4(noisecolor.rgb * reflFactor, 1.0) * nFactor;\r
103     vec4 mixedcolor = mix(texel, raincolor * (1.0 - refl_correction * (1.0 - lightness)), reflFactor);\r
104 \r
105     // the final reflection\r
106     vec4 fragColor = vec4(color.rgb * mixedcolor.rgb  + ambient_Correction * nFactor, color.a);\r
107     float doWater = step(0.1,  reflFactor);\r
108     int matIndex = int(doWater) * 253 + 1;\r
109     shininess += doWater * reflFactor * 240.0;\r
110 \r
111     encode_gbuffer(N, fragColor.rgb, matIndex, specular, shininess, emission, gl_FragCoord.z);\r
112 }\r