2011-12-01 20:52:05 +00:00
|
|
|
// -*- mode: C; -*-
|
2015-03-29 11:47:58 +00:00
|
|
|
// UBERSHADER - default forward rendering - fragment shader
|
2011-12-01 20:52:05 +00:00
|
|
|
// Licence: GPL v2
|
|
|
|
// Authors: Frederic Bouvier and Gijs de Rooy
|
|
|
|
// with major additions and revisions by
|
|
|
|
// Emilian Huminiuc and Vivian Meazza 2011
|
|
|
|
#version 120
|
|
|
|
|
2015-03-29 11:47:58 +00:00
|
|
|
varying vec4 diffuseColor;
|
2013-01-17 09:32:00 +00:00
|
|
|
varying vec3 VBinormal;
|
2011-12-01 20:52:05 +00:00
|
|
|
varying vec3 VNormal;
|
|
|
|
varying vec3 VTangent;
|
2013-01-17 09:32:00 +00:00
|
|
|
varying vec3 rawpos;
|
2015-03-29 11:47:58 +00:00
|
|
|
varying vec3 eyeVec;
|
|
|
|
varying vec3 eyeDir;
|
|
|
|
|
|
|
|
uniform sampler2D BaseTex;
|
|
|
|
uniform sampler2D LightMapTex;
|
|
|
|
uniform sampler2D NormalTex;
|
|
|
|
uniform sampler2D ReflGradientsTex;
|
|
|
|
uniform sampler2D ReflMapTex;
|
|
|
|
uniform sampler3D ReflNoiseTex;
|
|
|
|
uniform samplerCube Environment;
|
|
|
|
|
|
|
|
uniform int dirt_enabled;
|
|
|
|
uniform int dirt_multi;
|
|
|
|
uniform int lightmap_enabled;
|
|
|
|
uniform int lightmap_multi;
|
|
|
|
uniform int nmap_dds;
|
|
|
|
uniform int nmap_enabled;
|
|
|
|
uniform int refl_enabled;
|
|
|
|
uniform int refl_dynamic;
|
|
|
|
uniform int refl_map;
|
|
|
|
|
|
|
|
uniform float amb_correction;
|
|
|
|
uniform float dirt_b_factor;
|
|
|
|
uniform float dirt_g_factor;
|
|
|
|
uniform float dirt_r_factor;
|
|
|
|
uniform float lightmap_a_factor;
|
|
|
|
uniform float lightmap_b_factor;
|
|
|
|
uniform float lightmap_g_factor;
|
|
|
|
uniform float lightmap_r_factor;
|
|
|
|
uniform float nmap_tile;
|
|
|
|
uniform float refl_correction;
|
|
|
|
uniform float refl_fresnel;
|
|
|
|
uniform float refl_noise;
|
|
|
|
uniform float refl_rainbow;
|
|
|
|
|
|
|
|
uniform vec3 lightmap_r_color;
|
|
|
|
uniform vec3 lightmap_g_color;
|
|
|
|
uniform vec3 lightmap_b_color;
|
|
|
|
uniform vec3 lightmap_a_color;
|
|
|
|
|
|
|
|
uniform vec3 dirt_r_color;
|
|
|
|
uniform vec3 dirt_g_color;
|
|
|
|
uniform vec3 dirt_b_color;
|
|
|
|
|
|
|
|
///reflection orientation
|
|
|
|
uniform mat4 osg_ViewMatrixInverse;
|
|
|
|
uniform float latDeg;
|
|
|
|
uniform float lonDeg;
|
2011-12-10 09:40:19 +00:00
|
|
|
|
2011-12-01 20:52:05 +00:00
|
|
|
///fog include//////////////////////
|
|
|
|
uniform int fogType;
|
|
|
|
vec3 fog_Func(vec3 color, int type);
|
|
|
|
////////////////////////////////////
|
|
|
|
|
2015-03-29 11:47:58 +00:00
|
|
|
|
|
|
|
//////rotation matrices/////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
mat3 rotX(in float angle)
|
|
|
|
{
|
|
|
|
mat3 rotmat = mat3(
|
|
|
|
1.0, 0.0, 0.0,
|
|
|
|
0.0, cos(angle), -sin(angle),
|
|
|
|
0.0, sin(angle), cos(angle)
|
|
|
|
);
|
|
|
|
return rotmat;
|
|
|
|
}
|
|
|
|
|
|
|
|
mat3 rotY(in float angle)
|
|
|
|
{
|
|
|
|
mat3 rotmat = mat3(
|
|
|
|
cos(angle), 0.0, sin(angle),
|
|
|
|
0.0, 1.0, 0.0,
|
|
|
|
-sin(angle), 0.0, cos(angle)
|
|
|
|
);
|
|
|
|
return rotmat;
|
|
|
|
}
|
|
|
|
|
|
|
|
mat3 rotZ(in float angle)
|
|
|
|
{
|
|
|
|
mat3 rotmat = mat3(
|
|
|
|
cos(angle), -sin(angle), 0.0,
|
|
|
|
sin(angle), cos(angle), 0.0,
|
|
|
|
0.0, 0.0, 1.0
|
|
|
|
);
|
|
|
|
return rotmat;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2011-12-01 20:52:05 +00:00
|
|
|
void main (void)
|
|
|
|
{
|
|
|
|
vec4 texel = texture2D(BaseTex, gl_TexCoord[0].st);
|
2011-12-10 09:40:19 +00:00
|
|
|
vec4 nmap = texture2D(NormalTex, gl_TexCoord[0].st * nmap_tile);
|
2011-12-01 20:52:05 +00:00
|
|
|
vec4 reflmap = texture2D(ReflMapTex, gl_TexCoord[0].st);
|
|
|
|
vec4 noisevec = texture3D(ReflNoiseTex, rawpos.xyz);
|
|
|
|
vec4 lightmapTexel = texture2D(LightMapTex, gl_TexCoord[0].st);
|
|
|
|
|
|
|
|
vec3 mixedcolor;
|
2012-05-17 17:10:30 +00:00
|
|
|
vec3 N = vec3(0.0,0.0,1.0);
|
|
|
|
float pf = 0.0;
|
2011-12-01 20:52:05 +00:00
|
|
|
|
2015-03-29 11:47:58 +00:00
|
|
|
///BEGIN bump //////////////////////////////////////////////////////////////////
|
2014-01-15 22:26:26 +00:00
|
|
|
if (nmap_enabled > 0 ){
|
2011-12-01 20:52:05 +00:00
|
|
|
N = nmap.rgb * 2.0 - 1.0;
|
|
|
|
N = normalize(N.x * VTangent + N.y * VBinormal + N.z * VNormal);
|
|
|
|
if (nmap_dds > 0)
|
2012-05-01 11:58:35 +00:00
|
|
|
N = -N;
|
2011-12-01 20:52:05 +00:00
|
|
|
} else {
|
|
|
|
N = normalize(VNormal);
|
|
|
|
}
|
2015-03-29 11:47:58 +00:00
|
|
|
///END bump ////////////////////////////////////////////////////////////////////
|
|
|
|
vec3 viewN = normalize((gl_ModelViewMatrixTranspose * vec4(N,0.0)).xyz);
|
|
|
|
vec3 viewVec = normalize(eyeVec);
|
|
|
|
float v = abs(dot(viewVec, viewN));// Map a rainbowish color
|
2013-04-26 19:11:08 +00:00
|
|
|
vec4 fresnel = texture2D(ReflGradientsTex, vec2(v, 0.75));
|
|
|
|
vec4 rainbow = texture2D(ReflGradientsTex, vec2(v, 0.25));
|
2011-12-01 20:52:05 +00:00
|
|
|
|
2015-03-29 11:47:58 +00:00
|
|
|
mat4 reflMatrix = gl_ModelViewMatrixInverse;
|
|
|
|
vec3 wRefVec = reflect(viewVec,N);
|
|
|
|
|
|
|
|
////dynamic reflection /////////////////////////////
|
|
|
|
if (refl_dynamic > 0){
|
|
|
|
reflMatrix = osg_ViewMatrixInverse;
|
|
|
|
|
|
|
|
vec3 wVertVec = normalize(reflMatrix * vec4(viewVec,0.0)).xyz;
|
|
|
|
vec3 wNormal = normalize(reflMatrix * vec4(N,0.0)).xyz;
|
|
|
|
|
2015-05-25 14:29:56 +00:00
|
|
|
float latRad = radians(90.-latDeg);
|
2015-03-29 11:47:58 +00:00
|
|
|
float lonRad = radians(lonDeg);
|
|
|
|
|
|
|
|
mat3 rotCorrY = rotY(latRad);
|
2015-05-25 14:29:56 +00:00
|
|
|
mat3 rotCorrZ = rotZ(lonRad);
|
|
|
|
mat3 reflCorr = rotCorrY * rotCorrZ;
|
2015-03-29 11:47:58 +00:00
|
|
|
wRefVec = reflect(wVertVec,wNormal);
|
|
|
|
wRefVec = normalize(reflCorr * wRefVec);
|
|
|
|
} else { ///static reflection
|
|
|
|
wRefVec = normalize(reflMatrix * vec4(wRefVec,0.0)).xyz;
|
|
|
|
}
|
|
|
|
|
|
|
|
vec3 reflection = textureCube(Environment, wRefVec).xyz;
|
|
|
|
|
|
|
|
vec3 E = eyeDir;
|
|
|
|
E = normalize(E);
|
|
|
|
|
|
|
|
vec3 L = normalize((gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz);
|
|
|
|
vec3 H = normalize(L + E);
|
|
|
|
|
|
|
|
N = viewN;
|
|
|
|
|
|
|
|
float nDotVP = dot(N,L);
|
|
|
|
float nDotHV = dot(N,H);
|
|
|
|
float eDotLV = max(0.0, dot(-E,L));
|
|
|
|
|
2011-12-10 09:40:19 +00:00
|
|
|
//glare on the backside of tranparent objects
|
2015-03-29 11:47:58 +00:00
|
|
|
if ((gl_Color.a < .999 || texel.a < 1.0) && nDotVP < 0.0) {
|
|
|
|
nDotVP = dot(-N, L);
|
|
|
|
nDotHV = dot(-N, H);
|
2011-12-10 09:40:19 +00:00
|
|
|
}
|
2011-12-01 20:52:05 +00:00
|
|
|
|
2015-03-29 11:47:58 +00:00
|
|
|
nDotVP = max(0.0, nDotVP);
|
|
|
|
nDotHV = max(0.0, nDotHV);
|
|
|
|
|
2011-12-01 20:52:05 +00:00
|
|
|
if (nDotVP == 0.0)
|
|
|
|
pf = 0.0;
|
|
|
|
else
|
|
|
|
pf = pow(nDotHV, gl_FrontMaterial.shininess);
|
|
|
|
|
|
|
|
vec4 Diffuse = gl_LightSource[0].diffuse * nDotVP;
|
2013-01-17 09:32:00 +00:00
|
|
|
vec4 Specular = gl_FrontMaterial.specular * gl_LightSource[0].diffuse * pf;
|
2011-12-01 20:52:05 +00:00
|
|
|
|
2015-03-29 11:47:58 +00:00
|
|
|
vec4 color = gl_Color + Diffuse*diffuseColor;
|
2011-12-01 20:52:05 +00:00
|
|
|
color = clamp( color, 0.0, 1.0 );
|
2015-03-29 11:47:58 +00:00
|
|
|
color.a = texel.a * diffuseColor.a;
|
|
|
|
////////////////////////////////////////////////////////////////////
|
|
|
|
//BEGIN reflect
|
|
|
|
////////////////////////////////////////////////////////////////////
|
2014-01-15 22:26:26 +00:00
|
|
|
if (refl_enabled > 0 ){
|
2012-05-17 17:10:30 +00:00
|
|
|
float reflFactor = 0.0;
|
2011-12-01 20:52:05 +00:00
|
|
|
float transparency_offset = clamp(refl_correction, -1.0, 1.0);// set the user shininess offset
|
|
|
|
|
|
|
|
if(refl_map > 0){
|
|
|
|
// map the shininess of the object with user input
|
|
|
|
reflFactor = reflmap.a + transparency_offset;
|
2014-01-15 22:26:26 +00:00
|
|
|
} else if (nmap_enabled > 0) {
|
2011-12-01 20:52:05 +00:00
|
|
|
// set the reflectivity proportional to shininess with user input
|
2012-05-01 11:58:35 +00:00
|
|
|
reflFactor = gl_FrontMaterial.shininess * 0.0078125 * nmap.a + transparency_offset;
|
2011-12-01 20:52:05 +00:00
|
|
|
} else {
|
2012-05-01 11:58:35 +00:00
|
|
|
reflFactor = gl_FrontMaterial.shininess* 0.0078125 + transparency_offset;
|
2011-12-01 20:52:05 +00:00
|
|
|
}
|
|
|
|
reflFactor = clamp(reflFactor, 0.0, 1.0);
|
|
|
|
|
|
|
|
// add fringing fresnel and rainbow effects and modulate by reflection
|
2015-03-29 11:47:58 +00:00
|
|
|
vec3 reflcolor = mix(reflection, rainbow.rgb, refl_rainbow * v);
|
|
|
|
vec3 reflfrescolor = mix(reflcolor, fresnel.rgb, refl_fresnel * v);
|
|
|
|
vec3 noisecolor = mix(reflfrescolor, noisevec.rgb, refl_noise);
|
|
|
|
vec3 raincolor = noisecolor * reflFactor;
|
|
|
|
raincolor += Specular.rgb;
|
|
|
|
raincolor *= gl_LightSource[0].diffuse.rgb;
|
|
|
|
mixedcolor = mix(texel.rgb, raincolor, reflFactor);
|
2011-12-01 20:52:05 +00:00
|
|
|
} else {
|
|
|
|
mixedcolor = texel.rgb;
|
|
|
|
}
|
2011-12-24 15:47:09 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
//END reflect
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-03-29 11:47:58 +00:00
|
|
|
if (color.a<1.0){
|
|
|
|
color.a += .1 * eDotLV;
|
|
|
|
}
|
2011-12-24 15:47:09 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//begin DIRT
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
2014-01-12 17:06:23 +00:00
|
|
|
if (dirt_enabled >= 1){
|
2012-05-01 11:58:35 +00:00
|
|
|
vec3 dirtFactorIn = vec3 (dirt_r_factor, dirt_g_factor, dirt_b_factor);
|
|
|
|
vec3 dirtFactor = reflmap.rgb * dirtFactorIn.rgb;
|
|
|
|
mixedcolor.rgb = mix(mixedcolor.rgb, dirt_r_color, smoothstep(0.0, 1.0, dirtFactor.r));
|
2015-03-29 11:47:58 +00:00
|
|
|
if (color.a < 1.0) {
|
|
|
|
color.a += dirtFactor.r * eDotLV;
|
|
|
|
}
|
2011-12-24 15:47:09 +00:00
|
|
|
if (dirt_multi > 0) {
|
2012-05-01 11:58:35 +00:00
|
|
|
mixedcolor.rgb = mix(mixedcolor.rgb, dirt_g_color, smoothstep(0.0, 1.0, dirtFactor.g));
|
|
|
|
mixedcolor.rgb = mix(mixedcolor.rgb, dirt_b_color, smoothstep(0.0, 1.0, dirtFactor.b));
|
2015-03-29 11:47:58 +00:00
|
|
|
if (color.a < 1.0) {
|
|
|
|
color.a += dirtFactor.g * eDotLV;
|
|
|
|
color.a += dirtFactor.b * eDotLV;
|
|
|
|
}
|
2011-12-24 15:47:09 +00:00
|
|
|
}
|
2015-03-29 11:47:58 +00:00
|
|
|
|
2011-12-24 15:47:09 +00:00
|
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
//END Dirt
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-12-01 20:52:05 +00:00
|
|
|
|
|
|
|
// set ambient adjustment to remove bluiness with user input
|
|
|
|
float ambient_offset = clamp(amb_correction, -1.0, 1.0);
|
2012-06-28 11:37:45 +00:00
|
|
|
vec4 ambient = gl_LightModel.ambient + gl_LightSource[0].ambient;
|
2015-03-29 11:47:58 +00:00
|
|
|
|
|
|
|
vec3 ambient_Correction = vec3(ambient.rg, ambient.b * 0.6);
|
|
|
|
|
|
|
|
ambient_Correction *= ambient_offset;
|
2011-12-01 20:52:05 +00:00
|
|
|
ambient_Correction = clamp(ambient_Correction, -1.0, 1.0);
|
|
|
|
|
|
|
|
vec4 fragColor = vec4(color.rgb * mixedcolor + ambient_Correction.rgb, color.a);
|
2011-12-10 09:40:19 +00:00
|
|
|
|
2011-12-01 20:52:05 +00:00
|
|
|
fragColor += Specular * nmap.a;
|
|
|
|
|
2015-03-29 11:47:58 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// BEGIN lightmap
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
2011-12-01 20:52:05 +00:00
|
|
|
if ( lightmap_enabled >= 1 ) {
|
2012-05-17 17:10:30 +00:00
|
|
|
vec3 lightmapcolor = vec3(0.0);
|
2012-05-01 11:58:35 +00:00
|
|
|
vec4 lightmapFactor = vec4(lightmap_r_factor, lightmap_g_factor,
|
|
|
|
lightmap_b_factor, lightmap_a_factor);
|
|
|
|
lightmapFactor = lightmapFactor * lightmapTexel;
|
2014-01-15 22:26:26 +00:00
|
|
|
if (lightmap_multi > 0 ){
|
2012-05-01 11:58:35 +00:00
|
|
|
lightmapcolor = lightmap_r_color * lightmapFactor.r +
|
|
|
|
lightmap_g_color * lightmapFactor.g +
|
|
|
|
lightmap_b_color * lightmapFactor.b +
|
|
|
|
lightmap_a_color * lightmapFactor.a ;
|
2011-12-01 20:52:05 +00:00
|
|
|
} else {
|
2012-05-01 11:58:35 +00:00
|
|
|
lightmapcolor = lightmapTexel.rgb * lightmap_r_color * lightmapFactor.r;
|
2011-12-01 20:52:05 +00:00
|
|
|
}
|
2015-03-29 11:47:58 +00:00
|
|
|
fragColor.rgb = max(fragColor.rgb, lightmapcolor * smoothstep(0.0, 1.0, mixedcolor*.5 + lightmapcolor*.5));
|
2011-12-01 20:52:05 +00:00
|
|
|
}
|
2015-03-29 11:47:58 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// END lightmap
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
2011-12-01 20:52:05 +00:00
|
|
|
|
|
|
|
fragColor.rgb = fog_Func(fragColor.rgb, fogType);
|
2015-03-29 11:47:58 +00:00
|
|
|
|
2011-12-01 20:52:05 +00:00
|
|
|
gl_FragColor = fragColor;
|
2013-10-21 11:27:35 +00:00
|
|
|
}
|